KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > property > PropertySheetItem


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.repo.component.property;
18
19 import java.io.IOException JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21
22 import javax.faces.component.NamingContainer;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIOutput;
25 import javax.faces.component.UIPanel;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.ValueBinding;
28
29 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
30 import org.alfresco.web.app.Application;
31 import org.alfresco.web.bean.repository.DataDictionary;
32 import org.alfresco.web.bean.repository.Node;
33 import org.alfresco.web.ui.common.Utils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.springframework.web.jsf.FacesContextUtils;
37
38 /**
39  * Abstract base class for all items that can appear in a property sheet component
40  *
41  * @author gavinc
42  */

43 public abstract class PropertySheetItem extends UIPanel implements NamingContainer
44 {
45    private static Log logger = LogFactory.getLog(PropertySheetItem.class);
46    
47    protected String JavaDoc name;
48    protected String JavaDoc displayLabel;
49    protected String JavaDoc converter;
50    protected Boolean JavaDoc readOnly;
51    
52    /**
53     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
54     */

55    public void encodeBegin(FacesContext context) throws IOException JavaDoc
56    {
57       // get the variable being used from the parent
58
UIComponent parent = this.getParent();
59       if ((parent instanceof UIPropertySheet) == false)
60       {
61          throw new IllegalStateException JavaDoc(getIncorrectParentMsg());
62       }
63       
64       // only build the components if there are currently no children
65
int howManyKids = getChildren().size();
66       if (howManyKids == 0)
67       {
68          UIPropertySheet propSheet = (UIPropertySheet)parent;
69          generateItem(context, propSheet.getNode(), propSheet.getVar());
70       }
71       
72       super.encodeBegin(context);
73    }
74    
75    /**
76     * @return Returns the display label
77     */

78    public String JavaDoc getDisplayLabel()
79    {
80       if (this.displayLabel == null)
81       {
82          ValueBinding vb = getValueBinding("displayLabel");
83          if (vb != null)
84          {
85             this.displayLabel = (String JavaDoc)vb.getValue(getFacesContext());
86          }
87       }
88       
89       return this.displayLabel;
90    }
91
92    /**
93     * @param displayLabel Sets the display label
94     */

95    public void setDisplayLabel(String JavaDoc displayLabel)
96    {
97       this.displayLabel = displayLabel;
98    }
99
100    /**
101     * @return Returns the name
102     */

103    public String JavaDoc getName()
104    {
105       if (this.name == null)
106       {
107          ValueBinding vb = getValueBinding("name");
108          if (vb != null)
109          {
110             this.name = (String JavaDoc)vb.getValue(getFacesContext());
111          }
112       }
113       
114       return this.name;
115    }
116
117    /**
118     * @param name Sets the name
119     */

120    public void setName(String JavaDoc name)
121    {
122       this.name = name;
123    }
124    
125    /**
126     * @return Returns the converter
127     */

128    public String JavaDoc getConverter()
129    {
130       if (this.converter == null)
131       {
132          ValueBinding vb = getValueBinding("converter");
133          if (vb != null)
134          {
135             this.converter = (String JavaDoc)vb.getValue(getFacesContext());
136          }
137       }
138       
139       return this.converter;
140    }
141
142    /**
143     * @param converter Sets the converter
144     */

145    public void setConverter(String JavaDoc converter)
146    {
147       this.converter = converter;
148    }
149
150    /**
151     * @return Returns whether the property is read only
152     */

153    public boolean isReadOnly()
154    {
155       if (this.readOnly == null)
156       {
157          ValueBinding vb = getValueBinding("readOnly");
158          if (vb != null)
159          {
160             this.readOnly = (Boolean JavaDoc)vb.getValue(getFacesContext());
161          }
162       }
163       
164       if (this.readOnly == null)
165       {
166          this.readOnly = Boolean.FALSE;
167       }
168       
169       return this.readOnly;
170    }
171
172    /**
173     * @param readOnly Sets the read only flag for the component
174     */

175    public void setReadOnly(boolean readOnly)
176    {
177       this.readOnly = readOnly;
178    }
179    
180    /**
181     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
182     */

183    public void restoreState(FacesContext context, Object JavaDoc state)
184    {
185       Object JavaDoc values[] = (Object JavaDoc[])state;
186       // standard component attributes are restored by the super class
187
super.restoreState(context, values[0]);
188       this.name = (String JavaDoc)values[1];
189       this.displayLabel = (String JavaDoc)values[2];
190       this.readOnly = (Boolean JavaDoc)values[3];
191       this.converter = (String JavaDoc)values[4];
192    }
193    
194    /**
195     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
196     */

197    public Object JavaDoc saveState(FacesContext context)
198    {
199       Object JavaDoc values[] = new Object JavaDoc[5];
200       // standard component attributes are saved by the super class
201
values[0] = super.saveState(context);
202       values[1] = this.name;
203       values[2] = this.displayLabel;
204       values[3] = this.readOnly;
205       values[4] = this.converter;
206       return (values);
207    }
208    
209    /**
210     * Generates the label and control for the item
211     *
212     * @param context FacesContext
213     * @param node The Node we are displaying the property sheet for
214     * @param var The variable name used to store the Node in the session
215     */

216    protected abstract void generateItem(FacesContext context, Node node, String JavaDoc var)
217       throws IOException JavaDoc;
218    
219    /**
220     * Returns the message to use in the exception that is thrown if the component
221     * is not nested inside a PropertySheet component
222     *
223     * @return The message
224     */

225    protected abstract String JavaDoc getIncorrectParentMsg();
226    
227    /**
228     * Generates a JSF OutputText component/renderer
229     *
230     * @param context JSF context
231     * @param displayLabel The display label text
232     * @param parent The parent component for the label
233     */

234    protected void generateLabel(FacesContext context, String JavaDoc displayLabel)
235    {
236       UIOutput label = (UIOutput)context.getApplication().
237                         createComponent("javax.faces.Output");
238       label.setId(context.getViewRoot().createUniqueId());
239       label.setRendererType("javax.faces.Text");
240       label.setValue(displayLabel + ": ");
241       this.getChildren().add(label);
242       
243       if (logger.isDebugEnabled())
244          logger.debug("Created label " + label.getClientId(context) +
245                       " for '" + displayLabel + "' and added it to component " + this);
246    }
247 }
248
Popular Tags