KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > event > handlers > DisplayFieldHandlers


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.event.handlers;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.ViewBeanManager;
28 import com.iplanet.jato.view.DisplayField;
29 import com.iplanet.jato.view.ContainerView;
30 import com.iplanet.jato.view.View;
31
32 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
33 import com.sun.enterprise.tools.guiframework.view.DescriptorViewManager;
34
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39
40 /**
41  *
42  */

43 public class DisplayFieldHandlers {
44
45     /**
46      * This method retrieves the value from a DisplayField. It requires the
47      * id of the View and the name of the DisplayField be specified as
48      * Parameters. The key for the View is "viewID", the key for the
49      * DisplayField is "fieldName". You may optionally specify that you want
50      * an array of values. To do this, pass in the parameters "multiple"
51      * equal to "true". Note, if you are obtaining a View (not a ViewBean),
52      * then the View must be cached in order for this to work correctly (the
53      * container will not get set correctly).
54      *
55      * @param reqCtx The RequestContext
56      * @param handlerCtx The HandlerContext
57      */

58     public void getDisplayFieldValue(RequestContext reqCtx, HandlerContext handlerCtx) {
59     DisplayField field = (DisplayField)handlerCtx.getInputValue(DISPLAY_FIELD);
60     if (field == null) {
61         // We can get the result of getDisplayField this way
62
getDisplayField(reqCtx, handlerCtx);
63         field = (DisplayField)handlerCtx.getOutputValue(VALUE);
64     }
65
66     // Check to see if we should attempt to get 1 or many values
67
Boolean JavaDoc multi = (Boolean JavaDoc)handlerCtx.getInputValue(MULTI_VALUE);
68     boolean getMulti = false;
69     if (multi != null) {
70         getMulti = multi.booleanValue();
71     }
72
73     // If getMulti, then return Object[], else Object
74
Object JavaDoc value = getMulti ? field.getValues() : field.getValue();
75     handlerCtx.setOutputValue(VALUE, value);
76     }
77
78
79     /**
80      * This is a helper method for obtaining a View.
81      */

82     public void getDisplayField(RequestContext reqCtx, HandlerContext handlerCtx) {
83         // Get the View ID
84
Object JavaDoc viewID = handlerCtx.getInputValue(VIEW_ID);
85     if (viewID == null) {
86         throw new IllegalArgumentException JavaDoc(
87         "The parameter map did not contain "+VIEW_ID+"!");
88     }
89
90     // Try to obtain the ContainerView
91
ViewBeanManager vm = reqCtx.getViewBeanManager();
92     View view = null;
93     String JavaDoc topName = (viewID instanceof List JavaDoc) ?
94         ((List JavaDoc)viewID).get(0).toString() : viewID.toString();
95     try {
96         view = vm.getViewBean(topName);
97     } catch (ClassCastException JavaDoc ex) {
98         view = ((DescriptorViewManager)vm).getView(null, topName);
99     } catch (ClassNotFoundException JavaDoc ex) {
100         throw new RuntimeException JavaDoc(ex);
101     }
102     if (view == null) {
103         throw new RuntimeException JavaDoc(
104         "Unable to obtain the view for mapping!");
105     }
106
107     // If we have more than 1 ID, walk the View...
108
if (viewID instanceof List JavaDoc) {
109         Iterator JavaDoc iter = ((List JavaDoc)viewID).iterator();
110         iter.next(); // Already used the first one
111
while (iter.hasNext()) {
112         if (!(view instanceof ContainerView)) {
113             throw new RuntimeException JavaDoc("View ("+
114             view.getQualifiedName()+") is not a ContainerView!");
115         }
116         view = ((ContainerView)view).getChild(""+iter.next());
117         }
118     }
119
120     // Make sure we have a container
121
if (!(view instanceof ContainerView)) {
122         throw new RuntimeException JavaDoc(
123         "View ("+view.getQualifiedName()+") is not a ContainerView!");
124     }
125
126         // Get the DisplayField Name
127
Object JavaDoc fieldName = handlerCtx.getInputValue(FIELD_NAME);
128     if (fieldName == null) {
129         throw new IllegalArgumentException JavaDoc(
130         "The parameter map did not contain "+FIELD_NAME+"!");
131     }
132
133     // Try to get the DisplayField
134
DisplayField field =
135         ((ContainerView)view).getDisplayField(fieldName.toString());
136     if (field == null) {
137         throw new RuntimeException JavaDoc(
138         "Field ("+fieldName+") not found on View ("+viewID+")!");
139     }
140
141     // Return the View (as a ContainerView)
142
handlerCtx.setOutputValue(VALUE, field);
143     }
144
145
146     /**
147      * This method sets a DisplayField value. "params" must contain the View
148      * key and the DisplayField name. The key for the View is "viewID", the
149      * key for the DisplayField is "fieldName". You may optionally specify
150      * that you want an array of values. To do this, pass in the parameters
151      * "multiple" equal to "true". Note, if you are obtaining a View (not a
152      * ViewBean), then the View must be cached in order for this to work
153      * correctly (the container will not get set correctly). If you attmempt
154      * to set an array of values, but do not pass in an array type, an
155      * Object[] will be created with the value as its only emelent.
156      *
157      * @param reqCtx The RequestContext
158      * @param handlerCtx The HandlerContext
159      */

160     public void setDisplayFieldValue(RequestContext reqCtx, HandlerContext handlerCtx) {
161     DisplayField field = (DisplayField)handlerCtx.getInputValue(DISPLAY_FIELD);
162     if (field == null) {
163         // We can get the result of getDisplayField this way
164
getDisplayField(reqCtx, handlerCtx);
165         field = (DisplayField)handlerCtx.getOutputValue(VALUE);
166     }
167
168     // Check to see if we should attempt to set 1 or many values
169
Boolean JavaDoc multi = (Boolean JavaDoc)handlerCtx.getInputValue(MULTI_VALUE);
170     boolean getMulti = false;
171     if (multi != null) {
172         getMulti = multi.booleanValue();
173     }
174
175     // If getMulti, then return Object[], else Object
176
Object JavaDoc value = handlerCtx.getInputValue(VALUE);
177     if (getMulti) {
178         // Make sure we have an array
179
if ((value != null) && !value.getClass().isArray()) {
180         value = new Object JavaDoc[] {value};
181         }
182         field.setValues((Object JavaDoc [])value);
183     } else {
184         field.setValue(value);
185     }
186     }
187
188
189     public static final String JavaDoc VALUE = "value";
190     public static final String JavaDoc MULTI_VALUE = "multiple";
191     public static final String JavaDoc VIEW_ID = "viewID";
192     public static final String JavaDoc FIELD_NAME = "fieldName";
193     public static final String JavaDoc DISPLAY_FIELD = "displayField";
194 }
195
Popular Tags