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, 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.ade.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.client.Messages;
031import org.opencms.ade.containerpage.shared.CmsContainerElementData;
032import org.opencms.gwt.client.dnd.I_CmsDropTarget;
033import org.opencms.gwt.client.ui.CmsListItem;
034import org.opencms.gwt.client.ui.CmsListItemWidget;
035import org.opencms.gwt.client.ui.CmsPushButton;
036import org.opencms.gwt.client.ui.I_CmsButton;
037import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
038import org.opencms.gwt.client.util.CmsDomUtil;
039import org.opencms.gwt.shared.CmsAdditionalInfoBean;
040import org.opencms.gwt.shared.CmsListInfoBean;
041
042import com.google.gwt.event.dom.client.ClickEvent;
043import com.google.gwt.event.dom.client.ClickHandler;
044import com.google.gwt.event.shared.HandlerRegistration;
045
046/**
047 * Draggable menu element. Needed for favorite list.<p>
048 *
049 * @since 8.0.0
050 */
051public class CmsMenuListItem extends CmsListItem {
052
053    /** The element edit button. */
054    protected CmsPushButton m_editButton;
055
056    /** The edit click handler registration. */
057    private HandlerRegistration m_editHandlerRegistration;
058
059    /** The element delete button. */
060    private CmsPushButton m_removeButton;
061
062    /**
063     * Constructor.<p>
064     *
065     * @param element the element data
066     */
067    public CmsMenuListItem(CmsContainerElementData element) {
068
069        super(new CmsListItemWidget(new CmsListInfoBean(element.getTitle(), element.getSitePath(), null)));
070        if (!m_listItemWidget.hasAdditionalInfo()) {
071            m_listItemWidget.addAdditionalInfo(
072                new CmsAdditionalInfoBean("", Messages.get().key(Messages.GUI_NO_SETTINGS_TITLE_0), null));
073        }
074        setId(element.getClientId());
075        getListItemWidget().setIcon(element.getBigIconClasses());
076
077        m_removeButton = new CmsPushButton();
078        m_removeButton.setImageClass(I_CmsButton.CUT_SMALL);
079        m_removeButton.setButtonStyle(ButtonStyle.FONT_ICON, null);
080        m_removeButton.setTitle(Messages.get().key(Messages.GUI_BUTTON_REMOVE_TEXT_0));
081        m_removeButton.addClickHandler(new ClickHandler() {
082
083            /**
084             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
085             */
086            public void onClick(ClickEvent event) {
087
088                deleteElement();
089
090            }
091        });
092        m_editButton = new CmsPushButton();
093        m_editButton.setImageClass(I_CmsButton.ButtonData.EDIT.getSmallIconClass());
094        m_editButton.setButtonStyle(ButtonStyle.FONT_ICON, null);
095        m_editButton.setTitle(
096            org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_BUTTON_ELEMENT_EDIT_0));
097        m_editButton.setEnabled(false);
098        getListItemWidget().addButton(m_editButton);
099    }
100
101    /**
102     * Removes the element from it's parent widget.<p>
103     */
104    public void deleteElement() {
105
106        removeFromParent();
107    }
108
109    /**
110     * Disables the edit button with the given reason.<p>
111     *
112     * @param reason the disable reason
113     * @param locked <code>true</code> if the resource is locked
114     */
115    public void disableEdit(String reason, boolean locked) {
116
117        m_editButton.disable(reason);
118        if (locked) {
119            m_editButton.setImageClass("opencms-icon-lock-20");
120        }
121    }
122
123    /**
124     * Enables the edit button with the given click handler.<p>
125     *
126     * @param editClickHandler the edit click handler
127     */
128    public void enableEdit(ClickHandler editClickHandler) {
129
130        if (m_editHandlerRegistration != null) {
131            m_editHandlerRegistration.removeHandler();
132        }
133        m_editHandlerRegistration = m_editButton.addClickHandler(editClickHandler);
134        m_editButton.enable();
135    }
136
137    /**
138     * Hides the edit button.<p>
139     */
140    public void hideEditButton() {
141
142        if (m_editButton != null) {
143            getListItemWidget().removeButton(m_editButton);
144        }
145    }
146
147    /**
148     * Hides the element delete button.<p>
149     */
150    public void hideRemoveButton() {
151
152        getListItemWidget().removeButton(m_removeButton);
153    }
154
155    /**
156     * @see org.opencms.gwt.client.dnd.I_CmsDraggable#onDragCancel()
157     */
158    @Override
159    public void onDragCancel() {
160
161        super.onDragCancel();
162        clearDrag();
163    }
164
165    /**
166     * @see org.opencms.gwt.client.dnd.I_CmsDraggable#onDrop(org.opencms.gwt.client.dnd.I_CmsDropTarget)
167     */
168    @Override
169    public void onDrop(I_CmsDropTarget target) {
170
171        super.onDrop(target);
172        clearDrag();
173    }
174
175    /**
176     * @see org.opencms.gwt.client.dnd.I_CmsDraggable#onStartDrag(org.opencms.gwt.client.dnd.I_CmsDropTarget)
177     */
178    @Override
179    public void onStartDrag(I_CmsDropTarget target) {
180
181        super.onStartDrag(target);
182        getElement().getStyle().setOpacity(0.7);
183    }
184
185    /**
186     * Shows the element edit button.<p>
187     */
188    public void showEditButton() {
189
190        if (m_editButton != null) {
191            getListItemWidget().addButton(m_editButton);
192        }
193    }
194
195    /**
196     * Shows the element delete button.<p>
197     */
198    public void showRemoveButton() {
199
200        getListItemWidget().addButton(m_removeButton);
201    }
202
203    /**
204     * Removes all styling done during drag and drop.<p>
205     */
206    private void clearDrag() {
207
208        // using own implementation as GWT won't do it properly on IE7-8
209        CmsDomUtil.clearOpacity(getElement());
210
211        getElement().getStyle().clearDisplay();
212    }
213}