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.gwt.CmsCoreService;
031import org.opencms.main.CmsLog;
032import org.opencms.main.OpenCms;
033import org.opencms.ui.A_CmsUI;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.I_CmsDialogContext;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.ui.components.CmsOkCancelActionHandler;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.util.CmsUUID;
041
042import java.util.Collections;
043
044import org.apache.commons.logging.Log;
045
046import com.vaadin.v7.data.Property.ValueChangeEvent;
047import com.vaadin.v7.data.Property.ValueChangeListener;
048import com.vaadin.v7.data.util.IndexedContainer;
049import com.vaadin.server.Page;
050import com.vaadin.v7.shared.ui.combobox.FilteringMode;
051import com.vaadin.ui.Button;
052import com.vaadin.ui.Button.ClickEvent;
053import com.vaadin.ui.Button.ClickListener;
054import com.vaadin.v7.ui.ComboBox;
055import com.vaadin.ui.FormLayout;
056import com.vaadin.ui.UI;
057
058/**
059 * The site select dialog.<p>
060 */
061public class CmsSiteSelectDialog extends CmsBasicDialog {
062
063    /** Logger instance for this class. */
064    static final Log LOG = CmsLog.getLog(CmsSiteSelectDialog.class);
065
066    /** The project name property. */
067    private static final String CAPTION_PROPERTY = "caption";
068
069    /** The serial version id. */
070    private static final long serialVersionUID = 4455901453008760434L;
071
072    /** The cancel button. */
073    private Button m_cancelButton;
074
075    /** The dialog context. */
076    private I_CmsDialogContext m_context;
077
078    /** The site select. */
079    private ComboBox m_siteComboBox;
080
081    /**
082     * Constructor.<p>
083     *
084     * @param context the dialog context
085     */
086    public CmsSiteSelectDialog(I_CmsDialogContext context) {
087        m_context = context;
088        setContent(initForm());
089        m_cancelButton = createButtonCancel();
090        m_cancelButton.addClickListener(new ClickListener() {
091
092            private static final long serialVersionUID = 1L;
093
094            public void buttonClick(ClickEvent event) {
095
096                cancel();
097            }
098        });
099        addButton(m_cancelButton);
100
101        setActionHandler(new CmsOkCancelActionHandler() {
102
103            private static final long serialVersionUID = 1L;
104
105            @Override
106            protected void cancel() {
107
108                CmsSiteSelectDialog.this.cancel();
109            }
110
111            @Override
112            protected void ok() {
113
114                submit();
115            }
116        });
117    }
118
119    /**
120     * Cancels the dialog action.<p>
121     */
122    void cancel() {
123
124        m_context.finish(Collections.<CmsUUID> emptyList());
125    }
126
127    /**
128     * Submits the dialog action.<p>
129     */
130    void submit() {
131
132        String siteRoot = (String)m_siteComboBox.getValue();
133        if (!m_context.getCms().getRequestContext().getSiteRoot().equals(siteRoot)) {
134            A_CmsUI.get().changeSite(siteRoot);
135            if (CmsStringUtil.isEmptyOrWhitespaceOnly(siteRoot) || OpenCms.getSiteManager().isSharedFolder(siteRoot)) {
136                // switch to explorer view when selecting shared or root site
137
138                Page.getCurrent().open(CmsCoreService.getFileExplorerLink(A_CmsUI.getCmsObject(), siteRoot), "_top");
139                return;
140            }
141        } else {
142            siteRoot = null;
143        }
144        m_context.finish(null, siteRoot);
145    }
146
147    /**
148     * Initializes the form component.<p>
149     *
150     * @return the form component
151     */
152    private FormLayout initForm() {
153
154        FormLayout form = new FormLayout();
155        form.setWidth("100%");
156
157        IndexedContainer sites = CmsVaadinUtils.getAvailableSitesContainer(m_context.getCms(), CAPTION_PROPERTY);
158        m_siteComboBox = prepareComboBox(sites, org.opencms.workplace.Messages.GUI_LABEL_SITE_0);
159        m_siteComboBox.select(m_context.getCms().getRequestContext().getSiteRoot());
160        form.addComponent(m_siteComboBox);
161        ValueChangeListener changeListener = new ValueChangeListener() {
162
163            private static final long serialVersionUID = 1L;
164
165            public void valueChange(ValueChangeEvent event) {
166
167                submit();
168            }
169        };
170        m_siteComboBox.addValueChangeListener(changeListener);
171        return form;
172    }
173
174    /**
175     * Prepares a combo box.<p>
176     *
177     * @param container the indexed item container
178     * @param captionKey the caption message key
179     *
180     * @return the combo box
181     */
182    private ComboBox prepareComboBox(IndexedContainer container, String captionKey) {
183
184        ComboBox result = new ComboBox(CmsVaadinUtils.getWpMessagesForCurrentLocale().key(captionKey), container);
185        result.setTextInputAllowed(true);
186        result.setNullSelectionAllowed(false);
187        result.setWidth("100%");
188        result.setInputPrompt(
189            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_CLICK_TO_EDIT_0));
190        result.setItemCaptionPropertyId(CAPTION_PROPERTY);
191        result.setFilteringMode(FilteringMode.CONTAINS);
192        return result;
193    }
194}