KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > A_CmsHtmlWidget


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/A_CmsHtmlWidget.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.widgets;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.main.OpenCms;
37 import org.opencms.util.CmsStringUtil;
38 import org.opencms.workplace.CmsWorkplace;
39 import org.opencms.workplace.galleries.A_CmsGallery;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * Provides a widget that creates a rich input field using the matching component, for use on a widget dialog.<p>
50  *
51  * The matching component is determined by checking the installed editors for the best matching component to use.<p>
52  *
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.2 $
56  *
57  * @since 6.0.1
58  */

59 public abstract class A_CmsHtmlWidget extends A_CmsWidget {
60
61     /** The configured Html widget options. */
62     private CmsHtmlWidgetOption m_htmlWidgetOption;
63
64     /**
65      * Creates a new html editing widget.<p>
66      */

67     public A_CmsHtmlWidget() {
68
69         // empty constructor is required for class registration
70
super();
71     }
72
73     /**
74      * Creates a new html editing widget with the given configuration.<p>
75      *
76      * @param configuration the configuration to use
77      */

78     public A_CmsHtmlWidget(CmsHtmlWidgetOption configuration) {
79
80         super();
81         m_htmlWidgetOption = configuration;
82     }
83
84     /**
85      * Creates a new html editing widget with the given configuration.<p>
86      *
87      * @param configuration the configuration to use
88      */

89     public A_CmsHtmlWidget(String JavaDoc configuration) {
90
91         super(configuration);
92     }
93
94     /**
95      * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
96      */

97     public String JavaDoc getConfiguration() {
98
99         if (super.getConfiguration() != null) {
100             return super.getConfiguration();
101         }
102         return CmsHtmlWidgetOption.createConfigurationString(getHtmlWidgetOption());
103     }
104
105     /**
106      * Returns the configured Html widget options.<p>
107      *
108      * @return the configured Html widget options
109      */

110     public CmsHtmlWidgetOption getHtmlWidgetOption() {
111
112         return m_htmlWidgetOption;
113     }
114
115     /**
116      * @see org.opencms.widgets.I_CmsWidget#setConfiguration(java.lang.String)
117      */

118     public void setConfiguration(String JavaDoc configuration) {
119
120         super.setConfiguration(configuration);
121         m_htmlWidgetOption = new CmsHtmlWidgetOption(configuration);
122     }
123
124     /**
125      * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
126      */

127     public void setEditorValue(
128         CmsObject cms,
129         Map JavaDoc formParameters,
130         I_CmsWidgetDialog widgetDialog,
131         I_CmsWidgetParameter param) {
132
133         String JavaDoc[] values = (String JavaDoc[])formParameters.get(param.getId());
134         if ((values != null) && (values.length > 0)) {
135             String JavaDoc val = CmsEncoder.decode(values[0], CmsEncoder.ENCODING_UTF_8);
136             param.setStringValue(cms, val);
137         }
138     }
139
140     /**
141      * Sets the configured Html widget options.<p>
142      *
143      * @param htmlWidgetOption the configured Html widget options
144      */

145     public void setHtmlWidgetOption(CmsHtmlWidgetOption htmlWidgetOption) {
146
147         m_htmlWidgetOption = htmlWidgetOption;
148     }
149
150     /**
151      * Returns the HTML for the OpenCms specific button row for galleries and links.<p>
152      *
153      * Use this method to generate a button row with OpenCms specific dialog buttons in the
154      * {@link org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)}
155      * method to obtain the buttons.<p>
156      *
157      * Overwrite the method if the integrated editor needs a specific button generation
158      * (e.g. add format select or toggle source code button) or if some buttons should not be available.<p>
159      *
160      * @param widgetDialog the dialog where the widget is used on
161      * @param paramId the id of the current widget
162      * @return the html String for the OpenCms specific button row
163      */

164     protected String JavaDoc buildOpenCmsButtonRow(I_CmsWidgetDialog widgetDialog, String JavaDoc paramId) {
165
166         StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
167         // flag indicating if at least one button is active
168
boolean buttonsActive = false;
169
170         // generate button row start HTML
171
result.append(buildOpenCmsButtonRow(CmsWorkplace.HTML_START, widgetDialog));
172         
173         // build the link buttons
174
if (getHtmlWidgetOption().showLinkDialog()) {
175             result.append(widgetDialog.button("javascript:setActiveEditor('"
176                 + paramId
177                 + "');openLinkDialog('"
178                 + Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.GUI_BUTTON_LINKTO_0)
179                 + "');", null, "link", "button.linkto", widgetDialog.getButtonStyle()));
180             buttonsActive = true;
181         }
182         if (getHtmlWidgetOption().showAnchorDialog()) {
183             result.append(widgetDialog.button("javascript:setActiveEditor('"
184                 + paramId
185                 + "');openAnchorDialog('"
186                 + Messages.get().getBundle(widgetDialog.getLocale()).key(Messages.ERR_EDITOR_MESSAGE_NOSELECTION_0)
187                 + "');", null, "anchor", Messages.GUI_BUTTON_ANCHOR_0, widgetDialog.getButtonStyle()));
188             buttonsActive = true;
189         }
190
191         // build the gallery button row
192
Map JavaDoc galleryMap = OpenCms.getWorkplaceManager().getGalleries();
193         List JavaDoc galleries = new ArrayList JavaDoc(galleryMap.size());
194         Map JavaDoc typeMap = new HashMap JavaDoc(galleryMap.size());
195
196         Iterator JavaDoc i = galleryMap.keySet().iterator();
197         while (i.hasNext()) {
198             String JavaDoc key = (String JavaDoc)i.next();
199             A_CmsGallery currGallery = (A_CmsGallery)galleryMap.get(key);
200             galleries.add(currGallery);
201             // put the type name to the type Map
202
typeMap.put(currGallery, key);
203         }
204
205         // sort the found galleries by their order
206
Collections.sort(galleries);
207
208         StringBuffer JavaDoc galleryResult = new StringBuffer JavaDoc(8);
209         boolean showGallery = false;
210         for (int k = 0; k < galleries.size(); k++) {
211             A_CmsGallery currGallery = (A_CmsGallery)galleries.get(k);
212             String JavaDoc galleryType = (String JavaDoc)typeMap.get(currGallery);
213             if (getHtmlWidgetOption().showGalleryDialog(galleryType)) {
214                 // gallery is shown, build button
215
galleryResult.append(widgetDialog.button("javascript:setActiveEditor('"
216                     + paramId
217                     + "');openGallery('"
218                     + galleryType
219                     + "');", null, galleryType, Messages.getButtonName(CmsStringUtil.substitute(
220                     galleryType,
221                     "gallery",
222                     "")), widgetDialog.getButtonStyle()));
223                 showGallery = true;
224             }
225         }
226
227         if (showGallery) {
228             // at least one gallery is shown, create the gallery buttons
229
if (buttonsActive) {
230                 // show separator before gallery buttons
231
result.append(widgetDialog.buttonBarSeparator(5, 5));
232             }
233             result.append(galleryResult);
234             buttonsActive = true;
235         }
236
237         if (!buttonsActive) {
238             // no active buttons to show, return empty String
239
return "";
240         }
241
242         // generate button row end HTML
243
result.append(buildOpenCmsButtonRow(CmsWorkplace.HTML_END, widgetDialog));
244
245         // show the active buttons
246
return result.toString();
247
248     }
249
250     /**
251      * Returns the start or end HTML for the OpenCms specific button row.<p>
252      *
253      * Use this method to generate the start and end html for the button row.<p>
254      *
255      * Overwrite the method if the integrated editor needs a specific layout for the button row start or end html.<p>
256      *
257      * @param segment the HTML segment (START / END)
258      * @param widgetDialog the dialog where the widget is used on
259      * @return the html String for the OpenCms specific button row
260      */

261     protected String JavaDoc buildOpenCmsButtonRow(int segment, I_CmsWidgetDialog widgetDialog) {
262
263         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
264
265         if (segment == CmsWorkplace.HTML_START) {
266             // generate line and start row HTML
267
result.append(widgetDialog.buttonBarHorizontalLine());
268             result.append(widgetDialog.buttonBar(CmsWorkplace.HTML_START));
269             result.append(widgetDialog.buttonBarStartTab(0, 0));
270         } else {
271             // close button row and generate end line
272
result.append(widgetDialog.buttonBar(CmsWorkplace.HTML_END));
273             result.append(widgetDialog.buttonBarHorizontalLine());
274         }
275
276         return result.toString();
277     }
278 }
Popular Tags