KickJava   Java API By Example, From Geeks To Geeks.

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


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.MouseListener JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import javax.swing.ComboBoxModel JavaDoc;
21 import javax.swing.Icon JavaDoc;
22 import javax.swing.JButton JavaDoc;
23 import javax.swing.JComboBox JavaDoc;
24 import javax.swing.plaf.ComboBoxUI JavaDoc;
25
26 import org.compiere.plaf.CompiereComboBoxUI;
27 import org.compiere.plaf.CompierePLAF;
28 import org.compiere.util.Trace;
29
30 /**
31  * Compiere Colored Combo Box.
32  *
33  * @author Jorg Janke
34  * @version $Id: CComboBox.java,v 1.16 2003/09/27 11:08:51 jjanke Exp $
35  */

36 public class CComboBox extends JComboBox JavaDoc
37     implements CEditor
38 {
39     /**
40      * Creates a <code>JComboBox</code> that takes it's items from an
41      * existing <code>ComboBoxModel</code>. Since the
42      * <code>ComboBoxModel</code> is provided, a combo box created using
43      * this constructor does not create a default combo box model and
44      * may impact how the insert, remove and add methods behave.
45      *
46      * @param aModel the <code>ComboBoxModel</code> that provides the
47      * displayed list of items
48      * @see DefaultComboBoxModel
49      */

50     public CComboBox(ComboBoxModel JavaDoc aModel)
51     {
52         super(aModel);
53         init();
54     } // CComboBox
55

56     /**
57      * Creates a <code>JComboBox</code> that contains the elements
58      * in the specified array. By default the first item in the array
59      * (and therefore the data model) becomes selected.
60      *
61      * @param items an array of objects to insert into the combo box
62      * @see DefaultComboBoxModel
63      */

64     public CComboBox(final Object JavaDoc items[])
65     {
66         super(items);
67         init();
68     } // CComboBox
69

70     /**
71      * Creates a <code>JComboBox</code> that contains the elements
72      * in the specified Vector. By default the first item in the vector
73      * and therefore the data model) becomes selected.
74      *
75      * @param items an array of vectors to insert into the combo box
76      * @see DefaultComboBoxModel
77      */

78     public CComboBox(Vector JavaDoc items)
79     {
80         super(items);
81         init();
82     } // CComboBox
83

84     /**
85      * Creates a <code>JComboBox</code> with a default data model.
86      * The default data model is an empty list of objects.
87      * Use <code>addItem</code> to add items. By default the first item
88      * in the data model becomes selected.
89      *
90      * @see DefaultComboBoxModel
91      */

92     public CComboBox()
93     {
94         super();
95         init();
96     } // CComboBox
97

98     /**
99      * Common Init
100      */

101     private void init()
102     {
103         // overwrite - otherwise Label Font
104
setFont(CompierePLAF.getFont_Field());
105         setForeground(CompierePLAF.getTextColor_Normal());
106         setBackground(false);
107     } // init
108

109
110     /*************************************************************************/
111
112     /** Icon */
113     private Icon JavaDoc m_icon = null;
114
115     /**
116      * Set Icon of arrow button to icon
117      * @param defaultIcon Icon to be displayed
118      */

119     public void setIcon (Icon JavaDoc defaultIcon)
120     {
121         if (getUI() instanceof CompiereComboBoxUI)
122             ((CompiereComboBoxUI)getUI()).setIcon(defaultIcon);
123         m_icon = defaultIcon;
124     } // setIcon
125

126     /**
127      * Set UI and re-set Icon for arrow button
128      * @param ui
129      */

130     public void setUI (ComboBoxUI JavaDoc ui)
131     {
132         super.setUI(ui);
133         if (m_icon != null && ui instanceof CompiereComboBoxUI)
134             ((CompiereComboBoxUI)getUI()).setIcon(m_icon);
135     } // setUI
136

137     /**
138      * Display Popup.
139      * Called from CompiereComboPopup and allows to implement
140      * alternative actions than showing the popup
141      * @return if true, the popup should be displayed
142      */

143     public boolean displayPopup()
144     {
145         return true;
146     } // displayPopup
147

148     /*************************************************************************/
149
150     /** Mandatory (default false) */
151     private boolean m_mandatory = false;
152
153     /**
154      * Set Editor Mandatory
155      * @param mandatory true, if you have to enter data
156      */

157     public void setMandatory (boolean mandatory)
158     {
159         m_mandatory = mandatory;
160         setBackground(false);
161     } // setMandatory
162

163     /**
164      * Is Field mandatory
165      * @return true, if mandatory
166      */

167     public boolean isMandatory()
168     {
169         return m_mandatory;
170     } // isMandatory
171

172     /**
173      * Enable Editor
174      * @param rw true, if you can enter/select data
175      */

176     public void setReadWrite (boolean rw)
177     {
178         if (super.isEnabled() != rw)
179             super.setEnabled (rw);
180         setBackground(false);
181     } // setReadWrite
182

183     /**
184      * Is it possible to edit
185      * @return true, if editable
186      */

187     public boolean isReadWrite()
188     {
189         return super.isEnabled();
190     } // isReadWrite
191

192     /**
193      * Set Background based on editable / mandatory / error
194      * @param error if true, set background to error color, otherwise mandatory/editable
195      */

196     public void setBackground (boolean error)
197     {
198         if (error)
199             setBackground(CompierePLAF.getFieldBackground_Error());
200         else if (!isReadWrite())
201             setBackground(CompierePLAF.getFieldBackground_Inactive());
202         else if (m_mandatory)
203             setBackground(CompierePLAF.getFieldBackground_Mandatory());
204         else
205             setBackground(CompierePLAF.getFieldBackground_Normal());
206     } // setBackground
207

208     /**
209      * Set Background
210      * @param bg
211      */

212     public void setBackground (Color JavaDoc bg)
213     {
214         if (bg.equals(getBackground()))
215             return;
216         super.setBackground(bg);
217     } // setBackground
218

219     /**
220      * Set Editor to value
221      * @param value value of the editor
222      */

223     public void setValue (Object JavaDoc value)
224     {
225         super.setSelectedItem(value);
226     } // setValue
227

228     /**
229      * Return Editor value
230      * @return current value
231      */

232     public Object JavaDoc getValue()
233     {
234         return super.getSelectedItem();
235     } // getValue
236

237     /**
238      * Return Display Value
239      * @return displayed String value
240      */

241     public String JavaDoc getDisplay()
242     {
243         Object JavaDoc o = super.getSelectedItem();
244         if (o == null)
245             return "";
246         return o.toString();
247     } // getDisplay
248

249     /**
250      * Add Mouse Listener - 1-4-0 Bug.
251      * Bug in 1.4.0 Metal: arrowButton gets Mouse Events, so add the JComboBox
252      * MouseListeners to the arrowButton - No context menu if right-click
253      * @see also CompiereComboBoxUI#installUI()
254      * @param ml
255      */

256     public void addMouseListener (MouseListener JavaDoc ml)
257     {
258         super.addMouseListener(ml);
259         // ignore calls from javax.swing.plaf.basic.BasicComboBoxUI.installListeners(BasicComboBoxUI.java:271)
260
if (getUI() instanceof CompiereComboBoxUI && !Trace.getCallerClass(1).startsWith("javax"))
261         {
262             JButton JavaDoc b = ((CompiereComboBoxUI)getUI()).getArrowButton();
263             if (b != null)
264                 b.addMouseListener(ml);
265         }
266     } // addMouseListener
267

268     /**
269      * Remove Mouse Listener.
270      * @param ml
271      */

272     public void removeMouseListener (MouseListener JavaDoc ml)
273     {
274         super.removeMouseListener(ml);
275         if (getUI() instanceof CompiereComboBoxUI)
276         {
277             JButton JavaDoc b = ((CompiereComboBoxUI)getUI()).getArrowButton();
278             if (b != null)
279                 b.removeMouseListener(ml);
280         }
281     } // removeMouseListener
282

283 } // CComboBox
284
Popular Tags