KickJava   Java API By Example, From Geeks To Geeks.

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


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-2002 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.event.*;
20 import javax.swing.text.*;
21 import java.text.*;
22 import java.sql.*;
23 import java.beans.*;
24
25 import org.compiere.util.*;
26 import org.compiere.apps.*;
27 import org.compiere.plaf.*;
28 import org.compiere.swing.*;
29 import java.util.*;
30
31 /**
32  * Date Edit.
33  * Maintains data as Timestamp
34  *
35  * @author Jorg Janke
36  * @version $Id: VDate.java,v 1.31 2003/09/05 04:58:59 jjanke Exp $
37  */

38 public class VDate extends JComponent
39     implements VEditor, ActionListener, KeyListener, FocusListener
40 {
41     /**
42      * IDE Bean Constructor
43      */

44     public VDate()
45     {
46         this (DisplayType.Date);
47     } // VDate
48

49     /**
50      * Simple Constructor
51      * @param displayType display Type
52      */

53     public VDate (int displayType)
54     {
55         this("Date", false, false, true, displayType, "Date");
56     } // VDate
57

58     /**
59      * Create right aligned Date field
60      * @param columnName column name
61      * @param mandatory mandatory
62      * @param isReadOnly read only
63      * @param isUpdateable updateable
64      * @param displayType display type
65      * @param title title
66      */

67     public VDate(String JavaDoc columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
68         int displayType, String JavaDoc title)
69     {
70         super();
71         super.setName(columnName);
72         m_columnName = columnName;
73         m_title = title;
74         //
75
LookAndFeel.installBorder(this, "TextField.border");
76         this.setLayout(new BorderLayout());
77         // Size
78
this.setPreferredSize(m_text.getPreferredSize());
79         int height = m_text.getPreferredSize().height;
80
81         // *** Text ***
82
m_text.setBorder(null);
83         m_text.setHorizontalAlignment(JTextField.TRAILING);
84         if (m_displayType == DisplayType.Date)
85         {
86             m_text.addFocusListener(this);
87             m_text.addKeyListener(this);
88             m_text.setCaret(new VOvrCaret());
89         }
90         else if (m_displayType == DisplayType.DateTime)
91             m_text.setColumns(20);
92         // Background
93
setMandatory(mandatory);
94         this.add(m_text, BorderLayout.CENTER);
95         //
96
if (displayType == DisplayType.DateTime || displayType == DisplayType.Time)
97             m_displayType = displayType; // default = Date
98
setFormat();
99
100         // *** Button ***
101
m_button.setIcon(Env.getImageIcon("Calendar10.gif"));
102         m_button.setMargin(new Insets(0, 0, 0, 0));
103         m_button.setPreferredSize(new Dimension(height, height));
104         m_button.addActionListener(this);
105         m_button.setFocusable(false);
106         this.add(m_button, BorderLayout.EAST);
107
108         // Prefereed Size
109
this.setPreferredSize(this.getPreferredSize()); // causes r/o to be the same length
110

111         // ReadWrite
112
if (isReadOnly || !isUpdateable)
113             setReadWrite(false);
114         else
115             setReadWrite(true);
116     } // VDate
117

118     /**
119      * Dispose
120      */

121     public void dispose()
122     {
123         m_text = null;
124         m_button = null;
125     } // dispose
126

127     /**
128      * Set Document
129      * @param doc doc
130      */

131     protected void setDocument(Document doc)
132     {
133         m_text.setDocument(doc);
134     } // getDocument
135

136     private String JavaDoc m_columnName;
137     protected int m_displayType = DisplayType.Date;
138     private String JavaDoc m_title;
139     private boolean m_setting;
140     private String JavaDoc m_oldText;
141     private String JavaDoc m_initialText;
142     //
143
private SimpleDateFormat m_format;
144     //
145
private boolean m_readWrite;
146     private boolean m_mandatory;
147
148     /** The Text Field */
149     private CTextField m_text = new CTextField (12);
150     /** The Button */
151     private CButton m_button = new CButton();
152
153     /**
154      * Set ReadWrite - field is always r/o for Time or DateTime
155      * @param value value
156      */

157     public void setReadWrite (boolean value)
158     {
159         m_readWrite = value;
160         this.setFocusable(value);
161         // editor
162
if (m_displayType == DisplayType.Date)
163             m_text.setReadWrite(value); // sets Background
164
else
165         {
166             m_text.setEditable(false);
167             m_text.setFocusable(false);
168             setBackground(false);
169         }
170
171         // Don't show button if not ReadWrite
172
if (m_button.isVisible() != value)
173             m_button.setVisible(value);
174         if (m_button.isEnabled() != value)
175             m_button.setEnabled(value);
176     } // setReadWrite
177

178     /**
179      * IsReadWrite
180      * @return true if rw
181      */

182     public boolean isReadWrite()
183     {
184         return m_readWrite;
185     } // isReadWrite
186

187     /**
188      * Set Mandatory (and back bolor)
189      * @param mandatory mandatory
190      */

191     public void setMandatory (boolean mandatory)
192     {
193         m_mandatory = mandatory;
194         m_text.setMandatory(mandatory);
195         setBackground(false);
196     } // setMandatory
197

198     /**
199      * Is it mandatory
200      * @return true if mandatory
201      */

202     public boolean isMandatory()
203     {
204         return m_mandatory;
205     } // isMandatory
206

207     /**
208      * Set Background based on ReadWrite / mandatory / error
209      * @param error if true, set background to error color, otherwise mandatory/ReadWrite
210      */

211     public void setBackground (boolean error)
212     {
213         if (error)
214             m_text.setBackground(CompierePLAF.getFieldBackground_Error());
215         else if (!m_readWrite)
216             m_text.setBackground(CompierePLAF.getFieldBackground_Inactive());
217         else if (m_mandatory)
218             m_text.setBackground(CompierePLAF.getFieldBackground_Mandatory());
219         else
220             m_text.setBackground(CompierePLAF.getFieldBackground_Normal());
221     } // setBackground
222

223     /**
224      * Set Foreground
225      * @param fg color
226      */

227     public void setForeground(Color fg)
228     {
229         m_text.setForeground(fg);
230     } // setForeground
231

232     /**
233      * Set Format
234      * Required when Format/Locale changed
235      */

236     public void setFormat()
237     {
238         m_format = DisplayType.getDateFormat(m_displayType);
239         if (m_displayType == DisplayType.Date)
240             m_text.setDocument(new MDocDate(m_displayType, m_format, m_text, m_title));
241     } // setFormat
242

243     /**
244      * Set Editor to value
245      * @param value timestamp/date or String to be parsed
246      */

247     public void setValue (Object JavaDoc value)
248     {
249     // Log.trace(Log.l5_DData, "VDate.setValue", value);
250
m_oldText = "";
251         if (value == null)
252             ;
253         else if (value instanceof java.util.Date JavaDoc)
254             m_oldText = m_format.format(value);
255         else
256         {
257             String JavaDoc strValue = value.toString();
258             // String values - most likely in YYYY-MM-DD (JDBC format)
259
try
260             {
261                 java.util.Date JavaDoc date = DisplayType.getDateFormat_JDBC().parse (strValue);
262                 m_oldText = m_format.format(date); // convert to display value
263
}
264             catch (ParseException pe0)
265             {
266             // Try local string format
267
try
268                 {
269                     java.util.Date JavaDoc date = m_format.parse(strValue);
270                     m_oldText = m_format.format(date);
271                 }
272                 catch (ParseException pe1)
273                 {
274                     Log.error("VDate.setValue - " + pe1.getMessage());
275                     m_oldText = "";
276                 }
277             }
278         }
279         if (m_setting)
280             return;
281         m_text.setText(m_oldText);
282         m_initialText = m_oldText;
283     } // setValue
284

285     /**
286      * Property Change Listener
287      * @param evt event
288      */

289     public void propertyChange (PropertyChangeEvent evt)
290     {
291         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
292             setValue(evt.getNewValue());
293     } // propertyChange
294

295     /**
296      * Return Editor value
297      * @return value
298      */

299     public Timestamp getTimestamp()
300     {
301         if (m_text == null)
302             return null;
303         String JavaDoc value = m_text.getText();
304         if (value == null || value.length() == 0)
305             return null;
306         //
307
Timestamp ts = null;
308         try
309         {
310             java.util.Date JavaDoc date = m_format.parse(value);
311             ts = new Timestamp(date.getTime());
312         }
313         catch (ParseException pe)
314         {
315             Log.trace(Log.l6_Database, "VDate.getTimestamp - " + pe.getMessage());
316         }
317         return ts;
318     } // getValue
319

320     /**
321      * Return Editor value (Timestamp)
322      * @return value
323      */

324     public Object JavaDoc getValue()
325     {
326         return getTimestamp();
327     } // getValue
328

329     /**
330      * Return Display Value
331      * @return display value
332      */

333     public String JavaDoc getDisplay()
334     {
335         return m_text.getText();
336     } // getDisplay
337

338
339     /*************************************************************************/
340
341     /**
342      * Action Listener (Button)
343      * @param e event
344      */

345     public void actionPerformed (ActionEvent e)
346     {
347         if (e.getSource() == m_button)
348         {
349             m_button.setEnabled(false);
350             setValue(startCalendar(this, getTimestamp(), m_format, m_displayType, m_title));
351             try
352             {
353                 fireVetoableChange (m_columnName, m_oldText, getValue());
354             }
355             catch (PropertyVetoException pve) {}
356             m_button.setEnabled(true);
357             m_text.requestFocus();
358         }
359     } // actionPerformed
360

361     /**
362      * Action Listener Interface (Text)
363      * @param listener listener
364      */
// addActionListener
365

366     /**************************************************************************
367      * Key Listener Interface
368      * @param e Event
369      */

370     public void keyTyped(KeyEvent e) {}
371     public void keyPressed(KeyEvent e) {}
372
373     /**
374      * Key Listener.
375      * - Escape - Restore old Text
376      * - firstChange - signal change
377      * @param e event
378      */

379     public void keyReleased(KeyEvent e)
380     {
381     // System.out.println("VDate " + e);
382
// ESC
383
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
384             m_text.setText(m_initialText);
385         m_setting = true;
386         try
387         {
388             Timestamp ts = getTimestamp(); // getValue
389
if (ts == null) // format error - just indicate change
390
fireVetoableChange (m_columnName, m_oldText, null);
391             else
392                 fireVetoableChange (m_columnName, m_oldText, ts);
393         }
394         catch (PropertyVetoException pve) {}
395         m_setting = false;
396     } // keyReleased
397

398     /**
399      * Focus Gained - Save for Escape
400      * @param e event
401      */

402     public void focusGained (FocusEvent e)
403     {
404     } // focusGained
405

406     /**
407      * Data Binding to to GridController.
408      * @param e event
409      */

410     public void focusLost (FocusEvent e)
411     {
412         // did not get Focus first
413
if (e.isTemporary())
414             return;
415     // Log.trace(Log.l4_Data, "VDate.focusLost");
416
if (m_text == null || m_text.getText() == null)
417             return;
418         Object JavaDoc value = getValue();
419         if (value == null && isMandatory() )
420             setValue(startCalendar(this, getTimestamp(), m_format, m_displayType, m_title));
421         else
422             setValue(value);
423     } // focusLost
424

425     /**
426      * Invalid Entry - Start Calendar
427      * @param jc parent
428      * @param value value
429      * @param format format
430      * @param displayType display type
431      * @param title title
432      * @return formatted Date
433      */

434     public static Timestamp startCalendar(Container jc, Timestamp value,
435         SimpleDateFormat format, int displayType, String JavaDoc title)
436     {
437         Log.trace(Log.l3_Util, "VDate.startCalendar", value);
438
439         // Find frame
440
Frame frame = Env.getFrame(jc);
441         // Actual Call
442
Calendar cal = new Calendar(frame, title, value, displayType);
443         AEnv.showCenterWindow(frame, cal);
444         Timestamp result = cal.getTimestamp();
445         cal = null;
446         //
447
Log.trace(Log.l4_Data, "Result=" + result);
448         if (result != null)
449             return result;
450         else
451             return value; // original value
452
} // startCalendar
453

454     /**
455      * Set Field/WindowNo for ValuePreference (NOP)
456      * @param mField MField
457      */

458     public void setField (org.compiere.model.MField mField)
459     {
460     } // setField
461

462     /*************************************************************************/
463
464     /**
465      * Remove Action Listner
466      * @param l Action Listener
467      */

468     public void removeActionListener(ActionListener l)
469     {
470         listenerList.remove(ActionListener.class, l);
471     } // removeActionListener
472

473     /**
474      * Add Action Listner
475      * @param l Action Listener
476      */

477     public void addActionListener(ActionListener l)
478     {
479         listenerList.add(ActionListener.class, l);
480     } // addActionListener
481

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

508 } // VDate
509
Popular Tags