KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > wizard > forms > AbstractWizardPropertiesForm


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.wizard.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.ActionErrors;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.taglib.TagUtils;
33
34 import com.sslexplorer.boot.PropertyClass;
35 import com.sslexplorer.boot.PropertyDefinition;
36 import com.sslexplorer.boot.PropertyDefinitionCategory;
37 import com.sslexplorer.core.CoreException;
38 import com.sslexplorer.core.CoreUtil;
39 import com.sslexplorer.properties.PropertyItem;
40 import com.sslexplorer.properties.forms.PropertiesForm;
41 import com.sslexplorer.tabs.TabModel;
42 import com.sslexplorer.wizard.AbstractWizardSequence;
43
44
45 /**
46  *
47  * @author Brett Smith <brett@3sp.com>
48  */

49 public abstract class AbstractWizardPropertiesForm extends AbstractWizardForm implements PropertiesForm, TabModel {
50
51     public final static String JavaDoc ATTR_PROPERTY_ITEMS = "propertItems";
52     public final static String JavaDoc ATTR_PROPERTY_ITEM_VALUES = "propertItemValues";
53     public final static String JavaDoc ATTR_CATEGORY_DEFINITIONS = "categoryDefinitions";
54
55     private PropertyItem[] propertyItems;
56     private List JavaDoc<PropertyDefinitionCategory> categoryDefinitions;
57     private List JavaDoc<PropertyClass> propertyClasses;
58     private int selectedCategory = -1;
59     private List JavaDoc<String JavaDoc> tabTitles;
60     private HttpServletRequest JavaDoc request;
61     private String JavaDoc selectedTab;
62     private int categoryIdx;
63     
64     public AbstractWizardPropertiesForm(List JavaDoc<PropertyClass> propertyClasses) {
65         super();
66         setPropertyClasses(propertyClasses);
67     }
68
69     public List JavaDoc<PropertyClass> getPropertyClasses() {
70         return propertyClasses;
71     }
72
73     public void setPropertyClasses(List JavaDoc<PropertyClass> propertyClasses) {
74         this.propertyClasses = propertyClasses;
75     }
76
77     /* (non-Javadoc)
78      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
79      */

80     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
81         for(int i = 0 ; i < propertyItems.length ; i++) {
82             sequence.putAttribute(getKeyForForm(ATTR_PROPERTY_ITEM_VALUES) + "." + propertyItems[i].getName(), propertyItems[i].getValue());
83         }
84     }
85
86     /* (non-Javadoc)
87      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence)
88      */

89     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
90
91         this.request = request;
92         
93         /* Store the category definitions and property items so they can be used
94          * throughout the lifetime of the wizard
95          */

96         sequence.putAttribute(getKeyForForm(ATTR_PROPERTY_ITEMS), propertyItems);
97         if(sequence.getAttribute(getKeyForForm(ATTR_CATEGORY_DEFINITIONS), null) == null) {
98             sequence.putAttribute(getKeyForForm(ATTR_CATEGORY_DEFINITIONS), categoryDefinitions);
99         }
100         else {
101             for(int i = 0 ; i < propertyItems.length; i++) {
102                 String JavaDoc n = getKeyForForm(ATTR_PROPERTY_ITEM_VALUES) + "." + propertyItems[i].getName();
103                 Object JavaDoc val = sequence.getAttribute(n, null);
104                 if(val != null) {
105                     propertyItems[i].setValue(val);
106                 }
107             }
108         }
109     }
110     
111
112     /* (non-Javadoc)
113      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
114      */

115     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
116         super.reset(mapping, request);
117         categoryIdx = 0;
118         AbstractWizardSequence seq = getWizardSequence(request);
119         propertyItems = seq == null ? null : (PropertyItem[])seq.getAttribute(getKeyForForm(ATTR_PROPERTY_ITEMS), null);
120         categoryDefinitions = seq == null ? null : (List JavaDoc)seq.getAttribute(getKeyForForm(ATTR_CATEGORY_DEFINITIONS), null);
121         if(propertyItems != null) {
122             for(int i = 0 ; i < propertyItems.length; i++) {
123                 if(propertyItems[i].getType() == PropertyDefinition.TYPE_BOOLEAN) {
124                     propertyItems[i].setValue("false");
125                 }
126             }
127         }
128     }
129     
130
131     
132     /* (non-Javadoc)
133      * @see com.sslexplorer.properties.forms.PropertiesForm#getCategoryDefinitions()
134      */

135     public List JavaDoc getCategoryDefinitions() {
136         return categoryDefinitions;
137     }
138
139     /* (non-Javadoc)
140      * @see com.sslexplorer.properties.forms.PropertiesForm#getEnabled()
141      */

142     public boolean getEnabled() {
143         return true;
144     }
145
146     /* (non-Javadoc)
147      * @see com.sslexplorer.properties.forms.PropertiesForm#getParentCategory()
148      */

149     public abstract int getParentCategory();
150
151     /* (non-Javadoc)
152      * @see com.sslexplorer.properties.forms.PropertiesForm#getPropertyItem(int)
153      */

154     public PropertyItem getPropertyItem(int idx) {
155         return propertyItems[idx];
156     }
157
158     /* (non-Javadoc)
159      * @see com.sslexplorer.properties.forms.PropertiesForm#getPropertyItems()
160      */

161     public PropertyItem[] getPropertyItems() {
162         return propertyItems;
163     }
164
165     /* (non-Javadoc)
166      * @see com.sslexplorer.properties.forms.PropertiesForm#setCategoryDefinitions(java.util.List)
167      */

168     public void setCategoryDefinitions(List JavaDoc categoryDefinitions) {
169         this.categoryDefinitions = categoryDefinitions;
170         
171     }
172
173     /* (non-Javadoc)
174      * @see com.sslexplorer.properties.forms.PropertiesForm#setPropertyItem(int, com.sslexplorer.applications.PropertyItem)
175      */

176     public void setPropertyItem(int idx, PropertyItem item) {
177         propertyItems[idx] = item;
178         
179     }
180
181     /* (non-Javadoc)
182      * @see com.sslexplorer.properties.forms.PropertiesForm#setPropertyItems(com.sslexplorer.applications.PropertyItem[])
183      */

184     public void setPropertyItems(PropertyItem[] propertyItems) {
185         this.propertyItems = propertyItems;
186     }
187     
188     String JavaDoc getKeyForForm(String JavaDoc key) {
189         return getPageName() + "." + key;
190     }
191
192     /* (non-Javadoc)
193      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
194      */

195     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
196         if(isCommiting()) {
197             ActionErrors errs = null;
198             PropertyItem[] items = getPropertyItems();
199             for (int i = 0; i < items.length; i++) {
200                 PropertyDefinition def = items[i].getDefinition();
201                 try {
202                     def.validate(String.valueOf(items[i].getPropertyValue()), getClass().getClassLoader());
203                 }
204                 catch(CoreException ce) {
205                     if(errs == null) {
206                         errs = new ActionErrors();
207                     }
208                     ce.getBundleActionMessage().setArg3(CoreUtil.getMessageResources(request.getSession(),
209                         def.getMessageResourcesKey()).getMessage((Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY),
210                         def.getName() + ".name"));
211                     errs.add(Globals.ERROR_KEY, ce.getBundleActionMessage());
212                 }
213                 catch (Exception JavaDoc e) {
214                     errs.add(Globals.ERROR_KEY, new ActionMessage("properties.error.failedToValidate", e.getMessage()));
215                 }
216             }
217             if(errs != null)
218                 return errs;
219             
220         }
221         return super.validate(mapping, request);
222     }
223
224     public void setSelectedCategory(int selectedCategory) {
225         this.selectedCategory = selectedCategory;
226     }
227
228     public int getSelectedCategory() {
229         return selectedCategory;
230     }
231
232     /* (non-Javadoc)
233      * @see com.sslexplorer.tabs.TabModel#getSelectedTab()
234      */

235     public String JavaDoc getSelectedTab() {
236         return selectedTab;
237     }
238
239     /* (non-Javadoc)
240      * @see com.sslexplorer.tabs.TabModel#getTabBundle(int)
241      */

242     public String JavaDoc getTabBundle(int idx) {
243         return categoryDefinitions.get(idx).getBundle();
244     }
245
246     /* (non-Javadoc)
247      * @see com.sslexplorer.tabs.TabModel#getTabCount()
248      */

249     public int getTabCount() {
250         return categoryDefinitions.size();
251     }
252
253     /* (non-Javadoc)
254      * @see com.sslexplorer.tabs.TabModel#getTabName(int)
255      */

256     public String JavaDoc getTabName(int idx) {
257         return "category." + categoryDefinitions.get(idx).getId();
258     }
259
260     /* (non-Javadoc)
261      * @see com.sslexplorer.tabs.TabModel#getTabTitle(int)
262      */

263     public String JavaDoc getTabTitle(int idx) {
264         PropertyDefinitionCategory cat = categoryDefinitions.get(idx);
265         return CoreUtil.getMessageResources(
266             request.getSession(), cat.getBundle()).getMessage((Locale JavaDoc)request.getSession().getAttribute(Globals.LOCALE_KEY), getTabName(idx) + ".name");
267     }
268
269     /* (non-Javadoc)
270      * @see com.sslexplorer.tabs.TabModel#setSelectedTab(java.lang.String)
271      */

272     public void setSelectedTab(String JavaDoc selectedTab) {
273         this.selectedTab = selectedTab;
274     }
275     
276     /**
277      * Get a list of the next set of property items for the next category.
278      * This iterator is reset upon {@link #reset(ActionMapping, HttpServletRequest)}
279      * and returns <code>null</code> when there are no more lists.
280      *
281      * @return list of property items for the next category
282      */

283     public List JavaDoc<PropertyItem> getNextCategory() {
284         if(propertyItems.length == 0) {
285             return null;
286         }
287         List JavaDoc<PropertyItem> l = new ArrayList JavaDoc<PropertyItem>();
288         int curCat = -1;
289         while(categoryIdx < propertyItems.length) {
290             if(curCat == -1) {
291                 curCat = propertyItems[categoryIdx].getCategory();
292             }
293             else if(curCat != propertyItems[categoryIdx].getCategory()) {
294                 break;
295             }
296             l.add(propertyItems[categoryIdx]);
297             categoryIdx++;
298         }
299         return l;
300     }
301 }
302
Popular Tags