KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JTextArea


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: JTextArea.java,v $
11    Revision 1.26 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.25 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.24 2004/04/30 13:20:43 bobintetley
19    Fix to unrealised peer preferred sizes, forwarding window events to
20    content panes and fix for mouse drag events.
21
22    Revision 1.23 2004/04/28 11:02:03 bobintetley
23    PlainDocument implementation
24
25    Revision 1.22 2004/04/28 08:38:12 bobintetley
26    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
27
28    Revision 1.21 2004/03/18 16:21:37 bobintetley
29    JTextComponent/Caret implementation and fix to JFileChooser for bug in GNU classpath
30
31    Revision 1.20 2004/02/23 12:11:24 bobintetley
32    JScrollPane bug fixed, tabbing in JTextArea fixed
33
34    Revision 1.19 2004/02/19 09:58:44 bobintetley
35    Various small bug fixes and JTextArea should be much faster/lighter
36
37    Revision 1.18 2004/01/27 09:05:11 bobintetley
38    ListModel and List Selection implemented. ScrollPane fix so all components
39       scrollable
40
41    Revision 1.17 2004/01/20 07:38:05 bobintetley
42    Bug fixes and compatibility methods
43
44    Revision 1.16 2004/01/16 15:53:32 bobintetley
45    Many compatibility methods added to Container, Component, JInternalFrame,
46       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
47       and JRadioButton.
48
49    Revision 1.15 2004/01/08 12:56:30 bobintetley
50    Experiment with hotkeys - unworkable in current SWT
51
52    Revision 1.14 2004/01/08 12:17:51 bobintetley
53    JTextArea supports CTRL+A like Swing
54
55    Revision 1.13 2003/12/17 09:03:14 bobintetley
56    Closely matches Swing behaviour + JScrollPane support for Panels
57
58    Revision 1.12 2003/12/16 17:46:17 bobintetley
59    Additional thread safety methods
60
61    Revision 1.11 2003/12/16 13:14:33 bobintetley
62    Use of SwingWTUtils.isSWTControlAvailable instead of null test
63
64    Revision 1.10 2003/12/15 18:29:57 bobintetley
65    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
66
67    Revision 1.9 2003/12/15 15:54:25 bobintetley
68    Additional core methods
69
70    Revision 1.8 2003/12/14 09:13:38 bobintetley
71    Added CVS log to source headers
72
73 */

74
75
76 package swingwtx.swing;
77
78 import org.eclipse.swt.SWT;
79 import org.eclipse.swt.widgets.Text;
80
81 import swingwtx.swing.text.Document;
82
83 public class JTextArea extends swingwtx.swing.text.JTextComponent {
84
85     protected boolean pWordWrap = false;
86     protected int pRows = 0;
87     protected int pCols = 0;
88     
89     /** Return value for thread safe accessors */
90     private String JavaDoc retVal = "";
91     private int iRetVal = 0;
92     
93     public JTextArea() {
94         this(null, "", 0, 0);
95     }
96     public JTextArea(String JavaDoc text) {
97         this(null, text, 0, 0);
98     }
99     public JTextArea(int rows, int columns) {
100         this(null, "", rows, columns);
101     }
102     public JTextArea(String JavaDoc text, int rows, int columns) {
103         this(null, text, rows, columns);
104     }
105     public JTextArea(Document doc) {
106         this(doc, "", 0, 0);
107     }
108     public JTextArea(Document doc, String JavaDoc text, int rows, int columns) {
109         super();
110         if (doc != null) setDocument(doc);
111         if (text != null) {
112             setText(text);
113             view.updateModelFromComponent(getText());
114         }
115         pRows = rows;
116         pCols = columns;
117         calculateFromRowsCols();
118     }
119     
120     /**
121      * Calculates the component's preferred size based on how
122      * many rows/columns the user chose.
123      */

124     protected void calculateFromRowsCols() {
125         if (pRows != 0 && pCols != 0)
126             setPreferredSize( new swingwt.awt.Dimension((SwingWTUtils.getRenderStringWidth("W") * pCols), 25 * pRows ));
127         else if (pRows == 0 && pCols != 0)
128             setPreferredSize( new swingwt.awt.Dimension((SwingWTUtils.getRenderStringWidth("W") * pCols), 25 ));
129     }
130     
131     
132     public void append(final String JavaDoc text) {
133         pText += text;
134         SwingUtilities.invokeSync(new Runnable JavaDoc() {
135             public void run() {
136                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.append(text);
137             }
138         });
139     }
140     
141     public boolean getLineWrap() { return true; }
142     public void setLineWrap(boolean b) { }
143     public boolean getWrapStyleWord() { return pWordWrap; }
144     public void setWrapStyleWord(final boolean b) { pWordWrap = b; }
145     
146     /** Overriden to calculate non-realised
147      * preferred size.
148      */

149     protected swingwt.awt.Dimension calculatePreferredSize() {
150         swingwt.awt.Dimension size = new swingwt.awt.Dimension(
151             SwingWTUtils.getRenderStringWidth(pText),
152             SwingWTUtils.getRenderStringHeight(pText) + 4);
153         setSize(size);
154         return size;
155     }
156     
157     /**
158      * Once a parent component receives an "add" call for a child, this being
159      * the child, this should be called to tell us to instantiate the peer
160      * and load in any cached properties.
161      */

162     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
163         descendantHasPeer = true;
164         ppeer = new Text(parent.getComposite(), SWT.BORDER |
165              ( showHorizontalScrollBar ? SWT.H_SCROLL : SWT.NONE) |
166              ( showVerticalScrollBar ? SWT.V_SCROLL : SWT.NONE) |
167              SWT.MULTI |
168              ( pWordWrap ? SWT.WRAP : SWT.NONE )
169              );
170         ppeer.setText(pText);
171         ppeer.setEditable(pEditable);
172         ppeer.setTabs(0);
173         peer = ppeer;
174
175         ppeer.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
176             public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
177                 
178                 // This is so that CTRL+A selects everything in the text field
179
if ((e.keyCode == 97 ) && ((e.stateMask & SWT.CTRL) > 0)) {
180                     selectAll();
181                     e.doit = false;
182                 }
183                 
184                 // TAB navigation
185
if ((e.keyCode == SWT.TAB) && ((e.stateMask & SWT.SHIFT) > 0)) {
186                     ppeer.traverse(SWT.TRAVERSE_TAB_PREVIOUS);
187                     e.doit = false;
188                 }
189                 else if (e.keyCode == SWT.TAB) {
190                     ppeer.traverse(SWT.TRAVERSE_TAB_NEXT);
191                     e.doit = false;
192                 }
193                 
194             }
195         });
196         
197         this.parent = parent;
198     }
199     
200 }
201
Popular Tags