KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dialog JavaDoc;
18 import java.awt.Frame JavaDoc;
19 import java.awt.Window JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.text.DecimalFormat JavaDoc;
25 import java.text.NumberFormat JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.JComboBox JavaDoc;
31 import javax.swing.JOptionPane JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import javax.swing.plaf.TextUI JavaDoc;
34
35 import org.compiere.plaf.CompierePLAF;
36
37 /**
38  * Compiere Colored Field with external popup editor.
39  * It extends ComboBox for UI consistency purposes
40  *
41  * @author Jorg Janke
42  * @version $Id: CField.java,v 1.6 2003/09/27 11:08:51 jjanke Exp $
43  */

44 public class CField extends JComboBox JavaDoc
45     implements CEditor, ActionListener JavaDoc
46 {
47
48     public static CField createNumeric(NumberFormat JavaDoc format)
49     {
50         return null;
51     }
52     public static CField createNumeric()
53     {
54         return createNumeric(new DecimalFormat JavaDoc());
55     }
56     public static CField createDate(DateFormat JavaDoc format)
57     {
58         return null;
59     }
60     public static CField createDate()
61     {
62         return createDate(new SimpleDateFormat JavaDoc());
63     }
64     public static CField createText(Pattern JavaDoc p)
65     {
66         return null;
67     }
68     public static CField createText(int length)
69     {
70         return null;
71     }
72
73     /*************************************************************************/
74
75     public CField ()
76     {
77         this (null, null, "");
78     }
79
80     /**
81      * Construct Compiere Field with external popup editor
82      *
83      * @param editor the (validating) editor
84      * @param cFieldPopup the popup dialog
85      * @param title title for popup
86      */

87     public CField (CFieldEditor editor, Class JavaDoc cFieldPopup, String JavaDoc title)
88     {
89         super(new Object JavaDoc[] {"1", "2"});
90         if (editor != null)
91 // setEditor (editor);
92
setEditable(true);
93         m_title = title;
94
95         // Check popup
96
if (cFieldPopup != null)
97         {
98             Class JavaDoc[] interfaces = cFieldPopup.getInterfaces();
99             boolean found = false;
100             for (int i = 0; i < interfaces.length; i++)
101             {
102                 if (interfaces[i].equals(CFieldPopup.class))
103                 {
104                     found = true;
105                     break;
106                 }
107             }
108             if (!found)
109                 throw new IllegalArgumentException JavaDoc ("CField - Popup class must be CFieldPopup");
110         }
111         super.addActionListener(this);
112     } // CField
113

114     private CFieldEditor m_editor = null;
115     private Class JavaDoc m_popupClass = null;
116     private String JavaDoc m_title = null;
117     private Object JavaDoc m_oldValue = null;
118
119     /*************************************************************************/
120
121     /** Icon */
122     private Icon JavaDoc m_icon = null;
123
124     /**
125      * Set Icon of arrow button to icon
126      * @param defaultIcon Icon to be displayed
127      */

128     public void setIcon (Icon JavaDoc defaultIcon)
129     {
130         m_icon = defaultIcon;
131     } // setIcon
132

133     /**
134      * Get Icon of arrow button to icon
135      * @return defaultIcon Icon to be displayed
136      */

137     public Icon JavaDoc getIcon ()
138     {
139         return m_icon;
140     } // getIcon
141

142
143     /**
144      * Set UI and re-set Icon for arrow button
145      * @param ui
146      */

147     public void setUI (TextUI JavaDoc ui)
148     {
149         super.setUI(ui);
150     } // setUI
151

152     /**
153      * Display Popup.
154      * Called from CompiereComboPopup and allows to implement
155      * alternative actions than showing the popup
156      * @return if true, the popup should be displayed
157      */

158     public boolean displayPopup()
159     {
160         if (m_popupClass == null)
161             return false;
162         //
163
try
164         {
165             // Get Owner & Create Popup Instance
166
Window JavaDoc win = SwingUtilities.getWindowAncestor(this);
167             CFieldPopup popup = null;
168             if (win instanceof Dialog JavaDoc)
169             {
170                 Constructor JavaDoc constructor = m_popupClass.getConstructor
171                     (new Class JavaDoc[] {Dialog JavaDoc.class, String JavaDoc.class, Boolean JavaDoc.class});
172                 popup = (CFieldPopup)constructor.newInstance(new Object JavaDoc[]
173                     {(Dialog JavaDoc)win, m_title, new Boolean JavaDoc(true)});
174             }
175             else if (win instanceof Frame JavaDoc)
176             {
177                 Constructor JavaDoc constructor = m_popupClass.getConstructor
178                     (new Class JavaDoc[] {Frame JavaDoc.class, String JavaDoc.class, Boolean JavaDoc.class});
179                 popup = (CFieldPopup)constructor.newInstance(new Object JavaDoc[]
180                     {(Frame JavaDoc)win, m_title, new Boolean JavaDoc(true)});
181             }
182             if (popup == null)
183                 return false;
184             // Start Popup
185
popup.setValue (m_editor.getItem());
186             popup.setFormat (m_editor.getFormat());
187             popup.show();
188             m_editor.setItem (popup.getValue());
189             popup = null;
190         }
191         catch (Exception JavaDoc e)
192         {
193             notifyUser (e);
194         }
195         //
196
return false;
197     } // displayPopup
198

199     /**
200      * Notify User of a Ptoblem with starting popup
201      * @param e Exception
202      */

203     public void notifyUser (Exception JavaDoc e)
204     {
205         JOptionPane.showMessageDialog(this, e.toString(), "Field Error", JOptionPane.ERROR_MESSAGE);
206     } // notify User
207

208     /*************************************************************************/
209
210     /** Mandatory (default false) */
211     private boolean m_mandatory = false;
212
213     /**
214      * Set Editor Mandatory
215      * @param mandatory true, if you have to enter data
216      */

217     public void setMandatory (boolean mandatory)
218     {
219         m_mandatory = mandatory;
220         setBackground(false);
221     } // setMandatory
222

223     /**
224      * Is Field mandatory
225      * @return true, if mandatory
226      */

227     public boolean isMandatory()
228     {
229         return m_mandatory;
230     } // isMandatory
231

232     /**
233      * Enable Editor
234      * @param rw true, if you can enter/select data
235      */

236     public void setReadWrite (boolean rw)
237     {
238         if (super.isEnabled() != rw)
239             super.setEnabled (rw);
240         setBackground(false);
241     } // setReadWrite
242

243     /**
244      * Is it possible to edit
245      * @return true, if editable
246      */

247     public boolean isReadWrite()
248     {
249         return super.isEnabled();
250     } // isReadWrite
251

252
253     /**
254      * Set Background based on editable / mandatory / error
255      * @param error if true, set background to error color, otherwise mandatory/editable
256      */

257     public void setBackground (boolean error)
258     {
259         Color JavaDoc bg = null;
260         if (error)
261             bg = CompierePLAF.getFieldBackground_Error();
262         else if (!isReadWrite())
263             bg = CompierePLAF.getFieldBackground_Inactive();
264         else if (m_mandatory)
265             bg = CompierePLAF.getFieldBackground_Mandatory();
266         else
267             bg = CompierePLAF.getFieldBackground_Normal();
268         if (bg.equals(m_editor.getBackground()))
269             return;
270         m_editor.setBackground(bg);
271     } // setBackground
272

273     /**
274      * Set Editor to value
275      * @param value value of the editor
276      */

277     public void setValue (Object JavaDoc value)
278     {
279         m_oldValue = value;
280     // super.setSelectedItem(value);
281
} // setValue
282

283     /**
284      * Return Editor value
285      * @return current value
286      */

287     public Object JavaDoc getValue()
288     {
289         return null;//super.getSelectedItem();
290
} // getValue
291

292     /**
293      * Return Display Value
294      * @return displayed String value
295      */

296     public String JavaDoc getDisplay()
297     {
298     // if (super.getSelectedItem() == null)
299
return "";
300     // return super.getSelectedItem().toString();
301
} // getDisplay
302

303     /*************************************************************************/
304
305     /**
306      * Action Listener
307      * @param e ActionEvent
308      */

309     public void actionPerformed(ActionEvent JavaDoc e)
310     {
311         // Do er have a change?
312
Object JavaDoc newValue = getValue();
313         if ((newValue != null && newValue.equals(m_oldValue))
314             || (newValue == null && m_oldValue == null) )
315             return;
316         super.firePropertyChange("DataChanged", m_oldValue, newValue);
317         m_oldValue = newValue;
318     } // // actionPerformed
319

320 } // CField
321
Popular Tags