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;
029
030import org.opencms.gwt.client.dnd.CmsDNDHandler;
031import org.opencms.gwt.client.dnd.CmsDNDHandler.Orientation;
032import org.opencms.gwt.client.dnd.I_CmsDNDController;
033import org.opencms.gwt.client.dnd.I_CmsDraggable;
034import org.opencms.gwt.client.dnd.I_CmsDropTarget;
035import org.opencms.gwt.client.ui.CmsList;
036import org.opencms.gwt.client.ui.CmsListItem;
037
038import com.google.gwt.core.client.Scheduler;
039import com.google.gwt.core.client.Scheduler.ScheduledCommand;
040
041/**
042 * The favorites editing drag and drop controller.<p>
043 *
044 * @since 8.0.0
045 */
046public class CmsFavoritesDNDController implements I_CmsDNDController {
047
048    /** The starting position. */
049    private int m_startPosition;
050
051    /** The sortable list. */
052    private CmsList<CmsListItem> m_list;
053
054    /**
055     * @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)
056     */
057    public void onAnimationStart(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
058
059        // nothing to do
060    }
061
062    /**
063     * @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)
064     */
065    public boolean onBeforeDrop(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
066
067        // nothing to do
068        return true;
069    }
070
071    /**
072     * @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)
073     */
074    public void onDragCancel(I_CmsDraggable draggable, I_CmsDropTarget target, final CmsDNDHandler handler) {
075
076        m_list.insertItem((CmsListItem)draggable, m_startPosition);
077        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
078
079            /**
080             * @see com.google.gwt.user.client.Command#execute()
081             */
082            public void execute() {
083
084                handler.clearTargets();
085            }
086        });
087
088    }
089
090    /**
091     * @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)
092     */
093    @SuppressWarnings("unchecked")
094    public boolean onDragStart(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
095
096        handler.setOrientation(Orientation.VERTICAL);
097        if ((target instanceof CmsList) && (draggable instanceof CmsListItem)) {
098            m_list = (CmsList<CmsListItem>)target;
099            m_startPosition = m_list.getItemPosition((CmsListItem)draggable);
100            handler.clearTargets();
101            handler.addTarget(target);
102            target.getElement().insertBefore(handler.getPlaceholder(), draggable.getElement());
103            return true;
104        }
105        return false;
106    }
107
108    /**
109     * @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)
110     */
111    @SuppressWarnings("unchecked")
112    public void onDrop(I_CmsDraggable draggable, I_CmsDropTarget target, final CmsDNDHandler handler) {
113
114        if (target instanceof CmsList) {
115            CmsList<CmsListItem> list = (CmsList<CmsListItem>)target;
116            if (list.getWidgetCount() > target.getPlaceholderIndex()) {
117                list.insertItem((CmsListItem)draggable, target.getPlaceholderIndex());
118            } else {
119                list.addItem((CmsListItem)draggable);
120            }
121        }
122        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
123
124            /**
125             * @see com.google.gwt.user.client.Command#execute()
126             */
127            public void execute() {
128
129                handler.clearTargets();
130            }
131        });
132
133    }
134
135    /**
136     * @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)
137     */
138    public void onPositionedPlaceholder(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
139
140        // nothing to do
141    }
142
143    /**
144     * @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)
145     */
146    public boolean onTargetEnter(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
147
148        return true;
149    }
150
151    /**
152     * @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)
153     */
154    public void onTargetLeave(I_CmsDraggable draggable, I_CmsDropTarget target, CmsDNDHandler handler) {
155
156        // nothing to do
157    }
158
159}