001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.jbi.jsf.factory;
037:
038: import com.sun.jsftemplating.layout.descriptors.LayoutComponent;
039: import com.sun.jsftemplating.layout.descriptors.handler.Handler;
040:
041: import java.util.List;
042: import java.util.Map;
043: import java.util.HashMap;
044: import java.util.logging.Level;
045:
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048:
049: import com.sun.jsftemplating.component.factory.sun.PropertySheetSectionFactory;
050: import com.sun.jsftemplating.component.factory.sun.PropertyFactory;
051: import com.sun.jsftemplating.component.factory.sun.StaticTextFactory;
052: import com.sun.jsftemplating.component.factory.sun.DropDownFactory;
053: import com.sun.jsftemplating.component.factory.sun.HiddenFieldFactory;
054:
055: import com.sun.webui.jsf.component.PropertySheetSection;
056: import com.sun.webui.jsf.component.Property;
057: import com.sun.webui.jsf.component.StaticText;
058: import com.sun.webui.jsf.component.DropDown;
059: import com.sun.webui.jsf.component.HiddenField;
060:
061: /**
062: * <p> This class provides some of the implemenation for the methods required
063: * by the PropoertySheetAdaptor interface. This class may be extended to assist in
064: * implementing a PropertySheetAdaptor implementation.</p>
065: *
066: * <p> The <code>PropertySheetAdaptor</code> implementation must have a <code>public
067: * static PropertySheetAdaptor getInstance(FacesContext, LayoutComponent,
068: * UIComponent)</code> method in order to get access to an instance of the
069: * <code>PropertySheetAdaptor</code> instance.</p>
070: *
071: * @see PropertySheetAdaptor
072: *
073: */
074: public abstract class PropertySheetAdaptorBase implements
075: PropertySheetAdaptor {
076:
077: /**
078: * <p> This Constructor does nothing. If you need to store a reference
079: * to the <code>LayoutComponent</code> or <code>UIComponent</code>
080: * associated with this <code>PropertySheetAdaptor</code>, it may be more
081: * convenient to use a different constructor.</p>
082: */
083: protected PropertySheetAdaptorBase() {
084: }
085:
086: /**
087: * <p> This Constructor save the <code>LayoutComponent</code> and the
088: * <code>UIComponent</code> for easy use later.</p>
089: */
090: protected PropertySheetAdaptorBase(LayoutComponent desc,
091: UIComponent parent) {
092: setLayoutComponent(desc);
093: setParentUIComponent(parent);
094: }
095:
096: /**
097: * <p> This method retrieves the <code>LayoutComponent</code> associated
098: * with this <code>PropertySheetAdaptor</code>.</p>
099: */
100: public LayoutComponent getLayoutComponent() {
101: return mLayoutComponent;
102: }
103:
104: /**
105: * <p> This method sets the <code>LayoutComponent</code> associated
106: * with this <code>PropertySheetAdaptor</code>.</p>
107: */
108: public void setLayoutComponent(LayoutComponent comp) {
109: mLayoutComponent = comp;
110: }
111:
112: /**
113: * <p> This method retrieves the <code>UIComponent</code> associated
114: * with this <code>PropertySheetAdaptor</code>.</p>
115: */
116: public UIComponent getParentUIComponent() {
117: return mParent;
118: }
119:
120: /**
121: * <p> This method sets the <code>UIComponent</code> associated with this
122: * <code>PropertySheetAdaptor</code>.</p>
123: */
124: public void setParentUIComponent(UIComponent comp) {
125: mParent = comp;
126: }
127:
128: /**
129: * <p> This method is called shortly after
130: * getInstance(FacesContext, LayoutComponent, UIComponent). It
131: * provides a place for post-creation initialization to take occur.</p>
132: *
133: * <p> This implemenation does nothing.</p>
134: */
135: public void init() {
136: }
137:
138: /**
139: * <p> This method returns the <code>id</code> for the PropertySheet object.</p>
140: */
141: public String getId() {
142: return mPropertySheetId;
143: }
144:
145: /**
146: * <p> Sets the Component Id using the given id tag string.</p>
147: *
148: * @param parent The component.
149: * @param tagType The tag type.
150: * @param idTagKey The tag string.
151: */
152: public void setComponentId(UIComponent component, String tagType,
153: String idTagKey) {
154: String idNum = getNextIdNum(tagType);
155: setIdNum(tagType, idNum);
156: String idValue = idTagKey + idNum;
157: component.setId(idValue);
158: }
159:
160: /**
161: * <p> Sets the Component Id using the given id tag string.</p>
162: *
163: * @param parent The component.
164: * @param idTagKey The tag string.
165: */
166: public void setComponentId(UIComponent component, String idTagKey) {
167: setComponentId(component, idTagKey, idTagKey);
168: }
169:
170: /**
171: * <p> This method creates and returns the a PropertySheetSection Component.</p>
172: *
173: * @param parent The parent component.
174: */
175: public UIComponent getPropertySheetSectionComponent(
176: UIComponent parent) {
177: LayoutComponent desc = getLayoutComponent();
178: FacesContext ctx = FacesContext.getCurrentInstance();
179: PropertySheetSectionFactory factory = new PropertySheetSectionFactory();
180: UIComponent component = factory.create(ctx, desc, parent);
181: setComponentId(component, mPropertySheetSectionIdTag);
182: return component;
183: }
184:
185: /**
186: * <p> This method creates and returns the a Property Component Component.</p>
187: *
188: * @param parent The parent component.
189: */
190: public UIComponent getPropertyComponent(UIComponent parent) {
191: LayoutComponent desc = getLayoutComponent();
192: FacesContext ctx = FacesContext.getCurrentInstance();
193: PropertyFactory factory = new PropertyFactory();
194: UIComponent component = factory.create(ctx, desc, parent);
195: setComponentId(component, mPropertyIdTag);
196: return component;
197: }
198:
199: /**
200: * <p> This method creates and returns the a StaticText Component.</p>
201: *
202: * @param parent The parent component.
203: */
204: public UIComponent getStaticTextComponent(UIComponent parent) {
205: LayoutComponent desc = getLayoutComponent();
206: FacesContext ctx = FacesContext.getCurrentInstance();
207: StaticTextFactory factory = new StaticTextFactory();
208: UIComponent component = factory.create(ctx, desc, parent);
209: setComponentId(component, mStaticTextIdTag);
210: return component;
211: }
212:
213: /**
214: * <p> This method creates and returns the a Dropdown Component.</p>
215: *
216: * @param parent The parent component.
217: */
218: public UIComponent getDropDownComponent(UIComponent parent) {
219: LayoutComponent desc = getLayoutComponent();
220: FacesContext ctx = FacesContext.getCurrentInstance();
221: DropDownFactory factory = new DropDownFactory();
222: UIComponent component = factory.create(ctx, desc, parent);
223: setComponentId(component, mDropDownIdTag);
224: return component;
225: }
226:
227: /**
228: * <p> This method creates and returns the a HiddenField Component.</p>
229: *
230: * @param parent The parent component.
231: * @param id The component id.
232: */
233: public UIComponent getHiddenFieldComponent(UIComponent parent,
234: String id) {
235: LayoutComponent desc = getLayoutComponent();
236: FacesContext ctx = FacesContext.getCurrentInstance();
237: HiddenFieldFactory factory = new HiddenFieldFactory();
238: UIComponent component = factory.create(ctx, desc, parent);
239: setComponentId(component, mHiddenFieldIdTag, id);
240: return component;
241: }
242:
243: /**
244: * <p> This method creates and returns the a HiddenField Component.</p>
245: *
246: * @param parent The parent component.
247: */
248: public UIComponent getHiddenFieldComponent(UIComponent parent) {
249: return getHiddenFieldComponent(parent, mHiddenFieldIdTag);
250: }
251:
252: /**
253: * <p> This method returns the <code>UIComponent</code> factory class
254: * implementation that should be used to create a
255: * <code>PropertySheet</code> for the given property sheet model object.</p>
256: */
257: public String getFactoryClass() {
258: return "com.sun.jsftemplating.component.factory.sun.PropertySheetFactory";
259: }
260:
261: /**
262: * <p> This method returns any facets that should be applied to the
263: * <code>PropertySheet (comp)</code>.
264: *
265: * <p> Facets that already exist on <code>comp</code>, or facets that
266: * are directly added to <code>comp</code> do not need to be returned
267: * from this method.</p>
268: *
269: * @param nodeObject The (model) object representing the property sheet.
270: */
271: public Map<String, UIComponent> getFacets(Object nodeObject) {
272: return null;
273: }
274:
275: /**
276: * <p> Advanced framework feature which provides better handling for
277: * things such as expanding PropertySheet, beforeEncode, and other
278: * events.</p>
279: *
280: * <p> This method should return a <code>Map</code> of <code>List</code>
281: * of <code>Handler</code> objects. Each <code>List</code> in the
282: * <code>Map</code> should be registered under a key that cooresponds
283: * to to the "event" in which the <code>Handler</code>s should be
284: * invoked.</p>
285: *
286: * <p> This implementation returns null. This method must be overriden
287: * to take advantage of this feature.</p>
288: */
289: public Map<String, List<Handler>> getHandlersByType(
290: UIComponent comp, Object nodeObject) {
291: return null;
292: }
293:
294: /**
295: * <p> This method returnes the Optione Value. If optionName specifies
296: * the session or request scope, then the method will use the session
297: * or request map file to retrieve the value.
298: *
299: * @param optionName The name of the option.
300: * @param parent The parent component.
301: */
302: protected Object getOptionValue(String optionName,
303: UIComponent parent) {
304: LayoutComponent desc = getLayoutComponent();
305: FacesContext ctx = FacesContext.getCurrentInstance();
306: Object value = desc.getEvaluatedOption(ctx, optionName, parent);
307: if (value != null) {
308: String stringValue = value.toString();
309: int dotIndex = stringValue.indexOf(".");
310: if (dotIndex > 0) {
311: String scopeString = stringValue.substring(0, dotIndex);
312: if (scopeString.equalsIgnoreCase("#{sessionScope")) {
313: String sessionKey = stringValue
314: .substring(dotIndex + 1);
315: int endBraceIndex = stringValue.indexOf("}");
316: if (endBraceIndex > 0) {
317: sessionKey = stringValue.substring(
318: dotIndex + 1, endBraceIndex);
319: Map sessionMap = getSessionScope();
320: value = (Object) sessionMap.get(sessionKey);
321: }
322: } else if (scopeString
323: .equalsIgnoreCase("#{requestScope")) {
324: String key = stringValue.substring(dotIndex + 1);
325: int endBraceIndex = stringValue.indexOf("}");
326: if (endBraceIndex > 0) {
327: key = stringValue.substring(dotIndex + 1,
328: endBraceIndex);
329: Map sessionMap = getRequestScope();
330: value = (Object) sessionMap.get(key);
331: }
332: }
333:
334: }
335: }
336: if ((value != null) && (((String) value).length() == 0)) {
337: value = null;
338: }
339: return value;
340: }
341:
342: /**
343: * <p> This method returnes the Optione Value. If the requested option does
344: * not exists then an InvalidArgumementExceptin is thrown.
345: *
346: * @param optionName The name of the option.
347: * @param parent The parent component.
348: */
349: protected String getRequiredOptionValue(String optionName,
350: UIComponent parent) throws IllegalArgumentException {
351: String returnValue = null;
352: Object value = getOptionValue(optionName, parent);
353: if (value == null) {
354: String message = optionName + " must be specified!";
355: throw new IllegalArgumentException(message);
356: }
357: returnValue = (String) value;
358: return returnValue;
359: }
360:
361: /**
362: * <p> Returns the session scope Map.</p>
363: */
364: protected Map getSessionScope() {
365: FacesContext ctx = FacesContext.getCurrentInstance();
366: return (Map) ctx.getApplication().createValueBinding(
367: "#{sessionScope}").getValue(ctx);
368: }
369:
370: /**
371: * <p> Returns the request scope Map.</p>
372: */
373: protected Map getRequestScope() {
374: FacesContext ctx = FacesContext.getCurrentInstance();
375: return (Map) ctx.getApplication().createValueBinding(
376: "#{requestScope}").getValue(ctx);
377: }
378:
379: private String getIdNum(String idTagKey) {
380: String value = (String) mPropertySheetIds.get(idTagKey);
381: if (value == null) {
382: value = "0";
383: }
384: return value;
385: }
386:
387: private void setIdNum(String idTagKey, String value) {
388: mPropertySheetIds.put(idTagKey, value);
389: }
390:
391: private String getNextIdNum(String idTagKey) {
392: String nextIdNum = "";
393: String idTag = getIdNum(idTagKey);
394: int idTagValue = Integer.parseInt(idTag);
395: idTagValue++;
396: nextIdNum = idTagValue + "";
397: return nextIdNum;
398: }
399:
400: protected String mPropertySheetId = null;
401: protected String mPropertySheetSectionIdTag = null;
402: protected String mPropertyIdTag = null;
403: protected String mStaticTextIdTag = null;
404: protected String mDropDownIdTag = null;
405: protected String mHiddenFieldIdTag = null;
406:
407: private HashMap mPropertySheetIds = new HashMap();
408: private UIComponent mParent = null;
409: private LayoutComponent mLayoutComponent = null;
410: }
|