KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JTextField


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: JTextField.java,v $
11    Revision 1.21 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.20 2004/05/05 13:24:32 bobintetley
15    Bugfixes and Laurent's patch for binary compatibility on Container.add()
16
17    Revision 1.19 2004/05/04 09:31:43 bobintetley
18    PlainDocument/View support and implementation. Build script supports java/javax
19    packages - fix to build script to use nested args in bootclasspath (single path broke on my Ant 1.6.1/Linux)
20
21    Revision 1.18 2004/04/30 13:20:43 bobintetley
22    Fix to unrealised peer preferred sizes, forwarding window events to
23    content panes and fix for mouse drag events.
24
25    Revision 1.17 2004/04/28 11:02:03 bobintetley
26    PlainDocument implementation
27
28    Revision 1.16 2004/04/28 08:38:12 bobintetley
29    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
30
31    Revision 1.15 2004/03/18 16:21:37 bobintetley
32    JTextComponent/Caret implementation and fix to JFileChooser for bug in GNU classpath
33
34    Revision 1.14 2004/01/20 07:38:05 bobintetley
35    Bug fixes and compatibility methods
36
37    Revision 1.13 2003/12/16 15:47:45 bobintetley
38    Thread safety added to common methods
39
40    Revision 1.12 2003/12/16 13:14:33 bobintetley
41    Use of SwingWTUtils.isSWTControlAvailable instead of null test
42
43    Revision 1.11 2003/12/15 18:29:57 bobintetley
44    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
45
46    Revision 1.10 2003/12/15 17:54:36 bobintetley
47    Textfield editable support
48
49    Revision 1.9 2003/12/14 09:13:38 bobintetley
50    Added CVS log to source headers
51
52 */

53
54
55 package swingwtx.swing;
56
57 import swingwtx.swing.text.*;
58
59 import org.eclipse.swt.widgets.*;
60 import org.eclipse.swt.*;
61
62 public class JTextField extends swingwtx.swing.text.JTextComponent implements SwingConstants {
63
64     protected int pCols = 0;
65     
66     /** Used for thread safe property accessors */
67     private String JavaDoc retVal = "";
68     
69     public JTextField() { this(null, "", 0); }
70     public JTextField(int columns) { this(null, "", columns); }
71     public JTextField(String JavaDoc text) { this(null, text, 0); }
72     public JTextField(String JavaDoc text, int columns) { this(null, text, columns); }
73     public JTextField(Document doc, String JavaDoc text, int columns) {
74         super();
75         if (doc != null) setDocument(doc);
76         pText = text;
77         pCols = columns;
78         if (pCols != 0) calculateFromCols();
79         if (pText != null)
80             if (!pText.equals(""))
81                 view.updateModelFromComponent(pText);
82     }
83     
84     /**
85      * Calculates the component's preferred size based on how
86      * many columns the user chose.
87      */

88     protected void calculateFromCols() {
89         setPreferredSize( new swingwt.awt.Dimension((SwingWTUtils.getRenderStringWidth("W") * pCols), 25));
90     }
91     
92     public int getColumns() { return pCols; }
93     public void setColumns(int columns) { pCols = columns; if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setTextLimit(pCols); }
94     public void setEditable(boolean b) { pEditable = b; if (SwingWTUtils.isSWTControlAvailable(ppeer)) ppeer.setEditable(b); }
95     public boolean isEditable() { return pEditable; }
96     
97     /** Overriden to calculate non-realised
98      * preferred size.
99      */

100     protected swingwt.awt.Dimension calculatePreferredSize() {
101         swingwt.awt.Dimension size = new swingwt.awt.Dimension(
102             SwingWTUtils.getRenderStringWidth(pText),
103             SwingWTUtils.getRenderStringHeight(pText) + 4);
104         setSize(size);
105         return size;
106     }
107     
108     /**
109      * Once a parent component receives an "add" call for a child, this being
110      * the child, this should be called to tell us to instantiate the peer
111      * and load in any cached properties.
112      */

113     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
114         descendantHasPeer = true;
115         ppeer = new Text(parent.getComposite(), SWT.BORDER | SWT.SINGLE);
116         if (pText == null)
117             ppeer.setText("");
118         else
119             ppeer.setText(pText);
120         ppeer.setEditable(pEditable);
121         if (pCols > 0) ppeer.setTextLimit(pCols);
122         peer = ppeer;
123         this.parent = parent;
124     }
125     
126 }
127
Popular Tags