KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > view > DescriptorCCDateTime


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.guiframework.view;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.RequestManager;
28 import com.iplanet.jato.NavigationException;
29 import com.iplanet.jato.model.ModelControlException;
30 import com.iplanet.jato.view.ContainerView;
31 import com.iplanet.jato.view.DisplayField;
32 import com.iplanet.jato.view.DisplayFieldDescriptor;
33 import com.iplanet.jato.view.View;
34 import com.iplanet.jato.view.event.ChildContentDisplayEvent;
35 import com.iplanet.jato.view.event.ChildDisplayEvent;
36 import com.iplanet.jato.view.event.DisplayEvent;
37
38 import com.sun.enterprise.tools.guiframework.exception.ChildNotRegisteredException;
39 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
40 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
41
42 import com.sun.web.ui.model.CCDateTimeModelInterface;
43 import com.sun.web.ui.view.datetime.CCDateTime;
44
45 import java.text.DateFormat JavaDoc;
46 import java.text.ParseException JavaDoc;
47 import java.util.Date JavaDoc;
48
49
50 /**
51  * LH requires that validateDataInput() be called before the model values
52  * retrieved. This class will do that automatically if you use getValue(),
53  * however, if you attempt to pull values directly from the model you will
54  * need to ensure this method is called first.
55  */

56 public class DescriptorCCDateTime extends CCDateTime implements DescriptorContainerView, DisplayField {
57
58     /**
59      * Constructor
60      *
61      * @param ctx The RequestContext
62      * @param name The name of the ViewBean
63      * @param desc The ViewDescriptor
64      */

65     public DescriptorCCDateTime(RequestContext ctx, String JavaDoc name, ViewDescriptor desc, ContainerView parent, CCDateTimeModelInterface model) {
66     super(parent, model, name);
67     setRequestContext(
68         (ctx == null) ? RequestManager.getRequestContext() : ctx);
69     setViewDescriptor(desc);
70     registerViewDescriptorChildren();
71     }
72
73
74     //////////////////////////////////////////////////////////////////////
75
// DescriptorContainerView Methods //
76
//////////////////////////////////////////////////////////////////////
77

78     /**
79      * This method sets the ViewDescriptor for this View.
80      */

81     protected void setViewDescriptor(ViewDescriptor desc) {
82     _viewDesc = desc;
83     }
84
85
86     /**
87      * This method retrieves this View ViewDescriptor.
88      *
89      * @return This View's ViewDescriptor.
90      */

91     public ViewDescriptor getViewDescriptor() {
92     return _viewDesc;
93     }
94
95
96     /**
97      *
98      */

99     public View createChild(String JavaDoc name) {
100     View child = null;
101     try {
102         // Try to create the Child via a child descriptor
103
child = DescriptorViewHelper.createChild(this, name);
104     } catch (ChildNotRegisteredException ex) {
105         // Some children have built-in support via the super class
106
child = super.createChild(name);
107     }
108
109     // return the child
110
return child;
111     }
112
113
114     /**
115      * Make forwarding easy
116      */

117     public void forwardTo(RequestContext requestContext) throws NavigationException {
118     getParentViewBean().forwardTo(requestContext);
119     }
120
121
122     //////////////////////////////////////////////////////////////////////
123
// DisplayField Methods //
124
//////////////////////////////////////////////////////////////////////
125

126     /**
127      * Returns the current value of this view. In most cases, if this view
128      * is associated with a model, this value will the current value of the
129      * model.
130      *
131      * @return The current value. If there are multiple current values for
132      * this view, only the first value is returned. If there is no
133      * value, this method returns null.
134      */

135     public Object JavaDoc getValue() {
136     // Only call this if values have been submitted
137
if (((DisplayField)getChild(START_DATE_TEXT)).getValue() != null) {
138         // We want to make sure to call this at most once per request
139
if (_validateCheck != getRequestContext()) {
140         validateDataInput();
141         _validateCheck = getRequestContext();
142         }
143     }
144     return getModel().getStartDateTime();
145     }
146
147
148     /**
149      * This method returns either the toString() representation of a non-null
150      * displayField's value, or a blank string whenever the value is null
151      *
152      * @return a string representation of the value, guaranteed to be non-null
153      */

154     public String JavaDoc stringValue() {
155     Object JavaDoc value = getValue();
156     if (value == null) {
157         return "";
158     }
159     return value.toString();
160     }
161
162
163     /**
164      * Sets the current value of this view. This method overwrites any current
165      * value or values. If multiple values were present previously, they
166      * are all discarded. If this view is associated with a model, this value
167      * should be propagated to the model.
168      *
169      * @param value
170      * The value to set in this view
171      */

172     public void setValue(Object JavaDoc value) {
173     if (value == null) {
174         return;
175     }
176     Date JavaDoc date = null;
177     if (value instanceof Date JavaDoc) {
178         date = (Date JavaDoc)value;
179     } else {
180         try {
181         DateFormat JavaDoc df = DateFormat.getDateTimeInstance(
182             DateFormat.MEDIUM,
183             DateFormat.MEDIUM,
184             getRequestContext().getRequest().getLocale());
185         date = df.parse((String JavaDoc)value);
186         } catch (ParseException JavaDoc ex) {
187         throw new FrameworkException(
188             "Unable to set CCDateTime value: '"+value+
189             "' for CCDateTime field '"+getName()+"'.", ex,
190             getViewDescriptor(), this);
191         }
192     }
193     getModel().setStartDateTime(date);
194     }
195
196
197     /**
198      * Returns this view's current set of values. In most cases, if this view
199      * is associated with a model, these values will be the current values of
200      * the model.
201      *
202      * @return The current set of values. If there is no value, this method
203      * returns an array of zero length.
204      */

205     public Object JavaDoc[] getValues() {
206     return new Object JavaDoc[] {getValue()};
207     }
208
209
210     /**
211      * Sets this view's current set of values. All previous values are
212      * discarded. If this view is associated with a model, this set of values
213      * should be propagated to the model.
214      *
215      * @param values
216      * The set of values to set in this view
217      */

218     public void setValues(Object JavaDoc[] values) {
219     throw new FrameworkException(
220         "setValues() not supported.", getViewDescriptor(), this);
221     }
222
223
224     /**
225      * Returns the DisplayFieldDescriptor
226      *
227      * @return This field's DisplayFieldDescriptor, which may be null if
228      * a descriptor was not specified during construction
229      */

230     public DisplayFieldDescriptor getDescriptor() {
231     return null;
232     }
233
234
235     //////////////////////////////////////////////////////////////////////
236
// Event Methods //
237
//////////////////////////////////////////////////////////////////////
238

239     /**
240      * This method dispatches BeginDisplay events to each registered
241      * BeginDisplay event handler according the the ViewDescriptor.
242      * This method is defined in ContainerView, but it is important to
243      * to override this.
244      *
245      * @param event The DisplayEvent, created internally by JATO
246      */

247     public void beginDisplay(DisplayEvent event) throws ModelControlException {
248     DescriptorViewHelper.beginDisplay(this, event);
249     super.beginDisplay(event);
250     }
251
252
253     /**
254      * This method is defined in ContainerView, but it is important to
255      * override this.
256      */

257     public boolean beginChildDisplay(ChildDisplayEvent event) throws ModelControlException {
258     try {
259         return DescriptorViewHelper.beginChildDisplay(this, event);
260     } catch (Exception JavaDoc ex) {
261         throw new FrameworkException(ex, getViewDescriptor(), this);
262     }
263     }
264
265
266     /**
267      * This method is defined in ContainerView, but it is important to
268      * override this.
269      */

270     public String JavaDoc endChildDisplay(ChildContentDisplayEvent event) throws ModelControlException {
271     return DescriptorViewHelper.endChildDisplay(this, event);
272     }
273
274
275     /**
276      * This method is defined in ContainerView, but it is important to
277      * override this.
278      */

279     public void endDisplay(DisplayEvent event) {
280     DescriptorViewHelper.endDisplay(this, event);
281     super.endDisplay(event);
282     }
283
284
285     /**
286      * <P>Children need to be registered so that values can be submitted back
287      * to DisplayFields, CommandFields can be handled, and containers which
288      * contain these things will work. So... to ensure that
289      * DescriptorContainerView's work, the following
290      * registerViewDescriptorChildren() method must be implemented. This
291      * method is named this way to avoid accidentally overriding a
292      * registerChildren method. Since JATO did not make this part of an
293      * interface, we cannot do super.registerChildren(). <B>This method should
294      * be invoked from the constructor after the ViewDescriptor has been
295      * set.</B> Most implementations of this method should do the
296      * following:</P>
297      *
298      * <BLOCKQUOTE><CODE>
299      * DescriptorViewHelper.registerViewDescriptorChildren(getViewDescriptor(), this);
300      * </CODE></BLOCKQUOTE>
301      *
302      */

303     public void registerViewDescriptorChildren() {
304     DescriptorViewHelper.registerViewDescriptorChildren(getViewDescriptor(), this);
305     }
306
307
308     private RequestContext _ctx = null;
309     private RequestContext _validateCheck = null;
310     private ViewDescriptor _viewDesc = null;
311 }
312
Popular Tags