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.dialogs;
029
030import org.opencms.file.CmsProject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.A_CmsUI;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.ui.I_CmsDialogContext;
037import org.opencms.ui.apps.CmsFileExplorerConfiguration;
038import org.opencms.ui.apps.CmsPageEditorConfiguration;
039import org.opencms.ui.apps.I_CmsHasAppLaunchCommand;
040import org.opencms.ui.apps.I_CmsWorkplaceAppConfiguration;
041import org.opencms.ui.apps.Messages;
042import org.opencms.ui.components.CmsBasicDialog;
043import org.opencms.ui.components.CmsOkCancelActionHandler;
044import org.opencms.util.CmsUUID;
045
046import java.util.Collections;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.v7.data.Property.ValueChangeEvent;
051import com.vaadin.v7.data.Property.ValueChangeListener;
052import com.vaadin.v7.data.util.IndexedContainer;
053import com.vaadin.v7.shared.ui.combobox.FilteringMode;
054import com.vaadin.ui.Button;
055import com.vaadin.ui.Button.ClickEvent;
056import com.vaadin.ui.Button.ClickListener;
057import com.vaadin.v7.ui.ComboBox;
058import com.vaadin.ui.FormLayout;
059import com.vaadin.ui.UI;
060
061/**
062 * The project select dialog.<p>
063 */
064public class CmsProjectSelectDialog extends CmsBasicDialog {
065
066    /** Logger instance for this class. */
067    static final Log LOG = CmsLog.getLog(CmsProjectSelectDialog.class);
068
069    /** The project name property. */
070    private static final String CAPTION_PROPERTY = "caption";
071
072    /** The serial version id. */
073    private static final long serialVersionUID = 4455901453008760434L;
074
075    /** The cancel button. */
076    private Button m_cancelButton;
077
078    /** The dialog context. */
079    private I_CmsDialogContext m_context;
080
081    /** The project select. */
082    private ComboBox m_projectComboBox;
083
084    /** The site select. */
085    private ComboBox m_siteComboBox;
086
087    /**
088     * Constructor.<p>
089     *
090     * @param context the dialog context
091     */
092    public CmsProjectSelectDialog(I_CmsDialogContext context) {
093        m_context = context;
094        setContent(initForm());
095        m_cancelButton = createButtonCancel();
096        m_cancelButton.addClickListener(new ClickListener() {
097
098            private static final long serialVersionUID = 1L;
099
100            public void buttonClick(ClickEvent event) {
101
102                cancel();
103            }
104        });
105        addButton(m_cancelButton);
106
107        setActionHandler(new CmsOkCancelActionHandler() {
108
109            private static final long serialVersionUID = 1L;
110
111            @Override
112            protected void cancel() {
113
114                CmsProjectSelectDialog.this.cancel();
115            }
116
117            @Override
118            protected void ok() {
119
120                submit();
121            }
122        });
123    }
124
125    /**
126     * Cancels the dialog action.<p>
127     */
128    void cancel() {
129
130        m_context.finish(Collections.<CmsUUID> emptyList());
131    }
132
133    /**
134     * Submits the dialog action.<p>
135     */
136    void submit() {
137
138        try {
139            CmsProject project = m_context.getCms().readProject((CmsUUID)m_projectComboBox.getValue());
140            if (!m_context.getCms().getRequestContext().getCurrentProject().equals(project)) {
141                A_CmsUI.get().changeProject(project);
142            } else {
143                project = null;
144            }
145            String siteRoot = (String)m_siteComboBox.getValue();
146            if (!m_context.getCms().getRequestContext().getSiteRoot().equals(siteRoot)) {
147                A_CmsUI.get().changeSite(siteRoot);
148            } else {
149                siteRoot = null;
150            }
151            if ((siteRoot != null) && CmsFileExplorerConfiguration.APP_ID.equals(m_context.getAppId())) {
152                I_CmsWorkplaceAppConfiguration editorConf = OpenCms.getWorkplaceAppManager().getAppConfiguration(
153                    CmsPageEditorConfiguration.APP_ID);
154                if (editorConf.getVisibility(m_context.getCms()).isActive()) {
155                    ((I_CmsHasAppLaunchCommand)editorConf).getAppLaunchCommand().run();
156                    return;
157                }
158            }
159            m_context.finish(project, siteRoot);
160        } catch (CmsException e) {
161            m_context.error(e);
162        }
163    }
164
165    /**
166     * Initializes the form component.<p>
167     *
168     * @return the form component
169     */
170    private FormLayout initForm() {
171
172        FormLayout form = new FormLayout();
173        form.setWidth("100%");
174
175        IndexedContainer sites = CmsVaadinUtils.getAvailableSitesContainer(m_context.getCms(), CAPTION_PROPERTY);
176        m_siteComboBox = prepareComboBox(sites, org.opencms.workplace.Messages.GUI_LABEL_SITE_0);
177        m_siteComboBox.select(m_context.getCms().getRequestContext().getSiteRoot());
178        form.addComponent(m_siteComboBox);
179        ValueChangeListener changeListener = new ValueChangeListener() {
180
181            private static final long serialVersionUID = 1L;
182
183            public void valueChange(ValueChangeEvent event) {
184
185                submit();
186            }
187        };
188        m_siteComboBox.addValueChangeListener(changeListener);
189        IndexedContainer projects = CmsVaadinUtils.getProjectsContainer(m_context.getCms(), CAPTION_PROPERTY);
190        m_projectComboBox = prepareComboBox(projects, org.opencms.workplace.Messages.GUI_LABEL_PROJECT_0);
191        CmsUUID currentProjectId = m_context.getCms().getRequestContext().getCurrentProject().getUuid();
192        if (projects.containsId(currentProjectId)) {
193            m_projectComboBox.select(currentProjectId);
194        } else {
195            try {
196                CmsUUID ouProject = OpenCms.getOrgUnitManager().readOrganizationalUnit(
197                    m_context.getCms(),
198                    m_context.getCms().getRequestContext().getOuFqn()).getProjectId();
199                if (projects.containsId(ouProject)) {
200                    m_projectComboBox.select(ouProject);
201                }
202            } catch (CmsException e) {
203                LOG.error("Error while reading current OU.", e);
204            }
205        }
206
207        form.addComponent(m_projectComboBox);
208        m_projectComboBox.addValueChangeListener(changeListener);
209        return form;
210    }
211
212    /**
213     * Prepares a combo box.<p>
214     *
215     * @param container the indexed item container
216     * @param captionKey the caption message key
217     *
218     * @return the combo box
219     */
220    private ComboBox prepareComboBox(IndexedContainer container, String captionKey) {
221
222        ComboBox result = new ComboBox(CmsVaadinUtils.getWpMessagesForCurrentLocale().key(captionKey), container);
223        result.setTextInputAllowed(true);
224        result.setNullSelectionAllowed(false);
225        result.setWidth("100%");
226        result.setInputPrompt(
227            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_CLICK_TO_EDIT_0));
228        result.setItemCaptionPropertyId(CAPTION_PROPERTY);
229        result.setFilteringMode(FilteringMode.CONTAINS);
230        return result;
231    }
232}