KickJava   Java API By Example, From Geeks To Geeks.

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


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.JToggleButton JavaDoc;
21
22 import org.compiere.plaf.CompiereColor;
23 import org.compiere.plaf.CompierePLAF;
24 import org.compiere.util.Trace;
25
26 /**
27  * Compiere Color Taggle Button
28  *
29  * @author Jorg Janke
30  * @version $Id: CToggleButton.java,v 1.6 2003/09/27 11:08:51 jjanke Exp $
31  */

32 public class CToggleButton extends JToggleButton JavaDoc implements CEditor
33 {
34     /**
35      * Creates an initially unselected toggle button
36      * without setting the text or image.
37      */

38     public CToggleButton () {
39         this(null, null, false);
40     }
41
42     /**
43      * Creates an initially unselected toggle button
44      * with the specified image but no text.
45      *
46      * @param icon the image that the button should display
47      */

48     public CToggleButton(Icon JavaDoc icon) {
49         this(null, icon, false);
50     }
51
52     /**
53      * Creates a toggle button with the specified image
54      * and selection state, but no text.
55      *
56      * @param icon the image that the button should display
57      * @param selected if true, the button is initially selected;
58      * otherwise, the button is initially unselected
59      */

60     public CToggleButton(Icon JavaDoc icon, boolean selected) {
61         this(null, icon, selected);
62     }
63
64     /**
65      * Creates an unselected toggle button with the specified text.
66      *
67      * @param text the string displayed on the toggle button
68      */

69     public CToggleButton (String JavaDoc text) {
70         this(text, null, false);
71     }
72
73     /**
74      * Creates a toggle button with the specified text
75      * and selection state.
76      *
77      * @param text the string displayed on the toggle button
78      * @param selected if true, the button is initially selected;
79      * otherwise, the button is initially unselected
80      */

81     public CToggleButton (String JavaDoc text, boolean selected) {
82         this(text, null, selected);
83     }
84
85     /**
86      * Creates a toggle button where properties are taken from the
87      * Action supplied.
88      *
89      * @param a
90      */

91     public CToggleButton(Action JavaDoc a) {
92         this();
93     setAction(a);
94     }
95
96     /**
97      * Creates a toggle button that has the specified text and image,
98      * and that is initially unselected.
99      *
100      * @param text the string displayed on the button
101      * @param icon the image that the button should display
102      */

103     public CToggleButton(String JavaDoc text, Icon JavaDoc icon) {
104         this(text, icon, false);
105     }
106
107     /**
108      * Creates a toggle button with the specified text, image, and
109      * selection state.
110      *
111      * @param text the text of the toggle button
112      * @param icon the image that the button should display
113      * @param selected if true, the button is initially selected;
114      * otherwise, the button is initially unselected
115      */

116     public CToggleButton (String JavaDoc text, Icon JavaDoc icon, boolean selected)
117     {
118         super(text, icon, selected);
119         setContentAreaFilled(false);
120         setOpaque(false);
121         //
122
setFont(CompierePLAF.getFont_Label());
123         setForeground(CompierePLAF.getTextColor_Label());
124     }
125
126     /*************************************************************************/
127
128     /**
129      * Set Background - Differentiates between system & user call.
130      * If User Call, sets Opaque & ContextAreaFilled to true
131      * @param bg
132      */

133     public void setBackground(Color JavaDoc bg)
134     {
135         if (bg.equals(getBackground()))
136             return;
137         super.setBackground( bg);
138         // ignore calls from javax.swing.LookAndFeel.installColors(LookAndFeel.java:61)
139
if (!Trace.getCallerClass(1).startsWith("javax"))
140         {
141             setOpaque(true);
142             setContentAreaFilled(true);
143         }
144     } // setBackground
145

146     /**
147      * Set Background - NOP
148      * @param error
149      */

150     public void setBackground (boolean error)
151     {
152     } // setBackground
153

154     /**
155      * Set Standard Background
156      */

157     public void setBackgroundColor ()
158     {
159         setBackgroundColor (null);
160     } // setBackground
161

162     /**
163      * Set Background
164      * @param bg CompiereColor for Background, if null set standard background
165      */

166     public void setBackgroundColor (CompiereColor bg)
167     {
168         if (bg == null)
169             bg = CompiereColor.getDefaultBackground();
170         setOpaque(true);
171         putClientProperty(CompierePLAF.BACKGROUND, bg);
172         super.setBackground (bg.getFlatColor());
173     } // setBackground
174

175     /**
176      * Get Background
177      * @return Color for Background
178      */

179     public CompiereColor getBackgroundColor ()
180     {
181         try
182         {
183             return (CompiereColor)getClientProperty(CompierePLAF.BACKGROUND);
184         }
185         catch (Exception JavaDoc e)
186         {
187             System.err.println("CButton - ClientProperty: " + e.getMessage());
188         }
189         return null;
190     } // getBackgroundColor
191

192     /** Mandatory (default false) */
193     private boolean m_mandatory = false;
194
195     /**
196      * Set Editor Mandatory
197      * @param mandatory true, if you have to enter data
198      */

199     public void setMandatory (boolean mandatory)
200     {
201         m_mandatory = mandatory;
202         setBackground(false);
203     } // setMandatory
204

205     /**
206      * Is Field mandatory
207      * @return true, if mandatory
208      */

209     public boolean isMandatory()
210     {
211         return m_mandatory;
212     } // isMandatory
213

214     /**
215      * Enable Editor
216      * @param rw true, if you can enter/select data
217      */

218     public void setReadWrite (boolean rw)
219     {
220         if (super.isEnabled() != rw)
221             super.setEnabled(rw);
222     } // setReadWrite
223

224     /**
225      * Is it possible to edit
226      * @return true, if editable
227      */

228     public boolean isReadWrite()
229     {
230         return super.isEnabled();
231     } // isReadWrite
232

233     /**
234      * Set Editor to value
235      * @param value value of the editor
236      */

237     public void setValue (Object JavaDoc value)
238     {
239         if (value == null)
240             setText("");
241         else
242             setText(value.toString());
243     } // setValue
244

245     /**
246      * Return Editor value
247      * @return current value
248      */

249     public Object JavaDoc getValue()
250     {
251         return getText();
252     } // getValue
253

254     /**
255      * Return Display Value
256      * @return displayed String value
257      */

258     public String JavaDoc getDisplay()
259     {
260         return getText();
261     } // getDisplay
262

263 } // CToggleButton
264
Popular Tags