KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.NamingContainer;
25 import javax.faces.component.UIPanel;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.ValueBinding;
28
29 import org.alfresco.config.Config;
30 import org.alfresco.config.ConfigLookupContext;
31 import org.alfresco.config.ConfigService;
32 import org.alfresco.service.cmr.repository.NodeRef;
33 import org.alfresco.web.app.Application;
34 import org.alfresco.web.bean.repository.Node;
35 import org.alfresco.web.config.PropertySheetConfigElement;
36 import org.alfresco.web.config.PropertySheetConfigElement.AssociationConfig;
37 import org.alfresco.web.config.PropertySheetConfigElement.ChildAssociationConfig;
38 import org.alfresco.web.config.PropertySheetConfigElement.ItemConfig;
39 import org.alfresco.web.config.PropertySheetConfigElement.PropertyConfig;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 /**
44  * Component that represents the properties of a Node
45  *
46  * @author gavinc
47  */

48 public class UIPropertySheet extends UIPanel implements NamingContainer
49 {
50    public static final String JavaDoc VIEW_MODE = "view";
51    public static final String JavaDoc EDIT_MODE = "edit";
52    
53    private static Log logger = LogFactory.getLog(UIPropertySheet.class);
54    private static String JavaDoc DEFAULT_VAR_NAME = "node";
55    
56    private String JavaDoc variable;
57    private NodeRef nodeRef;
58    private Node node;
59    private Boolean JavaDoc readOnly;
60    private String JavaDoc mode;
61    private String JavaDoc configArea;
62    
63    /**
64     * Default constructor
65     */

66    public UIPropertySheet()
67    {
68       // set the default renderer for a property sheet
69
setRendererType("javax.faces.Grid");
70    }
71    
72    /**
73     * @see javax.faces.component.UIComponent#getFamily()
74     */

75    public String JavaDoc getFamily()
76    {
77       return "javax.faces.Panel";
78    }
79
80    /**
81     * @see javax.faces.component.UIComponent#encodeBegin(javax.faces.context.FacesContext)
82     */

83    public void encodeBegin(FacesContext context) throws IOException JavaDoc
84    {
85       int howManyKids = getChildren().size();
86       Boolean JavaDoc externalConfig = (Boolean JavaDoc)getAttributes().get("externalConfig");
87       
88       // generate a variable name to use if necessary
89
if (this.variable == null)
90       {
91          this.variable = DEFAULT_VAR_NAME;
92       }
93       
94       // force retrieval of node info
95
Node node = getNode();
96       
97       if (howManyKids == 0)
98       {
99          if (externalConfig != null && externalConfig.booleanValue())
100          {
101             // configure the component using the config service
102
if (logger.isDebugEnabled())
103                logger.debug("Configuring property sheet using ConfigService");
104
105             // get the properties to display
106
ConfigService configSvc = Application.getConfigService(FacesContext.getCurrentInstance());
107             Config configProps = null;
108             if (getConfigArea() == null)
109             {
110                configProps = configSvc.getConfig(node);
111             }
112             else
113             {
114                // only look within the given area
115
configProps = configSvc.getConfig(node, new ConfigLookupContext(getConfigArea()));
116             }
117             
118             PropertySheetConfigElement itemsToDisplay = (PropertySheetConfigElement)configProps.
119                getConfigElement("property-sheet");
120             
121             if (itemsToDisplay != null)
122             {
123                List JavaDoc<ItemConfig> itemsToRender = null;
124                
125                if (this.getMode().equalsIgnoreCase(EDIT_MODE))
126                {
127                   itemsToRender = itemsToDisplay.getEditableItemsToShow();
128                   
129                   if (logger.isDebugEnabled())
130                      logger.debug("Items to render: " + itemsToDisplay.getEditableItemNamesToShow());
131                }
132                else
133                {
134                   itemsToRender = itemsToDisplay.getItemsToShow();
135                   
136                   if (logger.isDebugEnabled())
137                      logger.debug("Items to render: " + itemsToDisplay.getItemNamesToShow());
138                }
139             
140                createComponentsFromConfig(context, itemsToRender);
141             }
142             else
143             {
144                if (logger.isDebugEnabled())
145                   logger.debug("There are no items to render!");
146             }
147          }
148          else
149          {
150             // show all the properties for the current node
151
if (logger.isDebugEnabled())
152                logger.debug("Configuring property sheet using node's current state");
153             
154             createComponentsFromNode(context, node);
155          }
156       }
157       
158       // put the node in the session if it is not there already
159
Map JavaDoc sessionMap = getFacesContext().getExternalContext().getSessionMap();
160       sessionMap.put(this.variable, node);
161
162       if (logger.isDebugEnabled())
163          logger.debug("Put node into session with key '" + this.variable + "': " + node);
164
165       super.encodeBegin(context);
166    }
167    
168    /**
169     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
170     */

171    public void restoreState(FacesContext context, Object JavaDoc state)
172    {
173       Object JavaDoc values[] = (Object JavaDoc[])state;
174       // standard component attributes are restored by the super class
175
super.restoreState(context, values[0]);
176       this.nodeRef = (NodeRef)values[1];
177       this.node = (Node)values[2];
178       this.variable = (String JavaDoc)values[3];
179       this.readOnly = (Boolean JavaDoc)values[4];
180       this.mode = (String JavaDoc)values[5];
181       this.configArea = (String JavaDoc)values[6];
182    }
183    
184    /**
185     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
186     */

187    public Object JavaDoc saveState(FacesContext context)
188    {
189       Object JavaDoc values[] = new Object JavaDoc[7];
190       // standard component attributes are saved by the super class
191
values[0] = super.saveState(context);
192       values[1] = this.nodeRef;
193       values[2] = this.node;
194       values[3] = this.variable;
195       values[4] = this.readOnly;
196       values[5] = this.mode;
197       values[6] = this.configArea;
198       return (values);
199    }
200    
201    /**
202     * @return Returns the node
203     */

204    public Node getNode()
205    {
206       Node node = null;
207       
208       if (this.node == null)
209       {
210          // use the value to get hold of the actual object
211
Object JavaDoc value = getAttributes().get("value");
212          
213          if (value == null)
214          {
215             ValueBinding vb = getValueBinding("value");
216             if (vb != null)
217             {
218                value = vb.getValue(getFacesContext());
219             }
220          }
221          
222          // TODO: for now we presume the object is a Node, but we need to support id's too
223
if (value instanceof Node)
224          {
225             node = (Node)value;
226          }
227       }
228       else
229       {
230          node = this.node;
231       }
232       
233       return node;
234    }
235    
236    /**
237     * @param node The node
238     */

239    public void setNode(Node node)
240    {
241       this.node = node;
242    }
243    
244    /**
245     * @return Returns the variable.
246     */

247    public String JavaDoc getVar()
248    {
249       return this.variable;
250    }
251
252    /**
253     * @param variable The variable to set.
254     */

255    public void setVar(String JavaDoc variable)
256    {
257       this.variable = variable;
258    }
259    
260    /**
261     * @return Returns whether the property sheet is read only
262     */

263    public boolean isReadOnly()
264    {
265       if (this.readOnly == null)
266       {
267          ValueBinding vb = getValueBinding("readOnly");
268          if (vb != null)
269          {
270             this.readOnly = (Boolean JavaDoc)vb.getValue(getFacesContext());
271          }
272       }
273       
274       if (this.readOnly == null)
275       {
276          this.readOnly = Boolean.FALSE;
277       }
278       
279       return this.readOnly;
280    }
281
282    /**
283     * @param readOnly Sets the read only flag for the property sheet
284     */

285    public void setReadOnly(boolean readOnly)
286    {
287       this.readOnly = Boolean.valueOf(readOnly);
288    }
289
290    /**
291     * @return Returns the mode
292     */

293    public String JavaDoc getMode()
294    {
295       if (this.mode == null)
296       {
297          ValueBinding vb = getValueBinding("mode");
298          if (vb != null)
299          {
300             this.mode = (String JavaDoc)vb.getValue(getFacesContext());
301          }
302       }
303       
304       if (this.mode == null)
305       {
306          mode = EDIT_MODE;
307       }
308       
309       return mode;
310    }
311
312    /**
313     * @param mode Sets the mode
314     */

315    public void setMode(String JavaDoc mode)
316    {
317       this.mode = mode;
318    }
319    
320    /**
321     * @return Returns the config area to use
322     */

323    public String JavaDoc getConfigArea()
324    {
325       if (this.configArea == null)
326       {
327          ValueBinding vb = getValueBinding("configArea");
328          if (vb != null)
329          {
330             this.configArea = (String JavaDoc)vb.getValue(getFacesContext());
331          }
332       }
333       
334       return configArea;
335    }
336    
337    /**
338     * @param configArea Sets the config area to use
339     */

340    public void setConfigArea(String JavaDoc configArea)
341    {
342       this.configArea = configArea;
343    }
344    
345    /**
346     * Creates all the property components required to display the properties held by the node.
347     *
348     * @param context JSF context
349     * @param node The Node to show all the properties for
350     * @throws IOException
351     */

352    private void createComponentsFromNode(FacesContext context, Node node)
353       throws IOException JavaDoc
354    {
355       // add all the properties of the node to the UI
356
Map JavaDoc<String JavaDoc, Object JavaDoc> props = node.getProperties();
357       for (String JavaDoc propertyName : props.keySet())
358       {
359          // create the property component
360
UIProperty propComp = (UIProperty)context.getApplication().createComponent("org.alfresco.faces.Property");
361          propComp.setId(context.getViewRoot().createUniqueId());
362          propComp.setName(propertyName);
363          
364          // if this property sheet is set as read only, set all properties to read only
365
if (isReadOnly())
366          {
367             propComp.setReadOnly(true);
368          }
369          
370          // NOTE: we don't know what the display label is so don't set it
371

372          this.getChildren().add(propComp);
373          
374          if (logger.isDebugEnabled())
375             logger.debug("Created property component " + propComp + "(" +
376                    propComp.getClientId(context) +
377                    ") for '" + propertyName +
378                    "' and added it to property sheet " + this);
379       }
380       
381       // add all the associations of the node to the UI
382
Map JavaDoc associations = node.getAssociations();
383       Iterator JavaDoc iter = associations.keySet().iterator();
384       while (iter.hasNext())
385       {
386          String JavaDoc assocName = (String JavaDoc)iter.next();
387          UIAssociation assocComp = (UIAssociation)context.getApplication().createComponent("org.alfresco.faces.Association");
388          assocComp.setId(context.getViewRoot().createUniqueId());
389          assocComp.setName(assocName);
390          
391          // if this property sheet is set as read only, set all properties to read only
392
if (isReadOnly())
393          {
394             assocComp.setReadOnly(true);
395          }
396          
397          // NOTE: we don't know what the display label is so don't set it
398

399          this.getChildren().add(assocComp);
400          
401          if (logger.isDebugEnabled())
402             logger.debug("Created association component " + assocComp + "(" +
403                    assocComp.getClientId(context) +
404                    ") for '" + assocName +
405                    "' and added it to property sheet " + this);
406       }
407       
408       // add all the child associations of the node to the UI
409
Map JavaDoc childAssociations = node.getChildAssociations();
410       iter = childAssociations.keySet().iterator();
411       while (iter.hasNext())
412       {
413          String JavaDoc assocName = (String JavaDoc)iter.next();
414          UIChildAssociation childAssocComp = (UIChildAssociation)context.getApplication().createComponent(
415                "org.alfresco.faces.ChildAssociation");
416          childAssocComp.setId(context.getViewRoot().createUniqueId());
417          childAssocComp.setName(assocName);
418          
419          // if this property sheet is set as read only, set all properties to read only
420
if (isReadOnly())
421          {
422             childAssocComp.setReadOnly(true);
423          }
424          
425          // NOTE: we don't know what the display label is so don't set it
426

427          this.getChildren().add(childAssocComp);
428          
429          if (logger.isDebugEnabled())
430             logger.debug("Created child association component " + childAssocComp + "(" +
431                    childAssocComp.getClientId(context) +
432                    ") for '" + assocName +
433                    "' and added it to property sheet " + this);
434       }
435    }
436    
437    /**
438     * Creates all the property components required to display the properties specified
439     * in an external config file.
440     *
441     * @param context JSF context
442     * @param properties List of properties to render (driven from configuration)
443     * @throws IOException
444     */

445    private void createComponentsFromConfig(FacesContext context, List JavaDoc<ItemConfig> items)
446       throws IOException JavaDoc
447    {
448       // **********************************
449
// TODO: Make a common base class for the UIxxx components so we can
450
// reduce the code below...
451
// **********************************
452

453       for (ItemConfig item : items)
454       {
455          // create the appropriate component
456
if (item instanceof PropertyConfig)
457          {
458             UIProperty propComp = (UIProperty)context.getApplication().createComponent("org.alfresco.faces.Property");
459             propComp.setId(context.getViewRoot().createUniqueId());
460             propComp.setName(item.getName());
461             propComp.setConverter(item.getConverter());
462             
463             String JavaDoc displayLabel = item.getDisplayLabel();
464             if (item.getDisplayLabelId() != null)
465             {
466                String JavaDoc label = Application.getMessage(context, item.getDisplayLabelId());
467                if (label != null)
468                {
469                   displayLabel = label;
470                }
471             }
472             propComp.setDisplayLabel(displayLabel);
473             
474             // if this property sheet is set as read only or the config says the property
475
// should be read only set it as such
476
if (isReadOnly() || item.isReadOnly())
477             {
478                propComp.setReadOnly(true);
479             }
480             
481             this.getChildren().add(propComp);
482             
483             if (logger.isDebugEnabled())
484                logger.debug("Created property component " + propComp + "(" +
485                       propComp.getClientId(context) +
486                       ") for '" + item.getName() +
487                       "' and added it to property sheet " + this);
488          }
489          else if (item instanceof AssociationConfig)
490          {
491             UIAssociation assocComp = (UIAssociation)context.getApplication().createComponent("org.alfresco.faces.Association");
492             assocComp.setId(context.getViewRoot().createUniqueId());
493             assocComp.setName(item.getName());
494             assocComp.setConverter(item.getConverter());
495             
496             String JavaDoc displayLabel = item.getDisplayLabel();
497             if (item.getDisplayLabelId() != null)
498             {
499                String JavaDoc label = Application.getMessage(context, item.getDisplayLabelId());
500                if (label != null)
501                {
502                   displayLabel = label;
503                }
504             }
505             assocComp.setDisplayLabel(displayLabel);
506             
507             // if this property sheet is set as read only or the config says the property
508
// should be read only set it as such
509
if (isReadOnly() || item.isReadOnly())
510             {
511                assocComp.setReadOnly(true);
512             }
513             
514             this.getChildren().add(assocComp);
515             
516             if (logger.isDebugEnabled())
517                logger.debug("Created association component " + assocComp + "(" +
518                       assocComp.getClientId(context) +
519                       ") for '" + item.getName() +
520                       "' and added it to property sheet " + this);
521          }
522          else if (item instanceof ChildAssociationConfig)
523          {
524             UIChildAssociation assocComp = (UIChildAssociation)context.getApplication().createComponent("org.alfresco.faces.ChildAssociation");
525             assocComp.setId(context.getViewRoot().createUniqueId());
526             assocComp.setName(item.getName());
527             assocComp.setConverter(item.getConverter());
528             
529             String JavaDoc displayLabel = item.getDisplayLabel();
530             if (item.getDisplayLabelId() != null)
531             {
532                String JavaDoc label = Application.getMessage(context, item.getDisplayLabelId());
533                if (label != null)
534                {
535                   displayLabel = label;
536                }
537             }
538             assocComp.setDisplayLabel(displayLabel);
539             
540             // if this property sheet is set as read only or the config says the property
541
// should be read only set it as such
542
if (isReadOnly() || item.isReadOnly())
543             {
544                assocComp.setReadOnly(true);
545             }
546             
547             this.getChildren().add(assocComp);
548             
549             if (logger.isDebugEnabled())
550                logger.debug("Created child association component " + assocComp + "(" +
551                       assocComp.getClientId(context) +
552                       ") for '" + item.getName() +
553                       "' and added it to property sheet " + this);
554          }
555       }
556    }
557 }
558
Popular Tags