KickJava   Java API By Example, From Geeks To Geeks.

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


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.Action JavaDoc;
19 import javax.swing.Icon JavaDoc;
20 import javax.swing.JCheckBox JavaDoc;
21
22 import org.compiere.plaf.CompierePLAF;
23
24 /**
25  * Compiere CheckBox
26  *
27  * @author Jorg Janke
28  * @version $Id: CCheckBox.java,v 1.6 2003/09/27 11:08:52 jjanke Exp $
29  */

30 public class CCheckBox extends JCheckBox JavaDoc implements CEditor
31 {
32     /**
33      * Creates an initially unselected check box button with no text, no icon.
34      */

35     public CCheckBox ()
36     {
37         super ();
38         init();
39     }
40
41     /**
42      * Creates an initially unselected check box with an icon.
43      *
44      * @param icon the Icon image to display
45      */

46     public CCheckBox(Icon JavaDoc icon)
47     {
48         super (icon);
49         init();
50     }
51
52     /**
53      * Creates a check box with an icon and specifies whether
54      * or not it is initially selected.
55      *
56      * @param icon the Icon image to display
57      * @param selected a boolean value indicating the initial selection
58      * state. If <code>true</code> the check box is selected
59      */

60     public CCheckBox(Icon JavaDoc icon, boolean selected)
61     {
62         super (icon, selected);
63         init();
64     }
65
66     /**
67      * Creates an initially unselected check box with text.
68      *
69      * @param text the text of the check box.
70      */

71     public CCheckBox (String JavaDoc text)
72     {
73         super (text);
74         init();
75     }
76
77     /**
78      * Creates a check box where properties are taken from the
79      * Action supplied.
80      * @param a
81      */

82     public CCheckBox(Action JavaDoc a)
83     {
84         super (a);
85         init();
86     }
87
88     /**
89      * Creates a check box with text and specifies whether
90      * or not it is initially selected.
91      *
92      * @param text the text of the check box.
93      * @param selected a boolean value indicating the initial selection
94      * state. If <code>true</code> the check box is selected
95      */

96     public CCheckBox (String JavaDoc text, boolean selected)
97     {
98         super (text, selected);
99         init();
100     }
101
102     /**
103      * Creates an initially unselected check box with
104      * the specified text and icon.
105      *
106      * @param text the text of the check box.
107      * @param icon the Icon image to display
108      */

109     public CCheckBox(String JavaDoc text, Icon JavaDoc icon)
110     {
111         super (text, icon, false);
112         init();
113     }
114
115     /**
116      * Creates a check box with text and icon,
117      * and specifies whether or not it is initially selected.
118      *
119      * @param text the text of the check box.
120      * @param icon the Icon image to display
121      * @param selected a boolean value indicating the initial selection
122      * state. If <code>true</code> the check box is selected
123      */

124     public CCheckBox (String JavaDoc text, Icon JavaDoc icon, boolean selected)
125     {
126         super (text, icon, selected);
127         init();
128     }
129
130     /**
131      * Common Init
132      */

133     private void init()
134     {
135         setFont(CompierePLAF.getFont_Label());
136         setForeground(CompierePLAF.getTextColor_Label());
137     } // init
138

139     /*************************************************************************/
140
141     /** Mandatory (default false) */
142     private boolean m_mandatory = false;
143     /** Read-Write */
144     private boolean m_readWrite = true;
145
146
147     /**
148      * Set Editor Mandatory
149      * @param mandatory true, if you have to enter data
150      */

151     public void setMandatory (boolean mandatory)
152     {
153         m_mandatory = mandatory;
154         setBackground(false);
155     } // setMandatory
156

157     /**
158      * Is Field mandatory
159      * @return true, if mandatory
160      */

161     public boolean isMandatory()
162     {
163         return m_mandatory;
164     } // isMandatory
165

166     /**
167      * Enable Editor
168      * @param rw true, if you can enter/select data
169      */

170     public void setReadWrite (boolean rw)
171     {
172         if (super.isEnabled() != rw)
173             super.setEnabled (rw);
174         setBackground(false);
175         m_readWrite = rw;
176     } // setEditable
177

178     /**
179      * Is it possible to edit
180      * @return true, if editable
181      */

182     public boolean isReadWrite()
183     {
184         return m_readWrite;
185     } // isEditable
186

187     /**
188      * Set Background based on editable/mandatory/error - ignored -
189      * @param error if true, set background to error color, otherwise mandatory/editable
190      */

191     public void setBackground (boolean error)
192     {
193     } // setBackground
194

195     /**
196      * Set Background
197      * @param bg
198      */

199     public void setBackground (Color JavaDoc bg)
200     {
201         if (bg.equals(getBackground()))
202             return;
203         super.setBackground(bg);
204     } // setBackground
205

206
207     /** Retain value */
208     private Object JavaDoc m_value = null;
209
210     /**
211      * Set Editor to value. Interpret Y/N and Boolean
212      * @param value value of the editor
213      */

214     public void setValue (Object JavaDoc value)
215     {
216         m_value = value;
217         boolean sel = false;
218         if (value == null)
219             sel = false;
220         else if (value.toString().equals("Y"))
221             sel = true;
222         else if (value.toString().equals("N"))
223             sel = false;
224         else if (value instanceof Boolean JavaDoc)
225             sel = ((Boolean JavaDoc)value).booleanValue();
226         else
227         {
228             try
229             {
230                 sel = Boolean.getBoolean(value.toString());
231             }
232             catch (Exception JavaDoc e)
233             {
234             }
235         }
236         this.setSelected(sel);
237     } // setValue
238

239     /**
240      * Return Editor value
241      * @return current value as String or Boolean
242      */

243     public Object JavaDoc getValue()
244     {
245         if (m_value instanceof String JavaDoc)
246             return super.isSelected() ? "Y" : "N";
247         return new Boolean JavaDoc (isSelected());
248     } // getValue
249

250     /**
251      * Return Display Value
252      * @return displayed String value
253      */

254     public String JavaDoc getDisplay()
255     {
256         if (m_value instanceof String JavaDoc)
257             return super.isSelected() ? "Y" : "N";
258         return Boolean.toString(super.isSelected());
259     } // getDisplay
260

261 } // CCheckBox
262
Popular Tags