KickJava   Java API By Example, From Geeks To Geeks.

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


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: JTextComponent.java,v $
11    Revision 1.4 2004/05/05 12:43:22 bobintetley
12    Patches/new files from Laurent Martell
13
14    Revision 1.3 2004/05/04 09:31:43 bobintetley
15    PlainDocument/View support and implementation. Build script supports java/javax
16    packages - fix to build script to use nested args in bootclasspath (single path broke on my Ant 1.6.1/Linux)
17
18    Revision 1.2 2004/04/16 10:19:07 dannaab
19    Misc bug fixes, InputMap implementation, preliminary undo support
20
21    Revision 1.1 2004/03/18 16:21:37 bobintetley
22    JTextComponent/Caret implementation and fix to JFileChooser for bug in GNU classpath
23
24
25 */

26
27 package swingwtx.swing.text;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.widgets.Text;
31
32 import swingwtx.swing.SwingUtilities;
33 import swingwtx.swing.SwingWTUtils;
34
35 /**
36  * Superclass for all textual components.
37  *
38  * Note that we have to use a few tricks here as Swing can do a whole
39  * bunch of stuff we can't because of the native peer widgets.
40  *
41  * Basically, we make this work like a JTextField and leave it upto
42  * subclasses to override whatever they need to.
43  *
44  * @author Robin Rawson-Tetley
45  */

46 public class JTextComponent extends swingwtx.swing.JComponent {
47     
48     protected Text ppeer = null;
49     protected String JavaDoc pText = "";
50     protected boolean pEditable = true;
51     protected boolean pWordWrap = false;
52     protected int pRows = 0;
53     protected int pCols = 0;
54     protected int selStart = -1;
55     protected int selEnd = -1;
56     protected Caret caret = new DefaultCaret();
57     protected Document doc = null;
58     protected PlainView view = null;
59     
60     /** Return value for thread safe accessors */
61     protected String JavaDoc retVal = "";
62     protected int iRetVal = 0;
63     
64     public JTextComponent() {
65         doc = new PlainDocument();
66         view = new PlainView(doc, this);
67     }
68     
69     public Document getDocument() {
70         return doc;
71     }
72     
73     public void setDocument(Document doc) {
74         this.doc = doc;
75         view.setDocument(doc);
76     }
77     
78     public String JavaDoc getText() {
79         retVal="";
80         SwingUtilities.invokeSync(new Runnable JavaDoc() {
81             public void run() {
82                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
83                     retVal = ppeer.getText();
84                 else retVal = pText;
85             }
86         }); return retVal;
87     }
88     
89     public void setText(String JavaDoc text) {
90         if (text == null)
91             pText = "";
92         else
93             pText = text;
94         SwingUtilities.invokeSync(new Runnable JavaDoc() {
95             public void run() {
96                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
97                     ppeer.setText(pText);}
98         });
99     }
100     
101     public void setEditable(boolean b) { pEditable = b; if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setEditable(b); }
102     public boolean isEditable() { return pEditable; }
103     
104     public int getCaretPosition() {
105         iRetVal = 0;
106         SwingUtilities.invokeSync(new Runnable JavaDoc() {
107             public void run() {
108                 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) iRetVal = -1; else iRetVal = ppeer.getCaretPosition();
109             }
110         });
111         return iRetVal;
112     }
113     
114     public void setCaretPosition(final int pos) {
115         SwingUtilities.invokeSync(new Runnable JavaDoc() {
116             public void run() {
117                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(pos);
118             }
119         });
120     }
121     
122     public void copy() {
123         SwingUtilities.invokeSync(new Runnable JavaDoc() {
124             public void run() {
125                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.copy();
126             }
127         });
128     }
129     
130     public void cut() {
131         SwingUtilities.invokeSync(new Runnable JavaDoc() {
132             public void run() {
133                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.cut();
134             }
135         });
136     }
137     
138     public void paste() {
139         SwingUtilities.invokeSync(new Runnable JavaDoc() {
140             public void run() {
141                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.paste();
142             }
143         });
144     }
145     
146     public Caret getCaret() {
147         return caret;
148     }
149     
150     public void setCaret(Caret caret) {
151         this.caret = caret;
152     }
153     
154     /** NOT IMPLEMENTED **/
155     public void replaceSelection(String JavaDoc content) {
156     }
157     
158     
159     public void select(final int selectionStart, final int selectionEnd) {
160         SwingUtilities.invokeSync(new Runnable JavaDoc() {
161             public void run() {
162                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setSelection(selectionStart, selectionEnd);
163             }
164         });
165     }
166     
167     public void selectAll() {
168         SwingUtilities.invokeSync(new Runnable JavaDoc() {
169             public void run() {
170                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.selectAll();
171             }
172         });
173     }
174     
175     public void setSelectionStart(int start) {
176         selStart = start;
177         if (selStart > 0 && selEnd > 0)
178             select(selStart, selEnd);
179     }
180     
181     public void setSelectionEnd(int end) {
182         selEnd = end;
183         if (selStart > 0 && selEnd > 0)
184             select(selStart, selEnd);
185     }
186     
187     /** NOT IMPLEMENTED - Has no meaning in native plaf **/
188     public void setSelectedTextColor(swingwt.awt.Color c) { }
189     /** NOT IMPLEMENTED - Has no meaning in native plaf **/
190     public void setSelectionColor(swingwt.awt.Color c) { }
191     
192     /**
193      * Once a parent component receives an "add" call for a child, this being
194      * the child, this should be called to tell us to instantiate the peer
195      * and load in any cached properties.
196      */

197     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
198         ppeer = new Text(parent.getComposite(), SWT.BORDER | SWT.SINGLE);
199         ppeer.setText(pText);
200         ppeer.setEditable(pEditable);
201         peer = ppeer;
202         this.parent = parent;
203     }
204
205     public void addInputMethodListener() {
206
207     }
208 }
209
Popular Tags