KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > explorer > CmsExplorerContextMenu


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/explorer/CmsExplorerContextMenu.java,v $
3  * Date : $Date: 2006/05/08 08:15:03 $
4  * Version: $Revision: 1.14 $
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.explorer;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.i18n.CmsMessages;
36 import org.opencms.main.CmsLog;
37 import org.opencms.main.OpenCms;
38 import org.opencms.security.CmsPermissionSet;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.workplace.CmsWorkplace;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.List JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * Provides methods to build a context menu for an explorer resource type.<p>
52  *
53  * This object stores all entries which are displayed in a context menu in a sorted list.
54  * The sort order is specified in an attribute of the context menu subnodes
55  * in the OpenCms configuration.<p>
56  *
57  * @author Andreas Zahner
58  *
59  * @version $Revision: 1.14 $
60  *
61  * @since 6.0.0
62  */

63 public class CmsExplorerContextMenu {
64
65     /** The log object for this class. */
66     private static final Log LOG = CmsLog.getLog(CmsExplorerContextMenu.class);
67
68     /** All context menu entries. */
69     private List JavaDoc m_allEntries;
70     /** Stores already generated javascript menu outputs with a Locale object as key. */
71     private HashMap JavaDoc m_generatedScripts;
72     /** Indicated if this is a multi context menu. */
73     private boolean m_multiMenu;
74
75     /**
76      * Default constructor.<p>
77      */

78     public CmsExplorerContextMenu() {
79
80         m_allEntries = new ArrayList JavaDoc();
81         m_generatedScripts = new HashMap JavaDoc();
82     }
83
84     /**
85      * Adds a list of CmsContextMenuItem objects to the context menu list.<p>
86      *
87      * The list is sorted by their order after that operation.<p>
88      *
89      * @param entries a list of initialized context menu items
90      */

91     public void addEntries(List JavaDoc entries) {
92
93         m_allEntries.addAll(entries);
94         sortEntries();
95     }
96
97     /**
98      * Adds a single CmsContextMenuItem object to the context menu list.<p>
99      *
100      * The list is sorted by their order after that operation.<p>
101      *
102      * @param entry a single context menu item
103      */

104     public void addEntry(CmsExplorerContextMenuItem entry) {
105
106         m_allEntries.add(entry);
107         sortEntries();
108     }
109
110     /**
111      * Adds a single context menu entry to the list of context menu items.<p>
112      *
113      * @param key the key of the current entry
114      * @param uri the dialog URI to call with the current entry
115      * @param rules the display rules
116      * @param target the frame target of the menu entry
117      * @param order the sort order of the current entry
118      */

119     public void addMenuEntry(String JavaDoc key, String JavaDoc uri, String JavaDoc rules, String JavaDoc target, String JavaDoc order) {
120
121         Integer JavaDoc orderValue = new Integer JavaDoc(0);
122         try {
123             orderValue = Integer.valueOf(order);
124         } catch (Exception JavaDoc e) {
125             if (LOG.isErrorEnabled()) {
126                 LOG.error(Messages.get().getBundle().key(Messages.LOG_WRONG_ORDER_CONTEXT_MENU_1, key));
127             }
128         }
129         CmsExplorerContextMenuItem item = new CmsExplorerContextMenuItem(
130             CmsExplorerContextMenuItem.TYPE_ENTRY,
131             key,
132             uri,
133             rules,
134             target,
135             orderValue);
136
137         addEntry(item);
138         if (LOG.isDebugEnabled()) {
139             LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_MENU_ENTRY_2, key, order));
140         }
141     }
142
143     /**
144      * Adds a menu separator to the list of context menu items.<p>
145      *
146      * @param order the sort order of the separator
147      */

148     public void addMenuSeparator(String JavaDoc order) {
149
150         Integer JavaDoc orderValue = new Integer JavaDoc(0);
151         try {
152             orderValue = Integer.valueOf(order);
153         } catch (Exception JavaDoc e) {
154             LOG.error(Messages.get().getBundle().key(Messages.LOG_WRONG_MENU_SEP_ORDER_0, order));
155         }
156         CmsExplorerContextMenuItem item = new CmsExplorerContextMenuItem(
157             CmsExplorerContextMenuItem.TYPE_SEPARATOR,
158             null,
159             null,
160             null,
161             null,
162             orderValue);
163         addEntry(item);
164         if (LOG.isDebugEnabled()) {
165             LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRONG_MENU_SEP_ORDER_0, order));
166         }
167     }
168
169     /**
170      * @see java.lang.Object#clone()
171      */

172     public Object JavaDoc clone() {
173
174         CmsExplorerContextMenu objectClone = new CmsExplorerContextMenu();
175         objectClone.setMultiMenu(m_multiMenu);
176         objectClone.setAllEntries(m_allEntries);
177         return objectClone;
178     }
179
180     /**
181      * Returns all entries of the context menu.<p>
182      *
183      * @return all entries of the context menu
184      */

185     public List JavaDoc getAllEntries() {
186
187         return m_allEntries;
188     }
189
190     /**
191      * Builds the Javascript to create the context menu.<p>
192      *
193      * @param cms the CmsObject
194      * @param settings the explorer type settings for which the context menu is created
195      * @param resTypeId the id of the resource type which uses the context menu
196      * @param messages the messages to generate the context menu with (should be the workplace messages)
197      * @return the JavaScript output to create the context menu
198      */

199     public String JavaDoc getJSEntries(CmsObject cms, CmsExplorerTypeSettings settings, int resTypeId, CmsMessages messages) {
200
201         // try to get the stored entries from the Map
202
String JavaDoc entries = (String JavaDoc)m_generatedScripts.get(messages.getLocale());
203
204         if (entries == null) {
205             //CmsMessages messages = OpenCms.getWorkplaceManager().getMessages(locale);
206

207             // entries not yet in Map, so generate them
208
StringBuffer JavaDoc result = new StringBuffer JavaDoc(4096);
209             String JavaDoc jspWorkplaceUri = OpenCms.getLinkManager().substituteLink(cms, CmsWorkplace.PATH_WORKPLACE);
210
211             if (!isMultiMenu()) {
212                 // create the JS for the resource object
213
result.append("\nvi.resource[").append(resTypeId).append("]=new res(\"").append(settings.getName()).append(
214                     "\", ");
215                 result.append("\"");
216                 result.append(messages.key(settings.getKey()));
217                 result.append("\", vi.skinPath + \"filetypes/");
218                 result.append(settings.getIcon());
219                 result.append("\", \"");
220                 result.append(settings.getNewResourceUri());
221                 result.append("\", true);\n");
222             }
223
224             Iterator JavaDoc i = getAllEntries().iterator();
225             while (i.hasNext()) {
226                 // create the context menu items
227
CmsExplorerContextMenuItem item = (CmsExplorerContextMenuItem)i.next();
228                 result.append("addMenuEntry(");
229                 if (isMultiMenu()) {
230                     result.append("'multi'");
231                 } else {
232                     result.append(resTypeId);
233                 }
234                 result.append(", ");
235                 if (CmsExplorerContextMenuItem.TYPE_ENTRY.equals(item.getType())) {
236                     // create a menu entry
237
result.append("\"").append(messages.key(item.getKey())).append("\", ");
238                     result.append("\"");
239                     if (item.getUri().startsWith("/")) {
240                         result.append(OpenCms.getLinkManager().substituteLink(cms, item.getUri()));
241                     } else {
242                         result.append(jspWorkplaceUri);
243                         result.append(item.getUri());
244                     }
245
246                     result.append("\", ");
247                     // check the item target
248
String JavaDoc target = item.getTarget();
249                     if (target == null) {
250                         target = "";
251                     }
252                     result.append("\"'");
253                     result.append(target);
254                     result.append("'\", ");
255                     // remove all blanks from the rule String
256
String JavaDoc rules = CmsStringUtil.substitute(item.getRules(), " ", "");
257                     // parse the rules to create the autolock column
258
rules = parseRules(rules, item.getKey());
259                     result.append("\"");
260                     result.append(rules);
261                     result.append("\");\n");
262                     // result: addMenuEntry([id], "[language_key]", "[dialogURI]", "'[target]'", "ddiiiiaaaiaaaiddddddddddddiiiidddd");
263
} else {
264                     // create a separator entry
265
result.append("\"-\", \" \", \"''\", \"\");\n");
266                     // result: addMenuEntry([id], "-", " ", "''", "ddaaaaaaaaaaaaddddddddddddaaaadddd");
267
}
268             }
269             entries = result.toString();
270             // store the generated entries
271
m_generatedScripts.put(messages.getLocale(), entries);
272         }
273
274         if (!isMultiMenu()) {
275             // determine if this resource type is editable for the current user
276
CmsPermissionSet permissions = settings.getAccess().getPermissions(cms);
277             if (!permissions.requiresWritePermission()) {
278                 // the type is not editable, set editable to false
279
entries += "vi.resource[" + resTypeId + "].editable = false;\n";
280             }
281         }
282
283         return entries;
284     }
285
286     /**
287      * Tests if the context menu is empty.<p>
288      *
289      * @return true or false
290      */

291     public boolean isEmpty() {
292
293         boolean empty = true;
294         if (m_allEntries.size() > 0) {
295             empty = false;
296         }
297         return empty;
298     }
299
300     /**
301      * Returns true if the menu is a multi context menu for more than one selected file.<p>
302      *
303      * @return if the menu is a multi context menu for more than one selected file
304      */

305     public boolean isMultiMenu() {
306
307         return m_multiMenu;
308     }
309
310     /**
311      * Sets all entries of the context menu.<p>
312      *
313      * The list is sorted by their order after that operation.<p>
314      *
315      * @param entries all entries of the context menu
316      */

317     public void setAllEntries(List JavaDoc entries) {
318
319         m_allEntries = entries;
320         sortEntries();
321     }
322
323     /**
324      * Sets if the menu is a multi context menu for more than one selected file.<p>
325      *
326      * @param multiMenu true, if the menu is a multi context menu for more than one selected file, otherwise false
327      */

328     public void setMultiMenu(boolean multiMenu) {
329
330         m_multiMenu = multiMenu;
331     }
332
333     /**
334      * Sorts the list of entries according to the value of the "order" attribute in the configuration.<p>
335      */

336     public void sortEntries() {
337
338         Collections.sort(m_allEntries);
339     }
340
341     /**
342      * Parses the rules and adds a column for the autolock feature of resources.<p>
343      *
344      * @param rules the current rules
345      * @param key the key name of the current item
346      * @return the rules with added autlock rules column
347      */

348     private String JavaDoc parseRules(String JavaDoc rules, String JavaDoc key) {
349
350         if (CmsStringUtil.isEmptyOrWhitespaceOnly(rules)) {
351             return "";
352         }
353         StringBuffer JavaDoc newRules = new StringBuffer JavaDoc(rules.length() + 4);
354         newRules.append(rules.substring(0, 6));
355         if (Messages.GUI_EXPLORER_CONTEXT_LOCK_0.equalsIgnoreCase(key)
356             || Messages.GUI_EXPLORER_CONTEXT_UNLOCK_0.equalsIgnoreCase(key)) {
357             // for "lock" and "unlock" item, use same rules as "unlocked" column
358
newRules.append(rules.substring(2, 6));
359         } else {
360             // for all other items, use same rules as "locked exclusively by current user" column
361
newRules.append(rules.substring(6, 10));
362         }
363         newRules.append(rules.substring(6));
364         return newRules.toString();
365     }
366 }
Popular Tags