KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > swing > CTextField


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.swing;
15
16 import java.awt.Color JavaDoc;
17
18 import javax.swing.JTextField JavaDoc;
19 import javax.swing.text.Document JavaDoc;
20
21 import org.compiere.plaf.CompierePLAF;
22
23 /**
24  * Compiere Text Field
25  *
26  * @author Jorg Janke
27  * @version $Id: CTextField.java,v 1.8 2003/09/27 11:08:52 jjanke Exp $
28  */

29 public class CTextField extends JTextField JavaDoc implements CEditor
30 {
31     /**
32      * Constructs a new <code>TextField</code>. A default model is created,
33      * the initial string is <code>null</code>,
34      * and the number of columns is set to 0.
35      */

36     public CTextField()
37     {
38         super();
39         init();
40     } // CTextField
41

42     /**
43      * Constructs a new <code>TextField</code> initialized with the
44      * specified text. A default model is created and the number of
45      * columns is 0.
46      *
47      * @param text the text to be displayed, or <code>null</code>
48      */

49     public CTextField (String JavaDoc text)
50     {
51         super (text);
52         init();
53     } // CTextField
54

55     /**
56      * Constructs a new empty <code>TextField</code> with the specified
57      * number of columns.
58      * A default model is created and the initial string is set to
59      * <code>null</code>.
60      *
61      * @param columns the number of columns to use to calculate
62      * the preferred width; if columns is set to zero, the
63      * preferred width will be whatever naturally results from
64      * the component implementation
65      */

66     public CTextField (int columns)
67     {
68         super (columns);
69         init();
70     } // CTextField
71

72     /**
73      * Constructs a new <code>TextField</code> initialized with the
74      * specified text and columns. A default model is created.
75      *
76      * @param text the text to be displayed, or <code>null</code>
77      * @param columns the number of columns to use to calculate
78      * the preferred width; if columns is set to zero, the
79      * preferred width will be whatever naturally results from
80      * the component implementation
81      */

82     public CTextField (String JavaDoc text, int columns)
83     {
84         super (text, columns);
85         init();
86     } // CTextField
87

88     /**
89      * Constructs a new <code>JTextField</code> that uses the given text
90      * storage model and the given number of columns.
91      * This is the constructor through which the other constructors feed.
92      * If the document is <code>null</code>, a default model is created.
93      *
94      * @param doc the text storage to use; if this is <code>null</code>,
95      * a default will be provided by calling the
96      * <code>createDefaultModel</code> method
97      * @param text the initial string to display, or <code>null</code>
98      * @param columns the number of columns to use to calculate
99      * the preferred width >= 0; if <code>columns</code>
100      * is set to zero, the preferred width will be whatever
101      * naturally results from the component implementation
102      * @exception IllegalArgumentException if <code>columns</code> < 0
103      */

104     public CTextField (Document JavaDoc doc, String JavaDoc text, int columns)
105     {
106         super (doc, text, columns);
107         init();
108     } // CTextField
109

110     /**
111      * Initialization
112      */

113     private void init()
114     {
115         setFont(CompierePLAF.getFont_Field());
116         setForeground(CompierePLAF.getTextColor_Normal());
117         setBackground (false);
118     } // init
119

120     /*************************************************************************/
121
122     /** Mandatory (default false) */
123     private boolean m_mandatory = false;
124
125     /**
126      * Set Editor Mandatory
127      * @param mandatory true, if you have to enter data
128      */

129     public void setMandatory (boolean mandatory)
130     {
131         m_mandatory = mandatory;
132         setBackground(false);
133     } // setMandatory
134

135     /**
136      * Is Field mandatory
137      * @return true, if mandatory
138      */

139     public boolean isMandatory()
140     {
141         return m_mandatory;
142     } // isMandatory
143

144     /**
145      * Enable Editor
146      * @param rw true, if you can enter/select data
147      */

148     public void setReadWrite (boolean rw)
149     {
150         if (super.isEditable() != rw)
151             super.setEditable (rw);
152         setBackground(false);
153     } // setEditable
154

155     /**
156      * Is it possible to edit
157      * @return true, if editable
158      */

159     public boolean isReadWrite()
160     {
161         return super.isEditable();
162     } // isReadWrite
163

164
165     /**
166      * Set Background based on editable / mandatory / error
167      * @param error if true, set background to error color, otherwise mandatory/editable
168      */

169     public void setBackground (boolean error)
170     {
171         if (error)
172             setBackground(CompierePLAF.getFieldBackground_Error());
173         else if (!isReadWrite())
174             setBackground(CompierePLAF.getFieldBackground_Inactive());
175         else if (m_mandatory)
176             setBackground(CompierePLAF.getFieldBackground_Mandatory());
177         else
178             setBackground(CompierePLAF.getFieldBackground_Normal());
179
180     } // setBackground
181

182     /**
183      * Set Background
184      * @param bg background
185      */

186     public void setBackground (Color JavaDoc bg)
187     {
188         if (bg.equals(getBackground()))
189             return;
190         super.setBackground(bg);
191     } // setBackground
192

193     /**
194      * Set Editor to value
195      * @param value value of the editor
196      */

197     public void setValue (Object JavaDoc value)
198     {
199         if (value == null)
200             setText("");
201         else
202             setText(value.toString());
203     } // setValue
204

205     /**
206      * Return Editor value
207      * @return current value
208      */

209     public Object JavaDoc getValue()
210     {
211         return getText();
212     } // getValue
213

214     /**
215      * Return Display Value
216      * @return displayed String value
217      */

218     public String JavaDoc getDisplay()
219     {
220         return getText();
221     } // getDisplay
222

223     /**
224      * Fire Action Performed
225      * Expose protected method.
226      */

227     public void fireActionPerformed()
228     {
229         super.fireActionPerformed();
230     } // fireActionPerformed
231

232 } // CTextField
233
Popular Tags