KickJava   Java API By Example, From Geeks To Geeks.

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


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-2005 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.ContentHandler;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.gui.control.Button;
19 import info.magnolia.cms.gui.control.ButtonSet;
20 import info.magnolia.cms.gui.control.ControlSuper;
21 import info.magnolia.cms.gui.control.Hidden;
22 import info.magnolia.cms.gui.misc.CssConstants;
23 import info.magnolia.cms.i18n.TemplateMessagesUtil;
24
25 import java.io.IOException JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.jcr.PropertyType;
32 import javax.jcr.RepositoryException;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.log4j.Logger;
38
39
40 /**
41  * @author Vinzenz Wyser
42  * @version 2.0
43  */

44 public class DialogButtonSet extends DialogBox {
45
46     /**
47      * Logger.
48      */

49     private static Logger log = Logger.getLogger(DialogButtonSet.class);
50
51     private int buttonType = ControlSuper.BUTTONTYPE_RADIO;
52
53     /**
54      * Empty constructor should only be used by DialogFactory.
55      */

56     protected DialogButtonSet() {
57     }
58
59     public void setOptions(Content configNode, boolean setDefaultSelected) {
60         // setDefaultSelected: does not work properly (no difference between never stored and removed...)
61
// therefor do only use for radio, not for checkbox
62
List JavaDoc options = new ArrayList JavaDoc();
63         try {
64             Iterator JavaDoc it = configNode.getContent("options") //$NON-NLS-1$
65
.getChildren(ItemType.CONTENTNODE.getSystemName(), ContentHandler.SORT_BY_SEQUENCE).iterator();
66             while (it.hasNext()) {
67                 Content n = ((Content) it.next());
68                 String JavaDoc value = n.getNodeData("value").getString(); //$NON-NLS-1$
69
String JavaDoc label = n.getNodeData("label").getString(); //$NON-NLS-1$
70
label = TemplateMessagesUtil.get(this.getRequest(), label);
71                 Button button = new Button(this.getName(), value);
72                 // if (n.getNodeData("label").isExist()) button.setLabel(n.getNodeData("label").getString());
73
button.setLabel(label);
74
75                 String JavaDoc iconSrc = n.getNodeData("iconSrc").getString(); //$NON-NLS-1$
76
if (StringUtils.isNotEmpty(iconSrc)) {
77                     button.setIconSrc(iconSrc);
78                 }
79
80                 if (setDefaultSelected && n.getNodeData("selected").getBoolean()) { //$NON-NLS-1$
81
button.setState(ControlSuper.BUTTONSTATE_PUSHED);
82                 }
83                 options.add(button);
84             }
85         }
86         catch (RepositoryException e) {
87             log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
88
}
89         this.setOptions(options);
90     }
91
92     public void setOption(Content configNode) {
93         // checkboxSwitch -> only one option, value always true/false
94
List JavaDoc options = new ArrayList JavaDoc();
95         Button button = new Button(this.getName() + "_dummy", StringUtils.EMPTY); //$NON-NLS-1$
96
String JavaDoc label = configNode.getNodeData("buttonLabel").getString(); //$NON-NLS-1$
97
label = TemplateMessagesUtil.get(this.getRequest(), label);
98         button.setLabel(label);
99
100         if (configNode.getNodeData("selected").getBoolean()) { //$NON-NLS-1$
101
button.setState(ControlSuper.BUTTONSTATE_PUSHED);
102         }
103
104         button.setValue("true"); //$NON-NLS-1$
105
button.setOnclick("mgnlDialogShiftCheckboxSwitch('" + this.getName() + "');"); //$NON-NLS-1$ //$NON-NLS-2$
106
options.add(button);
107         this.setOptions(options);
108     }
109
110     /**
111      * @see info.magnolia.cms.gui.dialog.DialogInterface#init(HttpServletRequest, HttpServletResponse, Content, Content)
112      */

113     public void init(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content websiteNode, Content configNode)
114         throws RepositoryException {
115         super.init(request, response, websiteNode, configNode);
116
117         // confignode can be null if instantiated directly
118
if (configNode != null) {
119             String JavaDoc controlType = configNode.getNodeData("controlType").getString(); //$NON-NLS-1$
120

121             if (log.isDebugEnabled()) {
122                 log.debug("Init DialogButtonSet with type=" + controlType); //$NON-NLS-1$
123
}
124
125             // custom settings
126
if (controlType.equals("radio")) { //$NON-NLS-1$
127
setButtonType(ControlSuper.BUTTONTYPE_RADIO);
128                 setOptions(configNode, true);
129             }
130             else if (controlType.equals("checkbox")) { //$NON-NLS-1$
131
setButtonType(ControlSuper.BUTTONTYPE_CHECKBOX);
132                 setOptions(configNode, false);
133                 setConfig("valueType", "multiple"); //$NON-NLS-1$ //$NON-NLS-2$
134
}
135             else if (controlType.equals("checkboxSwitch")) { //$NON-NLS-1$
136
setButtonType(ControlSuper.BUTTONTYPE_CHECKBOX);
137                 setOption(configNode);
138             }
139         }
140     }
141
142     public void drawHtmlPreSubs(Writer JavaDoc out) throws IOException JavaDoc {
143         this.drawHtmlPre(out);
144     }
145
146     public void drawHtmlPostSubs(Writer JavaDoc out) throws IOException JavaDoc {
147         this.drawHtmlPost(out);
148     }
149
150     public void setButtonType(int i) {
151         this.buttonType = i;
152     }
153
154     public int getButtonType() {
155         return this.buttonType;
156     }
157
158     /**
159      * @see info.magnolia.cms.gui.dialog.DialogInterface#drawHtml(Writer)
160      */

161     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
162         this.drawHtmlPre(out);
163         ButtonSet control;
164         if (this.getConfigValue("valueType").equals("multiple")) { //$NON-NLS-1$ //$NON-NLS-2$
165
// checkbox
166
control = new ButtonSet(this.getName(), this.getValues());
167             control.setValueType(ControlSuper.VALUETYPE_MULTIPLE);
168         }
169         else if (this.getButtonType() == ControlSuper.BUTTONTYPE_CHECKBOX) {
170             // checkboxSwitch
171
control = new ButtonSet(this.getName() + "_SWITCH", this.getValue()); //$NON-NLS-1$
172
}
173         else {
174             // radio
175
control = new ButtonSet(this.getName(), this.getValue());
176         }
177         control.setButtonType(this.getButtonType());
178
179         // maem: extension to allow for fine grained layout control. E.g. radio buttons with picture
180
control.setCssClass(this.getConfigValue("cssClass", CssConstants.CSSCLASS_BUTTONSETBUTTON)); //$NON-NLS-1$
181

182         if (this.getConfigValue("saveInfo").equals("false")) { //$NON-NLS-1$ //$NON-NLS-2$
183
control.setSaveInfo(false);
184         }
185         control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); //$NON-NLS-1$
186
String JavaDoc width = this.getConfigValue("width", null); //$NON-NLS-1$
187
control.setButtonHtmlPre("<tr><td class=\"" + CssConstants.CSSCLASS_BUTTONSETBUTTON + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
188
control.setButtonHtmlInter("</td><td class=\"" + CssConstants.CSSCLASS_BUTTONSETLABEL + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
189
control.setButtonHtmlPost("</td></tr>"); //$NON-NLS-1$
190
int cols = Integer.valueOf(this.getConfigValue("cols", "1")).intValue(); //$NON-NLS-1$ //$NON-NLS-2$
191
if (cols > 1) {
192             width = "100%"; // outer table squeezes inner table if outer's width is not defined... //$NON-NLS-1$
193
control.setHtmlPre(control.getHtmlPre() + "<tr>"); //$NON-NLS-1$
194
control.setHtmlPost("</tr>" + control.getHtmlPost()); //$NON-NLS-1$
195
int item = 1;
196             int itemsPerCol = (int) Math.ceil(this.getOptions().size() / ((double) cols));
197             for (int i = 0; i < this.getOptions().size(); i++) {
198                 Button b = (Button) this.getOptions().get(i);
199                 if (item == 1) {
200                     b.setHtmlPre("<td><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">" //$NON-NLS-1$
201
+ control.getButtonHtmlPre());
202                 }
203                 if (item == itemsPerCol) {
204                     b.setHtmlPost(control.getButtonHtmlPost() + "</table></td><td class=\"" //$NON-NLS-1$
205
+ CssConstants.CSSCLASS_BUTTONSETINTERCOL + "\"></td>"); //$NON-NLS-1$
206
item = 1;
207                 }
208                 else {
209                     item++;
210                 }
211             }
212             // very last button: close table
213
int lastIndex = this.getOptions().size() - 1;
214             // avoid ArrayIndexOutOfBoundsException, but should not happen
215
if (lastIndex > -1) {
216                 ((Button) this.getOptions().get(lastIndex)).setHtmlPost(control.getButtonHtmlPost() + "</table>"); //$NON-NLS-1$
217
}
218         }
219         if (width != null) {
220             control.setHtmlPre("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
221
}
222         control.setButtons(this.getOptions());
223         out.write(control.getHtml());
224         if (control.getButtonType() == ControlSuper.BUTTONTYPE_CHECKBOX
225             && control.getValueType() != ControlSuper.VALUETYPE_MULTIPLE) {
226             // checkboxSwitch: value is stored in a hidden field (allows default selecting)
227
String JavaDoc value = this.getValue();
228             if (StringUtils.isEmpty(value)) {
229                 if (this.getConfigValue("selected").equals("true")) { //$NON-NLS-1$ //$NON-NLS-2$
230
value = "true"; //$NON-NLS-1$
231
}
232                 else {
233                     value = "false"; //$NON-NLS-1$
234
}
235             }
236             out.write(new Hidden(this.getName(), value).getHtml());
237         }
238         this.drawHtmlPost(out);
239     }
240 }
241
Popular Tags