001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.acacia.client;
029
030import org.opencms.acacia.client.css.I_CmsLayoutBundle;
031import org.opencms.acacia.client.ui.CmsAttributeValueView;
032import org.opencms.acacia.client.ui.CmsValuePanel;
033import org.opencms.gwt.client.dnd.CmsDNDHandler;
034import org.opencms.gwt.client.dnd.CmsDNDHandler.Orientation;
035import org.opencms.gwt.client.dnd.I_CmsDNDController;
036import org.opencms.gwt.client.dnd.I_CmsDraggable;
037import org.opencms.gwt.client.dnd.I_CmsDropTarget;
038
039import com.google.gwt.core.client.Scheduler;
040import com.google.gwt.core.client.Scheduler.ScheduledCommand;
041import com.google.gwt.dom.client.Document;
042import com.google.gwt.dom.client.Element;
043import com.google.gwt.dom.client.Style.Position;
044import com.google.gwt.user.client.DOM;
045
046/**
047 * The drag and drop controller for attribute value sorting.<p>
048 */
049public class CmsAttributeDNDController implements I_CmsDNDController {
050
051    /** The drag overlay. */
052    private Element m_dragOverlay;
053
054    /** The starting position. */
055    private int m_startPosition;
056
057    /**
058     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onAnimationStart(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
059     */
060    public void onAnimationStart(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
061
062        // nothing to do
063    }
064
065    /**
066     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onBeforeDrop(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
067     */
068    public boolean onBeforeDrop(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
069
070        // nothing to do
071        return true;
072    }
073
074    /**
075     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onDragCancel(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
076     */
077    public void onDragCancel(I_CmsDraggable draggable, I_CmsDropTarget target, final CmsDNDHandler handler) {
078
079        removeDragOverlay();
080        clearTargets(handler);
081        // remove the drag helper reference from handler, to avoid helper.removeFromParent() call
082        handler.setDragHelper(null);
083    }
084
085    /**
086     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onDragStart(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
087     */
088    public boolean onDragStart(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
089
090        if ((target instanceof CmsValuePanel)
091            && (draggable instanceof CmsAttributeValueView)
092            && ((CmsAttributeValueView)draggable).isDragEnabled()) {
093            installDragOverlay();
094            handler.setOrientation(Orientation.VERTICAL);
095            m_startPosition = ((CmsAttributeValueView)draggable).getValueIndex();
096            handler.clearTargets();
097            handler.addTarget(target);
098            target.getElement().getStyle().setPosition(Position.RELATIVE);
099            target.getElement().insertBefore(handler.getPlaceholder(), draggable.getElement());
100            ((CmsValuePanel)target).highlightOutline();
101            return true;
102        }
103        return false;
104    }
105
106    /**
107     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onDrop(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
108     */
109    public void onDrop(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
110
111        CmsAttributeValueView attributeValue = (CmsAttributeValueView)draggable;
112        int targetIndex = target.getPlaceholderIndex();
113        if (targetIndex > m_startPosition) {
114            targetIndex--;
115        }
116        attributeValue.getHandler().moveAttributeValue(attributeValue, m_startPosition, targetIndex);
117        removeDragOverlay();
118        clearTargets(handler);
119        // remove the drag helper reference from handler, to avoid helper.removeFromParent() call
120        handler.setDragHelper(null);
121    }
122
123    /**
124     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onPositionedPlaceholder(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
125     */
126    public void onPositionedPlaceholder(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
127
128        ((CmsValuePanel)target).updateHighlightingPosition();
129    }
130
131    /**
132     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onTargetEnter(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
133     */
134    public boolean onTargetEnter(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
135
136        return true;
137    }
138
139    /**
140     * @see org.opencms.gwt.client.dnd.I_CmsDNDController#onTargetLeave(org.opencms.gwt.client.dnd.I_CmsDraggable, org.opencms.gwt.client.dnd.I_CmsDropTarget, org.opencms.gwt.client.dnd.CmsDNDHandler)
141     */
142    public void onTargetLeave(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
143
144        // nothing to do
145    }
146
147    /**
148     * Clears the handlers drag and drop targets.<p>
149     *
150     * @param handler the drag and drop handler
151     */
152    private void clearTargets(final CmsDNDHandler handler) {
153
154        if (handler.getCurrentTarget() != null) {
155            ((CmsValuePanel)handler.getCurrentTarget()).removeHighlighting();
156            handler.getCurrentTarget().getElement().getStyle().clearPosition();
157        }
158        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
159
160            /**
161             * @see com.google.gwt.user.client.Command#execute()
162             */
163            public void execute() {
164
165                handler.clearTargets();
166            }
167        });
168    }
169
170    /**
171     * Installs the drag overlay to avoid any mouse over issues or similar.<p>
172     */
173    private void installDragOverlay() {
174
175        if (m_dragOverlay != null) {
176            m_dragOverlay.removeFromParent();
177        }
178        m_dragOverlay = DOM.createDiv();
179        m_dragOverlay.addClassName(I_CmsLayoutBundle.INSTANCE.form().dragOverlay());
180        Document.get().getBody().appendChild(m_dragOverlay);
181    }
182
183    /**
184     * Removes the drag overlay.<p>
185     */
186    private void removeDragOverlay() {
187
188        if (m_dragOverlay != null) {
189            m_dragOverlay.removeFromParent();
190            m_dragOverlay = null;
191        }
192    }
193
194}