Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/awt/DatePanel.java


1   /*
2    * DatePanel.java
3    *
4    * Copyright $Date: 2003/08/19 04:06:47 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.awt;
12  
13  import java.awt.Checkbox;
14  import java.awt.Frame;
15  import java.awt.GridBagConstraints;
16  import java.awt.GridBagLayout;
17  import java.awt.Panel;
18  import java.awt.event.ActionListener;
19  import java.awt.event.ItemEvent;
20  import java.awt.event.ItemListener;
21  import java.beans.PropertyChangeListener;
22  import java.beans.PropertyChangeSupport;
23  import java.text.DateFormat;
24  import java.util.Date;
25  
26  //TODO:3PG fix or remove import symantec.itools.awt.RadioButtonGroupPanel;
27  
28  import com.flexstor.common.awt.field.ActionButton;
29  import com.flexstor.common.resources.Resources;
30  
31  /**
32   * This class implements a date selection panel which can be plugged
33   * into any container which needs this functionality.
34   * The panel is self contained, meaning it enables/disables its components
35   * correctly depending on user input and notifies potential
36   * property change listeners about relevant changes.
37   */
38  public class DatePanel extends Panel
39  //TODO:3PG fix or remove extends RadioButtonGroupPanel
40     implements ItemListener, ActionListener
41  {
42     protected PropertyChangeSupport changes = new PropertyChangeSupport(this);
43     protected boolean bValid = true;
44  
45     protected Date           dtSend       = null;   // date object to reference selected send date
46     protected Date           dtLowerLimit = null;   // lowest selectable date
47     protected Frame          fParent;
48  
49     protected Checkbox       rbImmediately;
50     protected Panel          pnlAt;
51     protected Checkbox       rbAt;
52     protected ActionButton pbSelectDate;
53     protected boolean        bCanbeEnabled = true;
54  
55     public DatePanel(Frame fParent)
56     {
57        GridBagLayout      gblMain = new GridBagLayout();
58        GridBagLayout      gblAt   = new GridBagLayout();
59        GridBagConstraints gbc     = new GridBagConstraints();
60  
61        this.fParent       = fParent;
62        
63  //  TODO:3PG fix or remove 
64  /*      try
65        {
66           setLabel(Resources.get(5283));
67           setPadding(10, 6, 4, 4); // t10 b6 l4 r4
68           setIPadBottom(1);
69           setBevelStyle(FlexBorderPanel.BEVEL_LINE);
70           setAlignStyle(FlexBorderPanel.ALIGN_LEFT);
71        }
72        catch(java.beans.PropertyVetoException e) { }
73  */
74        setLayout(gblMain);
75  
76        // radio button 'Immediately'
77        rbImmediately = new Checkbox(Resources.get(5284));
78        add(rbImmediately);
79        gbc = AWTUtil.getGbc(0,0,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL);
80        gblMain.setConstraints(rbImmediately, gbc);
81  
82        rbImmediately.setState ( true );
83  
84        // panel with radio button 'At' and 'Select' button
85        pnlAt = new Panel();
86        pnlAt.setLayout(gblAt);
87        add(pnlAt);
88        gbc.gridy = 1;
89        gblMain.setConstraints(pnlAt, gbc);
90  
91        // At: Date/Time
92        rbAt = new Checkbox(Resources.get(5285) + ": " + Resources.get(5286) + "");
93        add(rbAt); // added to RadioGroupPanel for radio button functionality
94        gbc = AWTUtil.getGbc(0, 0, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
95        gblAt.setConstraints(rbAt, gbc);
96        pnlAt.add(rbAt);
97  
98        pbSelectDate = new ActionButton(-1);
99        pbSelectDate.setLabel(Resources.get(5287));
100       gbc = AWTUtil.getGbc(1, 0, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE);
101       gblAt.setConstraints(pbSelectDate, gbc);
102       pnlAt.add(pbSelectDate);
103       pbSelectDate.setEnabled(!rbImmediately.getState());
104 
105       // dummy filler for Date/Time panel
106       Panel pnlDummy = new Panel();
107       gbc = AWTUtil.getGbc(2, 0, 1, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH);
108       gblAt.setConstraints( pnlDummy, gbc );
109       pnlAt.add( pnlDummy );
110 
111       // dummy filler for main panel
112       Panel dummy = new Panel();
113       gbc = AWTUtil.getGbc(0,2,1,1,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
114       gblMain.setConstraints(dummy, gbc);
115       add(dummy);
116 
117   //TODO:3PG fix or remove addItemListener(this);
118       pbSelectDate.addActionListener(this);
119    }
120 
121    /**
122     * strBorderLabel : Label of the border
123     */
124    public DatePanel(Frame fParent, String strBorderLabel)
125    {
126      this ( fParent );
127 //  TODO:3PG fix or remove 
128 /*
129      try
130      {
131       if ( ( strBorderLabel != null )&&
132            ( strBorderLabel.trim().length() != 0 ) )
133       {
134     setLabel ( strBorderLabel );
135       }
136      }
137      catch(java.beans.PropertyVetoException e) {}
138 */
139    }
140 
141 
142    /**
143     * Returns the currently selected date if the 'At' radio
144     * button is selected, otherwise null.
145     */
146    public Date getDate()
147    {
148 //  TODO:3PG fix or remove 
149 /*
150       if ( getSelectedRadioButton() == rbImmediately )
151          return null;
152       else
153 */
154          return dtSend;
155    }
156 
157    public void setDate ( Date date )
158    {
159       dtSend = date;
160 
161       if ( dtSend != null )
162       {
163 
164        DateFormat df = DateFormat.getDateTimeInstance( DateFormat.SHORT,
165                                                        DateFormat.SHORT );
166        df.setTimeZone( java.util.TimeZone.getDefault() );
167        rbAt.setLabel( Resources.get( 5285 ) + ": " + df.format( dtSend ) );
168        try
169        {
170     //TODO:3PG fix or remove setSelectedRadioButton( rbAt );
171        } catch ( Exception e ) {}
172 
173 
174        pnlAt.doLayout ();
175 
176       }
177    }
178 
179    /**
180     * Return true if all entries make the panel valid.
181     * Could not use isValid() because it overlaps with java.awt.Component.isValid()
182     * Ex: If 'At' is selected but no date, it would return false.
183     */
184    public boolean isComplete()
185    {
186       return bValid;
187    }
188 
189    /**
190     * Enables/Disables input.
191     * NOTE : Be ware, if this method is called with a 'false', there is NO WAY right
192     * now to enable any of the components on the panel ! It is written keeping in mind the
193     * requirement that the user should not be able to provide any input what so ever. One of
194     * requirement was found on settings dialog where this panel should be disabled fully
195     * when called from the admin tool.. but
196     * should be enabled when called from the Import frame.
197     */
198    public void enableInput ( boolean mode )
199    {
200      rbImmediately.setEnabled ( mode );
201      rbAt.setEnabled ( mode );
202      pbSelectDate.setEnabled ( mode );
203 
204      bCanbeEnabled = mode;
205    }
206    /**
207     * This method is overridden in association with 'enableInput' method.
208     */
209    public void setEnabled ( boolean mode )
210    {
211      super.setEnabled ( mode && bCanbeEnabled );
212    }
213 
214    /**
215     * Implementing ItemListener.
216     * Evaluates the status of the panel and notifies any property
217     * change listeners.
218     */
219    public void itemStateChanged(ItemEvent event)
220    {
221       if ( event.getSource() == this )
222       {
223          if ( rbAt == event.getItem() )
224          {
225             bValid = ( dtSend != null );
226          }
227          else if ( rbImmediately == event.getItem() )
228          {
229             bValid = true;
230          }
231          pbSelectDate.setEnabled(rbAt == event.getItem());
232          changes.firePropertyChange("valid", Boolean.FALSE, Boolean.TRUE);
233       }
234    }
235 
236    public void refresh()
237    {
238          if ( rbAt.getState() )
239          {
240             bValid = ( dtSend != null );
241          }
242          else if ( rbImmediately.getState())
243          {
244             bValid = true;
245          }
246          pbSelectDate.setEnabled(rbAt.getState());
247    }
248 
249    /**
250     * Implementing ActionListener.
251     * Shows the date selection dialog.
252     */
253    public void actionPerformed(java.awt.event.ActionEvent event)
254    {
255       if (event.getSource() == pbSelectDate)
256       {
257          pbSelectDate.setEnabled(false);
258          //showDateSelectionDlg();
259          pbSelectDate.setEnabled(true);
260       }
261    }
262 
263    public void setLowerLimit(Date date)
264    {
265       dtLowerLimit = date;
266    }
267 /*
268    protected void showDateSelectionDlg()
269    {
270       CalendarDlg d = new CalendarDlg(fParent, true);
271       if (dtLowerLimit != null)
272          d.setLowerLimit(dtLowerLimit);
273       d.setLocation(pbSelectDate.getLocationOnScreen().x,
274                     pbSelectDate.getLocationOnScreen().y + pbSelectDate.getSize().height);
275       d.setVisible(true);
276       dtSend = d.getDate();
277       if (dtSend != null)
278       {
279          DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
280          df.setTimeZone(java.util.TimeZone.getDefault());
281          rbAt.setLabel(Resources.get(5285) + ": " + df.format(dtSend));
282          pnlAt.doLayout();
283          //update status
284          itemStateChanged(new ItemEvent(this,
285                                         ItemEvent.ITEM_STATE_CHANGED  ,
286                                         rbAt,
287                                         ItemEvent.SELECTED));
288       }
289       d.dispose();
290    }
291 */
292    public void addPropertyChangeListener(PropertyChangeListener l)
293    {
294      changes.addPropertyChangeListener(l);
295    }
296 
297    public void removePropertyChangeListener(PropertyChangeListener l)
298    {
299      changes.removePropertyChangeListener(l);
300    }
301 
302    // the next two methods overwrite methods from Symantec's
303    // BorderPanel (superclass of RadioButtonGroupPanel)
304    // in order to return REASONABLE sizes dependant on its content
305    // instead of a fixed dimension
306    public java.awt.Dimension getPreferredSize()
307    {
308       if (getLayout() == null)
309          return super.getPreferredSize();
310       else
311          return getLayout().preferredLayoutSize(this);
312    }
313 
314    public java.awt.Dimension getMinimumSize()
315    {
316       if (getLayout() == null)
317          return super.getMinimumSize();
318       else
319          return getLayout().minimumLayoutSize(this);
320    }
321 
322 }  // end of class