KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > config > PropertySheetConfigElement


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.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.config.ConfigElement;
26 import org.alfresco.config.element.ConfigElementAdapter;
27 import org.alfresco.config.element.GenericConfigElement;
28
29 /**
30  * Custom config element that represents the config data for a property sheet
31  *
32  * @author gavinc
33  */

34 public class PropertySheetConfigElement extends ConfigElementAdapter
35 {
36    // TODO: Currently this object just deals with properties and associations to show,
37
// in the future it will also deal with properties and associations to hide.
38

39    private List JavaDoc<ItemConfig> items = new ArrayList JavaDoc<ItemConfig>();
40    private List JavaDoc<ItemConfig> editableItems = new ArrayList JavaDoc<ItemConfig>();
41    private Map JavaDoc<String JavaDoc, ItemConfig> itemsMap = new HashMap JavaDoc<String JavaDoc, ItemConfig>();
42    private Map JavaDoc<String JavaDoc, ItemConfig> editableItemsMap = new HashMap JavaDoc<String JavaDoc, ItemConfig>();
43    private List JavaDoc<String JavaDoc> itemNames = new ArrayList JavaDoc<String JavaDoc>();
44    private List JavaDoc<String JavaDoc> editableItemNames = new ArrayList JavaDoc<String JavaDoc>();
45    private boolean kidsPopulated = false;
46    
47    /**
48     * Default constructor
49     */

50    public PropertySheetConfigElement()
51    {
52       super("property-sheet");
53    }
54    
55    /**
56     * Constructor
57     *
58     * @param name Name of the element this config element represents
59     */

60    public PropertySheetConfigElement(String JavaDoc name)
61    {
62       super(name);
63    }
64    
65    /**
66     * @see org.alfresco.config.ConfigElement#getChildren()
67     */

68    public List JavaDoc<ConfigElement> getChildren()
69    {
70       // lazily build the list of generic config elements representing
71
// the properties as the caller may not even call this method
72

73       List JavaDoc<ConfigElement> kids = null;
74       
75       if (this.items.size() > 0)
76       {
77          if (this.kidsPopulated == false)
78          {
79             Iterator JavaDoc<ItemConfig> items = this.items.iterator();
80             while (items.hasNext())
81             {
82                ItemConfig pc = items.next();
83                GenericConfigElement ce = null;
84                if (pc instanceof PropertyConfig)
85                {
86                   ce = new GenericConfigElement(PropertySheetElementReader.ELEMENT_SHOW_PROPERTY);
87                }
88                else if (pc instanceof AssociationConfig)
89                {
90                   ce = new GenericConfigElement(PropertySheetElementReader.ELEMENT_SHOW_ASSOC);
91                }
92                else
93                {
94                   ce = new GenericConfigElement(PropertySheetElementReader.ELEMENT_SHOW_CHILD_ASSOC);
95                }
96                
97                ce.addAttribute(PropertySheetElementReader.ATTR_NAME, pc.getName());
98                ce.addAttribute(PropertySheetElementReader.ATTR_DISPLAY_LABEL, pc.getDisplayLabel());
99                ce.addAttribute(PropertySheetElementReader.ATTR_DISPLAY_LABEL_ID, pc.getDisplayLabelId());
100                ce.addAttribute(PropertySheetElementReader.ATTR_READ_ONLY, Boolean.toString(pc.isReadOnly()));
101                ce.addAttribute(PropertySheetElementReader.ATTR_CONVERTER, pc.getConverter());
102                ce.addAttribute(PropertySheetElementReader.ATTR_SHOW_IN_EDIT_MODE, Boolean.toString(pc.isShownInEditMode()));
103                this.children.add(ce);
104             }
105             
106             this.kidsPopulated = true;
107          }
108          
109          kids = super.getChildren();
110       }
111       
112       return kids;
113    }
114    
115    /**
116     * @see org.alfresco.config.ConfigElement#combine(org.alfresco.config.ConfigElement)
117     */

118    public ConfigElement combine(ConfigElement configElement)
119    {
120       PropertySheetConfigElement combined = new PropertySheetConfigElement();
121       
122       // add all the existing properties
123
Iterator JavaDoc<ItemConfig> items = this.getItemsToShow().iterator();
124       while (items.hasNext())
125       {
126          combined.addItem(items.next());
127       }
128       
129       // add all the properties from the given element
130
items = ((PropertySheetConfigElement)configElement).getItemsToShow().iterator();
131       while (items.hasNext())
132       {
133          combined.addItem(items.next());
134       }
135       
136       return combined;
137    }
138    
139    /**
140     * Adds an item to show
141     *
142     * @param itemConfig A pre-configured property or association config object
143     */

144    /*package*/ void addItem(ItemConfig itemConfig)
145    {
146       if (this.itemsMap.containsKey(itemConfig.getName()) == false)
147       {
148          this.items.add(itemConfig);
149          this.itemsMap.put(itemConfig.getName(), itemConfig);
150          this.itemNames.add(itemConfig.getName());
151          
152          // also add to the edit items collections if necessary
153
if (itemConfig.isShownInEditMode())
154          {
155             this.editableItems.add(itemConfig);
156             this.editableItemsMap.put(itemConfig.getName(), itemConfig);
157             this.editableItemNames.add(itemConfig.getName());
158          }
159       }
160    }
161    
162    /**
163     * Adds a property to show
164     *
165     * @param name The name of the property
166     * @param displayLabel Display label to use for the property
167     * @param displayLabelId Display label message id to use for the property
168     * @param readOnly Sets whether the property should be rendered as read only
169     * @param converter The name of a converter to apply to the property control
170     * @param inEdit Sets whether the property should be shown when the property
171     * sheet is in edit mode
172     */

173    /*package*/ void addProperty(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId, String JavaDoc readOnly,
174                                 String JavaDoc converter, String JavaDoc inEdit)
175    {
176       addItem(new PropertyConfig(name, displayLabel, displayLabelId, Boolean.parseBoolean(readOnly),
177             converter, inEdit));
178    }
179    
180    /**
181     * Adds an association to show
182     *
183     * @param name The name of the association
184     * @param displayLabel Display label to use for the property
185     * @param displayLabelId Display label message id to use for the property
186     * @param readOnly Sets whether the association should be rendered as read only
187     * @param converter The name of a converter to apply to the association control
188     * @param inEdit Sets whether the property should be shown when the property
189     * sheet is in edit mode
190     */

191    /*package*/ void addAssociation(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId, String JavaDoc readOnly,
192                                    String JavaDoc converter, String JavaDoc inEdit)
193    {
194       addItem(new AssociationConfig(name, displayLabel, displayLabelId, Boolean.parseBoolean(readOnly),
195             converter, inEdit));
196    }
197    
198    /**
199     * Adds a child association to show
200     *
201     * @param name The name of the child association
202     * @param displayLabel Display label to use for the property
203     * @param displayLabelId Display label message id to use for the property
204     * @param readOnly Sets whether the association should be rendered as read only
205     * @param converter The name of a converter to apply to the association control
206     */

207    /*package*/ void addChildAssociation(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId, String JavaDoc readOnly,
208                                         String JavaDoc converter, String JavaDoc inEdit)
209    {
210       addItem(new ChildAssociationConfig(name, displayLabel, displayLabelId, Boolean.parseBoolean(readOnly),
211             converter, inEdit));
212    }
213    
214    /**
215     * @return Returns a list of item names to display
216     */

217    public List JavaDoc<String JavaDoc> getItemNamesToShow()
218    {
219       return this.itemNames;
220    }
221    
222    /**
223     * @return Returns the list of item config objects that represent those to display
224     */

225    public List JavaDoc<ItemConfig> getItemsToShow()
226    {
227       return this.items;
228    }
229    
230    /**
231     * @return Returns a map of the item names to show
232     */

233    public Map JavaDoc<String JavaDoc, ItemConfig> getItemsMapToShow()
234    {
235       return this.itemsMap;
236    }
237    
238    /**
239     * @return Returns a list of item names to display
240     */

241    public List JavaDoc<String JavaDoc> getEditableItemNamesToShow()
242    {
243       return this.editableItemNames;
244    }
245    
246    /**
247     * @return Returns the list of item config objects that represent those to display
248     */

249    public List JavaDoc<ItemConfig> getEditableItemsToShow()
250    {
251       return this.editableItems;
252    }
253    
254    /**
255     * @return Returns a map of the item names to show
256     */

257    public Map JavaDoc<String JavaDoc, ItemConfig> getEditableItemsMapToShow()
258    {
259       return this.editableItemsMap;
260    }
261    
262    /**
263     * Inner class to represent a configured property sheet item
264     */

265    public abstract class ItemConfig
266    {
267       private String JavaDoc name;
268       private String JavaDoc displayLabel;
269       private String JavaDoc displayLabelId;
270       private String JavaDoc converter;
271       private boolean readOnly;
272       private boolean showInEditMode = true;
273       
274       public ItemConfig(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId,
275             boolean readOnly, String JavaDoc converter, String JavaDoc inEdit)
276       {
277          this.name = name;
278          this.displayLabel = displayLabel;
279          this.displayLabelId = displayLabelId;
280          this.readOnly = readOnly;
281          this.converter = converter;
282          
283          if (inEdit != null)
284          {
285             this.showInEditMode = Boolean.parseBoolean(inEdit);
286          }
287       }
288       
289       /**
290        * @return The display label
291        */

292       public String JavaDoc getDisplayLabel()
293       {
294          return this.displayLabel;
295       }
296       
297       /**
298        * @return The display label message id
299        */

300       public String JavaDoc getDisplayLabelId()
301       {
302          return this.displayLabelId;
303       }
304       
305       /**
306        * @return The property name
307        */

308       public String JavaDoc getName()
309       {
310          return this.name;
311       }
312       
313       /**
314        * @return Determines whether the property is configured as read only
315        */

316       public boolean isReadOnly()
317       {
318          return this.readOnly;
319       }
320       
321       public String JavaDoc getConverter()
322       {
323          return this.converter;
324       }
325       
326       public boolean isShownInEditMode()
327       {
328          return this.showInEditMode;
329       }
330       
331       /**
332        * @see java.lang.Object#toString()
333        */

334       public String JavaDoc toString()
335       {
336          StringBuilder JavaDoc buffer = new StringBuilder JavaDoc(super.toString());
337          buffer.append(" (name=").append(this.name);
338          buffer.append(" displaylabel=").append(this.displayLabel);
339          buffer.append(" displaylabelId=").append(this.displayLabelId);
340          buffer.append(" converter=").append(this.converter);
341          buffer.append(" readonly=").append(this.readOnly);
342          buffer.append(" showInEditMode=").append(this.showInEditMode).append(")");
343          return buffer.toString();
344       }
345    }
346    
347    /**
348     * Inner class to represent a configured property
349     */

350    public class PropertyConfig extends ItemConfig
351    {
352       public PropertyConfig(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId,
353             boolean readOnly, String JavaDoc converter, String JavaDoc inEdit)
354       {
355          super(name, displayLabel, displayLabelId, readOnly, converter, inEdit);
356       }
357    }
358    
359    /**
360     * Inner class to represent a configured association
361     */

362    public class AssociationConfig extends ItemConfig
363    {
364       public AssociationConfig(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId,
365             boolean readOnly, String JavaDoc converter, String JavaDoc inEdit)
366       {
367          super(name, displayLabel, displayLabelId, readOnly, converter, inEdit);
368       }
369    }
370    
371    /**
372     * Inner class to represent a configured child association
373     */

374    public class ChildAssociationConfig extends ItemConfig
375    {
376       public ChildAssociationConfig(String JavaDoc name, String JavaDoc displayLabel, String JavaDoc displayLabelId,
377             boolean readOnly, String JavaDoc converter, String JavaDoc inEdit)
378       {
379          super(name, displayLabel, displayLabelId, readOnly, converter, inEdit);
380       }
381    }
382 }
383
Popular Tags