1 24 package org.riotfamily.riot.list.command.dialog.ui; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.util.Map ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 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 47 public class DialogFormController extends AjaxFormController 48 implements FormSubmissionHandler { 49 50 private ListRepository listRepository; 51 52 private String commandIdAttribute = "commandId"; 53 54 private String 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 getCommandId(HttpServletRequest request) { 66 return (String ) request.getAttribute(commandIdAttribute); 67 } 68 69 protected DialogCommand getCommand(HttpServletRequest request) { 70 return (DialogCommand) listRepository.getCommand(getCommandId(request)); 71 } 72 73 76 protected Form createForm(HttpServletRequest request) { 77 return getForm(request); 78 } 79 80 84 protected void populateForm(Form form, HttpServletRequest request) { 85 } 86 87 protected String getSessionAttribute(HttpServletRequest request) { 88 return getCommand(request).getFormSessionAttribute(); 89 } 90 91 protected String getTitle(Form form, HttpServletRequest request) { 92 String 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 request, HttpServletResponse response) { 99 100 StringWriter sw = new StringWriter (); 101 renderForm(form, new PrintWriter (sw)); 102 103 Map 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 request, HttpServletResponse response) 112 throws Exception { 113 114 Object input = form.populateBackingObject(); 115 return getCommand(request).handleInput(input); 116 } 117 118 } 119 | Popular Tags |