KickJava   Java API By Example, From Geeks To Geeks.

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


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 association within a property sheet
39  *
40  * @author gavinc
41  */

42 public class UIAssociation extends PropertySheetItem
43 {
44    private static final String JavaDoc MSG_ERROR_ASSOC = "error_association";
45    private static final String JavaDoc MSG_ERROR_NOT_ASSOC = "error_not_association";
46
47    private static Log logger = LogFactory.getLog(UIAssociation.class);
48    
49    /**
50     * Default constructor
51     */

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

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

69    protected String JavaDoc getIncorrectParentMsg()
70    {
71       return "The association 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 association definition for association '" + associationName + "'");
89          
90          // add an error message as the property is not defined in the data dictionary and
91
// not in the node's set of properties
92
String JavaDoc msg = MessageFormat.format(Application.getMessage(context, MSG_ERROR_ASSOC), new Object JavaDoc[] {associationName});
93          Utils.addErrorMessage(msg);
94       }
95       else
96       {
97          // we've found the association definition but we also need to check
98
// that the association is a parent child one
99
if (assocDef.isChild())
100          {
101             String JavaDoc msg = MessageFormat.format(Application.getMessage(context, MSG_ERROR_NOT_ASSOC), new Object JavaDoc[] {associationName});
102             Utils.addErrorMessage(msg);
103          }
104          else
105          {
106             String JavaDoc displayLabel = (String JavaDoc)getDisplayLabel();
107             if (displayLabel == null)
108             {
109                // try and get the repository assigned label
110
displayLabel = assocDef.getTitle();
111                
112                // if the label is still null default to the local name of the property
113
if (displayLabel == null)
114                {
115                   displayLabel = assocDef.getName().getLocalName();
116                }
117             }
118             
119             // generate the label and type specific control
120
generateLabel(context, displayLabel);
121             generateControl(context, assocDef, var);
122          }
123       }
124    }
125    
126    /**
127     * Generates an appropriate control for the given property
128     *
129     * @param context JSF context
130     * @param propDef The definition of the association to create the control for
131     * @param varName Name of the variable the node is stored in the session as
132     * (used for value binding expression)
133     * @param parent The parent component for the control
134     */

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