package research;
import research.figure.TextAreaFigure;
import java.awt.*;
import java.awt.event.InputEvent;
/**
* author: zhangwei
* Date: 2003-6-8
* Time: 15:33:35
*/
public class AreaFontSizeHandle extends LocatorHandle {
public AreaFontSizeHandle(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) {
TextAreaFigure textOwner = (TextAreaFigure) owner();
UndoActivity activity = (UndoActivity) getUndoActivity();
int newSize = activity.getFont().getSize() + y - anchorY;
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) {
TextAreaFigure textOwner = (TextAreaFigure) 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) {
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()) {
((TextAreaFigure) 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;
}
}
}