KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogEditWithCustomButtons


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.cms.gui.dialog;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.gui.control.Button;
18 import info.magnolia.cms.gui.control.Edit;
19 import info.magnolia.cms.gui.misc.CssConstants;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jcr.PropertyType;
28 import javax.jcr.RepositoryException;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 /**
37  * <p>
38  * A simple input text with additional configurable buttons. For most uses you can probably stay with the standard date
39  * and link dialogs provided with Magnolia, but if you need additional or custom buttoms you can use this control.
40  * </p>
41  * <p>
42  * Sample configuration:
43  * </p>
44  *
45  * <pre>
46  * + dialog
47  * + tabText
48  * + sampleEdit
49  * + buttons
50  * + button1
51  * * label "Say hello"
52  * * onclick "alert('hello!')"
53  * + button2
54  * * label "Browse"
55  * * onclick "mgnlDialogLinkOpenBrowser('link','website','html', false)"
56  * + button3
57  * * label "Clear"
58  * * onclick "getElementById('link').value=''"
59  * * type "String"
60  * * label "sample link"
61  * * rows "1"
62  * * name "link"
63  * * controlType "editWithButtons"
64  * </pre>
65  *
66  * @author Fabrizio Giustina
67  * @since 2.2
68  */

69 public class DialogEditWithCustomButtons extends DialogBox {
70
71     /**
72      * Logger.
73      */

74     private static Logger log = LoggerFactory.getLogger(DialogEditWithCustomButtons.class);
75
76     /**
77      * List of Buttons loaded from configuration.
78      */

79     private List JavaDoc buttons = new ArrayList JavaDoc();
80
81     /**
82      * Empty constructor should only be used by DialogFactory.
83      */

84     protected DialogEditWithCustomButtons() {
85     }
86
87     /**
88      * @see info.magnolia.cms.gui.dialog.DialogControl#init(HttpServletRequest, HttpServletResponse, Content, Content)
89      */

90     public void init(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content websiteNode, Content configNode)
91         throws RepositoryException {
92         super.init(request, response, websiteNode, configNode);
93
94         try {
95             Iterator JavaDoc it = configNode.getContent("buttons").getChildren(ItemType.CONTENTNODE.getSystemName()).iterator(); //$NON-NLS-1$
96
while (it.hasNext()) {
97                 Content n = (Content) it.next();
98
99                 Button button = new Button();
100                 button.setOnclick(n.getNodeData("onclick").getString()); //$NON-NLS-1$
101

102                 String JavaDoc label = null;
103                 if (n.getNodeData("label").isExist()) { //$NON-NLS-1$
104
label = n.getNodeData("label").getString(); //$NON-NLS-1$
105
label = this.getMessage(label);
106                 }
107                 button.setLabel(label);
108
109                 buttons.add(button);
110             }
111         }
112         catch (RepositoryException e) {
113             log.error("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
114
}
115
116     }
117
118     /**
119      * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
120      */

121     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
122         Edit control = new Edit(this.getName(), this.getValue());
123         control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); //$NON-NLS-1$
124
if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
125
control.setSaveInfo(false);
126         }
127         control.setCssClass(CssConstants.CSSCLASS_EDIT);
128         control.setRows(this.getConfigValue("rows", "1")); //$NON-NLS-1$ //$NON-NLS-2$
129
control.setCssStyles("width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
130
if (this.getConfigValue("onchange", null) != null) { //$NON-NLS-1$
131
control.setEvent("onchange", this.getConfigValue("onchange")); //$NON-NLS-1$ //$NON-NLS-2$
132
}
133         this.drawHtmlPre(out);
134         String JavaDoc width = this.getConfigValue("width", "95%"); //$NON-NLS-1$ //$NON-NLS-2$
135
out.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
136
out.write("<tr><td width=\"100%\" class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
137
out.write(control.getHtml());
138
139         for (Iterator JavaDoc iter = this.buttons.iterator(); iter.hasNext();) {
140             Button button = (Button) iter.next();
141             out.write("</td><td></td><td class=\"" + CssConstants.CSSCLASS_EDITWITHBUTTON + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
142
out.write(button.getHtml());
143         }
144
145         out.write("</td></tr></table>"); //$NON-NLS-1$
146

147         this.drawHtmlPost(out);
148     }
149 }
Popular Tags