KickJava   Java API By Example, From Geeks To Geeks.

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


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.view.event.ChildContentDisplayEvent;
28 import com.sun.web.ui.taglib.html.CCTextFieldTag;
29 import com.sun.web.ui.taglib.html.CCTextAreaTag;
30
31 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
32 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
33 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Stack JavaDoc;
40
41
42 /**
43  *
44  */

45 public class ViewDescriptorHandlers {
46
47     /**
48      * This method obtains the hierachy from the top-most ViewDescriptor to
49      * the inner-most ViewDescriptor contains this mapping. It stores the
50      * names of the ViewDescriptors in the path from the top-most to the
51      * inner-most as a List of Strings and returns this List.
52      *
53      * @param reqCtx The RequestContext
54      * @param handlerCtx The HandlerContext
55      */

56     public void getHierarchy(RequestContext reqCtx, HandlerContext handlerCtx) {
57     // Get the inner-most containing ViewDescriptor
58
ViewDescriptor topViewDesc = handlerCtx.getViewDescriptor();
59
60     // Use a stack to flip the names
61
Stack JavaDoc nameStack = new Stack JavaDoc();
62
63     // Add names to the stack until we run out of ViewDescriptors
64
do {
65         nameStack.push(topViewDesc.getName());
66         topViewDesc = topViewDesc.getParent();
67     } while (topViewDesc != null);
68
69
70     // Add the names to path
71
List JavaDoc path = new ArrayList JavaDoc();
72     while (!nameStack.empty()) {
73         path.add(nameStack.pop());
74     }
75
76     // Return path List
77
handlerCtx.setOutputValue(VALUE, path);
78     }
79
80
81     /**
82      * This mapping obtains a value of a parameter within a ViewDescriptor.
83      * You must specify the ViewDescriptor by name. It will search the top-
84      * level ViewDescriptors for the given name. If you wish to use a child
85      * descriptor of that ViewDescriptor, you may specify as many child
86      * descritpor names as well. These names (top-level + child names) are
87      * specified by the parameter: 'ViewDescriptorName' (this parameter may
88      * be a List). You must also specify the name of the parameter(s) to
89      * obtain with parameter: 'ParameterName'. If you specify more than one
90      * value for 'parameterName', then each parameter of the ViewDescriptor
91      * that you specified will be added to a List which will be returned.
92      *
93      * @param reqCtx The RequestContext
94      * @param handlerCtx The HandlerContext
95      */

96     public void getViewDescriptorParameter(RequestContext reqCtx, HandlerContext handlerCtx) {
97     // Get the Name of the ViewDescriptor to obtain a parameter value
98
// Get the Parameter name to obtain
99
Object JavaDoc paramName = handlerCtx.getInputValue(PARAMETER_NAME);
100     if (paramName == null) {
101         throw new IllegalArgumentException JavaDoc(
102         "No 'Parameter Name' Specified!");
103     }
104
105     ViewDescriptor viewDesc = null;
106     Object JavaDoc vdName = handlerCtx.getInputValue(VIEW_DESCRIPTOR_NAME);
107     if (vdName == null) {
108         // Use the current ViewDescriptor
109
viewDesc = handlerCtx.getViewDescriptor();
110     } else {
111         // Get the ViewDescriptorManager
112
ViewDescriptorManager mgr = ViewDescriptorManager.getInstance();
113
114         // Get the ViewDescriptor using the VIEW_DESCRIPTOR_NAME
115
if (vdName instanceof List JavaDoc) {
116         // Iterate through the keys to find the child ViewDescriptor
117
Iterator JavaDoc it = ((List JavaDoc)vdName).iterator();
118         String JavaDoc key = it.next().toString();
119         viewDesc = mgr.getViewDescriptor(key);
120         if (viewDesc == null) {
121             throw new IllegalArgumentException JavaDoc(
122             "Top-level ViewDescriptor '"+key+"' not found!");
123         }
124         while (it.hasNext()) {
125             key = it.next().toString();
126             viewDesc = viewDesc.getChildDescriptor(key);
127             if (viewDesc == null) {
128             throw new IllegalArgumentException JavaDoc(
129                 "Child ViewDescriptor '"+key+"' not found!");
130             }
131         }
132         } else {
133         // Obtain the top-level ViewDescriptor directly
134
viewDesc = mgr.getViewDescriptor(vdName.toString());
135         if (viewDesc == null) {
136             throw new IllegalArgumentException JavaDoc(
137             "Top-level ViewDescriptor '"+vdName.toString()+
138             "' not found!");
139         }
140         }
141     }
142
143     // if a list of parameters is required, get each parameter's value
144
Object JavaDoc value = null;
145     if (paramName instanceof List JavaDoc) {
146         List JavaDoc paramList = (List JavaDoc)paramName;
147         ArrayList JavaDoc paramValueList = new ArrayList JavaDoc();
148         for (int i = 0; i < paramList.size(); i++) {
149         paramValueList.add(viewDesc.getParameter((String JavaDoc)paramList.get(i)));
150         }
151         value = paramValueList;
152     } else {
153         value = viewDesc.getParameter(paramName.toString());
154     }
155
156     // Set the output value
157
handlerCtx.setOutputValue(VALUE, value);
158     }
159
160 // private String addHtmlProp(String tag, String addText) {
161
// int i = tag.indexOf(' ');
162
// if (i<0)
163
// return tag;
164
// return tag.substring(0,i) + " " + addText + tag.substring(i,tag.length());
165
// }
166
// private String removeHtmlProp(String tag, String prop) {
167
// int i = tag.indexOf(prop);
168
// if (i<0) return tag;
169
// int j = tag.indexOf('\"', i+2+prop.length());
170
// if (j<0) return tag;
171
// return tag.substring(0,i) + tag.substring(j+2, tag.length());
172
// }
173
//
174
// private String extractHtmlProp(String tag, String prop) {
175
// int i = tag.indexOf(prop);
176
// if (i<0) return "";
177
// int j = tag.indexOf('\"', i+2+prop.length());
178
// if (j<0) return "";
179
// return tag.substring(i+2+prop.length(), j);
180
// }
181
//
182
// expertimental code to adjust the size of text boxes.
183
// public String endFixTextDisplay(RequestContext ctx, HandlerContext handlerCtx) {
184
// ChildContentDisplayEvent ev = (ChildContentDisplayEvent)handlerCtx.getEvent();
185
// String html = ev.getContent();
186
//
187
// if ((ev.getSource() instanceof CCTextFieldTag) == false)
188
// return html;
189
//
190
// String value = extractHtmlProp(html, "value");
191
// if (value == null || value.length() == 0)
192
// return html;
193
//
194
// value = value.trim();
195
//
196
// String sizeStr = extractHtmlProp(html, "size");
197
// if (sizeStr == null || sizeStr.length()== 0)
198
// return html;
199
// int currentSize = 0;
200
// try {
201
// currentSize = Integer.parseInt(sizeStr);
202
// } catch (Exception ex) {
203
// return html;
204
// }
205
// int size = ((value.length() / 8) + 1) * 10;
206
// if (size > 100) size = 100;
207
// if (currentSize >= size)
208
// return html;
209
// html = removeHtmlProp(html, "size");
210
// html = addHtmlProp(html, ("size="+size));
211
// System.out.println(">>>>>>> current/new size: "+currentSize+" "+size);
212
//
213
// return html;
214
// }
215

216     public void beginFixTextDisplay(RequestContext ctx, HandlerContext handlerCtx) {
217         Object JavaDoc tag = handlerCtx.getEvent().getSource();
218         if (tag instanceof CCTextFieldTag) {
219             ((CCTextFieldTag)tag).setLocalizeDisplayFieldValue("false");
220         }
221         else if (tag instanceof CCTextAreaTag) {
222             ((CCTextAreaTag)tag).setLocalizeDisplayFieldValue("false");
223         }
224     }
225     
226     public static final String JavaDoc VIEW_DESCRIPTOR_NAME = "ViewDescriptorName";
227     public static final String JavaDoc PARAMETER_NAME = "ParameterName";
228     public static final String JavaDoc VALUE = "value";
229 }
230
Popular Tags