KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > text > PlainView


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: PlainView.java,v $
11    Revision 1.1 2004/05/04 09:31:43 bobintetley
12    PlainDocument/View support and implementation. Build script supports java/javax
13    packages - fix to build script to use nested args in bootclasspath (single path broke on my Ant 1.6.1/Linux)
14
15
16 */

17
18 package swingwtx.swing.text;
19
20 import swingwt.awt.event.*;
21 import swingwtx.swing.event.*;
22
23 /**
24  * Mapped view descendant that allows translation
25  * of a Document to a plain SWT Text component (works
26  * for JTextField, JPasswordField and JTextArea - ie.
27  * Is used by JTextComponent).
28  *
29  * Since it takes text either en-masse, this is appallingly
30  * inefficient. It will do for now (and since JTextField/
31  * JPasswordField and JTextArea generally don't contain that
32  * much changeable text, it should be ok - we'll use
33  * something better for JEditorPane/JTextPane).
34  *
35  * @author Robin Rawson-Tetley
36  */

37 public class PlainView extends View {
38     
39     protected JTextComponent comp = null;
40     
41     private boolean componentJustChanged = false;
42     
43     public PlainView(Document doc, JTextComponent component) {
44         super(doc); this.comp = component;
45         
46         // Subscribe to key presses so we know when to mark the document
47
// as updated
48
comp.addKeyListener(new KeyAdapter() {
49             public void KeyTyped(KeyEvent e) {
50                 updateModelFromComponent(comp.getText());
51             }
52         });
53         
54         // Subscribe to Document events so we know when to update
55
// the component from the model.
56
doc.addDocumentListener(new DocumentListener() {
57             public void insertUpdate(DocumentEvent e) { updateComponentFromModel(); }
58             public void removeUpdate(DocumentEvent e) { updateComponentFromModel(); }
59             public void changedUpdate(DocumentEvent e) { updateComponentFromModel(); }
60         });
61     }
62     
63     /** Copies the text passed in from the component to the model,
64      * replacing any content already there entirely. */

65     public void updateModelFromComponent(String JavaDoc newText) {
66         try {
67             doc.remove(0, doc.getLength());
68             doc.insertString(0, newText, null);
69             componentJustChanged = true; // Let the twin routine know not to run
70
// as a result of this change (no need to
71
// sync component/model twice).
72
}
73         catch (BadLocationException e) {
74             // Shouldn't ever happen since we're replacing the whole lot
75
e.printStackTrace();
76         }
77     }
78     
79     /**
80      * Reads the text from the model and replaces the text in the
81      * component.
82      */

83     public void updateComponentFromModel() {
84         
85         // We set this flag when a keypress fires a model
86
// change - this ensures that if the model was just
87
// updated from the component, the resulting Document
88
// change doesn't fire a component update (losing
89
// the caret position and looking weird to the user).
90
// Besides - if this flag is set, the two have just
91
// been synced so there's no need to do it again.
92
if (componentJustChanged) {
93             componentJustChanged = false;
94             return;
95         }
96         
97         try {
98             comp.setText( doc.getText( 0, doc.getLength()));
99         }
100         catch (BadLocationException e) {
101             // Shouldn't ever happen
102
e.printStackTrace();
103         }
104     }
105
106     /** Sets a replacement Document - this routine calls
107      * updateComponentFromModel to load the model's
108      * content into the component
109      */

110     public void setDocument(Document doc) {
111         this.doc = doc;
112         updateComponentFromModel();
113     }
114     
115 }
116
Popular Tags