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.gwt.client.ui.input.category;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.Messages;
032import org.opencms.gwt.client.rpc.CmsRpcAction;
033import org.opencms.gwt.client.ui.CmsListItemWidget;
034import org.opencms.gwt.client.ui.CmsPopup;
035import org.opencms.gwt.client.ui.CmsPushButton;
036import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
037import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
038import org.opencms.gwt.shared.CmsListInfoBean.LockIcon;
039import org.opencms.gwt.shared.CmsResourceCategoryInfo;
040import org.opencms.util.CmsUUID;
041
042import java.util.List;
043
044import com.google.gwt.event.dom.client.ClickEvent;
045import com.google.gwt.event.dom.client.ClickHandler;
046import com.google.gwt.event.logical.shared.ValueChangeEvent;
047import com.google.gwt.event.logical.shared.ValueChangeHandler;
048import com.google.gwt.user.client.Command;
049import com.google.gwt.user.client.ui.FlowPanel;
050
051/**
052 * Dialog to display and change resource categories.<p>
053 */
054public class CmsCategoryDialog extends CmsPopup {
055
056    /** The category tree widget. */
057    CmsCategoryTree m_categoryTree;
058
059    /** The on save command. Called when categories have been changed. */
060    Command m_onSave;
061
062    /** The resource structure id. */
063    CmsUUID m_structureId;
064
065    /** The cancel button. */
066    private CmsPushButton m_cancelButton;
067
068    /** Flag, indicating if the category tree should be collapsed when the dialog opens. */
069    private boolean m_collapsed;
070
071    /** The is initialized flag. */
072    private boolean m_initialized;
073
074    /** The is initializing flag. */
075    private boolean m_initializing;
076
077    /** The main content panel. */
078    private FlowPanel m_main;
079
080    /** The save button. */
081    private CmsPushButton m_saveButton;
082
083    /**
084     * Constructor.<p>
085     *
086     * @param structureId the resource structure id
087     * @param onSave the on save command, called when categories have been changed
088     * @param collapsed flag, indicating if the categories tree should be displayed collapsed when the dialog is opened
089     */
090    public CmsCategoryDialog(CmsUUID structureId, Command onSave, boolean collapsed) {
091
092        super(Messages.get().key(Messages.GUI_DIALOG_CATEGORIES_TITLE_0), WIDE_WIDTH);
093        m_collapsed = collapsed;
094        m_structureId = structureId;
095        m_onSave = onSave;
096        setGlassEnabled(true);
097        catchNotifications();
098        addDialogClose(null);
099        m_main = new FlowPanel();
100        setMainContent(m_main);
101        m_cancelButton = new CmsPushButton();
102        m_cancelButton.setText(Messages.get().key(Messages.GUI_CANCEL_0));
103        m_cancelButton.setUseMinWidth(true);
104        m_cancelButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
105        m_cancelButton.addClickHandler(new ClickHandler() {
106
107            /**
108             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
109             */
110            public void onClick(ClickEvent event) {
111
112                hide();
113            }
114        });
115        addButton(m_cancelButton);
116        m_saveButton = new CmsPushButton();
117        m_saveButton.setText(Messages.get().key(Messages.GUI_SAVE_0));
118        m_saveButton.setUseMinWidth(true);
119        m_saveButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
120        m_saveButton.addClickHandler(new ClickHandler() {
121
122            /**
123             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
124             */
125            public void onClick(ClickEvent event) {
126
127                saveCategories();
128            }
129        });
130        addButton(m_saveButton);
131        m_saveButton.disable(Messages.get().key(Messages.GUI_LOADING_0));
132    }
133
134    /**
135     * Initializes the dialog content.<p>
136     *
137     * @param categoryInfo the resource category info
138     */
139    public void initialize(CmsResourceCategoryInfo categoryInfo) {
140
141        m_initializing = false;
142        m_initialized = true;
143        m_main.add(new CmsListItemWidget(categoryInfo.getResourceInfo()));
144        m_categoryTree = new CmsCategoryTree(
145            categoryInfo.getCurrentCategories(),
146            300,
147            false,
148            categoryInfo.getCategoryTree(),
149            m_collapsed);
150        m_categoryTree.addValueChangeHandler(new ValueChangeHandler<List<String>>() {
151
152            public void onValueChange(ValueChangeEvent<List<String>> event) {
153
154                setChanged();
155            }
156        });
157        m_main.add(m_categoryTree);
158        m_categoryTree.truncate("CATEGORIES", WIDE_WIDTH - 20);
159        LockIcon lock = categoryInfo.getResourceInfo().getLockIcon();
160        if ((lock == null)
161            || lock.equals(LockIcon.NONE)
162            || lock.equals(LockIcon.OPEN)
163            || lock.equals(LockIcon.SHARED_OPEN)) {
164            m_saveButton.disable(Messages.get().key(Messages.GUI_NOTHING_CHANGED_0));
165        } else {
166            m_categoryTree.disable(Messages.get().key(Messages.GUI_RESOURCE_LOCKED_0));
167            m_saveButton.disable(Messages.get().key(Messages.GUI_RESOURCE_LOCKED_0));
168        }
169        setWidth(WIDE_WIDTH);
170        if (isShowing()) {
171            center();
172        }
173    }
174
175    /**
176     * @see org.opencms.gwt.client.ui.CmsPopup#show()
177     */
178    @Override
179    public void show() {
180
181        super.show();
182        if (!m_initialized && !m_initializing) {
183            m_initializing = true;
184            CmsRpcAction<CmsResourceCategoryInfo> action = new CmsRpcAction<CmsResourceCategoryInfo>() {
185
186                @Override
187                public void execute() {
188
189                    setHeight(450);
190                    start(0, true);
191                    CmsCoreProvider.getService().getCategoryInfo(m_structureId, this);
192                }
193
194                @Override
195                protected void onResponse(CmsResourceCategoryInfo result) {
196
197                    stop(false);
198                    setHeight(-1);
199                    initialize(result);
200                }
201            };
202            action.execute();
203        }
204    }
205
206    /**
207     * Saves the selected categories and hides the dialog.<p>
208     */
209    protected void saveCategories() {
210
211        final List<String> categories = m_categoryTree.getAllSelected();
212        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
213
214            @Override
215            public void execute() {
216
217                start(0, true);
218                CmsCoreProvider.getService().setResourceCategories(m_structureId, categories, this);
219            }
220
221            @Override
222            protected void onResponse(Void result) {
223
224                stop(false);
225                hide();
226                if (m_onSave != null) {
227                    m_onSave.execute();
228                }
229            }
230        };
231        action.execute();
232    }
233
234    /**
235     * Sets the dialog state to changed, enabling the save button.<p>
236     */
237    void setChanged() {
238
239        m_saveButton.enable();
240    }
241}