KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsDialogProperty


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsDialogProperty.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.9 $
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.workplace.editors;
33
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.i18n.CmsMessages;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.workplace.commons.CmsPropertyCustom;
41 import org.opencms.workplace.explorer.CmsNewResourceXmlPage;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.TreeMap JavaDoc;
47
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50 import javax.servlet.jsp.PageContext JavaDoc;
51
52 import org.apache.commons.logging.Log;
53
54 /**
55  * Provides methods for the special xmlpage property dialog.<p>
56  *
57  * This is a special dialog that is used for xmlpages in the workplace and the editors.<p>
58  * Uses methods from the customized property dialog where possible.<p>
59  *
60  * The following files use this class:
61  * <ul>
62  * <li>/jsp/editors/dialogs/property.html
63  * </ul>
64  * <p>
65  *
66  * @author Andreas Zahner
67  *
68  * @version $Revision: 1.9 $
69  *
70  * @since 6.0.0
71  */

72 public class CmsDialogProperty extends CmsPropertyCustom {
73
74     /** The log object for this class. */
75     private static final Log LOG = CmsLog.getLog(CmsDialogProperty.class);
76
77     /** Flag indicating if the template property was changed. */
78     private boolean m_templateChanged;
79
80     /**
81      * Public constructor with JSP action element.<p>
82      *
83      * @param jsp an initialized JSP action element
84      */

85     public CmsDialogProperty(CmsJspActionElement jsp) {
86
87         super(jsp);
88     }
89
90     /**
91      * Public constructor with JSP variables.<p>
92      *
93      * @param context the JSP page context
94      * @param req the JSP request
95      * @param res the JSP response
96      */

97     public CmsDialogProperty(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
98
99         this(new CmsJspActionElement(context, req, res));
100     }
101
102     /**
103      * Creates the HTML String for the edit properties form.<p>
104      *
105      * @return the HTML output String for the edit properties form
106      */

107     public String JavaDoc buildEditForm() {
108
109         CmsMessages messages = Messages.get().getBundle(getLocale());
110         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(2048);
111
112         // check if the properties are editable
113
boolean editable = isEditable();
114         // create "disabled" attribute if properties are not editable
115
String JavaDoc disabled = "";
116         if (!editable) {
117             disabled = " disabled=\"disabled\"";
118         }
119
120         retValue.append("<table border=\"0\">\n");
121         retValue.append("<tr>\n");
122         retValue.append("\t<td class=\"textbold\">" + messages.key(Messages.GUI_INPUT_PROPERTY_0) + "</td>\n");
123         retValue.append("\t<td class=\"textbold\">" + messages.key(Messages.GUI_LABEL_VALUE_0) + "</td>\n");
124         retValue.append("\t<td class=\"textbold\" style=\"white-space: nowrap;\">"
125             + messages.key(Messages.GUI_INPUT_USEDPROPERTY_0)
126             + "</td>\n");
127         retValue.append("</tr>\n");
128         retValue.append("<tr><td><span style=\"height: 6px;\"></span></td></tr>\n");
129
130         // create template select box row
131
retValue.append(buildTableRowStart(messages.key(Messages.GUI_INPUT_TEMPLATE_0)));
132         retValue.append(buildSelectTemplates("name=\""
133             + CmsPropertyDefinition.PROPERTY_TEMPLATE
134             + "\" class=\"maxwidth noborder\""
135             + disabled));
136         retValue.append("</td>\n");
137         retValue.append("\t<td class=\"textcenter\">");
138         retValue.append("&nbsp;");
139         retValue.append(buildTableRowEnd());
140
141         // create the text property input rows
142
retValue.append(buildTextInput(editable));
143
144         // show navigation properties if enabled in explorer type settings
145
if (showNavigation()) {
146             retValue.append(buildNavigationProperties(editable));
147         }
148
149         retValue.append("</table>");
150
151         return retValue.toString();
152     }
153
154     /**
155      * Builds the html for the page template select box.<p>
156      *
157      * @param attributes optional attributes for the &lt;select&gt; tag
158      * @return the html for the page template select box
159      */

160     public String JavaDoc buildSelectTemplates(String JavaDoc attributes) {
161
162         List JavaDoc options = new ArrayList JavaDoc();
163         List JavaDoc values = new ArrayList JavaDoc();
164         int selectedValue = -1;
165         String JavaDoc currentTemplate = null;
166         TreeMap JavaDoc templates = null;
167         try {
168             // read the current template
169
currentTemplate = getCms().readPropertyObject(
170                 getParamResource(),
171                 CmsPropertyDefinition.PROPERTY_TEMPLATE,
172                 true).getValue();
173             // get all available templates
174
templates = CmsNewResourceXmlPage.getTemplates(getCms(), getParamResource());
175         } catch (CmsException e) {
176             // ignore this exception
177
if (LOG.isInfoEnabled()) {
178                 LOG.info(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_FAILED_0), e);
179             }
180         }
181         if (currentTemplate == null) {
182             currentTemplate = "";
183         }
184         if (templates == null) {
185             // no valid templated found, use only current one
186
addCurrentTemplate(currentTemplate, options, values);
187         } else {
188             boolean found = false;
189             // templates found, create option and value lists
190
Iterator JavaDoc i = templates.keySet().iterator();
191             int counter = 0;
192             while (i.hasNext()) {
193                 String JavaDoc key = (String JavaDoc)i.next();
194                 String JavaDoc path = (String JavaDoc)templates.get(key);
195                 if (currentTemplate.equals(path)) {
196                     // mark the currently selected template
197
selectedValue = counter;
198                     found = true;
199                 }
200                 options.add(key);
201                 values.add(path);
202                 counter++;
203             }
204             if (!found) {
205                 // current template was not found among module templates, add current template as option
206
addCurrentTemplate(currentTemplate, options, values);
207                 selectedValue = 0;
208             }
209         }
210
211         String JavaDoc hiddenField = "<input type=\"hidden\" name=\""
212             + PREFIX_HIDDEN
213             + CmsPropertyDefinition.PROPERTY_TEMPLATE
214             + "\" value=\""
215             + currentTemplate
216             + "\">";
217         return buildSelect(attributes, options, values, selectedValue, false) + hiddenField;
218     }
219
220     /**
221      * Returns if the template property was changed.<p>
222      *
223      * @return true if the template property was changed, otherwise false
224      */

225     public boolean hasTemplateChanged() {
226
227         return m_templateChanged;
228     }
229
230     /**
231      * Performs the editing of the resources properties.<p>
232      *
233      * @param request the HttpServletRequest
234      * @return true, if the properties were successfully changed, otherwise false
235      * @throws CmsException if editing is not successful
236      */

237     protected boolean performEditOperation(HttpServletRequest JavaDoc request) throws CmsException {
238
239         boolean useTempfileProject = Boolean.valueOf(getParamUsetempfileproject()).booleanValue();
240         try {
241             if (useTempfileProject) {
242                 switchToTempProject();
243             }
244             // write the common properties defined in the explorer type settings
245
Iterator JavaDoc i = getExplorerTypeSettings().getProperties().iterator();
246             // iterate over the properties
247
while (i.hasNext()) {
248                 String JavaDoc curProperty = (String JavaDoc)i.next();
249                 String JavaDoc paramValue = request.getParameter(PREFIX_VALUE + curProperty);
250                 String JavaDoc oldValue = request.getParameter(PREFIX_HIDDEN + curProperty);
251                 writeProperty(curProperty, paramValue, oldValue);
252             }
253
254             // write special file properties
255
String JavaDoc paramValue = null;
256             String JavaDoc oldValue = null;
257
258             // write the navigation properties if enabled
259
if (showNavigation()) {
260                 // get the navigation enabled parameter
261
paramValue = request.getParameter("enablenav");
262                 if (Boolean.valueOf(paramValue).booleanValue()) {
263                     // navigation enabled, update params
264
paramValue = request.getParameter("navpos");
265                     if (!"-1".equals(paramValue)) {
266                         // update the property only when it is different from "-1" (meaning no change)
267
oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_NAVPOS);
268                         writeProperty(CmsPropertyDefinition.PROPERTY_NAVPOS, paramValue, oldValue);
269                     }
270                     paramValue = request.getParameter(PREFIX_VALUE + CmsPropertyDefinition.PROPERTY_NAVTEXT);
271                     oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_NAVTEXT);
272                     writeProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, paramValue, oldValue);
273                 } else {
274                     // navigation disabled, delete property values
275
writeProperty(CmsPropertyDefinition.PROPERTY_NAVPOS, null, null);
276                     writeProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, null, null);
277                 }
278             }
279
280             // get the template parameter
281
paramValue = request.getParameter(CmsPropertyDefinition.PROPERTY_TEMPLATE);
282             oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_TEMPLATE);
283             writeProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, paramValue, oldValue);
284             if (paramValue != null && !paramValue.equals(oldValue)) {
285                 // template has changed, refresh editor window
286
m_templateChanged = true;
287             }
288
289         } finally {
290             if (useTempfileProject) {
291                 switchToCurrentProject();
292             }
293         }
294         return true;
295     }
296
297     /**
298      * Adds the currently selected template value to the option and value list.<p>
299      *
300      * @param currentTemplate the currently selected template to add
301      * @param options the option list
302      * @param values the value list
303      */

304     private void addCurrentTemplate(String JavaDoc currentTemplate, List JavaDoc options, List JavaDoc values) {
305
306         CmsMessages messages = Messages.get().getBundle(getLocale());
307
308         // template was not found in regular template folders, add current template value
309
if (CmsStringUtil.isEmpty(currentTemplate)) {
310             // current template not available, add "please select" value
311
options.add(0, "--- " + messages.key(Messages.GUI_PLEASE_SELECT_0) + " ---");
312             values.add(0, "");
313         } else {
314             // current template was set to some value, add this value to the selection
315
String JavaDoc name = null;
316             try {
317                 // read the title of the current template
318
name = getCms().readPropertyObject(currentTemplate, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue();
319             } catch (CmsException e) {
320                 // ignore this exception - the title for this template was not readable
321
if (LOG.isInfoEnabled()) {
322                     LOG.info(messages.key(Messages.LOG_READ_TITLE_PROP_FAILED_1, currentTemplate), e);
323                 }
324             }
325             if (name == null) {
326                 name = currentTemplate;
327             }
328             options.add(0, "* " + name);
329             values.add(0, currentTemplate);
330         }
331     }
332 }
Popular Tags