01: package jimm.datavision.gui.cmd;
02:
03: import jimm.datavision.field.Rectangle;
04: import jimm.datavision.gui.SectionWidget;
05: import jimm.datavision.gui.TextFieldWidget;
06: import jimm.util.I18N;
07: import javax.swing.JTextPane;
08:
09: /**
10: * Moves a single field.
11: *
12: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
13: */
14: public class TypingCommand extends CommandAdapter {
15:
16: protected TextFieldWidget fw;
17: protected SectionWidget sw;
18: protected String oldText;
19: protected String newText;
20: protected Rectangle oldBounds;
21: protected Rectangle newBounds;
22: protected int origHeight;
23: protected SectionResizeCommand sectionResizeCommand;
24:
25: public TypingCommand(TextFieldWidget fw, int origHeight) {
26: super (I18N.get("TypingCommand.name"));
27:
28: this .fw = fw;
29: this .origHeight = origHeight;
30: sw = fw.getSectionWidget();
31:
32: oldText = (String) fw.getField().getValue();
33: oldBounds = new Rectangle(fw.getField().getBounds()); // Make a copy
34: sectionResizeCommand = new SectionResizeCommand(sw);
35: }
36:
37: /**
38: * Saves new text. The type has already been performed by the user.
39: */
40: public void perform() {
41: JTextPane textPane = (JTextPane) fw.getComponent();
42: if (textPane.isEditable()) {
43: // If we wait until later to grab the text, then we loose text with
44: // newlines in it. I don't know why. Go figure. It may be the
45: // updates caused by the bounds change that cause it; I don't know.
46: newText = textPane.getText();
47:
48: textPane.setEditable(false);
49: textPane.getCaret().setVisible(false);
50:
51: newBounds = new Rectangle(textPane.getBounds());
52: if (newBounds.height != origHeight)
53: fw.getField().getBounds().setBounds(newBounds);
54:
55: // Set text after settings bounds because setting the text triggers
56: // an update which in turn re-sets the field's bounds, erasing any
57: // changes we made in keyTyped().
58: //
59: // See also the note above: we must retrieve the text from the edit
60: // widget before getting here.
61: fw.getField().setValue(newText);
62:
63: fw.getSectionWidget().setIgnoreKeys(false);
64:
65: textPane.addMouseListener(fw);
66: textPane.addMouseMotionListener(fw);
67: }
68:
69: sectionResizeCommand.perform();
70: }
71:
72: public void undo() {
73: fw.getField().setValue(oldText);
74: fw.getField().getBounds().setBounds(oldBounds);
75:
76: sectionResizeCommand.undo();
77: }
78:
79: public void redo() {
80: fw.getField().setValue(newText);
81: fw.getField().getBounds().setBounds(newBounds);
82: sectionResizeCommand.redo();
83: }
84:
85: }
|