KickJava   Java API By Example, From Geeks To Geeks.

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


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.FacesException;
23 import javax.faces.component.UIOutput;
24 import javax.faces.context.FacesContext;
25 import javax.faces.convert.Converter;
26 import javax.faces.el.ValueBinding;
27
28 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
29 import org.alfresco.web.app.Application;
30 import org.alfresco.web.bean.repository.DataDictionary;
31 import org.alfresco.web.bean.repository.Node;
32 import org.alfresco.web.ui.common.Utils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.springframework.web.jsf.FacesContextUtils;
36
37 /**
38  * Component to represent an individual child association within a property sheet
39  *
40  * @author gavinc
41  */

42 public class UIChildAssociation extends PropertySheetItem
43 {
44    private static final String JavaDoc MSG_ERROR_CHILD_ASSOC = "error_child_association";
45    private static final String JavaDoc MSG_ERROR_NOT_CHILD_ASSOC = "error_not_child_association";
46
47    private static Log logger = LogFactory.getLog(UIChildAssociation.class);
48    
49    /**
50     * Default constructor
51     */

52    public UIChildAssociation()
53    {
54       // set the default renderer
55
setRendererType("org.alfresco.faces.ChildAssociationRenderer");
56    }
57    
58    /**
59     * @see javax.faces.component.UIComponent#getFamily()
60     */

61    public String JavaDoc getFamily()
62    {
63       return "org.alfresco.faces.ChildAssociation";
64    }
65
66    /**
67     * @see org.alfresco.web.ui.repo.component.property.PropertySheetItem#getIncorrectParentMsg()
68     */

69    protected String JavaDoc getIncorrectParentMsg()
70    {
71       return "The childAssociation component must be nested within a property sheet component";
72    }
73    
74    /**
75     * @see org.alfresco.web.ui.repo.component.property.PropertySheetItem#generateItem(javax.faces.context.FacesContext, org.alfresco.web.bean.repository.Node, java.lang.String)
76     */

77    protected void generateItem(FacesContext context, Node node, String JavaDoc var) throws IOException JavaDoc
78    {
79       String JavaDoc associationName = (String JavaDoc)getName();
80
81       // get details of the association
82
DataDictionary dd = (DataDictionary)FacesContextUtils.getRequiredWebApplicationContext(
83             context).getBean(Application.BEAN_DATA_DICTIONARY);
84       AssociationDefinition assocDef = dd.getAssociationDefinition(node, associationName);
85       
86       if (assocDef == null)
87       {
88          logger.warn("Failed to find child association definition for association '" + associationName + "'");
89          
90          // add an error message as the property is not defined in the data dictionary
91
String JavaDoc msg = MessageFormat.format(Application.getMessage(context, MSG_ERROR_CHILD_ASSOC), new Object JavaDoc[] {associationName});
92          Utils.addErrorMessage(msg);
93       }
94       else
95       {
96          // we've found the association definition but we also need to check
97
// that the association is a parent child one
98
if (assocDef.isChild() == false)
99          {
100             String JavaDoc msg = MessageFormat.format(Application.getMessage(context, MSG_ERROR_NOT_CHILD_ASSOC), new Object JavaDoc[] {associationName});
101             Utils.addErrorMessage(msg);
102          }
103          else
104          {
105             String JavaDoc displayLabel = (String JavaDoc)getDisplayLabel();
106             if (displayLabel == null)
107             {
108                // try and get the repository assigned label
109
displayLabel = assocDef.getTitle();
110                
111                // if the label is still null default to the local name of the property
112
if (displayLabel == null)
113                {
114                   displayLabel = assocDef.getName().getLocalName();
115                }
116             }
117             
118             // generate the label and type specific control
119
generateLabel(context, displayLabel);
120             generateControl(context, assocDef, var);
121          }
122       }
123    }
124    
125    /**
126     * Generates an appropriate control for the given property
127     *
128     * @param context JSF context
129     * @param propDef The definition of the association to create the control for
130     * @param varName Name of the variable the node is stored in the session as
131     * (used for value binding expression)
132     * @param parent The parent component for the control
133     */

134    private void generateControl(FacesContext context, AssociationDefinition assocDef,
135                                 String JavaDoc varName)
136    {
137       UIPropertySheet propSheet = (UIPropertySheet)this.getParent();
138       ValueBinding vb = context.getApplication().createValueBinding("#{" + varName + "}");
139       
140       UIChildAssociationEditor control = (UIChildAssociationEditor)context.
141          getApplication().createComponent("org.alfresco.faces.ChildAssociationEditor");
142       control.setAssociationName(assocDef.getName().toString());
143       
144       // set up the common aspects of the control
145
control.setId(context.getViewRoot().createUniqueId());
146       control.setValueBinding("value", vb);
147       
148       // disable the component if necessary
149
if (propSheet.getMode().equalsIgnoreCase(UIPropertySheet.VIEW_MODE) || isReadOnly() || assocDef.isProtected())
150       {
151          control.setDisabled(true);
152       }
153       
154       // add the control itself
155
this.getChildren().add(control);
156    }
157 }
158
Popular Tags