001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/explorer/menu/CmsMenuRule.java,v $
003: * Date : $Date: 2008-02-27 12:05:22 $
004: * Version: $Revision: 1.4 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.explorer.menu;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.main.CmsLog;
036: import org.opencms.main.CmsRuntimeException;
037: import org.opencms.workplace.explorer.CmsResourceUtil;
038:
039: import java.util.ArrayList;
040: import java.util.Collections;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import org.apache.commons.logging.Log;
045:
046: /**
047: * Defines a named menu rule set to check the visibility of a single context menu item in the explorer view.<p>
048: *
049: * @author Andreas Zahner
050: *
051: * @version $Revision: 1.4 $
052: *
053: * @since 6.5.6
054: */
055: public class CmsMenuRule {
056:
057: /** The log object for this class. */
058: private static final Log LOG = CmsLog.getLog(CmsMenuRule.class);
059:
060: /** Indicates if the menu rule set is frozen. */
061: private boolean m_frozen;
062:
063: /** The menu item rules that are part of this rule set. */
064: private List m_menuItemRules;
065:
066: /** The name of the menu rule set. */
067: private String m_name;
068:
069: /**
070: * Constructor without parameters, needed for initialization from OpenCms configuration.<p>
071: */
072: public CmsMenuRule() {
073:
074: // initialize members
075: m_menuItemRules = new ArrayList(5);
076: }
077:
078: /**
079: * Adds a single menu item rule to the list of rules.<p>
080: *
081: * @param menuItemRule the menu item rule to add
082: */
083: public void addMenuItemRule(I_CmsMenuItemRule menuItemRule) {
084:
085: try {
086: m_menuItemRules.add(menuItemRule);
087: } catch (Exception e) {
088: if (LOG.isErrorEnabled()) {
089: LOG.error(Messages.get().getBundle().key(
090: Messages.ERR_INITIALIZE_MENUITEMRULE_1,
091: menuItemRule.getClass().getName()));
092: }
093: }
094: }
095:
096: /**
097: * Adds a single menu item rule to the list of rules.<p>
098: *
099: * @param className the class name of the menu item rule to add
100: */
101: public void addMenuItemRuleName(String className) {
102:
103: try {
104: m_menuItemRules.add(Class.forName(className).newInstance());
105: } catch (Exception e) {
106: if (LOG.isErrorEnabled()) {
107: LOG.error(Messages.get().getBundle().key(
108: Messages.ERR_INITIALIZE_MENUITEMRULE_1,
109: className));
110: }
111: }
112: }
113:
114: /**
115: * Freezes the name and the items of the menu rule set.<p>
116: *
117: * They can not be modified anymore after calling this method.<p>
118: */
119: public void freeze() {
120:
121: if (!m_frozen) {
122: m_frozen = true;
123: // freeze the item rules list
124: m_menuItemRules = Collections
125: .unmodifiableList(m_menuItemRules);
126: }
127: }
128:
129: /**
130: * Returns the first matching rule for the resource to create the context menu for.<p>
131: *
132: * @param cms the current OpenCms user context
133: * @param resourceUtil the initialized resource utilities of the resource
134: * @return the first matching rule for the resource
135: */
136: public I_CmsMenuItemRule getMatchingRule(CmsObject cms,
137: CmsResourceUtil[] resourceUtil) {
138:
139: Iterator i = getMenuItemRules().iterator();
140: while (i.hasNext()) {
141: I_CmsMenuItemRule rule = (I_CmsMenuItemRule) i.next();
142: if (rule.matches(cms, resourceUtil)) {
143: return rule;
144: }
145: }
146: return null;
147: }
148:
149: /**
150: * Returns the menu item rules class instances.<p>
151: *
152: * @return the menu item rules class instances
153: */
154: public List getMenuItemRules() {
155:
156: return m_menuItemRules;
157: }
158:
159: /**
160: * Returns the name of the menu rule set.<p>
161: *
162: * @return the name of the menu rule set
163: */
164: public String getName() {
165:
166: return m_name;
167: }
168:
169: /**
170: * Returns <code>true</code> if this menu rule set is frozen, that is read only.<p>
171: *
172: * @return <code>true</code> if this menu rule set is frozen, that is read only
173: */
174: public boolean isFrozen() {
175:
176: return m_frozen;
177: }
178:
179: /**
180: * Sets the menu item rules class instances.<p>
181: *
182: * @param menuItemRules the menu item rules class instances
183: */
184: public void setMenuItemRules(List menuItemRules) {
185:
186: m_menuItemRules = menuItemRules;
187: }
188:
189: /**
190: * Sets the name of the menu rule set.<p>
191: *
192: * @param name the name of the menu rule set
193: */
194: public void setName(String name) {
195:
196: if (m_frozen) {
197: throw new CmsRuntimeException(Messages.get().container(
198: Messages.ERR_MENURULE_FROZEN_0));
199: }
200: m_name = name;
201: }
202:
203: }
|