KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > dialogs > ParagraphSelectDialog


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.jcr.RepositoryException;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.apache.commons.lang.StringUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 /**
45  * If there are more than one paragraph available you have first to choose one.
46  * @author philipp
47  */

48 public class ParagraphSelectDialog extends DialogMVCHandler {
49
50     /**
51      * Logger.
52      */

53     private static Logger log = LoggerFactory.getLogger(ParagraphSelectDialog.class);
54
55     private String JavaDoc paragraph = StringUtils.EMPTY;
56
57     public ParagraphSelectDialog(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
58         super(name, request, response);
59         paragraph = params.getParameter("mgnlParagraph"); //$NON-NLS-1$
60
}
61
62     /**
63      * @see .DialogMVCHandler#createDialog(Content, Content)
64      */

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); //$NON-NLS-1$
69

70         dialog.setLabel(msgs.get("dialog.paragraph.createNew")); //$NON-NLS-1$
71
dialog.setConfig("saveLabel", msgs.get("buttons.ok")); //$NON-NLS-1$ //$NON-NLS-2$
72

73         DialogHidden h1 = DialogFactory.getDialogHiddenInstance(request, response, null, null);
74         h1.setName("mgnlParagraphSelected"); //$NON-NLS-1$
75
h1.setValue("true"); //$NON-NLS-1$
76
h1.setConfig("saveInfo", "false"); //$NON-NLS-1$ //$NON-NLS-2$
77
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"); //$NON-NLS-1$ //$NON-NLS-2$
84
c0.setValue(msgs.get("dialog.paragraph.select")); //$NON-NLS-1$
85
c0.setBoxType((DialogBox.BOXTYPE_1COL));
86         tab.addSub(c0);
87
88         DialogButtonSet c1 = DialogFactory.getDialogButtonSetInstance(request, response, null, null);
89         c1.setName("mgnlParagraph"); //$NON-NLS-1$
90
c1.setButtonType(ControlImpl.BUTTONTYPE_RADIO);
91         c1.setBoxType(DialogBox.BOXTYPE_1COL);
92         c1.setConfig("saveInfo", "false"); //$NON-NLS-1$ //$NON-NLS-2$
93
c1.setConfig("width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
94

95         String JavaDoc[] pars = paragraph.split(","); //$NON-NLS-1$
96
for (int i = 0; i < pars.length; i++) {
97             try {
98                 Paragraph paragraphInfo = ParagraphManager.getInstance().getInfo(pars[i]);
99
100                 // prevent NPEs
101
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 JavaDoc label = new StringBuffer JavaDoc();
107
108                 Messages msgs = TemplateMessagesUtil.getMessages();
109                 if (StringUtils.isNotEmpty(paragraphInfo.getI18nBasename())) {
110                     msgs = MessagesManager.getMessages(paragraphInfo.getI18nBasename());
111                 }
112
113                 label.append("<strong>" //$NON-NLS-1$
114
+ msgs.getWithDefault(paragraphInfo.getTitle(), paragraphInfo.getTitle())
115                     + "</strong><br />"); //$NON-NLS-1$
116

117                 String JavaDoc description = paragraphInfo.getDescription();
118                 if (StringUtils.isNotEmpty(description)) {
119                     label.append(msgs.getWithDefault(description, description));
120                 }
121                 label.append("<br /><br />"); //$NON-NLS-1$
122
button.setLabel(label.toString());
123                 button.setOnclick("document.mgnlFormMain.submit();"); //$NON-NLS-1$
124
c1.addOption(button);
125             }
126             catch (Exception JavaDoc e) {
127                 // paragraph definition does not exist
128
log.warn("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
129
}
130         }
131
132         tab.addSub(c1);
133
134         return dialog;
135     }
136
137     /**
138      * @see info.magnolia.module.admininterface.DialogMVCHandler#getWesiteNode()
139      */

140     public Content getStorageNode() {
141         return null;
142     }
143
144     /**
145      * @see info.magnolia.module.admininterface.DialogMVCHandler#getConfigNode()
146      */

147     public Content getConfigNode() {
148         return null;
149     }
150
151     /**
152      * @see info.magnolia.module.admininterface.DialogMVCHandler#save()
153      */

154     public String JavaDoc save() {
155         try {
156             // copy all parameters exept mgnlDialog
157
StringBuffer JavaDoc query = new StringBuffer JavaDoc();
158             for (Iterator JavaDoc iter = form.getParameters().keySet().iterator(); iter.hasNext();) {
159                 String JavaDoc key = (String JavaDoc) iter.next();
160                 if (!key.equals("mgnlDialog")) { //$NON-NLS-1$
161
if (query.length() != 0) {
162                         query.append("&"); //$NON-NLS-1$
163
}
164                     query.append(key);
165                     query.append("="); //$NON-NLS-1$
166
query.append(form.getParameter(key));
167                 }
168
169             }
170             response.sendRedirect(request.getContextPath() + "/.magnolia/dialogs/" + this.paragraph + ".html?" + query); //$NON-NLS-1$ //$NON-NLS-2$
171
}
172         catch (IOException JavaDoc e) {
173             log.error("can't redirect to the paragraph-dialog", e); //$NON-NLS-1$
174
}
175         return VIEW_NOTHING;
176     }
177
178 }
Popular Tags