001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsTool.java,v $
003: * Date : $Date: 2008-02-27 12:05:31 $
004: * Version: $Revision: 1.27 $
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.tools;
033:
034: import org.opencms.main.OpenCms;
035: import org.opencms.workplace.CmsWorkplace;
036:
037: import java.util.Iterator;
038: import java.util.List;
039:
040: /**
041: * Implementation of an administration tool.<p>
042: *
043: * An admin tool can be a link to itself through
044: * the <code>{@link #buttonHtml(CmsWorkplace)}</code> method,
045: * as also a group of <code>{@link CmsToolGroup}</code>s through the
046: * <code>{@link #groupHtml(CmsWorkplace)}</code> method.<p>
047: *
048: * @author Michael Moossen
049: *
050: * @version $Revision: 1.27 $
051: *
052: * @since 6.0.0
053: */
054: public class CmsTool {
055:
056: /** Sub-tools container. */
057: private final CmsIdentifiableObjectContainer m_container = new CmsIdentifiableObjectContainer(
058: true, true);
059:
060: /** Handler that represents this tool. */
061: private I_CmsToolHandler m_handler;
062:
063: /** Dhtml id, from name. */
064: private final String m_id;
065:
066: /**
067: * Default Constructor.<p>
068: *
069: * @param id a unique id
070: * @param handler the handler that represents this tool
071: */
072: public CmsTool(String id, I_CmsToolHandler handler) {
073:
074: m_id = id;
075: m_handler = handler;
076:
077: }
078:
079: /**
080: * Adds a group.<p>
081: *
082: * @param group the group
083: *
084: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object)
085: */
086: public void addToolGroup(CmsToolGroup group) {
087:
088: m_container.addIdentifiableObject(group.getName(), group);
089: }
090:
091: /**
092: * Adds a group at the given position.<p>
093: *
094: * @param group the group
095: * @param position the position
096: *
097: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object, float)
098: */
099: public void addToolGroup(CmsToolGroup group, float position) {
100:
101: m_container.addIdentifiableObject(group.getName(), group,
102: position);
103: }
104:
105: /**
106: * Returns the necessary html code for a link to this tool.<p>
107: *
108: * @param wp the jsp page to write the code to
109: *
110: * @return html code
111: */
112: public String buttonHtml(CmsWorkplace wp) {
113:
114: if (!m_handler.isVisible(wp)) {
115: return "";
116: }
117: String link = CmsToolManager.linkForToolPath(wp.getJsp(),
118: getHandler().getPath(), getHandler().getParameters(wp));
119: String onClic = "openPage('" + link + "');";
120: return A_CmsHtmlIconButton.defaultButtonHtml(
121: CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT, getId(),
122: m_handler.getShortName(),
123: m_handler.isEnabled(wp) ? m_handler.getHelpText()
124: : m_handler.getDisabledHelpText(), m_handler
125: .isEnabled(wp), m_handler.getIconPath(),
126: m_handler.getConfirmationMessage(), onClic);
127: }
128:
129: /**
130: * Compares two tools by name.<p>
131: *
132: * @param obj the other tool
133: *
134: * @return <code>true</code> if the tools have the same name
135: */
136: public boolean equals(Object obj) {
137:
138: if (obj == this ) {
139: return true;
140: }
141: if (obj instanceof CmsTool) {
142: return ((CmsTool) obj).m_id.equals(m_id);
143: }
144: return false;
145: }
146:
147: /**
148: * Returns the handler.<p>
149: *
150: * @return the handler
151: */
152: public I_CmsToolHandler getHandler() {
153:
154: return m_handler;
155: }
156:
157: /**
158: * Returns the dhtml unique id.<p>
159: *
160: * @return the dhtml unique id
161: */
162: public String getId() {
163:
164: return m_id;
165: }
166:
167: /**
168: * Returns the requested group.<p>
169: *
170: * @param name the name of the group
171: *
172: * @return the group
173: *
174: * @see org.opencms.workplace.tools.I_CmsIdentifiableObjectContainer#getObject(String)
175: */
176: public CmsToolGroup getToolGroup(String name) {
177:
178: return (CmsToolGroup) m_container.getObject(name);
179: }
180:
181: /**
182: * Retuns a list of groups.<p>
183: *
184: * @return a list of <code>{@link CmsToolGroup}</code>
185: */
186: public List getToolGroups() {
187:
188: return m_container.elementList();
189: }
190:
191: /**
192: * Returns the necessary html code for the tool subgroups.<p>
193: *
194: * @param wp the jsp page to write the code to
195: *
196: * @return html code
197: */
198: public String groupHtml(CmsWorkplace wp) {
199:
200: List subTools = OpenCms.getWorkplaceManager().getToolManager()
201: .getToolsForPath(wp, getHandler().getPath(), false);
202: Iterator itSubTools = subTools.iterator();
203: m_container.clear();
204: while (itSubTools.hasNext()) {
205: CmsTool subTool = (CmsTool) itSubTools.next();
206: // locate group
207: CmsToolGroup group = null;
208: String groupName = CmsToolMacroResolver.resolveMacros(
209: subTool.getHandler().getGroup(), wp);
210:
211: // in the parent tool
212: group = getToolGroup(groupName);
213: if (group == null) {
214: // if does not exist, create it
215: String gid = "group" + getToolGroups().size();
216: group = new CmsToolGroup(gid, groupName);
217: addToolGroup(group, subTool.getHandler().getPosition());
218: }
219:
220: // add to group
221: group.addAdminTool(subTool, subTool.getHandler()
222: .getPosition());
223:
224: }
225:
226: StringBuffer html = new StringBuffer(512);
227: Iterator itHtml = getToolGroups().iterator();
228: while (itHtml.hasNext()) {
229: CmsToolGroup group = (CmsToolGroup) itHtml.next();
230: html.append(group.groupHtml(wp));
231: }
232: return CmsToolMacroResolver.resolveMacros(html.toString(), wp);
233: }
234:
235: /**
236: * @see java.lang.Object#hashCode()
237: */
238: public int hashCode() {
239:
240: return m_handler.getName().hashCode();
241: }
242: }
|