KickJava   Java API By Example, From Geeks To Geeks.

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


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.JPasswordField JavaDoc;
19 import javax.swing.text.Document JavaDoc;
20
21 import org.compiere.plaf.CompierePLAF;
22
23 /**
24  * Password Field
25  *
26  * @author Jorg Janke
27  * @version $Id: CPassword.java,v 1.5 2003/09/27 11:08:52 jjanke Exp $
28  */

29 public class CPassword extends JPasswordField JavaDoc implements CEditor
30 {
31     /**
32      * Constructs a new <code>JPasswordField</code>,
33      * with a default document, <code>null</code> starting
34      * text string, and 0 column width.
35      */

36     public CPassword()
37     {
38         super();
39         init();
40     }
41
42     /**
43      * Constructs a new <code>JPasswordField</code> initialized
44      * with the specified text. The document model is set to the
45      * default, and the number of columns to 0.
46      *
47      * @param text the text to be displayed, <code>null</code> if none
48      */

49     public CPassword (String JavaDoc text)
50     {
51         super (text);
52         init();
53     }
54
55     /**
56      * Constructs a new empty <code>JPasswordField</code> with the specified
57      * number of columns. A default model is created, and the initial string
58      * is set to <code>null</code>.
59      *
60      * @param columns the number of columns >= 0
61      */

62     public CPassword (int columns)
63     {
64         super (columns);
65         init();
66     }
67
68     /**
69      * Constructs a new <code>JPasswordField</code> initialized with
70      * the specified text and columns. The document model is set to
71      * the default.
72      *
73      * @param text the text to be displayed, <code>null</code> if none
74      * @param columns the number of columns >= 0
75      */

76     public CPassword (String JavaDoc text, int columns)
77     {
78         super (text,columns);
79         init();
80     }
81
82     /**
83      * Constructs a new <code>JPasswordField</code> that uses the
84      * given text storage model and the given number of columns.
85      * This is the constructor through which the other constructors feed.
86      * The echo character is set to '*'. If the document model is
87      * <code>null</code>, a default one will be created.
88      *
89      * @param doc the text storage to use
90      * @param txt the text to be displayed, <code>null</code> if none
91      * @param columns the number of columns to use to calculate
92      * the preferred width >= 0; if columns is set to zero, the
93      * preferred width will be whatever naturally results from
94      * the component implementation
95      */

96     public CPassword (Document JavaDoc doc, String JavaDoc txt, int columns)
97     {
98         super (doc, txt, columns);
99         init();
100     }
101
102     /**
103      * Common Init
104      */

105     private void init()
106     {
107         setFont(CompierePLAF.getFont_Field());
108         setForeground(CompierePLAF.getTextColor_Normal());
109     } // init
110

111     /*************************************************************************/
112
113     /** Mandatory (default false) */
114     private boolean m_mandatory = false;
115
116     /**
117      * Set Editor Mandatory
118      * @param mandatory true, if you have to enter data
119      */

120     public void setMandatory (boolean mandatory)
121     {
122         m_mandatory = mandatory;
123         setBackground(false);
124     } // setMandatory
125

126     /**
127      * Is Field mandatory
128      * @return true, if mandatory
129      */

130     public boolean isMandatory()
131     {
132         return m_mandatory;
133     } // isMandatory
134

135     /**
136      * Enable Editor
137      * @param rw true, if you can enter/select data
138      */

139     public void setReadWrite (boolean rw)
140     {
141         if (super.isEditable() != rw)
142             super.setEditable (rw);
143         setBackground(false);
144     } // setEditable
145

146     /**
147      * Is it possible to edit
148      * @return true, if editable
149      */

150     public boolean isReadWrite()
151     {
152         return super.isEditable();
153     } // isReadWrite
154

155     /**
156      * Set Background based on editable / mandatory / error
157      * @param error if true, set background to error color, otherwise mandatory/editable
158      */

159     public void setBackground (boolean error)
160     {
161         if (error)
162             setBackground(CompierePLAF.getFieldBackground_Error());
163         else if (!isReadWrite())
164             setBackground(CompierePLAF.getFieldBackground_Inactive());
165         else if (m_mandatory)
166             setBackground(CompierePLAF.getFieldBackground_Mandatory());
167         else
168             setBackground(CompierePLAF.getFieldBackground_Normal());
169     } // setBackground
170

171     /**
172      * Set Background
173      * @param bg
174      */

175     public void setBackground (Color JavaDoc bg)
176     {
177         if (bg.equals(getBackground()))
178             return;
179         super.setBackground(bg);
180     } // setBackground
181

182     /**
183      * Set Editor to value
184      * @param value value of the editor
185      */

186     public void setValue (Object JavaDoc value)
187     {
188         if (value == null)
189             setText("");
190         else
191             setText(value.toString());
192     } // setValue
193

194     /**
195      * Return Editor value
196      * @return current value
197      */

198     public Object JavaDoc getValue()
199     {
200         return new String JavaDoc(super.getPassword());
201     } // getValue
202

203     /**
204      * Return Display Value
205      * @return displayed String value
206      */

207     public String JavaDoc getDisplay()
208     {
209         return new String JavaDoc(super.getPassword());
210     } // getDisplay
211

212
213 }
Popular Tags