1 13 package info.magnolia.module.admininterface.dialogs; 14 15 import info.magnolia.cms.beans.config.Paragraph; 16 import info.magnolia.cms.beans.config.ParagraphManager; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.gui.control.Button; 19 import info.magnolia.cms.gui.control.ControlImpl; 20 import info.magnolia.cms.gui.dialog.Dialog; 21 import info.magnolia.cms.gui.dialog.DialogBox; 22 import info.magnolia.cms.gui.dialog.DialogButtonSet; 23 import info.magnolia.cms.gui.dialog.DialogFactory; 24 import info.magnolia.cms.gui.dialog.DialogHidden; 25 import info.magnolia.cms.gui.dialog.DialogStatic; 26 import info.magnolia.cms.gui.dialog.DialogTab; 27 import info.magnolia.cms.i18n.Messages; 28 import info.magnolia.cms.i18n.MessagesManager; 29 import info.magnolia.cms.i18n.TemplateMessagesUtil; 30 import info.magnolia.module.admininterface.DialogMVCHandler; 31 32 import java.io.IOException ; 33 import java.util.Iterator ; 34 35 import javax.jcr.RepositoryException; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 39 import org.apache.commons.lang.StringUtils; 40 import org.slf4j.Logger; 41 import org.slf4j.LoggerFactory; 42 43 44 48 public class ParagraphSelectDialog extends DialogMVCHandler { 49 50 53 private static Logger log = LoggerFactory.getLogger(ParagraphSelectDialog.class); 54 55 private String paragraph = StringUtils.EMPTY; 56 57 public ParagraphSelectDialog(String name, HttpServletRequest request, HttpServletResponse response) { 58 super(name, request, response); 59 paragraph = params.getParameter("mgnlParagraph"); } 61 62 65 protected Dialog createDialog(Content configNode, Content websiteNode) throws RepositoryException { 66 Dialog dialog = DialogFactory.getDialogInstance(request, response, null, null); 67 68 dialog.setConfig("paragraph", paragraph); 70 dialog.setLabel(msgs.get("dialog.paragraph.createNew")); dialog.setConfig("saveLabel", msgs.get("buttons.ok")); 73 DialogHidden h1 = DialogFactory.getDialogHiddenInstance(request, response, null, null); 74 h1.setName("mgnlParagraphSelected"); h1.setValue("true"); h1.setConfig("saveInfo", "false"); dialog.addSub(h1); 78 79 DialogTab tab = dialog.addTab(); 80 81 DialogStatic c0 = DialogFactory.getDialogStaticInstance(request, response, null, null); 82 83 c0.setConfig("line", "false"); c0.setValue(msgs.get("dialog.paragraph.select")); c0.setBoxType((DialogBox.BOXTYPE_1COL)); 86 tab.addSub(c0); 87 88 DialogButtonSet c1 = DialogFactory.getDialogButtonSetInstance(request, response, null, null); 89 c1.setName("mgnlParagraph"); c1.setButtonType(ControlImpl.BUTTONTYPE_RADIO); 91 c1.setBoxType(DialogBox.BOXTYPE_1COL); 92 c1.setConfig("saveInfo", "false"); c1.setConfig("width", "100%"); 95 String [] pars = paragraph.split(","); for (int i = 0; i < pars.length; i++) { 97 try { 98 Paragraph paragraphInfo = ParagraphManager.getInstance().getInfo(pars[i]); 99 100 if (paragraphInfo == null) { 102 log.error("Unable to load paragraph {}", pars[i]); 103 continue; 104 } 105 Button button = new Button(c1.getName(), paragraphInfo.getName()); 106 StringBuffer label = new StringBuffer (); 107 108 Messages msgs = TemplateMessagesUtil.getMessages(); 109 if (StringUtils.isNotEmpty(paragraphInfo.getI18nBasename())) { 110 msgs = MessagesManager.getMessages(paragraphInfo.getI18nBasename()); 111 } 112 113 label.append("<strong>" + msgs.getWithDefault(paragraphInfo.getTitle(), paragraphInfo.getTitle()) 115 + "</strong><br />"); 117 String description = paragraphInfo.getDescription(); 118 if (StringUtils.isNotEmpty(description)) { 119 label.append(msgs.getWithDefault(description, description)); 120 } 121 label.append("<br /><br />"); button.setLabel(label.toString()); 123 button.setOnclick("document.mgnlFormMain.submit();"); c1.addOption(button); 125 } 126 catch (Exception e) { 127 log.warn("Exception caught: " + e.getMessage(), e); } 130 } 131 132 tab.addSub(c1); 133 134 return dialog; 135 } 136 137 140 public Content getStorageNode() { 141 return null; 142 } 143 144 147 public Content getConfigNode() { 148 return null; 149 } 150 151 154 public String save() { 155 try { 156 StringBuffer query = new StringBuffer (); 158 for (Iterator iter = form.getParameters().keySet().iterator(); iter.hasNext();) { 159 String key = (String ) iter.next(); 160 if (!key.equals("mgnlDialog")) { if (query.length() != 0) { 162 query.append("&"); } 164 query.append(key); 165 query.append("="); query.append(form.getParameter(key)); 167 } 168 169 } 170 response.sendRedirect(request.getContextPath() + "/.magnolia/dialogs/" + this.paragraph + ".html?" + query); } 172 catch (IOException e) { 173 log.error("can't redirect to the paragraph-dialog", e); } 175 return VIEW_NOTHING; 176 } 177 178 } | Popular Tags |