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; 124 try { 125 link += "&backlink=" + URLEncoder.encode(backLink, CmsEncoder.ENCODING_UTF_8); 126 } catch (UnsupportedEncodingException e1) { 127 LOG.error(e1.getLocalizedMessage(), e1); 128 } 129 if (params != null) { 130 for (Entry<String, String> entry : params.entrySet()) { 131 try { 132 link += "&" + entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), CmsEncoder.ENCODING_UTF_8); 133 } catch (UnsupportedEncodingException e) { 134 LOG.error(e.getLocalizedMessage(), e); 135 } 136 } 137 } 138 m_frame.setSource(new ExternalResource(link)); 139 m_frame.setSizeFull(); 140 context.showInfoArea(false); 141 context.hideToolbar(); 142 m_frame.addStyleName("o-editor-frame"); 143 context.setAppContent(m_frame); 144 context.setAppTitle( 145 CmsVaadinUtils.getMessageText( 146 Messages.GUI_CONTENT_EDITOR_TITLE_2, 147 resource == null ? "new resource" : resource.getName(), 148 CmsResource.getParentFolder(sitepath))); 149 m_editorState = new CmsEditorStateExtension(m_frame); 150 } 151 152 /** 153 * @see org.opencms.ui.editors.I_CmsEditor#matchesResource(org.opencms.file.CmsObject, org.opencms.file.CmsResource, boolean) 154 */ 155 public boolean matchesResource(CmsObject cms, CmsResource resource, boolean plainText) { 156 157 I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource); 158 return matchesType(type, plainText); 159 } 160 161 /** 162 * Returns the editor URI.<p> 163 * 164 * @return the editor URI 165 */ 166 protected abstract String getEditorUri(); 167 168 /** 169 * Leaves the editor view.<p> 170 * 171 * @param navigator the navigator instance 172 * @param target the target view 173 */ 174 void leaveEditor(Navigator navigator, String target) { 175 176 m_leaving = true; 177 tryUnlock(); 178 navigator.navigateTo(target); 179 } 180 181 /** 182 * Tries to unlock the current resource.<p> 183 */ 184 private void tryUnlock() { 185 186 if (m_resource != null) { 187 try { 188 A_CmsUI.getCmsObject().unlockResource(m_resource); 189 } catch (CmsException e) { 190 LOG.debug("Unlocking resource " + m_resource.getRootPath() + " failed", e); 191 } 192 } 193 } 194}