KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.event.FocusListener JavaDoc;
18 import java.awt.event.InputMethodListener JavaDoc;
19 import java.awt.event.KeyListener JavaDoc;
20 import java.awt.event.MouseListener JavaDoc;
21 import java.awt.im.InputMethodRequests JavaDoc;
22
23 import javax.swing.InputVerifier JavaDoc;
24 import javax.swing.JScrollPane JavaDoc;
25 import javax.swing.JTextArea JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27
28 import org.compiere.plaf.CompierePLAF;
29
30 /**
31  * Compiere TextArea - A ScrollPane with a JTextArea.
32  * Manages visibility, opaque and color consistently
33  *
34  * @author Jorg Janke
35  * @version $Id: CText.java,v 1.9 2003/10/01 20:47:44 jjanke Exp $
36  */

37 public class CText extends JScrollPane JavaDoc
38     implements CEditor
39 {
40     /**
41      * Constructs a new TextArea. A default model is set, the initial string
42      * is null, and rows/columns are set to 0.
43      */

44     public CText()
45     {
46         this (new JTextArea JavaDoc());
47     } // CText
48

49     /**
50      * Constructs a new TextArea with the specified text displayed.
51      * A default model is created and rows/columns are set to 0.
52      *
53      * @param text the text to be displayed, or null
54      */

55     public CText (String JavaDoc text)
56     {
57         this (new JTextArea JavaDoc (text));
58     } // CText
59

60     /**
61      * Constructs a new empty TextArea with the specified number of
62      * rows and columns. A default model is created, and the initial
63      * string is null.
64      *
65      * @param rows the number of rows >= 0
66      * @param columns the number of columns >= 0
67      * @exception IllegalArgumentException if the rows or columns
68      * arguments are negative.
69      */

70     public CText (int rows, int columns)
71     {
72         this (new JTextArea JavaDoc (rows, columns));
73     } // CText
74

75     /**
76      * Constructs a new TextArea with the specified text and number
77      * of rows and columns. A default model is created.
78      *
79      * @param text the text to be displayed, or null
80      * @param rows the number of rows >= 0
81      * @param columns the number of columns >= 0
82      * @exception IllegalArgumentException if the rows or columns
83      * arguments are negative.
84      */

85     public CText (String JavaDoc text, int rows, int columns)
86     {
87         this (new JTextArea JavaDoc(text, rows, columns));
88     } // CText
89

90     /**
91      * Constructs a new JTextArea with the given document model, and defaults
92      * for all of the other arguments (null, 0, 0).
93      *
94      * @param doc the model to use
95      */

96     public CText (Document JavaDoc doc)
97     {
98         this (new JTextArea JavaDoc (doc));
99     } // CText
100

101     /**
102      * Constructs a new JTextArea with the specified number of rows
103      * and columns, and the given model. All of the constructors
104      * feed through this constructor.
105      *
106      * @param doc the model to use, or create a default one if null
107      * @param text the text to be displayed, null if none
108      * @param rows the number of rows >= 0
109      * @param columns the number of columns >= 0
110      * @exception IllegalArgumentException if the rows or columns
111      * arguments are negative.
112      */

113     public CText (Document JavaDoc doc, String JavaDoc text, int rows, int columns)
114     {
115         this (new JTextArea JavaDoc (doc, text, rows, columns));
116     } // CText
117

118
119     /**
120      * Create a JScrollArea with a JTextArea
121      * @param textArea
122      */

123     public CText (JTextArea JavaDoc textArea)
124     {
125         super (textArea);
126         m_textArea = textArea;
127         super.setOpaque(false);
128         super.getViewport().setOpaque(false);
129         m_textArea.setFont(CompierePLAF.getFont_Field());
130         m_textArea.setForeground(CompierePLAF.getTextColor_Normal());
131     } // CText
132

133     private JTextArea JavaDoc m_textArea = null;
134
135     /*************************************************************************/
136
137     /** Mandatory (default false) */
138     private boolean m_mandatory = false;
139
140     /**
141      * Set Editor Mandatory
142      * @param mandatory true, if you have to enter data
143      */

144     public void setMandatory (boolean mandatory)
145     {
146         m_mandatory = mandatory;
147         setBackground(false);
148     } // setMandatory
149

150     /**
151      * Is Field mandatory
152      * @return true, if mandatory
153      */

154     public boolean isMandatory()
155     {
156         return m_mandatory;
157     } // isMandatory
158

159     /**
160      * Enable Editor
161      * @param rw true, if you can enter/select data
162      */

163     public void setReadWrite (boolean rw)
164     {
165         if (m_textArea.isEditable() != rw)
166             m_textArea.setEditable (rw);
167         setBackground(false);
168     } // setReadWrite
169

170     /**
171      * Is it possible to edit
172      * @return true, if editable
173      */

174     public boolean isReadWrite()
175     {
176         return m_textArea.isEditable();
177     } // isReadWrite
178

179     /**
180      * Set Background based on editable / mandatory / error
181      * @param error if true, set background to error color, otherwise mandatory/editable
182      */

183     public void setBackground (boolean error)
184     {
185         if (error)
186             setBackground(CompierePLAF.getFieldBackground_Error());
187         else if (!isReadWrite())
188             setBackground(CompierePLAF.getFieldBackground_Inactive());
189         else if (m_mandatory)
190             setBackground(CompierePLAF.getFieldBackground_Mandatory());
191         else
192             setBackground(CompierePLAF.getFieldBackground_Normal());
193     } // setBackground
194

195     public void setBackground (Color JavaDoc color)
196     {
197         if (color.equals(getBackground()))
198             return;
199         if (m_textArea == null) // during init
200
super.setBackground(color);
201         else
202             m_textArea.setBackground(color);
203     }
204     public Color JavaDoc getBackground ()
205     {
206         if (m_textArea == null) // during init
207
return super.getBackground();
208         else
209             return m_textArea.getBackground();
210     }
211     public void setForeground (Color JavaDoc color)
212     {
213         if (m_textArea == null) // during init
214
super.setForeground(color);
215         else
216             m_textArea.setForeground(color);
217     }
218     public Color JavaDoc getForeground ()
219     {
220         if (m_textArea == null) // during init
221
return super.getForeground();
222         else
223             return m_textArea.getForeground();
224     }
225
226     /**
227      * Set Editor to value
228      * @param value value of the editor
229      */

230     public void setValue (Object JavaDoc value)
231     {
232         if (value == null)
233             m_textArea.setText("");
234         else
235             m_textArea.setText(value.toString());
236     } // setValue
237

238     /**
239      * Return Editor value
240      * @return current value
241      */

242     public Object JavaDoc getValue()
243     {
244         return m_textArea.getText();
245     } // getValue
246

247     /**
248      * Return Display Value
249      * @return displayed String value
250      */

251     public String JavaDoc getDisplay()
252     {
253         return m_textArea.getText();
254     } // getDisplay
255

256     /*************************************************************************/
257     /**
258      * Set Text
259      * @param text
260      */

261     public void setText (String JavaDoc text)
262     {
263         m_textArea.setText(text);
264     }
265     public String JavaDoc getText()
266     {
267         return m_textArea.getText();
268     }
269     public void append (String JavaDoc text)
270     {
271         m_textArea.append (text);
272     }
273
274     public void setColumns (int cols)
275     {
276         m_textArea.setColumns (cols);
277     }
278     public int getColumns()
279     {
280         return m_textArea.getColumns();
281     }
282
283     public void setRows (int rows)
284     {
285         m_textArea.setRows(rows);
286     }
287     public int getRows()
288     {
289         return m_textArea.getRows();
290     }
291
292     public void setCaretPosition (int pos)
293     {
294         m_textArea.setCaretPosition (pos);
295     }
296     public int getCaretPosition()
297     {
298         return m_textArea.getCaretPosition();
299     }
300
301     public void setEditable (boolean edit)
302     {
303         m_textArea.setEditable(edit);
304     }
305     public boolean isEditable()
306     {
307         return m_textArea.isEditable();
308     }
309
310     public void setLineWrap (boolean wrap)
311     {
312         m_textArea.setLineWrap (wrap);
313     }
314     public void setWrapStyleWord (boolean word)
315     {
316         m_textArea.setWrapStyleWord (word);
317     }
318
319     public void setOpaque (boolean isOpaque)
320     {
321         // JScrollPane & Viewport is always not Opaque
322
if (m_textArea == null) // during init of JScrollPane
323
super.setOpaque(isOpaque);
324         else
325             m_textArea.setOpaque(isOpaque);
326     } // setOpaque
327

328     public void addFocusListener (FocusListener JavaDoc l)
329     {
330         if (m_textArea == null) // during init
331
super.addFocusListener(l);
332         else
333             m_textArea.addFocusListener(l);
334     }
335     public void addMouseListener (MouseListener JavaDoc l)
336     {
337         m_textArea.addMouseListener(l);
338     }
339     public void addKeyListener (KeyListener JavaDoc l)
340     {
341         m_textArea.addKeyListener(l);
342     }
343
344     public void addInputMethodListener (InputMethodListener JavaDoc l)
345     {
346         m_textArea.addInputMethodListener(l);
347     }
348     public InputMethodRequests JavaDoc getInputMethodRequests()
349     {
350         return m_textArea.getInputMethodRequests();
351     }
352     public void setInputVerifier (InputVerifier JavaDoc l)
353     {
354         m_textArea.setInputVerifier(l);
355     }
356
357
358
359 } // CTextArea
360
Popular Tags