KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsTool.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
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.main.OpenCms;
35 import org.opencms.util.CmsIdentifiableObjectContainer;
36 import org.opencms.workplace.CmsWorkplace;
37
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * Implementation of an administration tool.<p>
43  *
44  * An admin tool can be a link to itself through
45  * the <code>{@link #buttonHtml(CmsWorkplace)}</code> method,
46  * as also a group of <code>{@link CmsToolGroup}</code>s through the
47  * <code>{@link #groupHtml(CmsWorkplace)}</code> method.<p>
48  *
49  * @author Michael Moossen
50  *
51  * @version $Revision: 1.24 $
52  *
53  * @since 6.0.0
54  */

55 public class CmsTool {
56
57     /** Sub-tools container. */
58     private final CmsIdentifiableObjectContainer m_container = new CmsIdentifiableObjectContainer(true, true);
59
60     /** Handler that represents this tool. */
61     private I_CmsToolHandler m_handler;
62
63     /** Dhtml id, from name. */
64     private final String JavaDoc m_id;
65
66     /**
67      * Default Constructor.<p>
68      *
69      * @param id a unique id
70      * @param handler the handler that represents this tool
71      */

72     public CmsTool(String JavaDoc id, I_CmsToolHandler handler) {
73
74         m_id = id;
75         m_handler = handler;
76
77     }
78
79     /**
80      * Adds a group.<p>
81      *
82      * @param group the group
83      *
84      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object)
85      */

86     public void addToolGroup(CmsToolGroup group) {
87
88         m_container.addIdentifiableObject(group.getName(), group);
89     }
90
91     /**
92      * Adds a group at the given position.<p>
93      *
94      * @param group the group
95      * @param position the position
96      *
97      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object, float)
98      */

99     public void addToolGroup(CmsToolGroup group, float position) {
100
101         m_container.addIdentifiableObject(group.getName(), group, position);
102     }
103
104     /**
105      * Returns the necessary html code for a link to this tool.<p>
106      *
107      * @param wp the jsp page to write the code to
108      *
109      * @return html code
110      */

111     public String JavaDoc buttonHtml(CmsWorkplace wp) {
112
113         if (!m_handler.isVisible(wp.getCms())) {
114             return "";
115         }
116         String JavaDoc link = CmsToolManager.linkForToolPath(
117             wp.getJsp(),
118             getHandler().getPath(),
119             getHandler().getParameters(wp));
120         String JavaDoc onClic = "openPage('" + link + "');";
121         return A_CmsHtmlIconButton.defaultButtonHtml(
122             wp.getJsp(),
123             CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT,
124             getId(),
125             m_handler.getName(),
126             m_handler.isEnabled(wp.getCms()) ? m_handler.getHelpText() : m_handler.getDisabledHelpText(),
127             m_handler.isEnabled(wp.getCms()),
128             m_handler.getIconPath(),
129             m_handler.getConfirmationMessage(),
130             onClic);
131     }
132
133     /**
134      * Compares two tools by name.<p>
135      *
136      * @param obj the other tool
137      *
138      * @return <code>true</code> if the tools have the same name
139      */

140     public boolean equals(Object JavaDoc obj) {
141
142         if (obj == this) {
143             return true;
144         }
145         if (obj instanceof CmsTool) {
146             return ((CmsTool)obj).m_id.equals(m_id);
147         }
148         return false;
149     }
150
151     /**
152      * Returns the handler.<p>
153      *
154      * @return the handler
155      */

156     public I_CmsToolHandler getHandler() {
157
158         return m_handler;
159     }
160
161     /**
162      * Returns the dhtml unique id.<p>
163      *
164      * @return the dhtml unique id
165      */

166     public String JavaDoc getId() {
167
168         return m_id;
169     }
170
171     /**
172      * Returns the requested group.<p>
173      *
174      * @param name the name of the group
175      *
176      * @return the group
177      *
178      * @see org.opencms.util.I_CmsIdentifiableObjectContainer#getObject(String)
179      */

180     public CmsToolGroup getToolGroup(String JavaDoc name) {
181
182         return (CmsToolGroup)m_container.getObject(name);
183     }
184
185     /**
186      * Retuns a list of groups.<p>
187      *
188      * @return a list of <code>{@link CmsToolGroup}</code>
189      */

190     public List JavaDoc getToolGroups() {
191
192         return m_container.elementList();
193     }
194
195     /**
196      * Returns the necessary html code for the tool subgroups.<p>
197      *
198      * @param wp the jsp page to write the code to
199      *
200      * @return html code
201      */

202     public String JavaDoc groupHtml(CmsWorkplace wp) {
203
204         List JavaDoc subTools = OpenCms.getWorkplaceManager().getToolManager().getToolsForPath(wp, getHandler().getPath(), false);
205         Iterator JavaDoc itSubTools = subTools.iterator();
206         m_container.clear();
207         while (itSubTools.hasNext()) {
208             CmsTool subTool = (CmsTool)itSubTools.next();
209             // locate group
210
CmsToolGroup group = null;
211             String JavaDoc groupName = CmsToolMacroResolver.resolveMacros(subTool.getHandler().getGroup(), wp);
212
213             // in the parent tool
214
group = getToolGroup(groupName);
215             if (group == null) {
216                 // if does not exist, create it
217
String JavaDoc gid = "group" + getToolGroups().size();
218                 group = new CmsToolGroup(gid, groupName);
219                 addToolGroup(group, subTool.getHandler().getPosition());
220             }
221
222             // add to group
223
group.addAdminTool(subTool, subTool.getHandler().getPosition());
224
225         }
226
227         StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
228         Iterator JavaDoc itHtml = getToolGroups().iterator();
229         while (itHtml.hasNext()) {
230             CmsToolGroup group = (CmsToolGroup)itHtml.next();
231             html.append(group.groupHtml(wp));
232         }
233         return CmsToolMacroResolver.resolveMacros(html.toString(), wp);
234     }
235
236     /**
237      * @see java.lang.Object#hashCode()
238      */

239     public int hashCode() {
240
241         return m_handler.getName().hashCode();
242     }
243 }
Popular Tags