KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > A_CmsToolHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/A_CmsToolHandler.java,v $
3  * Date : $Date: 2006/04/28 15:20:52 $
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.tools;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.i18n.CmsMessages;
38 import org.opencms.jsp.CmsJspNavBuilder;
39 import org.opencms.jsp.CmsJspNavElement;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.util.CmsStringUtil;
43 import org.opencms.workplace.CmsWorkplace;
44
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Map JavaDoc;
48
49 import org.apache.commons.logging.Log;
50
51 /**
52  * Helper class to build easily other admin tool handlers.<p>
53  *
54  * @author Michael Moossen
55  *
56  * @version $Revision: 1.24 $
57  *
58  * @since 6.0.0
59  */

60 public abstract class A_CmsToolHandler implements I_CmsToolHandler {
61
62     /** Property for the params arg.<p> */
63     public static final String JavaDoc ARG_PARAM_NAME = "params";
64
65     /** Property for the path arg.<p> */
66     public static final String JavaDoc ARG_PATH_NAME = "path";
67
68     /** Property definition for the args.<p> */
69     public static final String JavaDoc ARGS_PROPERTY_DEFINITION = "admintoolhandler-args";
70
71     /** Argument separator.<p> */
72     public static final String JavaDoc ARGUMENT_SEPARATOR = "|";
73
74     /** Default disabled help text constant.<p> */
75     public static final String JavaDoc DEFAULT_DISABLED_HELPTEXT = "${key." + Messages.GUI_TOOLS_DISABLED_HELP_0 + "}";
76
77     /** Argument name and value separator.<p> */
78     public static final String JavaDoc VALUE_SEPARATOR = ":";
79
80     /** Property for the confirmation message arg.<p> */
81     private static final String JavaDoc ARG_CONFIRMATION_NAME = "confirmation";
82
83     /** Confirmation message. */
84     private String JavaDoc m_confirmationMessage;
85
86     /** Help text or description if disabled. */
87     private String JavaDoc m_disabledHelpText;
88
89     /** Group to be included in. */
90     private String JavaDoc m_group;
91
92     /** Help text or description. */
93     private String JavaDoc m_helpText;
94
95     /** Icon path (32x32). */
96     private String JavaDoc m_iconPath;
97
98     /** Link pointer. */
99     private String JavaDoc m_link;
100
101     /** Display name. */
102     private String JavaDoc m_name;
103
104     /** Needed parameters. */
105     private String JavaDoc m_parameters;
106
107     /** Tool path to install in. */
108     private String JavaDoc m_path;
109
110     /** Relative position in group. */
111     private float m_position;
112
113     /** Menu item name. */
114     private String JavaDoc m_shortName;
115
116     /** Small icon path (16x16). */
117     private String JavaDoc m_smallIconPath;
118
119     /** The static log object for this class. */
120     private static final Log LOG = CmsLog.getLog(A_CmsToolHandler.class);
121
122     /**
123      * Returns the confirmation Message.<p>
124      *
125      * @return the confirmation Message
126      */

127     public String JavaDoc getConfirmationMessage() {
128
129         return m_confirmationMessage;
130     }
131
132     /**
133      * @see org.opencms.workplace.tools.I_CmsToolHandler#getDisabledHelpText()
134      */

135     public String JavaDoc getDisabledHelpText() {
136
137         return m_disabledHelpText;
138     }
139
140     /**
141      * @see org.opencms.workplace.tools.I_CmsToolHandler#getGroup()
142      */

143     public String JavaDoc getGroup() {
144
145         return m_group;
146     }
147
148     /**
149      * @see org.opencms.workplace.tools.I_CmsToolHandler#getHelpText()
150      */

151     public String JavaDoc getHelpText() {
152
153         return m_helpText;
154     }
155
156     /**
157      * @see org.opencms.workplace.tools.I_CmsToolHandler#getIconPath()
158      */

159     public String JavaDoc getIconPath() {
160
161         return m_iconPath;
162     }
163
164     /**
165      * @see org.opencms.workplace.tools.I_CmsToolHandler#getLink()
166      */

167     public String JavaDoc getLink() {
168
169         return m_link;
170     }
171
172     /**
173      * @see org.opencms.workplace.tools.I_CmsToolHandler#getName()
174      */

175     public String JavaDoc getName() {
176
177         return m_name;
178     }
179
180     /**
181      * @see org.opencms.workplace.tools.I_CmsToolHandler#getParameters(org.opencms.workplace.CmsWorkplace)
182      */

183     public Map JavaDoc getParameters(CmsWorkplace wp) {
184
185         Map JavaDoc argMap = new HashMap JavaDoc();
186         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_parameters)) {
187             String JavaDoc toolParams = wp.resolveMacros(m_parameters);
188             Iterator JavaDoc itArgs = CmsStringUtil.splitAsList(toolParams, "&").iterator();
189             while (itArgs.hasNext()) {
190                 String JavaDoc arg = (String JavaDoc)itArgs.next();
191                 int pos = arg.indexOf("=");
192                 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(arg.substring(pos + 1))) {
193                     argMap.put(arg.substring(0, pos), arg.substring(pos + 1));
194                 }
195             }
196         }
197         return argMap;
198     }
199
200     /**
201      * @see org.opencms.workplace.tools.I_CmsToolHandler#getPath()
202      */

203     public String JavaDoc getPath() {
204
205         return m_path;
206     }
207
208     /**
209      * @see org.opencms.workplace.tools.I_CmsToolHandler#getPosition()
210      */

211     public float getPosition() {
212
213         return m_position;
214     }
215
216     /**
217      * @see org.opencms.workplace.tools.I_CmsToolHandler#getShortName()
218      */

219     public String JavaDoc getShortName() {
220
221         return m_shortName;
222     }
223
224     /**
225      * @see org.opencms.workplace.tools.I_CmsToolHandler#getSmallIconPath()
226      */

227     public String JavaDoc getSmallIconPath() {
228
229         return m_smallIconPath;
230     }
231
232     /**
233      * Sets the confirmation Message.<p>
234      *
235      * @param confirmationMessage the confirmation Message to set
236      */

237     public void setConfirmationMessage(String JavaDoc confirmationMessage) {
238
239         m_confirmationMessage = confirmationMessage;
240     }
241
242     /**
243      * Sets the help text if disabled.<p>
244      *
245      * @param disabledHelpText the help text to set
246      */

247     public void setDisabledHelpText(String JavaDoc disabledHelpText) {
248
249         m_disabledHelpText = disabledHelpText;
250     }
251
252     /**
253      * Sets the group.<p>
254      *
255      * @param group the group to set
256      */

257     public void setGroup(String JavaDoc group) {
258
259         m_group = group;
260     }
261
262     /**
263      * Sets the help text.<p>
264      *
265      * @param helpText the help text to set
266      */

267     public void setHelpText(String JavaDoc helpText) {
268
269         m_helpText = helpText;
270     }
271
272     /**
273      * Sets the icon path.<p>
274      *
275      * @param iconPath the icon path to set
276      */

277     public void setIconPath(String JavaDoc iconPath) {
278
279         m_iconPath = iconPath;
280     }
281
282     /**
283      * Sets the link.<p>
284      *
285      * @param link the link to set
286      */

287     public void setLink(String JavaDoc link) {
288
289         m_link = link;
290     }
291
292     /**
293      * Sets the name.<p>
294      *
295      * @param name the name to set
296      */

297     public void setName(String JavaDoc name) {
298
299         m_name = name;
300     }
301
302     /**
303      * Sets the parameter string.<p>
304      *
305      * @param paramString the parameter string to set
306      */

307     public void setParameterString(String JavaDoc paramString) {
308
309         m_parameters = paramString;
310     }
311
312     /**
313      * Sets the path.<p>
314      *
315      * @param path the path to set
316      */

317     public void setPath(String JavaDoc path) {
318
319         m_path = path;
320     }
321
322     /**
323      * Sets the position.<p>
324      *
325      * @param position the position to set
326      */

327     public void setPosition(float position) {
328
329         m_position = position;
330     }
331
332     /**
333      * Sets the short name.<p>
334      *
335      * @param shortName the short name to set
336      */

337     public void setShortName(String JavaDoc shortName) {
338
339         m_shortName = shortName;
340     }
341
342     /**
343      * Sets the small icon path.<p>
344      *
345      * @param smallIconPath the samll icon path to set
346      */

347     public void setSmallIconPath(String JavaDoc smallIconPath) {
348
349         m_smallIconPath = smallIconPath;
350     }
351
352     /**
353      * Default implementation.
354      *
355      * It takes the icon path from <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVIMAGE}</code> property,
356      * or uses a default icon if undefined, the name is taken from the
357      * <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVTEXT}</code> property,
358      * or uses the <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_TITLE}</code> property if undefined,
359      * or an default text, if still undefined. if you want 2 different names, one for the big icon tools and one for
360      * the menu/navbar entries, use a <code>{@link A_CmsToolHandler#VALUE_SEPARATOR}</code> to separate them in the property.
361      * (if you do so, the first one is for big icons and the second one for menu/navbar entries). the help text is taken from the
362      * <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_DESCRIPTION}</code> property or a
363      * default text if undefined, if you want to customize a help text while disabled, use a
364      * <code>{@link A_CmsToolHandler#VALUE_SEPARATOR}</code> as a separator in the same property.<p>
365      *
366      * The group is taken from the <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVINFO}</code> property,
367      * the position from the <code>{@link org.opencms.file.CmsPropertyDefinition#PROPERTY_NAVPOS}</code>
368      * and the install path is given by the folder structure if the <code>{@link #ARGS_PROPERTY_DEFINITION}</code>
369      * property does not include path information.<p>
370      *
371      * For the icon path you can specify 2 paths separated by a <code>{@link A_CmsToolHandler#VALUE_SEPARATOR}</code>,
372      * the first one will be used as a group icon (32x32), and the second as an menu icon (16x16). The paths are relative
373      * to the /system/workplace/resources/ folder. If the tool is disabled, the names of the icons are composed as
374      * ${name}_disabled.${ext}<p>
375      *
376      * The confirmation message is taken from the <code>{@link #ARGS_PROPERTY_DEFINITION}</code> with key
377      * <code>{@link #ARG_CONFIRMATION_NAME}</code>
378      *
379      * @see org.opencms.workplace.tools.I_CmsToolHandler#setup(org.opencms.file.CmsObject, CmsToolRootHandler, java.lang.String)
380      */

381     public boolean setup(CmsObject cms, CmsToolRootHandler root, String JavaDoc resourcePath) {
382
383         CmsJspNavElement navElem = CmsJspNavBuilder.getNavigationForResource(cms, resourcePath);
384
385         String JavaDoc name = navElem.getNavText();
386         if (CmsMessages.isUnknownKey(name)) {
387             name = navElem.getTitle();
388         }
389         if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
390             name = "${key." + Messages.GUI_TOOLS_DEFAULT_NAME_0 + "}";
391         }
392         String JavaDoc shortName = name;
393         if (name.indexOf(VALUE_SEPARATOR) >= 0) {
394             shortName = name.substring(name.indexOf(VALUE_SEPARATOR) + 1);
395             name = name.substring(0, name.indexOf(VALUE_SEPARATOR));
396         }
397         setName(name);
398         setShortName(shortName);
399
400         String JavaDoc iconPath = navElem.getNavImage();
401         if (CmsStringUtil.isEmptyOrWhitespaceOnly(iconPath)) {
402             iconPath = "admin/images/default_tool_big.png:admin/images/default_tool_small.png";
403         }
404         String JavaDoc smallIconPath = iconPath;
405         if (iconPath.indexOf(VALUE_SEPARATOR) >= 0) {
406             smallIconPath = iconPath.substring(iconPath.indexOf(VALUE_SEPARATOR) + 1);
407             iconPath = iconPath.substring(0, iconPath.indexOf(VALUE_SEPARATOR));
408         }
409         setIconPath(iconPath);
410         setSmallIconPath(smallIconPath);
411
412         String JavaDoc helpText = navElem.getDescription();
413         if (CmsStringUtil.isEmptyOrWhitespaceOnly(helpText)) {
414             helpText = "${key." + Messages.GUI_TOOLS_DEFAULT_HELP_0 + "}";
415         }
416         String JavaDoc disabledHelpText = DEFAULT_DISABLED_HELPTEXT;
417         if (helpText.indexOf(VALUE_SEPARATOR) >= 0) {
418             disabledHelpText = helpText.substring(helpText.indexOf(VALUE_SEPARATOR) + 1);
419             helpText = helpText.substring(0, helpText.indexOf(VALUE_SEPARATOR));
420         }
421         setHelpText(helpText);
422         setDisabledHelpText(disabledHelpText);
423
424         String JavaDoc group = navElem.getInfo();
425         if (CmsStringUtil.isEmptyOrWhitespaceOnly(group)) {
426             group = "${key." + Messages.GUI_TOOLS_DEFAULT_GROUP_0 + "}";
427         }
428
429         String JavaDoc path = resourcePath;
430         setLink(cms, resourcePath);
431         try {
432             if (cms.readResource(resourcePath).isFolder()) {
433                 path = CmsToolManager.TOOLPATH_SEPARATOR
434                     + resourcePath.substring(
435                         root.getUri().length(),
436                         resourcePath.lastIndexOf(CmsToolManager.TOOLPATH_SEPARATOR));
437             } else {
438                 path = CmsToolManager.TOOLPATH_SEPARATOR
439                     + resourcePath.substring(root.getUri().length(), resourcePath.lastIndexOf('.'));
440             }
441         } catch (CmsException e) {
442             // ignore
443
}
444         // install point
445
setPath(path);
446         setGroup(group);
447         setPosition(navElem.getNavPosition());
448
449         // parameters
450
setParameters(cms, resourcePath);
451
452         return !path.equals(resourcePath);
453     }
454
455     /**
456      * @see java.lang.Object#toString()
457      */

458     public String JavaDoc toString() {
459
460         return m_path + " - " + m_group + " - " + m_position;
461     }
462
463     /**
464      * Sets the link for the given resource.<p>
465      *
466      * Use the <code>resourcePath</code> as link if it is not a folder.
467      *
468      * If it is a folder, try to use the folder default file property value as link.
469      * if not use the {@link CmsToolManager#VIEW_JSPPAGE_LOCATION}.
470      *
471      * @param cms the cms context
472      * @param resourcePath the path to the resource to set the link for
473      */

474     protected void setLink(CmsObject cms, String JavaDoc resourcePath) {
475
476         String JavaDoc link = resourcePath;
477         try {
478             // make sure the res is a folder
479
cms.readFolder(resourcePath);
480
481             // adjust the path
482
if (resourcePath.endsWith(CmsToolManager.TOOLPATH_SEPARATOR)) {
483                 resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf(CmsToolManager.TOOLPATH_SEPARATOR));
484             }
485
486             // set admin page as link
487
link = CmsToolManager.VIEW_JSPPAGE_LOCATION;
488
489             // try to use the folder def file as link
490
CmsProperty prop = cms.readPropertyObject(resourcePath, CmsPropertyDefinition.PROPERTY_DEFAULT_FILE, false);
491             String JavaDoc defFile = "index.jsp";
492             if (!prop.isNullProperty()) {
493                 defFile = prop.getValue();
494             }
495             if (!defFile.startsWith(CmsToolManager.TOOLPATH_SEPARATOR)) {
496                 // try to use this relative link
497
defFile = resourcePath + CmsToolManager.TOOLPATH_SEPARATOR + defFile;
498             }
499             if (defFile.indexOf("?") > 0) {
500                 if (cms.existsResource(defFile.substring(0, defFile.indexOf("?")))) {
501                     link = defFile;
502                 }
503             } else if (cms.existsResource(defFile)) {
504                 link = defFile;
505             }
506         } catch (CmsException e) {
507             // noop
508
}
509
510         setLink(link);
511     }
512
513     /**
514      * Sets the needed properties from the {@link #ARGS_PROPERTY_DEFINITION} property of the given resource.<p>
515      *
516      * @param cms the cms context
517      * @param resourcePath the path to the resource to read the property from
518      */

519     protected void setParameters(CmsObject cms, String JavaDoc resourcePath) {
520
521         try {
522             CmsProperty prop = cms.readPropertyObject(resourcePath, ARGS_PROPERTY_DEFINITION, false);
523             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(prop.getValue())) {
524                 Map JavaDoc argsMap = new HashMap JavaDoc();
525                 Iterator JavaDoc itArgs = CmsStringUtil.splitAsList(prop.getValue(), ARGUMENT_SEPARATOR).iterator();
526                 while (itArgs.hasNext()) {
527                     String JavaDoc arg = "";
528                     try {
529                         arg = (String JavaDoc)itArgs.next();
530                         int pos = arg.indexOf(VALUE_SEPARATOR);
531                         argsMap.put(arg.substring(0, pos), arg.substring(pos + 1));
532                     } catch (StringIndexOutOfBoundsException JavaDoc e) {
533                         LOG.error("sep: " + VALUE_SEPARATOR + "arg: " + arg);
534                         throw e;
535                     }
536                 }
537                 if (argsMap.get(ARG_PATH_NAME) != null) {
538                     setPath((String JavaDoc)argsMap.get(ARG_PATH_NAME));
539                 }
540                 if (argsMap.get(ARG_CONFIRMATION_NAME) != null) {
541                     setConfirmationMessage((String JavaDoc)argsMap.get(ARG_CONFIRMATION_NAME));
542                 }
543                 if (argsMap.get(ARG_PARAM_NAME) != null) {
544                     setParameterString((String JavaDoc)argsMap.get(ARG_PARAM_NAME));
545                 }
546             }
547         } catch (CmsException e) {
548             // noop
549
}
550     }
551 }
Popular Tags