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.editors;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.types.I_CmsResourceType;
033import org.opencms.i18n.CmsEncoder;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.apps.I_CmsAppUIContext;
040import org.opencms.ui.apps.Messages;
041import org.opencms.ui.components.CmsBrowserFrame;
042import org.opencms.ui.components.CmsConfirmationDialog;
043
044import java.io.UnsupportedEncodingException;
045import java.net.URLEncoder;
046import java.util.Map;
047import java.util.Map.Entry;
048
049import org.apache.commons.logging.Log;
050
051import com.vaadin.navigator.Navigator;
052import com.vaadin.navigator.ViewChangeListener;
053import com.vaadin.server.ExternalResource;
054
055/**
056 * Class to extended by frame based editors.<p>
057 */
058public abstract class A_CmsFrameEditor implements I_CmsEditor, ViewChangeListener {
059
060    /** Log instance for this class. */
061    private static final Log LOG = CmsLog.getLog(A_CmsFrameEditor.class);
062
063    /** The serial version id. */
064    private static final long serialVersionUID = 6944345583913510988L;
065
066    /** The editor state. */
067    protected CmsEditorStateExtension m_editorState;
068
069    /** Flag indicating a view change. */
070    boolean m_leaving;
071
072    /** The currently edited resource. */
073    CmsResource m_resource;
074
075    /** The frame component. */
076    private CmsBrowserFrame m_frame;
077
078    /**
079     * @see com.vaadin.navigator.ViewChangeListener#afterViewChange(com.vaadin.navigator.ViewChangeListener.ViewChangeEvent)
080     */
081    public void afterViewChange(ViewChangeEvent event) {
082
083        // nothing to do
084    }
085
086    /**
087     * @see com.vaadin.navigator.ViewChangeListener#beforeViewChange(com.vaadin.navigator.ViewChangeListener.ViewChangeEvent)
088     */
089    public boolean beforeViewChange(final ViewChangeEvent event) {
090
091        if (!m_leaving && m_editorState.hasChanges()) {
092            final String target = event.getViewName();
093            CmsConfirmationDialog.show(
094                CmsVaadinUtils.getMessageText(Messages.GUI_EDITOR_CLOSE_CAPTION_0),
095                CmsVaadinUtils.getMessageText(Messages.GUI_EDITOR_CLOSE_TEXT_0),
096                new Runnable() {
097
098                    public void run() {
099
100                        leaveEditor(event.getNavigator(), target);
101                    }
102                });
103            return false;
104        } else if (!m_leaving) {
105            tryUnlock();
106        }
107
108        return true;
109    }
110
111    /**
112     * @see org.opencms.ui.editors.I_CmsEditor#initUI(org.opencms.ui.apps.I_CmsAppUIContext, org.opencms.file.CmsResource, java.lang.String, java.util.Map)
113     */
114    public void initUI(I_CmsAppUIContext context, CmsResource resource, String backLink, Map<String, String> params) {
115
116        m_resource = resource;
117        CmsObject cms = A_CmsUI.getCmsObject();
118        String sitepath = m_resource != null ? cms.getSitePath(m_resource) : "";
119        String link = OpenCms.getLinkManager().substituteLinkForRootPath(cms, getEditorUri());
120        m_frame = new CmsBrowserFrame();
121        m_frame.setDescription("Editor");
122        m_frame.setName("edit");
123        link += "?resource=" + sitepath + "&backlink=" + backLink;
124        if (params != null) {
125            for (Entry<String, String> entry : params.entrySet()) {
126                try {
127                    link += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), CmsEncoder.ENCODING_UTF_8);
128                } catch (UnsupportedEncodingException e) {
129                    LOG.error(e.getLocalizedMessage(), e);
130                }
131            }
132        }
133        m_frame.setSource(new ExternalResource(link));
134        m_frame.setSizeFull();
135        context.showInfoArea(false);
136        context.hideToolbar();
137        m_frame.addStyleName("o-editor-frame");
138        context.setAppContent(m_frame);
139        context.setAppTitle(
140            CmsVaadinUtils.getMessageText(
141                Messages.GUI_CONTENT_EDITOR_TITLE_2,
142                resource == null ? "new resource" : resource.getName(),
143                CmsResource.getParentFolder(sitepath)));
144        m_editorState = new CmsEditorStateExtension(m_frame);
145    }
146
147    /**
148     * @see org.opencms.ui.editors.I_CmsEditor#matchesResource(org.opencms.file.CmsResource, boolean)
149     */
150    public boolean matchesResource(CmsResource resource, boolean plainText) {
151
152        I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource);
153        return matchesType(type, plainText);
154    }
155
156    /**
157     * Returns the editor URI.<p>
158     *
159     * @return the editor URI
160     */
161    protected abstract String getEditorUri();
162
163    /**
164     * Leaves the editor view.<p>
165     *
166     * @param navigator the navigator instance
167     * @param target the target view
168     */
169    void leaveEditor(Navigator navigator, String target) {
170
171        m_leaving = true;
172        tryUnlock();
173        navigator.navigateTo(target);
174    }
175
176    /**
177     * Tries to unlock the current resource.<p>
178     */
179    private void tryUnlock() {
180
181        if (m_resource != null) {
182            try {
183                A_CmsUI.getCmsObject().unlockResource(m_resource);
184            } catch (CmsException e) {
185                LOG.debug("Unlocking resource " + m_resource.getRootPath() + " failed", e);
186            }
187        }
188    }
189}