KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > components > DjDateChooser


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.components;
31
32 import java.awt.event.ActionListener JavaDoc;
33 import java.awt.event.FocusEvent JavaDoc;
34 import java.awt.event.FocusListener JavaDoc;
35
36 import javax.swing.JComponent JavaDoc;
37
38 import com.genimen.djeneric.repository.DjProperty;
39 import com.genimen.djeneric.repository.exceptions.DjenericException;
40 import com.genimen.djeneric.structure.PropertyUsage;
41 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
42 import com.genimen.djeneric.ui.DateChooser;
43 import com.genimen.djeneric.util.DjLogger;
44
45 public class DjDateChooser extends DateChooser implements DjBindable, FocusListener JavaDoc
46 {
47   private static final long serialVersionUID = 1L;
48   private BindingMediator _mediator;
49   private ObjectViewer _viewer;
50   private String JavaDoc _propertyName;
51   private boolean _required = true;
52
53   public DjDateChooser(ObjectViewer viewer, String JavaDoc propertyName) throws DjenericException
54   {
55     super();
56     getFocussableComponent().addFocusListener(this);
57     _mediator = new BindingMediator(this, viewer, propertyName);
58     _viewer = viewer;
59     _propertyName = propertyName;
60     setDateFormat(propertyName);
61   }
62
63   public DjDateChooser(DjProperty p)
64   {
65     super();
66     _propertyName = p.getName();
67     setDateFormatPattern(p.getType().getFormatMask());
68   }
69
70   private void setDateFormat(String JavaDoc propertyName) throws DjenericException
71   {
72     PropertyUsage pu = _viewer.getPropertyUsageByName(propertyName);
73     DjProperty p = pu.getProperty();
74     setDateFormatPattern(p.getType().getFormatMask());
75   }
76
77   public boolean isComponentWritable()
78   {
79     return _mediator.isComponentWritable();
80   }
81
82   public void clear()
83   {
84     super.clear();
85   }
86
87   public void apply() throws DjenericException
88   {
89     _mediator.setValue(getTextField().getText());
90   }
91
92   public void focusGained(FocusEvent JavaDoc e)
93   {
94     super.selectAll();
95   }
96
97   public void focusLost(FocusEvent JavaDoc e)
98   {
99     try
100     {
101       apply();
102       synchronize();
103     }
104     catch (Exception JavaDoc x)
105     {
106       DjLogger.log(x);
107     }
108   }
109
110   public void setPropertyName(String JavaDoc propertyName) throws DjenericException
111   {
112     _mediator.setPropertyName(propertyName);
113   }
114
115   public void setViewer(ObjectViewer viewer) throws DjenericException
116   {
117     if (_viewer == null)
118     {
119       _viewer = viewer;
120       _mediator = new BindingMediator(this, _viewer, _propertyName);
121       setDateFormat(_propertyName);
122     }
123   }
124
125   public void synchronize() throws DjenericException
126   {
127     try
128     {
129       String JavaDoc dateString = _mediator.getPropertyValueString();
130       if (dateString != null && dateString.trim().length() > 0) setDate(getDateFormat().parse(dateString));
131     }
132     catch (java.text.ParseException JavaDoc e)
133     {
134       DjLogger.log(e);
135     }
136   }
137
138   public Object JavaDoc getDisplayedValue()
139   {
140     if (getDate() == null)
141     {
142       return "";
143     }
144     else
145     {
146       return getDateFormat().format(getDate());
147     }
148   }
149
150   public void setDisplayedValue(Object JavaDoc value)
151   {
152     try
153     {
154       if (value != null)
155       {
156         setDate(getDateFormat().parse(value.toString()));
157       }
158     }
159     catch (java.text.ParseException JavaDoc e)
160     {
161       DjLogger.log(e);
162     }
163   }
164
165   public String JavaDoc getPropertyName()
166   {
167     return _mediator.getPropertyName();
168   }
169
170   public void addActionListener(ActionListener JavaDoc l)
171   {
172
173   }
174
175   public void removeActionListener(ActionListener JavaDoc l)
176   {
177
178   }
179
180   public boolean isRequired()
181   {
182     return _required;
183   }
184
185   public void setRequired(boolean b)
186   {
187     _required = b;
188   }
189
190   public JComponent JavaDoc getFocussableComponent()
191   {
192     return getTextField();
193   }
194 }
Popular Tags