KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > command > dialog > ui > DialogFormController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.list.command.dialog.ui;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.riotfamily.common.collection.FlatMap;
34 import org.riotfamily.common.util.ResourceUtils;
35 import org.riotfamily.forms.Form;
36 import org.riotfamily.forms.controller.AjaxFormController;
37 import org.riotfamily.forms.controller.ButtonFactory;
38 import org.riotfamily.forms.controller.FormSubmissionHandler;
39 import org.riotfamily.riot.list.ListRepository;
40 import org.riotfamily.riot.list.command.dialog.DialogCommand;
41 import org.springframework.web.servlet.ModelAndView;
42
43 /**
44  * @author Felix Gnass [fgnass at neteye dot de]
45  * @since 6.4
46  */

47 public class DialogFormController extends AjaxFormController
48         implements FormSubmissionHandler {
49     
50     private ListRepository listRepository;
51     
52     private String JavaDoc commandIdAttribute = "commandId";
53     
54     private String JavaDoc viewName = ResourceUtils.getPath(
55             DialogFormController.class, "DialogFormView.ftl");
56     
57     public DialogFormController(ListRepository listRepository) {
58         this.listRepository = listRepository;
59         ButtonFactory buttonFactory = new ButtonFactory(this);
60         buttonFactory.setLabelKey("label.dialog.button.execute");
61         buttonFactory.setCssClass("button button-execute");
62         addButton(buttonFactory);
63     }
64
65     protected String JavaDoc getCommandId(HttpServletRequest JavaDoc request) {
66         return (String JavaDoc) request.getAttribute(commandIdAttribute);
67     }
68     
69     protected DialogCommand getCommand(HttpServletRequest JavaDoc request) {
70         return (DialogCommand) listRepository.getCommand(getCommandId(request));
71     }
72     
73     /**
74      * Delegates the form creation to the DialogCommand.
75      */

76     protected Form createForm(HttpServletRequest JavaDoc request) {
77         return getForm(request);
78     }
79     
80     /**
81      * Overwrites the super implementation to do nothing since the form
82      * created by the command is already populated by contract.
83      */

84     protected void populateForm(Form form, HttpServletRequest JavaDoc request) {
85     }
86     
87     protected String JavaDoc getSessionAttribute(HttpServletRequest JavaDoc request) {
88         return getCommand(request).getFormSessionAttribute();
89     }
90     
91     protected String JavaDoc getTitle(Form form, HttpServletRequest JavaDoc request) {
92         String JavaDoc commandId = getCommandId(request);
93         return form.getFormContext().getMessageResolver().getMessage(
94                 "label.dialog." + commandId, commandId);
95     }
96     
97     protected ModelAndView showForm(final Form form,
98             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
99         
100         StringWriter JavaDoc sw = new StringWriter JavaDoc();
101         renderForm(form, new PrintWriter JavaDoc(sw));
102         
103         Map JavaDoc model = new FlatMap();
104         model.put("form", sw.toString());
105         model.put("title", getTitle(form, request));
106         return new ModelAndView(viewName, model);
107     }
108
109     
110     public ModelAndView handleFormSubmission(Form form,
111             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
112             throws Exception JavaDoc {
113         
114         Object JavaDoc input = form.populateBackingObject();
115         return getCommand(request).handleInput(input);
116     }
117     
118 }
119
Popular Tags