001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.tools;
029
030import org.opencms.main.OpenCms;
031import org.opencms.workplace.CmsWorkplace;
032
033import java.util.Iterator;
034import java.util.List;
035
036/**
037 * Implementation of an administration tool.<p>
038 *
039 * An admin tool can be a link to itself through
040 * the <code>{@link #buttonHtml(CmsWorkplace)}</code> method,
041 * as also a group of <code>{@link CmsToolGroup}</code>s through the
042 * <code>{@link #groupHtml(CmsWorkplace)}</code> method.<p>
043 *
044 * @since 6.0.0
045 */
046public class CmsTool {
047
048    /** Sub-tools container. */
049    private final CmsIdentifiableObjectContainer<CmsToolGroup> m_container = new CmsIdentifiableObjectContainer<CmsToolGroup>(
050        true,
051        true);
052
053    /** Handler that represents this tool. */
054    private I_CmsToolHandler m_handler;
055
056    /** Dhtml id, from name. */
057    private final String m_id;
058
059    /**
060     * Default Constructor.<p>
061     *
062     * @param id a unique id
063     * @param handler the handler that represents this tool
064     */
065    public CmsTool(String id, I_CmsToolHandler handler) {
066
067        m_id = id;
068        m_handler = handler;
069
070    }
071
072    /**
073     * Adds a group.<p>
074     *
075     * @param group the group
076     *
077     * @see org.opencms.workplace.tools.CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object)
078     */
079    public void addToolGroup(CmsToolGroup group) {
080
081        m_container.addIdentifiableObject(group.getName(), group);
082    }
083
084    /**
085     * Adds a group at the given position.<p>
086     *
087     * @param group the group
088     * @param position the position
089     *
090     * @see org.opencms.workplace.tools.CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object, float)
091     */
092    public void addToolGroup(CmsToolGroup group, float position) {
093
094        m_container.addIdentifiableObject(group.getName(), group, position);
095    }
096
097    /**
098     * Returns the necessary html code for a link to this tool.<p>
099     *
100     * @param wp the jsp page to write the code to
101     *
102     * @return html code
103     */
104    public String buttonHtml(CmsWorkplace wp) {
105
106        if (!m_handler.isVisible(wp)) {
107            return "";
108        }
109        String link = CmsToolManager.linkForToolPath(
110            wp.getJsp(),
111            getHandler().getPath(),
112            getHandler().getParameters(wp));
113        String onClic = "openPage('" + link + "');";
114        return A_CmsHtmlIconButton.defaultButtonHtml(
115            CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT,
116            getId(),
117            m_handler.getShortName(),
118            m_handler.isEnabled(wp) ? m_handler.getHelpText() : m_handler.getDisabledHelpText(),
119            m_handler.isEnabled(wp),
120            m_handler.getIconPath(),
121            m_handler.getConfirmationMessage(),
122            onClic);
123    }
124
125    /**
126     * Compares two tools by name.<p>
127     *
128     * @param obj the other tool
129     *
130     * @return <code>true</code> if the tools have the same name
131     */
132    @Override
133    public boolean equals(Object obj) {
134
135        if (obj == this) {
136            return true;
137        }
138        if (obj instanceof CmsTool) {
139            return ((CmsTool)obj).m_id.equals(m_id);
140        }
141        return false;
142    }
143
144    /**
145     * Returns the handler.<p>
146     *
147     * @return the handler
148     */
149    public I_CmsToolHandler getHandler() {
150
151        return m_handler;
152    }
153
154    /**
155     * Returns the dhtml unique id.<p>
156     *
157     * @return the dhtml unique id
158     */
159    public String getId() {
160
161        return m_id;
162    }
163
164    /**
165     * Returns the requested group.<p>
166     *
167     * @param name the name of the group
168     *
169     * @return the group
170     *
171     * @see org.opencms.workplace.tools.CmsIdentifiableObjectContainer#getObject(String)
172     */
173    public CmsToolGroup getToolGroup(String name) {
174
175        return m_container.getObject(name);
176    }
177
178    /**
179     * Returns a list of groups.<p>
180     *
181     * @return a list of <code>{@link CmsToolGroup}</code>
182     */
183    public List<CmsToolGroup> getToolGroups() {
184
185        return m_container.elementList();
186    }
187
188    /**
189     * Returns the necessary html code for the tool subgroups.<p>
190     *
191     * @param wp the jsp page to write the code to
192     *
193     * @return html code
194     */
195    public String groupHtml(CmsWorkplace wp) {
196
197        List<CmsTool> subTools = OpenCms.getWorkplaceManager().getToolManager().getToolsForPath(
198            wp,
199            getHandler().getPath(),
200            false);
201        Iterator<CmsTool> itSubTools = subTools.iterator();
202        m_container.clear();
203        while (itSubTools.hasNext()) {
204            CmsTool subTool = itSubTools.next();
205            // locate group
206            CmsToolGroup group = null;
207            String groupName = CmsToolMacroResolver.resolveMacros(subTool.getHandler().getGroup(), wp);
208
209            // in the parent tool
210            group = getToolGroup(groupName);
211            if (group == null) {
212                // if does not exist, create it
213                String gid = "group" + getToolGroups().size();
214                group = new CmsToolGroup(gid, groupName);
215                addToolGroup(group, subTool.getHandler().getPosition());
216            }
217
218            // add to group
219            group.addAdminTool(subTool, subTool.getHandler().getPosition());
220
221        }
222
223        StringBuffer html = new StringBuffer(512);
224        Iterator<CmsToolGroup> itHtml = getToolGroups().iterator();
225        while (itHtml.hasNext()) {
226            CmsToolGroup group = itHtml.next();
227            html.append(group.groupHtml(wp));
228        }
229        return CmsToolMacroResolver.resolveMacros(html.toString(), wp);
230    }
231
232    /**
233     * @see java.lang.Object#hashCode()
234     */
235    @Override
236    public int hashCode() {
237
238        return m_handler.getName().hashCode();
239    }
240}