KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > attributes > AttributeValueItem


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.properties.attributes;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.util.MessageResources;
32
33 import com.sslexplorer.boot.PropertyDefinition;
34 import com.sslexplorer.boot.TypeMetaListItem;
35 import com.sslexplorer.boot.Util;
36 import com.sslexplorer.core.CoreUtil;
37 import com.sslexplorer.properties.Pair;
38 import com.sslexplorer.properties.PairListDataSource;
39
40 /**
41  * Wrapper bean for displaying and editing a attribute and its value.
42  *
43  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
44  */

45 public class AttributeValueItem implements Comparable JavaDoc {
46     
47     final static Log log = LogFactory.getLog(AttributeValueItem.class);
48
49     private AttributeDefinition definition;
50     private String JavaDoc label;
51     private Object JavaDoc value;
52     private String JavaDoc categoryLabel;
53     private String JavaDoc categoryId;
54     private int rows, columns;
55     private Pair[] listItems;
56
57     /**
58      * Constructor.
59      *
60      * @param definition definition
61      * @param request request
62      * @param value initial value
63      */

64     public AttributeValueItem(AttributeDefinition definition, HttpServletRequest JavaDoc request, String JavaDoc value) {
65         super();
66
67         MessageResources messageResources = null;
68         if (definition.getMessageResourcesKey() != null) {
69             messageResources = CoreUtil.getMessageResources(request.getSession(), definition.getMessageResourcesKey());
70         }
71         
72         this.definition = definition;
73         this.label = definition.getLabel();
74
75         // Get an internationalised category label if possible
76
this.categoryLabel = definition.getCategoryLabel();
77         String JavaDoc s = messageResources == null ? null : messageResources.getMessage("attributeCategory." + definition.getCategory()
78             + ".title");
79         if (s != null && !s.equals("")) {
80             this.categoryLabel = s;
81         } else {
82             if (categoryLabel == null || categoryLabel.equals("")) {
83                 this.categoryLabel = "Attributes";
84             }
85         }
86
87         // Get an internationalised label if possible, otherwise use default
88
// label, otherwise use name
89
s = messageResources == null ? null : messageResources.getMessage("attribute." + definition.getName() + ".title");
90         if (s != null && !s.equals("")) {
91             label = s;
92         } else {
93             if (label != null && label.equals("")) {
94                 label = definition.getName();
95             }
96         }
97         if (getCategoryLabel() != null)
98             categoryId = Util.makeConstantKey(getCategoryLabel());
99
100         //
101
this.value = getDefinition().parseValue(value);
102
103         // Process the type meta for any type specific parameters
104
if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) {
105             this.value = value;
106             StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(definition.getTypeMeta().equals("") ? "25x5" : definition.getTypeMeta(), "x");
107             try {
108                 columns = Integer.parseInt(t.nextToken());
109                 rows = Integer.parseInt(t.nextToken());
110             } catch (Exception JavaDoc e) {
111             }
112         } else if (definition.getType() == PropertyDefinition.TYPE_LIST) {
113             List JavaDoc<Pair> listItemsList = new ArrayList JavaDoc<Pair>();
114             if (!definition.getTypeMeta().startsWith("!")) {
115                 for (Iterator JavaDoc i = ((List JavaDoc) definition.getTypeMetaObject()).iterator(); i.hasNext();) {
116                     TypeMetaListItem item = (TypeMetaListItem) i.next();
117                     Pair pair = new Pair(item.getValue(), item.getValue());
118                     if (item.getValue().equals(value)) {
119                         this.value = pair.getValue();
120                     }
121                     listItemsList.add(pair);
122                 }
123             }
124             else {
125                 String JavaDoc className = definition.getTypeMeta().substring(1);
126                 try {
127                     Class JavaDoc clazz = Class.forName(className);
128                     Object JavaDoc obj = clazz.newInstance();
129                     if(obj instanceof PairListDataSource)
130                         listItemsList.addAll(((PairListDataSource)obj).getValues(request));
131                     else
132                         throw new Exception JavaDoc("Not a PairListDataSource.");
133                 }
134                 catch(Exception JavaDoc e) {
135                     log.error("Failed to create list data source.", e);
136                 }
137                 this.value = value;
138             }
139             listItems = new Pair[listItemsList.size()];
140             listItemsList.toArray(listItems);
141         } else if (definition.getType() == PropertyDefinition.TYPE_STRING) {
142             columns = 25;
143             if (!definition.getTypeMeta().equals("")) {
144                 try {
145                     columns = Integer.parseInt(definition.getTypeMeta());
146                 } catch (NumberFormatException JavaDoc nfe) {
147                 }
148             }
149         } else if (definition.getType() == PropertyDefinition.TYPE_INTEGER) {
150             columns = 8;
151             if (!definition.getTypeMeta().equals("")) {
152                 try {
153                     columns = Integer.parseInt(definition.getTypeMeta());
154                 } catch (NumberFormatException JavaDoc e) {
155                 }
156             }
157         } else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) {
158             columns = 25;
159             if (!definition.getTypeMeta().equals("")) {
160                 try {
161                     columns = Integer.parseInt(definition.getTypeMeta());
162                 } catch (NumberFormatException JavaDoc nfe) {
163                 }
164             }
165         }
166     }
167
168     /**
169      * Get choices for a type {@link PropertyDefinition#TYPE_LIST}.
170      *
171      * @return value
172      */

173     public Pair[] getListItems() {
174         return listItems;
175     }
176
177     /**
178      * Get the label. This will either the label in the definition itself or a
179      * label from a message resource (passed in the constructor) if it exists.
180      *
181      * @return label
182      */

183
184     public String JavaDoc getLabel() {
185         return label;
186     }
187
188     /**
189      * Get the attribute definition this object wraps.
190      *
191      * @return definition
192      */

193     public AttributeDefinition getDefinition() {
194         return definition;
195     }
196
197     /**
198      * Get the value
199      *
200      * @return value
201      */

202     public Object JavaDoc getValue() {
203         return value;
204     }
205
206     /**
207      * Set the value
208      *
209      * @param value value
210      */

211     public void setValue(Object JavaDoc value) {
212         this.value = value;
213     }
214
215     /**
216      * Get the category label
217      *
218      * @return category label
219      */

220     public String JavaDoc getCategoryLabel() {
221         return categoryLabel;
222     }
223
224     /**
225      * Get the category Id
226      *
227      * @return category ID
228      */

229     public String JavaDoc getCategoryId() {
230         return categoryId;
231     }
232
233     /**
234      * Compare this value item with another based on its definitions sort order
235      * and name.
236      *
237      * @param arg0 object to compare against.
238      * @return comparison
239      */

240     public int compareTo(Object JavaDoc arg0) {
241         int i = (categoryId == null ? "" : categoryId).compareTo(((AttributeValueItem) arg0).categoryId == null ? ""
242             : ((AttributeValueItem) arg0).categoryId);
243         if (i == 0) {
244             i = new Integer JavaDoc(getDefinition().getSortOrder()).compareTo(new Integer JavaDoc(((AttributeValueItem) arg0).getDefinition()
245                             .getSortOrder()));
246             return i == 0 ? (getDefinition().getName().compareTo(((AttributeValueItem) arg0).getDefinition().getName())) : i;
247         } else {
248             return i;
249         }
250     }
251
252     /**
253      * Get the number of columns appropriate for this property definition. Only
254      * applies to property definitions of type
255      * {@link PropertyDefinition#TYPE_PASSWORD},
256      * {@link PropertyDefinition#TYPE_STRING}
257      *
258      * @return number of columns
259      */

260     public int getColumns() {
261         return columns;
262     }
263
264     /**
265      * Get the number of row appropriate for this property definition. Only
266      * applies to property definitions of type
267      * {@link PropertyDefinition#TYPE_TEXT_AREA}.
268      *
269      * @return number of rows
270      */

271     public int getRows() {
272         return rows;
273     }
274
275     /**
276      * For boolean property definitions such as checkkbox that will return
277      * <code>true</code> if the value is true.
278      *
279      * @return selected.
280      */

281     public boolean getSelected() {
282         return value.equals(Boolean.TRUE);
283     }
284
285     /**
286      * Set the value as a boolean. For boolean property definitions such as
287      * checkbox.
288      *
289      * @param selected selected
290      */

291     public void setSelected(boolean selected) {
292         this.value = Boolean.valueOf(selected);
293     }
294 }
295
Popular Tags