/*
* @(#)FontSizeHandle.java
*
* Project: JHotdraw - a GUI framework for technical drawings
* http://www.jhotdraw.org
* http://jhotdraw.sourceforge.net
* Copyright: ? by the original author(s) and all contributors
* License: Lesser GNU Public License (LGPL)
* http://www.opensource.org/licenses/lgpl-license.html
*/
package research;
import research.figure.TextFigure;
import research.figure.TextAreaFigure;
import research.figure.TextFigure0;
import java.awt.*;
import java.awt.event.InputEvent;
/**
* A Handle to change the font size by direct manipulation.
*
* @version <$CURRENT_VERSION$>
*/
public class FontSizeHandle extends LocatorHandle {
public FontSizeHandle(Figure owner, Locator l) {
super(owner, l);
}
public void invokeStart(int x, int y, DrawingView view) {
setUndoActivity(createUndoActivity(view));
getUndoActivity().setAffectedFigures(new SingleFigureEnumerator(owner()));
}
public void invokeStep(InputEvent inputEvent, int x, int y, int anchorX, int anchorY, DrawingView view) {
TextFigure0 textOwner = (TextFigure0) owner();
UndoActivity activity = (UndoActivity) getUndoActivity();
double scale = view.getScale();
double realDeltaY = (y - anchorY) / scale;
int visualDeltaY = (int) realDeltaY;
System.out.println("visualDeltaY = " + visualDeltaY);
int newSize = activity.getFont().getSize() + visualDeltaY;
if (newSize < 1)
newSize = 1;
textOwner.setFont(new Font(activity.getFont().getName(), activity.getFont().getStyle(), newSize));
}
public void invokeEnd(int x, int y, int anchorX, int anchorY, DrawingView view) {
TextFigure0 textOwner = (TextFigure0) owner();
UndoActivity activity = (UndoActivity) getUndoActivity();
// there has been no change so there is nothing to undo
if (textOwner.getFont().getSize() == activity.getOldFontSize()) {
setUndoActivity(null);
} else {
activity.setFont(textOwner.getFont());
}
}
public void draw(Graphics g) {
Rectangle r = displayBox();
g.setColor(Color.yellow);
g.fillOval(r.x, r.y, r.width, r.height);
g.setColor(Color.black);
g.drawOval(r.x, r.y, r.width, r.height);
}
/**
* Factory method for undo activity
*/
/**
protected Undoable createUndoActivity(DrawingView newView) {
TextFigure textOwner = (TextFigure) owner();
return new UndoActivity(newView, textOwner.getFont());
}
**/
protected Undoable createUndoActivity(DrawingView newView) {
if (owner() instanceof TextFigure) {
TextFigure textOwner = (TextFigure) owner();
return new UndoActivity(newView, textOwner.getFont());
}
if (owner() instanceof TextFigure0) {
TextFigure0 textOwner = (TextFigure0) owner();
return new UndoActivity(newView, textOwner.getFont());
}
if (owner() instanceof TextAreaFigure) {
TextAreaFigure textOwner = (TextAreaFigure) owner();
return new UndoActivity(newView, textOwner.getFont());
}
return null;
}
public static class UndoActivity extends UndoableAdapter {
private Font myFont;
private int myOldFontSize;
public UndoActivity(DrawingView newView, Font newFont) {
super(newView);
setFont(newFont);
setOldFontSize(getFont().getSize());
setUndoable(true);
setRedoable(true);
}
public boolean undo() {
if (!super.undo()) {
return false;
}
swapFont();
return true;
}
public boolean redo() {
// do not call execute directly as the selection might has changed
if (!isRedoable()) {
return false;
}
swapFont();
return true;
}
protected void swapFont() {
setOldFontSize(replaceFontSize());
FigureEnumeration fe = getAffectedFigures();
while (fe.hasMoreElements()) {
((TextFigure) fe.nextFigure()).setFont(getFont());
}
}
private int replaceFontSize() {
int tempFontSize = getFont().getSize();
setFont(new Font(getFont().getName(), getFont().getStyle(), getOldFontSize()));
return tempFontSize;
}
protected void setFont(Font newFont) {
myFont = newFont;
}
public Font getFont() {
return myFont;
}
protected void setOldFontSize(int newOldFontSize) {
myOldFontSize = newOldFontSize;
}
public int getOldFontSize() {
return myOldFontSize;
}
}
}