KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsChnav


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsChnav.java,v $
3  * Date : $Date: 2006/09/15 14:19:57 $
4  * Version: $Revision: 1.24 $
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.commons;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.i18n.CmsEncoder;
39 import org.opencms.i18n.CmsMessages;
40 import org.opencms.jsp.CmsJspActionElement;
41 import org.opencms.jsp.CmsJspNavBuilder;
42 import org.opencms.jsp.CmsJspNavElement;
43 import org.opencms.main.CmsException;
44 import org.opencms.main.CmsLog;
45 import org.opencms.main.OpenCms;
46 import org.opencms.security.CmsPermissionSet;
47 import org.opencms.util.CmsStringUtil;
48 import org.opencms.workplace.CmsDialog;
49 import org.opencms.workplace.CmsWorkplace;
50 import org.opencms.workplace.CmsWorkplaceSettings;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.List JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57 import javax.servlet.jsp.JspException JavaDoc;
58 import javax.servlet.jsp.PageContext JavaDoc;
59
60 import org.apache.commons.logging.Log;
61
62 /**
63  * Provides methods for the change navigation dialog.<p>
64  *
65  * The following files use this class:
66  * <ul>
67  * <li>/commons/chnav.jsp
68  * </ul>
69  * <p>
70  *
71  * @author Andreas Zahner
72  *
73  * @version $Revision: 1.24 $
74  *
75  * @since 6.0.0
76  */

77 public class CmsChnav extends CmsDialog {
78
79     /** Value for the action: change the navigation. */
80     public static final int ACTION_CHNAV = 100;
81
82     /** The dialog type. */
83     public static final String JavaDoc DIALOG_TYPE = "chnav";
84
85     /** Request parameter name for the navigation position. */
86     public static final String JavaDoc PARAM_NAVPOS = "navpos";
87
88     /** Request parameter name for the navigation text. */
89     public static final String JavaDoc PARAM_NAVTEXT = "navtext";
90
91     /** The log object for this class. */
92     private static final Log LOG = CmsLog.getLog(CmsChnav.class);
93
94     private String JavaDoc m_paramNavpos;
95
96     private String JavaDoc m_paramNavtext;
97
98     /**
99      * Public constructor.<p>
100      *
101      * @param jsp an initialized JSP action element
102      */

103     public CmsChnav(CmsJspActionElement jsp) {
104
105         super(jsp);
106     }
107
108     /**
109      * Public constructor with JSP variables.<p>
110      *
111      * @param context the JSP page context
112      * @param req the JSP request
113      * @param res the JSP response
114      */

115     public CmsChnav(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
116
117         this(new CmsJspActionElement(context, req, res));
118     }
119
120     /**
121      * Builds the HTML for the select box of the navigation position.<p>
122      *
123      * @param cms the CmsObject
124      * @param filename the current file
125      * @param attributes optional attributes for the &lt;select&gt; tag, do not add the "name" atribute!
126      * @param messages the localized workplace messages
127      *
128      * @return the HTML for a navigation position select box
129      */

130     public static String JavaDoc buildNavPosSelector(CmsObject cms, String JavaDoc filename, String JavaDoc attributes, CmsMessages messages) {
131
132         List JavaDoc navList = new ArrayList JavaDoc();
133         List JavaDoc options = new ArrayList JavaDoc();
134         List JavaDoc values = new ArrayList JavaDoc();
135
136         // get current file navigation element
137
CmsJspNavElement curNav = CmsJspNavBuilder.getNavigationForResource(cms, filename);
138
139         // get the parent folder of the current file
140
filename = CmsResource.getParentFolder(filename);
141
142         // get navigation of the current folder
143
navList = CmsJspNavBuilder.getNavigationForFolder(cms, filename);
144         float maxValue = 0;
145         float nextPos = 0;
146
147         // calculate value for the first navigation position
148
float firstValue = 1;
149         if (navList.size() > 0) {
150             try {
151                 CmsJspNavElement ne = (CmsJspNavElement)navList.get(0);
152                 maxValue = ne.getNavPosition();
153             } catch (Exception JavaDoc e) {
154                 // should usually never happen
155
LOG.error(e.getLocalizedMessage());
156             }
157         }
158
159         if (maxValue != 0) {
160             firstValue = maxValue / 2;
161         }
162
163         // add the first entry: before first element
164
options.add(messages.key(Messages.GUI_CHNAV_POS_FIRST_0));
165         values.add(firstValue + "");
166
167         // show all present navigation elements in box
168
for (int i = 0; i < navList.size(); i++) {
169             CmsJspNavElement ne = (CmsJspNavElement)navList.get(i);
170             String JavaDoc navText = ne.getNavText();
171             float navPos = ne.getNavPosition();
172             // get position of next nav element
173
nextPos = navPos + 2;
174             if ((i + 1) < navList.size()) {
175                 nextPos = ((CmsJspNavElement)navList.get(i + 1)).getNavPosition();
176             }
177             // calculate new position of current nav element
178
float newPos;
179             if ((nextPos - navPos) > 1) {
180                 newPos = navPos + 1;
181             } else {
182                 newPos = (navPos + nextPos) / 2;
183             }
184
185             // check new maxValue of positions and increase it
186
if (navPos > maxValue) {
187                 maxValue = navPos;
188             }
189
190             // if the element is the current file, mark it in selectbox
191
if (curNav.getNavText().equals(navText) && curNav.getNavPosition() == navPos) {
192                 options.add(CmsEncoder.escapeHtml(messages.key(
193                     Messages.GUI_CHNAV_POS_CURRENT_1,
194                     new Object JavaDoc[] {ne.getFileName()})));
195                 values.add("-1");
196             } else {
197                 options.add(CmsEncoder.escapeHtml(navText + " [" + ne.getFileName() + "]"));
198                 values.add(newPos + "");
199             }
200         }
201
202         // add the entry: at the last position
203
options.add(messages.key(Messages.GUI_CHNAV_POS_LAST_0));
204         values.add((maxValue + 1) + "");
205
206         // add the entry: no change
207
options.add(messages.key(Messages.GUI_CHNAV_NO_CHANGE_0));
208         if (curNav.getNavPosition() == Float.MAX_VALUE) {
209             // current resource has no valid position, use "last position"
210
values.add((maxValue + 1) + "");
211         } else {
212             // current resource has valid position, use "-1" for no change
213
values.add("-1");
214         }
215
216         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(attributes)) {
217             attributes = " " + attributes;
218         } else {
219             attributes = "";
220         }
221         return CmsWorkplace.buildSelect(
222             "name=\"" + PARAM_NAVPOS + "\"" + attributes,
223             options,
224             values,
225             values.size() - 1,
226             true);
227     }
228
229     /**
230      * Performs the navigation change.<p>
231      *
232      * @throws JspException if including a JSP subelement is not successful
233      */

234     public void actionChangeNav() throws JspException JavaDoc {
235
236         // save initialized instance of this class in request attribute for included sub-elements
237
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
238
239         // get request parameters
240
String JavaDoc filename = getParamResource();
241         String JavaDoc newText = getParamNavtext();
242         String JavaDoc selectedPosString = getParamNavpos();
243
244         try {
245             // lock resource if autolock is enabled
246
checkLock(getParamResource());
247             // save the new NavText if not null
248
if (newText != null) {
249                 CmsProperty newNavText = new CmsProperty();
250                 newNavText.setName(CmsPropertyDefinition.PROPERTY_NAVTEXT);
251                 CmsProperty oldNavText = getCms().readPropertyObject(
252                     filename,
253                     CmsPropertyDefinition.PROPERTY_NAVTEXT,
254                     false);
255                 if (oldNavText.isNullProperty()) {
256                     // property value was not already set
257
if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
258                         newNavText.setStructureValue(newText);
259                     } else {
260                         newNavText.setResourceValue(newText);
261                     }
262                 } else {
263                     if (oldNavText.getStructureValue() != null) {
264                         newNavText.setStructureValue(newText);
265                         newNavText.setResourceValue(oldNavText.getResourceValue());
266                     } else {
267                         newNavText.setResourceValue(newText);
268                     }
269                 }
270
271                 String JavaDoc oldStructureValue = oldNavText.getStructureValue();
272                 String JavaDoc newStructureValue = newNavText.getStructureValue();
273                 if (CmsStringUtil.isEmpty(oldStructureValue)) {
274                     oldStructureValue = CmsProperty.DELETE_VALUE;
275                 }
276                 if (CmsStringUtil.isEmpty(newStructureValue)) {
277                     newStructureValue = CmsProperty.DELETE_VALUE;
278                 }
279
280                 String JavaDoc oldResourceValue = oldNavText.getResourceValue();
281                 String JavaDoc newResourceValue = newNavText.getResourceValue();
282                 if (CmsStringUtil.isEmpty(oldResourceValue)) {
283                     oldResourceValue = CmsProperty.DELETE_VALUE;
284                 }
285                 if (CmsStringUtil.isEmpty(newResourceValue)) {
286                     newResourceValue = CmsProperty.DELETE_VALUE;
287                 }
288
289                 // change nav text only if it has been changed
290
if (!oldResourceValue.equals(newResourceValue) || !oldStructureValue.equals(newStructureValue)) {
291                     getCms().writePropertyObject(getParamResource(), newNavText);
292                 }
293             }
294
295             // determine the selected position
296
float selectedPos = -1;
297             try {
298                 selectedPos = Float.parseFloat(selectedPosString);
299             } catch (Exception JavaDoc e) {
300                 // can usually be ignored
301
if (LOG.isInfoEnabled()) {
302                     LOG.info(e.getLocalizedMessage());
303                 }
304             }
305
306             // only update the position if a change is requested
307
if (selectedPos != -1) {
308                 CmsProperty newNavPos = new CmsProperty();
309                 newNavPos.setName(CmsPropertyDefinition.PROPERTY_NAVPOS);
310                 CmsProperty oldNavPos = getCms().readPropertyObject(
311                     filename,
312                     CmsPropertyDefinition.PROPERTY_NAVPOS,
313                     false);
314                 if (oldNavPos.isNullProperty()) {
315                     // property value was not already set
316
if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
317                         newNavPos.setStructureValue(selectedPosString);
318                     } else {
319                         newNavPos.setResourceValue(selectedPosString);
320                     }
321                 } else {
322                     if (oldNavPos.getStructureValue() != null) {
323                         newNavPos.setStructureValue(selectedPosString);
324                         newNavPos.setResourceValue(oldNavPos.getResourceValue());
325                     } else {
326                         newNavPos.setResourceValue(selectedPosString);
327                     }
328                 }
329                 getCms().writePropertyObject(filename, newNavPos);
330             }
331         } catch (Throwable JavaDoc e) {
332             // error during chnav, show error dialog
333
includeErrorpage(this, e);
334         }
335         // chnav operation was successful, return to workplace
336
actionCloseDialog();
337     }
338
339     /**
340      * Builds the HTML for the select box of the navigation position.<p>
341      *
342      * @return the HTML for a navigation position select box
343      */

344     public String JavaDoc buildNavPosSelector() {
345
346         return buildNavPosSelector(getCms(), getParamResource(), null, getMessages());
347     }
348
349     /**
350      * Returns the escaped NavText property value of the current resource.<p>
351      *
352      * @return the NavText property value of the current resource
353      */

354     public String JavaDoc getCurrentNavText() {
355
356         try {
357             String JavaDoc navText = getCms().readPropertyObject(
358                 getParamResource(),
359                 CmsPropertyDefinition.PROPERTY_NAVTEXT,
360                 false).getValue();
361             if (navText == null) {
362                 navText = "";
363             }
364             return CmsEncoder.escapeXml(navText);
365         } catch (CmsException e) {
366             // can usually be ignored
367
if (LOG.isInfoEnabled()) {
368                 LOG.info(e.getLocalizedMessage());
369             }
370             return "";
371         }
372
373     }
374
375     /**
376      * Returns the value of the navigation position parameter,
377      * or null if this parameter was not provided.<p>
378      *
379      * The navigation position parameter defines the new value for
380      * the NavPos property.<p>
381      *
382      * @return the value of the target parameter
383      */

384     public String JavaDoc getParamNavpos() {
385
386         return m_paramNavpos;
387     }
388
389     /**
390      * Returns the value of the navigation text parameter,
391      * or null if this parameter was not provided.<p>
392      *
393      * The navigation text parameter defines the new value for
394      * the NavText property.<p>
395      *
396      * @return the value of the target parameter
397      */

398     public String JavaDoc getParamNavtext() {
399
400         return m_paramNavtext;
401     }
402
403     /**
404      * Sets the value of the navigation position parameter.<p>
405      *
406      * @param value the value to set
407      */

408     public void setParamNavpos(String JavaDoc value) {
409
410         m_paramNavpos = value;
411     }
412
413     /**
414      * Sets the value of the navigation text parameter.<p>
415      *
416      * @param value the value to set
417      */

418     public void setParamNavtext(String JavaDoc value) {
419
420         m_paramNavtext = value;
421     }
422
423     /**
424      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
425      */

426     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
427
428         // fill the parameter values in the get/set methods
429
fillParamValues(request);
430
431         // check the required permissions to change navigation of the resource
432
if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
433             // no write permissions for the resource, set cancel action to close dialog
434
setParamAction(DIALOG_CANCEL);
435         }
436
437         // set the dialog type
438
setParamDialogtype(DIALOG_TYPE);
439         // set the action for the JSP switch
440
if (DIALOG_TYPE.equals(getParamAction())) {
441             setAction(ACTION_CHNAV);
442         } else if (DIALOG_CANCEL.equals(getParamAction())) {
443             setAction(ACTION_CANCEL);
444         } else {
445             setAction(ACTION_DEFAULT);
446             // build title for chnav dialog
447
setParamTitle(key(Messages.GUI_CHNAV_1, new Object JavaDoc[] {CmsResource.getName(getParamResource())}));
448         }
449     }
450
451 }
452
Popular Tags