KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > wizard > AbstractWizardBean


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.bean.wizard;
18
19 import javax.faces.context.FacesContext;
20 import javax.faces.event.ActionEvent;
21
22 import org.alfresco.service.cmr.model.FileFolderService;
23 import org.alfresco.service.cmr.repository.NodeService;
24 import org.alfresco.service.cmr.search.SearchService;
25 import org.alfresco.web.app.Application;
26 import org.alfresco.web.app.context.UIContextService;
27 import org.alfresco.web.bean.BrowseBean;
28 import org.alfresco.web.bean.NavigationBean;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Abstract bean used as the base class for all wizard backing beans.
34  *
35  * @author gavinc
36  */

37 public abstract class AbstractWizardBean
38 {
39    private static Log logger = LogFactory.getLog(AbstractWizardBean.class);
40    
41    /** I18N messages */
42    private static final String JavaDoc MSG_NOT_SET = "value_not_set";
43    
44    protected static final String JavaDoc FINISH_OUTCOME = "finish";
45    protected static final String JavaDoc CANCEL_OUTCOME = "cancel";
46    protected static final String JavaDoc DEFAULT_INSTRUCTION_ID = "default_instruction";
47    protected static final String JavaDoc SUMMARY_TITLE_ID = "summary";
48    protected static final String JavaDoc SUMMARY_DESCRIPTION_ID = "summary_desc";
49    
50    // common wizard properties
51
protected int currentStep = 1;
52    protected boolean editMode = false;
53    protected NodeService nodeService;
54    protected FileFolderService fileFolderService;
55    protected SearchService searchService;
56    protected NavigationBean navigator;
57    protected BrowseBean browseBean;
58    
59    /**
60     * @return Returns the wizard description
61     */

62    public abstract String JavaDoc getWizardDescription();
63
64    /**
65     * @return Returns the wizard title
66     */

67    public abstract String JavaDoc getWizardTitle();
68    
69    /**
70     * @return Returns the title for the current step
71     */

72    public abstract String JavaDoc getStepTitle();
73
74    /**
75     * @return Returns the description for the current step
76     */

77    public abstract String JavaDoc getStepDescription();
78    
79    /**
80     * @return Returns the instructional text for the current step
81     */

82    public abstract String JavaDoc getStepInstructions();
83
84    /**
85     * Determines the outcome string for the given step number
86     *
87     * @param step The step number to get the outcome for
88     * @return The outcome
89     */

90    protected abstract String JavaDoc determineOutcomeForStep(int step);
91    
92    /**
93     * Handles the finish button being pressed
94     *
95     * @return The finish outcome
96     */

97    public abstract String JavaDoc finish();
98    
99    /**
100     * Action listener called when the wizard is being launched allowing
101     * state to be setup
102     */

103    public void startWizard(ActionEvent event)
104    {
105       // refresh the UI, calling this method now is fine as it basically makes sure certain
106
// beans clear the state - so when we finish the wizard other beans will have been reset
107
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
108       
109       // make sure the wizard is not in edit mode
110
this.editMode = false;
111       
112       // initialise the wizard in case we are launching
113
// after it was navigated away from
114
init();
115       
116       if (logger.isDebugEnabled())
117          logger.debug("Started wizard : " + getWizardTitle());
118    }
119    
120    /**
121     * Action listener called when the wizard is being launched for
122     * editing an existing node.
123     */

124    public void startWizardForEdit(ActionEvent event)
125    {
126       // refresh the UI, calling this method now is fine as it basically makes sure certain
127
// beans clear the state - so when we finish the wizard other beans will have been reset
128
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
129       
130       // set the wizard in edit mode
131
this.editMode = true;
132       
133       // populate the wizard's default values with the current value
134
// from the node being edited
135
init();
136       populate();
137       
138       if (logger.isDebugEnabled())
139          logger.debug("Started wizard : " + getWizardTitle() + " for editing");
140    }
141  
142    /**
143     * Determines whether the wizard is in edit mode
144     *
145     * @return true if the wizard is in edit mode, false if it is in creation mode
146     */

147    public boolean isInEditMode()
148    {
149       return this.editMode;
150    }
151    
152    /**
153     * Deals with the next button being pressed
154     *
155     * @return
156     */

157    public String JavaDoc next()
158    {
159       this.currentStep++;
160       
161       // determine which page to go to next
162
String JavaDoc outcome = determineOutcomeForStep(this.currentStep);
163             
164       if (logger.isDebugEnabled())
165       {
166          logger.debug("current step is now: " + this.currentStep);
167          logger.debug("Next outcome: " + outcome);
168       }
169       
170       // return the outcome for navigation
171
return outcome;
172    }
173    
174    /**
175     * Deals with the back button being pressed
176     *
177     * @return
178     */

179    public String JavaDoc back()
180    {
181       this.currentStep--;
182       
183       // determine which page to go to next
184
String JavaDoc outcome = determineOutcomeForStep(this.currentStep);
185       
186       if (logger.isDebugEnabled())
187       {
188          logger.debug("current step is now: " + this.currentStep);
189          logger.debug("Back outcome: " + outcome);
190       }
191       
192       // return the outcome for navigation
193
return outcome;
194    }
195    
196    /**
197     * Handles the cancelling of the wizard
198     *
199     * @return The cancel outcome
200     */

201    public String JavaDoc cancel()
202    {
203       // reset the state
204
init();
205       
206       return CANCEL_OUTCOME;
207    }
208    
209    /**
210     * Initialises the wizard
211     */

212    public void init()
213    {
214       this.currentStep = 1;
215    }
216    
217    /**
218     * Populates the wizard's values with the current values
219     * of the node about to be edited
220     */

221    public void populate()
222    {
223       // subclasses will override this method to setup accordingly
224
}
225
226    /**
227     * @return Returns the nodeService.
228     */

229    public NodeService getNodeService()
230    {
231       return this.nodeService;
232    }
233
234    /**
235     * @param nodeService The nodeService to set.
236     */

237    public void setNodeService(NodeService nodeService)
238    {
239       this.nodeService = nodeService;
240    }
241
242    /**
243     * @param fileFolderService used to manipulate folder/folder model nodes
244     */

245    public void setFileFolderService(FileFolderService fileFolderService)
246    {
247       this.fileFolderService = fileFolderService;
248    }
249
250    /**
251     * @param searchService the service used to find nodes
252     */

253    public void setSearchService(SearchService searchService)
254    {
255       this.searchService = searchService;
256    }
257
258    /**
259     * @return Returns the navigation bean instance.
260     */

261    public NavigationBean getNavigator()
262    {
263       return navigator;
264    }
265    
266    /**
267     * @param navigator The NavigationBean to set.
268     */

269    public void setNavigator(NavigationBean navigator)
270    {
271       this.navigator = navigator;
272    }
273
274    /**
275     * @return The BrowseBean
276     */

277    public BrowseBean getBrowseBean()
278    {
279       return this.browseBean;
280    }
281
282    /**
283     * @param browseBean The BrowseBean to set.
284     */

285    public void setBrowseBean(BrowseBean browseBean)
286    {
287       this.browseBean = browseBean;
288    }
289    
290    /**
291     * Build summary table from the specified list of Labels and Values
292     *
293     * @param labels Array of labels to display
294     * @param values Array of values to display
295     *
296     * @return summary table HTML
297     */

298    protected String JavaDoc buildSummary(String JavaDoc[] labels, String JavaDoc[] values)
299    {
300       if (labels == null || values == null || labels.length != values.length)
301       {
302          throw new IllegalArgumentException JavaDoc("Labels and Values passed to summary must be valid and of equal length.");
303       }
304       
305       String JavaDoc msg = Application.getMessage(FacesContext.getCurrentInstance(), MSG_NOT_SET);
306       String JavaDoc notSetMsg = "<" + msg + ">";
307       
308       StringBuilder JavaDoc buf = new StringBuilder JavaDoc(256);
309       
310       buf.append("<table cellspacing='4' cellpadding='2' border='0' class='summary'>");
311       for (int i=0; i<labels.length; i++)
312       {
313          String JavaDoc value = values[i];
314          buf.append("<tr><td valign='top'><b>");
315          buf.append(labels[i]);
316          buf.append(":</b></td><td>");
317          buf.append(value != null ? value : notSetMsg);
318          buf.append("</td></tr>");
319       }
320       buf.append("</table>");
321       
322       return buf.toString();
323    }
324 }
325
Popular Tags