KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > cmd > TypingCommand


1 package jimm.datavision.gui.cmd;
2 import jimm.datavision.field.Rectangle;
3 import jimm.datavision.gui.SectionWidget;
4 import jimm.datavision.gui.TextFieldWidget;
5 import jimm.util.I18N;
6 import javax.swing.JTextPane JavaDoc;
7
8 /**
9  * Moves a single field.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public class TypingCommand extends CommandAdapter {
14
15 protected TextFieldWidget fw;
16 protected SectionWidget sw;
17 protected String JavaDoc oldText;
18 protected String JavaDoc newText;
19 protected Rectangle oldBounds;
20 protected Rectangle newBounds;
21 protected int origHeight;
22 protected SectionResizeCommand sectionResizeCommand;
23
24 public TypingCommand(TextFieldWidget fw, int origHeight) {
25     super(I18N.get("TypingCommand.name"));
26
27     this.fw = fw;
28     this.origHeight = origHeight;
29     sw = fw.getSectionWidget();
30
31     oldText = (String JavaDoc)fw.getField().getValue();
32     oldBounds = new Rectangle(fw.getField().getBounds()); // Make a copy
33
sectionResizeCommand = new SectionResizeCommand(sw);
34 }
35
36 /**
37  * Saves new text. The type has already been performed by the user.
38  */

39 public void perform() {
40     JTextPane JavaDoc textPane = (JTextPane JavaDoc)fw.getComponent();
41     if (textPane.isEditable()) {
42     // If we wait until later to grab the text, then we loose text with
43
// newlines in it. I don't know why. Go figure. It may be the
44
// updates caused by the bounds change that cause it; I don't know.
45
newText = textPane.getText();
46
47     textPane.setEditable(false);
48     textPane.getCaret().setVisible(false);
49
50     newBounds = new Rectangle(textPane.getBounds());
51     if (newBounds.height != origHeight)
52         fw.getField().getBounds().setBounds(newBounds);
53
54     // Set text after settings bounds because setting the text triggers
55
// an update which in turn re-sets the field's bounds, erasing any
56
// changes we made in keyTyped().
57
//
58
// See also the note above: we must retrieve the text from the edit
59
// widget before getting here.
60
fw.getField().setValue(newText);
61
62     fw.getSectionWidget().setIgnoreKeys(false);
63
64     textPane.addMouseListener(fw);
65     textPane.addMouseMotionListener(fw);
66     }
67
68     sectionResizeCommand.perform();
69 }
70
71 public void undo() {
72     fw.getField().setValue(oldText);
73     fw.getField().getBounds().setBounds(oldBounds);
74
75     sectionResizeCommand.undo();
76 }
77
78 public void redo() {
79     fw.getField().setValue(newText);
80     fw.getField().getBounds().setBounds(newBounds);
81     sectionResizeCommand.redo();
82 }
83
84 }
85
Popular Tags