KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > VNumber


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.grid.ed;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import javax.swing.text.*;
20 import java.text.*;
21 import java.math.*;
22 import java.beans.*;
23
24 import org.compiere.util.*;
25 import org.compiere.apps.*;
26 import org.compiere.plaf.*;
27 import org.compiere.swing.*;
28 import org.compiere.model.*;
29
30 /**
31  * Number Control
32  *
33  * @author Jorg Janke
34  * @version $Id: VNumber.java,v 1.26 2003/09/07 04:48:35 jjanke Exp $
35  */

36 public final class VNumber extends JComponent
37     implements VEditor, ActionListener, KeyListener, FocusListener
38 {
39     public final static int SIZE = 12; // width of field
40

41     /**
42      * IDE Bean Constructor
43      */

44     public VNumber()
45     {
46         this("Number", false, false, true, DisplayType.Number, "Number");
47     } // VNumber
48

49     /**
50      * Create right aligned Number field.
51      * no popup, if WindowNo == 0 (for IDs)
52      * @param columnName column name
53      * @param mandatory mandatory
54      * @param isReadOnly read only
55      * @param isUpdateable updateable
56      * @param displayType display type
57      * @param title title
58      */

59     public VNumber(String JavaDoc columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
60         int displayType, String JavaDoc title)
61     {
62         super();
63         super.setName(columnName);
64         m_columnName = columnName;
65         m_title = title;
66         setDisplayType(displayType);
67         //
68
LookAndFeel.installBorder(this, "TextField.border");
69         this.setLayout(new BorderLayout());
70         // Size
71
this.setPreferredSize(m_text.getPreferredSize()); // causes r/o to be the same length
72
int height = m_text.getPreferredSize().height;
73
74         // *** Text ***
75
m_text.setBorder(null);
76         m_text.setHorizontalAlignment(JTextField.TRAILING);
77         m_text.addKeyListener(this);
78         m_text.addFocusListener(this);
79         // Background
80
setMandatory(mandatory);
81         this.add(m_text, BorderLayout.CENTER);
82
83         // *** Button ***
84
m_button.setIcon(Env.getImageIcon("Calculator10.gif"));
85         m_button.setMargin(new Insets(0, 0, 0, 0));
86         m_button.setFocusable(false);
87         m_button.setPreferredSize(new Dimension(height, height));
88         m_button.addActionListener(this);
89         this.add (m_button, BorderLayout.EAST);
90
91         // Prefereed Size
92
this.setPreferredSize(this.getPreferredSize()); // causes r/o to be the same length
93

94         // ReadWrite
95
if (isReadOnly || !isUpdateable)
96             setReadWrite(false);
97         else
98             setReadWrite(true);
99     } // VNumber
100

101     /**
102      * Dispose
103      */

104     public void dispose()
105     {
106         m_text = null;
107         m_button = null;
108         m_mField = null;
109     } // dispose
110

111     /**
112      * Set Document
113      * @param doc document
114      */

115     protected void setDocument(Document doc)
116     {
117         m_text.setDocument(doc);
118     } // getDocument
119

120     private String JavaDoc m_columnName;
121     protected int m_displayType; // Currency / UoM via Context
122
private DecimalFormat m_format;
123     private String JavaDoc m_title;
124     private boolean m_setting;
125     private String JavaDoc m_oldText;
126     private String JavaDoc m_initialText;
127
128     private boolean m_rangeSet = false;
129     private Double JavaDoc m_minValue;
130     private Double JavaDoc m_maxValue;
131
132     /** The Field */
133     private CTextField m_text = new CTextField(SIZE); // Standard
134
/** The Button */
135     private CButton m_button = new CButton();
136
137     private MField m_mField = null;
138
139     /**
140      * Set Range with min & max
141      * @param minValue min value
142      * @param maxValue max value
143      * @return true, if accepted
144      */

145     public boolean setRange(Double JavaDoc minValue, Double JavaDoc maxValue)
146     {
147         m_rangeSet = true;
148         m_minValue = minValue;
149         m_maxValue = maxValue;
150         return m_rangeSet;
151     } // setRange
152

153     /**
154      * Set Range with min & max = parse US style number w/o Gouping
155      * @param minValue min value
156      * @param maxValue max value
157      * @return true if accepted
158      */

159     public boolean setRange(String JavaDoc minValue, String JavaDoc maxValue)
160     {
161         if (minValue == null || maxValue == null)
162             return false;
163         try
164         {
165             m_minValue = Double.valueOf(minValue);
166             m_maxValue = Double.valueOf(maxValue);
167         }
168         catch (NumberFormatException JavaDoc nfe)
169         {
170             return false;
171         }
172         m_rangeSet = true;
173         return m_rangeSet;
174     } // setRange
175

176     /**
177      * Set and check DisplayType
178      * @param displayType display type
179      */

180     public void setDisplayType (int displayType)
181     {
182         m_displayType = displayType;
183         if (!DisplayType.isNumeric(displayType))
184             m_displayType = DisplayType.Number;
185         m_format = DisplayType.getNumberFormat(displayType);
186         m_text.setDocument (new MDocNumber(displayType, m_format, m_text, m_title));
187     } // setDisplayType
188

189     /**
190      * Set ReadWrite
191      * @param value value
192      */

193     public void setReadWrite (boolean value)
194     {
195         if (m_text.isReadWrite() != value)
196             m_text.setReadWrite(value);
197         if (m_button.isReadWrite() != value)
198             m_button.setReadWrite(value);
199         // Don't show button if not ReadWrite
200
if (m_button.isVisible() != value)
201             m_button.setVisible(value);
202     } // setReadWrite
203

204     /**
205      * IsReadWrite
206      * @return true if rw
207      */

208     public boolean isReadWrite()
209     {
210         return m_text.isReadWrite();
211     } // isReadWrite
212

213     /**
214      * Set Mandatory (and back bolor)
215      * @param mandatory mandatory
216      */

217     public void setMandatory (boolean mandatory)
218     {
219         m_text.setMandatory(mandatory);
220     } // setMandatory
221

222     /**
223      * Is it mandatory
224      * @return true if mandatory
225      */

226     public boolean isMandatory()
227     {
228         return m_text.isMandatory();
229     } // isMandatory
230

231     /**
232      * Set Background
233      * @param color color
234      */

235     public void setBackground(Color color)
236     {
237         m_text.setBackground(color);
238     } // setBackground
239

240     /**
241      * Set Background
242      * @param error error
243      */

244     public void setBackground (boolean error)
245     {
246         m_text.setBackground(error);
247     } // setBackground
248

249     /**
250      * Set Foreground
251      * @param fg foreground
252      */

253     public void setForeground(Color fg)
254     {
255         m_text.setForeground(fg);
256     } // setForeground
257

258     /**
259      * Set Editor to value
260      * @param value value
261      */

262     public void setValue(Object JavaDoc value)
263     {
264         Log.trace(Log.l6_Database, "VNumber.setValue", value);
265         if (value == null)
266             m_oldText = "";
267         else
268             m_oldText = m_format.format(value);
269         // only set when not updated here
270
if (m_setting)
271             return;
272         m_text.setText (m_oldText);
273         m_initialText = m_oldText;
274     } // setValue
275

276     /**
277      * Property Change Listener
278      * @param evt event
279      */

280     public void propertyChange (PropertyChangeEvent evt)
281     {
282         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
283             setValue(evt.getNewValue());
284     } // propertyChange
285

286     /**
287      * Return Editor value
288      * @return value value
289      */

290     public Object JavaDoc getValue()
291     {
292         if (m_text == null)
293             return null;
294         String JavaDoc value = m_text.getText();
295         if (value == null || value.length() == 0)
296             return null;
297         if (value.equals(".") || value.equals(",") || value.equals("-"))
298             value = "0";
299         try
300         {
301             Number JavaDoc number = m_format.parse(value);
302             value = number.toString(); // converts it to US w/o thousands
303
BigDecimal bd = new BigDecimal(value);
304             return bd.setScale(m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP);
305         }
306         catch (Exception JavaDoc e)
307         {
308             Log.error("VNumber.getValue", e);
309         }
310         return Env.ZERO;
311     } // getValue
312

313     /**
314      * Return Display Value
315      * @return value
316      */

317     public String JavaDoc getDisplay()
318     {
319         return m_text.getText();
320     } // getDisplay
321

322
323     /*************************************************************************/
324
325     /**
326      * Action Listener
327      * @param e event
328      */

329     public void actionPerformed (ActionEvent e)
330     {
331         Log.trace(Log.l4_Data, "VNumber.actionPerformed", e.getActionCommand());
332         if (e.getActionCommand().equals(ValuePreference.NAME))
333         {
334             ValuePreference.start (m_mField, getValue());
335             return;
336         }
337
338         if (e.getSource() == m_button)
339         {
340             m_button.setEnabled(false);
341             String JavaDoc str = startCalculator(this, m_text.getText(), m_format, m_displayType, m_title);
342             m_text.setText(str);
343             m_button.setEnabled(true);
344             try
345             {
346                 fireVetoableChange (m_columnName, m_oldText, getValue());
347             }
348             catch (PropertyVetoException pve) {}
349             m_text.requestFocus();
350         }
351     } // actionPerformed
352

353     /**************************************************************************
354      * Key Listener Interface
355      * @param e event
356      */

357     public void keyTyped(KeyEvent e) {}
358     public void keyPressed(KeyEvent e) {}
359
360     /**
361      * Key Listener.
362      * - Escape - Restore old Text
363      * - firstChange - signal change
364      * @param e event
365      */

366     public void keyReleased(KeyEvent e)
367     {
368         Log.trace(Log.l4_Data, "VNumber.keyReleased - " + e.getKeyCode());
369         // ESC
370
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
371             m_text.setText(m_initialText);
372         m_setting = true;
373         try
374         {
375             fireVetoableChange (m_columnName, m_oldText, null); //getValue());
376
}
377         catch (PropertyVetoException pve) {}
378         m_setting = false;
379     } // keyReleased
380

381     /**
382      * Focus Gained
383      * @param e event
384      */

385     public void focusGained (FocusEvent e)
386     {
387         if (m_text != null)
388             m_text.selectAll();
389     } // focusGained
390

391     /**
392      * Data Binding to MTable (via GridController).
393      * @param e event
394      */

395     public void focusLost (FocusEvent e)
396     {
397         try
398         {
399             fireVetoableChange (m_columnName, m_initialText, getValue());
400         }
401         catch (PropertyVetoException pve) {}
402     } // focusLost
403

404     /**
405      * Invalid Entry - Start Calculator
406      * @param jc parent
407      * @param value value
408      * @param format format
409      * @param displayType display type
410      * @param title title
411      * @return value
412      */

413     public static String JavaDoc startCalculator(Container jc, String JavaDoc value,
414         DecimalFormat format, int displayType, String JavaDoc title)
415     {
416         Log.trace(Log.l3_Util, "VNumber startCalculator - " + value);
417         BigDecimal startValue = new BigDecimal(0.0);
418         try
419         {
420             if (value != null && value.length() > 0)
421             {
422                 Number JavaDoc number = format.parse(value);
423                 startValue = new BigDecimal (number.toString());
424             }
425         }
426         catch (ParseException pe)
427         {
428             Log.error("VNumber invalidEntry - " + pe.getMessage());
429         }
430
431         // Find frame
432
Frame frame = Env.getFrame(jc);
433         // Actual Call
434
Calculator calc = new Calculator(frame, title,
435             displayType, format, startValue);
436         AEnv.showCenterWindow(frame, calc);
437         BigDecimal result = calc.getNumber();
438         Log.trace(Log.l4_Data, "Result=" + result);
439         //
440
calc = null;
441         if (result != null)
442             return format.format(result);
443         else
444             return value; // original value
445
} // startCalculator
446

447     /**
448      * Set Field/WindowNo for ValuePreference
449      * @param mField field
450      */

451     public void setField (MField mField)
452     {
453     // m_mField = mField;
454
// if (m_mField != null)
455
// ValuePreference.addMenu (this, popupMenu);
456
} // setField
457

458     /*************************************************************************/
459
460     /**
461      * Remove Action Listner
462      * @param l Action Listener
463      */

464     public void removeActionListener(ActionListener l)
465     {
466         listenerList.remove(ActionListener.class, l);
467     } // removeActionListener
468

469     /**
470      * Add Action Listner
471      * @param l Action Listener
472      */

473     public void addActionListener(ActionListener l)
474     {
475         listenerList.add(ActionListener.class, l);
476     } // addActionListener
477

478     /**
479      * Fire Action Event to listeners
480      *
481     protected void fireActionPerformed()
482     {
483         int modifiers = 0;
484         AWTEvent currentEvent = EventQueue.getCurrentEvent();
485         if (currentEvent instanceof InputEvent)
486             modifiers = ((InputEvent)currentEvent).getModifiers();
487         else if (currentEvent instanceof ActionEvent)
488             modifiers = ((ActionEvent)currentEvent).getModifiers();
489         ActionEvent ae = new ActionEvent (this, ActionEvent.ACTION_PERFORMED,
490             "VDate", EventQueue.getMostRecentEventTime(), modifiers);
491
492         // Guaranteed to return a non-null array
493         Object[] listeners = listenerList.getListenerList();
494         // Process the listeners last to first, notifying those that are interested in this event
495         for (int i = listeners.length-2; i>=0; i-=2)
496         {
497             if (listeners[i]==ActionListener.class)
498             {
499                 ((ActionListener)listeners[i+1]).actionPerformed(ae);
500             }
501         }
502     } // fireActionPerformed
503     /**/

504 } // VNumber
505
Popular Tags