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;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.file.CmsResource;
033import org.opencms.ui.apps.CmsAppWorkplaceUi;
034import org.opencms.ui.components.CmsBasicDialog;
035import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
036import org.opencms.ui.components.CmsErrorDialog;
037import org.opencms.util.CmsUUID;
038
039import java.util.Collection;
040import java.util.Collections;
041import java.util.List;
042
043import com.vaadin.ui.Component;
044import com.vaadin.ui.Window;
045
046/**
047 * Abstract dialog context.<p>
048 */
049public abstract class A_CmsDialogContext implements I_CmsDialogContext {
050
051    /** The window used to display the dialog. */
052    protected Window m_window;
053
054    /** The app id. */
055    private String m_appId;
056
057    /** The context type. */
058    private ContextType m_contextType;
059
060    /** The list of resources. */
061    private List<CmsResource> m_resources;
062
063    /**
064     * Constructor.<p>
065     *
066     * @param appId the app id
067     * @param contextType the context type, to be used for visibility evaluation
068     * @param resources the list of resources
069     */
070    protected A_CmsDialogContext(String appId, ContextType contextType, List<CmsResource> resources) {
071
072        m_appId = appId;
073        m_resources = resources != null ? resources : Collections.<CmsResource> emptyList();
074        m_contextType = contextType;
075    }
076
077    /**
078     * Closes the dialog window.<p>
079     */
080    protected void closeWindow() {
081
082        if (m_window != null) {
083            m_window.close();
084            m_window = null;
085        }
086    }
087
088    /**
089     * @see org.opencms.ui.I_CmsDialogContext#error(java.lang.Throwable)
090     */
091    public void error(Throwable error) {
092
093        closeWindow();
094        CmsErrorDialog.showErrorDialog(error, new Runnable() {
095
096            public void run() {
097
098                finish(null);
099            }
100        });
101    }
102
103    /**
104     * @see org.opencms.ui.I_CmsDialogContext#finish(org.opencms.file.CmsProject, java.lang.String)
105     */
106    public void finish(CmsProject project, String siteRoot) {
107
108        if ((project != null) || (siteRoot != null)) {
109            reload();
110        } else {
111            finish(null);
112        }
113    }
114
115    /**
116     * @see org.opencms.ui.I_CmsDialogContext#finish(java.util.Collection)
117     */
118    public void finish(Collection<CmsUUID> result) {
119
120        closeWindow();
121        CmsAppWorkplaceUi.get().enableGlobalShortcuts();
122    }
123
124    /**
125     * @see org.opencms.ui.I_CmsDialogContext#getAppId()
126     */
127    public String getAppId() {
128
129        return m_appId;
130    }
131
132    /**
133     * @see org.opencms.ui.I_CmsDialogContext#getCms()
134     */
135    public CmsObject getCms() {
136
137        return A_CmsUI.getCmsObject();
138    }
139
140    /**
141     * @see org.opencms.ui.I_CmsDialogContext#getContextType()
142     */
143    public ContextType getContextType() {
144
145        return m_contextType;
146    }
147
148    /**
149     * @see org.opencms.ui.I_CmsDialogContext#getResources()
150     */
151    public List<CmsResource> getResources() {
152
153        return m_resources;
154    }
155
156    /**
157     * Returns the window.
158     *
159     * @return the window
160     */
161    public Window getWindow() {
162
163        return m_window;
164    }
165
166    /**
167     * @see org.opencms.ui.I_CmsDialogContext#navigateTo(java.lang.String)
168     */
169    public void navigateTo(String appId) {
170
171        closeWindow();
172        A_CmsUI.get().getNavigator().navigateTo(appId);
173    }
174
175    /**
176     * @see org.opencms.ui.I_CmsDialogContext#onViewChange()
177     */
178    public void onViewChange() {
179
180        if (m_window != null) {
181            m_window.center();
182        }
183    }
184
185    /**
186     * @see org.opencms.ui.I_CmsDialogContext#reload()
187     */
188    public void reload() {
189
190        closeWindow();
191        CmsAppWorkplaceUi.get().reload();
192    }
193
194    /**
195     * @see org.opencms.ui.I_CmsDialogContext#setWindow(com.vaadin.ui.Window)
196     */
197    public void setWindow(Window window) {
198
199        m_window = window;
200    }
201
202    /**
203     * @see org.opencms.ui.I_CmsDialogContext#start(java.lang.String, com.vaadin.ui.Component)
204     */
205    public void start(String title, Component dialog) {
206
207        start(title, dialog, DialogWidth.narrow);
208    }
209
210    /**
211     * @see org.opencms.ui.I_CmsDialogContext#start(java.lang.String, com.vaadin.ui.Component)
212     */
213    public void start(String title, Component dialog, DialogWidth style) {
214
215        if (dialog != null) {
216            CmsAppWorkplaceUi.get().disableGlobalShortcuts();
217            m_window = CmsBasicDialog.prepareWindow(style);
218            m_window.setCaption(title);
219            m_window.setContent(dialog);
220            CmsAppWorkplaceUi.get().addWindow(m_window);
221            if (dialog instanceof CmsBasicDialog) {
222                ((CmsBasicDialog)dialog).initActionHandler(m_window);
223            }
224        }
225    }
226}