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.ui.apps;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.OpenCms;
032import org.opencms.ui.A_CmsUI;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.components.CmsErrorDialog;
035import org.opencms.ui.components.OpenCmsTheme;
036
037import java.util.ArrayList;
038import java.util.Collection;
039import java.util.List;
040import java.util.Locale;
041
042import com.vaadin.ui.Button;
043import com.vaadin.ui.Button.ClickEvent;
044import com.vaadin.ui.Button.ClickListener;
045import com.vaadin.ui.Component;
046import com.vaadin.ui.CssLayout;
047import com.vaadin.ui.UI;
048import com.vaadin.ui.VerticalLayout;
049import com.vaadin.ui.dnd.DragSourceExtension;
050import com.vaadin.ui.dnd.DropTargetExtension;
051import com.vaadin.ui.dnd.event.DropEvent;
052import com.vaadin.ui.dnd.event.DropListener;
053import com.vaadin.ui.themes.ValoTheme;
054
055/**
056 * App to edit the quick launch menu.<p>
057 */
058public class CmsQuickLaunchEditor extends VerticalLayout {
059
060    /**
061     * The sorting drop listener.<p>
062     */
063    protected class LayoutDropListener implements DropListener<CssLayout> {
064
065        /** The item height. */
066        private static final int ITEM_HEIGHT = 88;
067
068        /** The item width. */
069        private static final int ITEM_WIDTH = 176;
070
071        /** The layout width. */
072        private static final int LAYOUT_WIDTH = 1158;
073
074        /** The serial version id. */
075        private static final long serialVersionUID = 8420945711551716630L;
076
077        /** The drag target ordered layout. */
078        private CssLayout m_layout;
079
080        /**
081         * Constructor.<p>
082         *
083         * @param layout the drop target layout
084         */
085        protected LayoutDropListener(CssLayout layout) {
086
087            m_layout = layout;
088        }
089
090        /**
091         * @see com.vaadin.ui.dnd.event.DropListener#drop(com.vaadin.ui.dnd.event.DropEvent)
092         */
093        public void drop(DropEvent<CssLayout> event) {
094
095            // depending on the browser window width, different margins and paddings apply
096            int layoutWidth = LAYOUT_WIDTH;
097            int windowWidth = UI.getCurrent().getPage().getBrowserWindowWidth();
098            if (windowWidth <= 983) {
099                layoutWidth = windowWidth - 22;
100            } else if (windowWidth <= 1220) {
101                layoutWidth = windowWidth - 62;
102            }
103            int top = event.getMouseEventDetails().getRelativeY();
104            int left = event.getMouseEventDetails().getRelativeX();
105            int columnCount = layoutWidth / ITEM_WIDTH;
106            int column = left / ITEM_WIDTH;
107            int row = top / ITEM_HEIGHT;
108            int index = (row * columnCount) + column;
109            if (((column * ITEM_WIDTH) + (ITEM_WIDTH / 2)) < left) {
110                index++;
111            }
112            Component sourceComponent = event.getDragSourceComponent().get();
113            int currentIndex = m_layout.getComponentIndex(sourceComponent);
114            if ((currentIndex != -1) && (currentIndex < index)) {
115                index--;
116            }
117
118            if (currentIndex == index) {
119                return;
120            }
121
122            // move component within the layout
123            m_layout.removeComponent(sourceComponent);
124            // avoid index out of bounds exceptions
125            if (m_layout.getComponentCount() < index) {
126                index = m_layout.getComponentCount();
127            }
128            m_layout.addComponent(sourceComponent, index);
129        }
130    }
131
132    /** The serial version id. */
133    private static final long serialVersionUID = -6608352673763873030L;
134
135    /** The available apps drop target wrapper. */
136    private CssLayout m_availableApps;
137
138    /** The cancel button. */
139    private Button m_reset;
140
141    /** The standard apps layout. */
142    private CssLayout m_standardApps;
143
144    /** The user apps drop target wrapper. */
145    private CssLayout m_userApps;
146
147    /**
148     * Constructor.<p>
149     */
150    public CmsQuickLaunchEditor() {
151
152        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
153        DropTargetExtension<CssLayout> userDrop = new DropTargetExtension<>(m_userApps);
154        userDrop.addDropListener(new LayoutDropListener(m_userApps));
155        m_userApps.addStyleName(ValoTheme.LAYOUT_HORIZONTAL_WRAPPING);
156        DropTargetExtension<CssLayout> availablesDrop = new DropTargetExtension<>(m_availableApps);
157        availablesDrop.addDropListener(new LayoutDropListener(m_availableApps));
158        m_reset.addClickListener(new ClickListener() {
159
160            private static final long serialVersionUID = 1L;
161
162            public void buttonClick(ClickEvent event) {
163
164                resetAppIcons();
165            }
166        });
167        addStyleName(OpenCmsTheme.QUICK_LAUNCH_EDITOR);
168    }
169
170    /**
171     * Initializes the app icon items.<p>
172     */
173    protected void resetAppIcons() {
174
175        CmsObject cms = A_CmsUI.getCmsObject();
176        Locale locale = UI.getCurrent().getLocale();
177        m_standardApps.removeAllComponents();
178        m_userApps.removeAllComponents();
179        m_availableApps.removeAllComponents();
180        Collection<I_CmsWorkplaceAppConfiguration> allApps = OpenCms.getWorkplaceAppManager().getWorkplaceApps();
181        Collection<I_CmsWorkplaceAppConfiguration> standardApps = OpenCms.getWorkplaceAppManager().getDefaultQuickLaunchConfigurations();
182        Collection<I_CmsWorkplaceAppConfiguration> userApps = OpenCms.getWorkplaceAppManager().getUserQuickLauchConfigurations(
183            cms);
184        for (I_CmsWorkplaceAppConfiguration config : standardApps) {
185            CmsAppVisibilityStatus visibility = config.getVisibility(cms);
186            if (visibility.isVisible()) {
187                Button button = CmsDefaultAppButtonProvider.createAppIconButton(config, locale);
188                m_standardApps.addComponent(button);
189            }
190        }
191        for (I_CmsWorkplaceAppConfiguration config : userApps) {
192            CmsAppVisibilityStatus visibility = config.getVisibility(cms);
193            if (visibility.isVisible() && visibility.isActive()) {
194                Button button = CmsDefaultAppButtonProvider.createAppIconButton(config, locale);
195                //    button.setWidth("166px");
196                DragSourceExtension<Button> extButton = new DragSourceExtension<>(button);
197                button.setData(config.getId());
198                extButton.setDataTransferText(config.getId());
199                m_userApps.addComponent(button);
200            }
201        }
202        for (I_CmsWorkplaceAppConfiguration config : allApps) {
203            CmsAppVisibilityStatus visibility = config.getVisibility(cms);
204            if (!standardApps.contains(config)
205                && !userApps.contains(config)
206                && visibility.isVisible()
207                && visibility.isActive()) {
208                Button button = CmsDefaultAppButtonProvider.createAppIconButton(config, locale);
209                //  button.setWidth("166px");
210                DragSourceExtension<Button> extButton = new DragSourceExtension<>(button);
211                button.setData(config.getId());
212                extButton.setDataTransferText(config.getId());
213                m_availableApps.addComponent(button);
214            }
215        }
216    }
217
218    /**
219     * Saves the changed apps setting.<p>
220     */
221    void saveToUser() {
222
223        List<String> apps = new ArrayList<String>();
224        int count = m_userApps.getComponentCount();
225        for (int i = 0; i < count; i++) {
226            Button button = (Button)m_userApps.getComponent(i);
227            apps.add((String)button.getData());
228        }
229
230        try {
231            OpenCms.getWorkplaceAppManager().setUserQuickLaunchApps(A_CmsUI.getCmsObject(), apps);
232        } catch (Exception e) {
233            CmsErrorDialog.showErrorDialog("Could not write user Quicklaunch apps", e);
234        }
235    }
236}