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.groupeditor;
029
030import org.opencms.ade.containerpage.client.CmsContainerpageController;
031import org.opencms.ade.containerpage.client.CmsContainerpageHandler;
032import org.opencms.ade.containerpage.client.Messages;
033import org.opencms.ade.containerpage.client.ui.CmsContainerPageContainer;
034import org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel;
035import org.opencms.ade.containerpage.client.ui.CmsGroupContainerElementPanel;
036import org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle;
037import org.opencms.ade.containerpage.shared.CmsContainerElement;
038import org.opencms.ade.containerpage.shared.CmsContainerElementData;
039import org.opencms.ade.containerpage.shared.CmsGroupContainer;
040import org.opencms.gwt.client.ui.CmsPushButton;
041import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
042import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
043import org.opencms.gwt.client.ui.input.CmsTextBox;
044import org.opencms.gwt.client.util.CmsDebugLog;
045import org.opencms.gwt.client.util.I_CmsSimpleCallback;
046import org.opencms.util.CmsUUID;
047
048import java.util.HashSet;
049import java.util.List;
050import java.util.Map;
051import java.util.Set;
052
053import com.google.gwt.dom.client.Style.Float;
054import com.google.gwt.dom.client.Style.Unit;
055import com.google.gwt.event.dom.client.ClickEvent;
056import com.google.gwt.event.dom.client.ClickHandler;
057import com.google.gwt.user.client.ui.RootPanel;
058import com.google.gwt.user.client.ui.Widget;
059
060/**
061 * The group-container editor.<p>
062 *
063 * @since 8.0.0
064 */
065public final class CmsGroupContainerEditor extends A_CmsGroupEditor {
066
067    /** The editor instance. */
068    private static CmsGroupContainerEditor INSTANCE;
069
070    /** The button to break up the container. */
071    private CmsPushButton m_breakUpButton;
072
073    /** The group container bean. */
074    private CmsGroupContainer m_groupContainerBean;
075
076    /** The description input. */
077    private CmsTextBox m_inputDescription;
078
079    /** The title input. */
080    private CmsTextBox m_inputTitle;
081
082    /**
083     * Constructor.<p>
084     *
085     * @param groupContainer the group container widget
086     * @param controller the container page controller
087     * @param handler the container page handler
088     */
089    private CmsGroupContainerEditor(
090        CmsGroupContainerElementPanel groupContainer,
091        CmsContainerpageController controller,
092        CmsContainerpageHandler handler) {
093
094        super(groupContainer, controller, handler);
095        // Loading data of all contained elements including group-container element
096        getController().getElements(getElementIds(), new I_CmsSimpleCallback<Map<String, CmsContainerElementData>>() {
097
098            public void execute(Map<String, CmsContainerElementData> arg) {
099
100                setGroupContainerData(arg);
101            }
102        });
103    }
104
105    /**
106     * Gets the editor instance.<p>
107     *
108     * @return the editor instance
109     */
110    public static CmsGroupContainerEditor getInstance() {
111
112        return INSTANCE;
113    }
114
115    /**
116     * Returns true if the editor is active.<p>
117     *
118     * @return true if the editor is active
119     */
120    public static boolean isActive() {
121
122        return INSTANCE != null;
123    }
124
125    /**
126     * Opens the group container editor.<p>
127     *
128     * @param groupContainer the group container
129     * @param controller the container page controller
130     * @param handler the container page handler
131     *
132     * @return the editor instance
133     */
134    public static CmsGroupContainerEditor openGroupcontainerEditor(
135        CmsGroupContainerElementPanel groupContainer,
136        CmsContainerpageController controller,
137        CmsContainerpageHandler handler) {
138
139        // making sure only a single instance of the group-container editor is open
140        if (INSTANCE != null) {
141            CmsDebugLog.getInstance().printLine("group-container editor already open");
142        } else {
143            CmsGroupContainerEditor editor = new CmsGroupContainerEditor(groupContainer, controller, handler);
144            RootPanel.get().add(editor);
145            editor.openDialog(Messages.get().key(Messages.GUI_GROUPCONTAINER_CAPTION_0));
146            editor.getGroupContainerWidget().refreshHighlighting();
147            INSTANCE = editor;
148        }
149        return INSTANCE;
150    }
151
152    /**
153     * Clears the instance reference.<p>
154     */
155    private static void clear() {
156
157        INSTANCE = null;
158    }
159
160    /**
161     * @see org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor#reinitializeButtons()
162     */
163    @Override
164    public void reinitializeButtons() {
165
166        for (Widget widget : getGroupContainerWidget()) {
167            if (widget instanceof CmsContainerPageElementPanel) {
168                CmsContainerPageElementPanel elemWidget = (CmsContainerPageElementPanel)widget;
169                if (getController().requiresOptionBar(elemWidget, elemWidget.getParentTarget())) {
170                    getController().getContainerpageUtil().addOptionBar(elemWidget);
171                } else {
172                    // otherwise remove any present option bar
173                    elemWidget.setElementOptionBar(null);
174                }
175            }
176        }
177    }
178
179    /**
180     * @see org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor#addButtons()
181     */
182    @Override
183    protected void addButtons() {
184
185        addCancelButton();
186        addSaveButton();
187        m_breakUpButton = new CmsPushButton();
188        m_breakUpButton.setText(Messages.get().key(Messages.GUI_BUTTON_BREAK_UP_TEXT_0));
189        m_breakUpButton.setUseMinWidth(true);
190        m_breakUpButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
191        m_breakUpButton.addClickHandler(new ClickHandler() {
192
193            public void onClick(ClickEvent event) {
194
195                breakUpContainer();
196            }
197        });
198        m_breakUpButton.getElement().getStyle().setFloat(Float.LEFT);
199        m_breakUpButton.getElement().getStyle().setMarginLeft(0, Unit.PX);
200        addButton(m_breakUpButton);
201
202    }
203
204    /**
205     * @see org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor#addInputFields()
206     */
207    @Override
208    protected void addInputFields() {
209
210        m_inputTitle = new CmsTextBox();
211        addInputField(Messages.get().key(Messages.GUI_GROUPCONTAINER_LABEL_TITLE_0), m_inputTitle);
212        m_inputDescription = new CmsTextBox();
213        addInputField(Messages.get().key(Messages.GUI_GROUPCONTAINER_LABEL_DESCRIPTION_0), m_inputDescription);
214    }
215
216    /**
217     * Breaks up the group container inserting it's elements into the parent container instead.<p>
218     */
219    protected void breakUpContainer() {
220
221        final List<CmsContainerElement> elements = getElements();
222        if (elements.isEmpty()) {
223            closeDialog(true);
224            getController().setPageChanged();
225            return;
226        }
227        Set<String> elementIds = new HashSet<String>();
228        for (CmsContainerElement element : elements) {
229            elementIds.add(element.getClientId());
230        }
231        I_CmsSimpleCallback<Map<String, CmsContainerElementData>> callback = new I_CmsSimpleCallback<Map<String, CmsContainerElementData>>() {
232
233            public void execute(Map<String, CmsContainerElementData> elementsData) {
234
235                breakUpContainer(elements, elementsData);
236            }
237        };
238        getController().getElements(elementIds, callback);
239    }
240
241    /**
242     * Breaks up the group container inserting the given elements into the parent container instead.<p>
243     *
244     * @param elements the group container elements
245     * @param elementsData the elements data
246     */
247    protected void breakUpContainer(
248        List<CmsContainerElement> elements,
249        Map<String, CmsContainerElementData> elementsData) {
250
251        int index = getIndexPosition();
252        for (CmsContainerElement element : elements) {
253            try {
254                CmsContainerPageElementPanel containerElement = getController().getContainerpageUtil().createElement(
255                    elementsData.get(element.getClientId()),
256                    getParentContainer(),
257                    false);
258                getParentContainer().insert(containerElement, index);
259                index++;
260            } catch (Exception e) {
261                CmsDebugLog.getInstance().printLine(e.getMessage());
262            }
263        }
264        getController().addToRecentList(m_groupContainerBean.getClientId(), null);
265        closeDialog(true);
266        getController().unlockResource(
267            new CmsUUID(CmsContainerpageController.getServerId(m_groupContainerBean.getClientId())));
268        getController().setPageChanged();
269    }
270
271    /**
272     * @see org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor#cancelEdit()
273     */
274    @Override
275    protected void cancelEdit() {
276
277        removeAllChildren();
278        for (CmsContainerPageElementPanel element : getBackUpElements()) {
279            getGroupContainerWidget().add(element);
280        }
281        if (getBackUpElements().size() == 0) {
282            getGroupContainerWidget().addStyleName(I_CmsLayoutBundle.INSTANCE.containerpageCss().emptyGroupContainer());
283        }
284        getController().unlockResource(
285            new CmsUUID(CmsContainerpageController.getServerId(m_groupContainerBean.getClientId())));
286        closeDialog(false);
287    }
288
289    /**
290     * @see org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor#clearInstance()
291     */
292    @Override
293    protected void clearInstance() {
294
295        clear();
296    }
297
298    /**
299     * @see org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor#saveEdit()
300     */
301    @Override
302    protected void saveEdit() {
303
304        m_groupContainerBean.setTitle(m_inputTitle.getFormValueAsString());
305        m_groupContainerBean.setDescription(m_inputDescription.getFormValueAsString());
306        m_groupContainerBean.setElements(getElements());
307        getController().saveGroupcontainer(m_groupContainerBean, getGroupContainerWidget());
308        closeDialog(false);
309    }
310
311    /**
312     * Sets the data of the group-container to edit.<p>
313     *
314     * @param elementsData the data of all contained elements and the group-container itself
315     */
316    protected void setGroupContainerData(Map<String, CmsContainerElementData> elementsData) {
317
318        m_elementData = elementsData.get(getGroupContainerWidget().getId());
319        if (m_elementData != null) {
320            setSaveEnabled(true, null);
321            m_groupContainerBean = new CmsGroupContainer();
322            m_groupContainerBean.setClientId(m_elementData.getClientId());
323            m_groupContainerBean.setResourceType(getGroupContainerWidget().getNewType());
324            m_groupContainerBean.setNew(getGroupContainerWidget().isNew());
325            m_groupContainerBean.setSitePath(m_elementData.getSitePath());
326            if (m_elementData.getTypes().isEmpty()) {
327                Set<String> types = new HashSet<String>();
328                types.add(((CmsContainerPageContainer)getGroupContainerWidget().getParentTarget()).getContainerType());
329                m_elementData.setTypes(types);
330                m_groupContainerBean.setTypes(types);
331            } else {
332                m_groupContainerBean.setTypes(m_elementData.getTypes());
333            }
334            m_inputDescription.setFormValueAsString(m_elementData.getDescription());
335            m_inputTitle.setFormValueAsString(m_elementData.getTitle());
336            m_groupContainerBean.setTitle(m_elementData.getTitle());
337            m_groupContainerBean.setDescription(m_elementData.getDescription());
338        } else {
339            CmsDebugLog.getInstance().printLine("Loading groupcontainer error.");
340        }
341    }
342}