KickJava   Java API By Example, From Geeks To Geeks.

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


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.JButton JavaDoc;
21
22 import org.compiere.plaf.CompiereColor;
23 import org.compiere.plaf.CompierePLAF;
24 import org.compiere.util.Trace;
25
26 /**
27  * Compiere Button supporting colored Background
28  *
29  * @author Jorg Janke
30  * @version $Id: CButton.java,v 1.12 2003/09/27 11:08:52 jjanke Exp $
31  */

32 public class CButton extends JButton JavaDoc implements CEditor
33 {
34     /**
35      * Creates a button with no set text or icon.
36      */

37     public CButton()
38     {
39         this (null, null);
40     }
41
42     /**
43      * Creates a button with an icon.
44      *
45      * @param icon the Icon image to display on the button
46      */

47     public CButton(Icon JavaDoc icon)
48     {
49         this (null, icon);
50     }
51
52     /**
53      * Creates a button with text.
54      *
55      * @param text the text of the button
56      */

57     public CButton(String JavaDoc text)
58     {
59         this (text, null);
60     }
61
62     /**
63      * Creates a button where properties are taken from the
64      * <code>Action</code> supplied.
65      *
66      * @param a the <code>Action</code> used to specify the new button
67      *
68      * @since 1.3
69      */

70     public CButton (Action JavaDoc a)
71     {
72         super (a);
73         setContentAreaFilled(false);
74         setOpaque(false);
75     }
76
77     /**
78      * Creates a button with initial text and an icon.
79      *
80      * @param text the text of the button
81      * @param icon the Icon image to display on the button
82      */

83     public CButton(String JavaDoc text, Icon JavaDoc icon)
84     {
85         super (text, icon);
86         setContentAreaFilled(false);
87         setOpaque(false);
88         //
89
setFont(CompierePLAF.getFont_Label());
90         setForeground(CompierePLAF.getTextColor_Label());
91     } // CButton
92

93     /*************************************************************************/
94
95     /**
96      * Set Background - Differentiates between system & user call.
97      * If User Call, sets Opaque & ContextAreaFilled to true
98      * @param bg background color
99      */

100     public void setBackground (Color JavaDoc bg)
101     {
102         if (bg.equals(getBackground()))
103             return;
104         super.setBackground (bg);
105         // ignore calls from javax.swing.LookAndFeel.installColors(LookAndFeel.java:61)
106
if (!Trace.getCallerClass(1).startsWith("javax"))
107         {
108             setOpaque(true);
109             setContentAreaFilled(true);
110         }
111         this.repaint();
112     } // setBackground
113

114     /**
115      * Set Background - NOP
116      * @param error error
117      */

118     public void setBackground (boolean error)
119     {
120     } // setBackground
121

122     /**
123      * Set Standard Background
124      */

125     public void setBackgroundColor ()
126     {
127         setBackgroundColor (null);
128     } // setBackground
129

130     /**
131      * Set Background
132      * @param bg CompiereColor for Background, if null set standard background
133      */

134     public void setBackgroundColor (CompiereColor bg)
135     {
136         if (bg == null)
137             bg = CompiereColor.getDefaultBackground();
138         setOpaque(true);
139         putClientProperty(CompierePLAF.BACKGROUND, bg);
140         super.setBackground (bg.getFlatColor());
141         this.repaint();
142     } // setBackground
143

144     /**
145      * Get Background
146      * @return Color for Background
147      */

148     public CompiereColor getBackgroundColor ()
149     {
150         try
151         {
152             return (CompiereColor)getClientProperty(CompierePLAF.BACKGROUND);
153         }
154         catch (Exception JavaDoc e)
155         {
156             System.err.println("CButton - ClientProperty: " + e.getMessage());
157         }
158         return null;
159     } // getBackgroundColor
160

161     /** Mandatory (default false) */
162     private boolean m_mandatory = false;
163     /** Read-Write */
164     private boolean m_readWrite = true;
165
166     /**
167      * Set Editor Mandatory
168      * @param mandatory true, if you have to enter data
169      */

170     public void setMandatory (boolean mandatory)
171     {
172         m_mandatory = mandatory;
173         setBackground(false);
174     } // setMandatory
175

176     /**
177      * Is Field mandatory
178      * @return true, if mandatory
179      */

180     public boolean isMandatory()
181     {
182         return m_mandatory;
183     } // isMandatory
184

185     /**
186      * Enable Editor
187      * @param rw true, if you can enter/select data
188      */

189     public void setReadWrite (boolean rw)
190     {
191         if (super.isEnabled() != rw)
192             super.setEnabled(rw);
193         m_readWrite = rw;
194     } // setReadWrite
195

196     /**
197      * Is it possible to edit
198      * @return true, if editable
199      */

200     public boolean isReadWrite()
201     {
202         return m_readWrite;
203     } // isReadWrite
204

205     /**
206      * Set Editor to value
207      * @param value value of the editor
208      */

209     public void setValue (Object JavaDoc value)
210     {
211         if (value == null)
212             setText("");
213         else
214             setText(value.toString());
215     } // setValue
216

217     /**
218      * Return Editor value
219      * @return current value
220      */

221     public Object JavaDoc getValue()
222     {
223         return getText();
224     } // getValue
225

226     /**
227      * Return Display Value
228      * @return displayed String value
229      */

230     public String JavaDoc getDisplay()
231     {
232         return getText();
233     } // getDisplay
234

235
236 } // CButton
237
Popular Tags