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.apps.modules.edit; 029 030import org.opencms.ade.galleries.CmsSiteSelectorOptionBuilder; 031import org.opencms.ade.galleries.shared.CmsSiteSelectorOption; 032import org.opencms.db.CmsUserSettings; 033import org.opencms.file.CmsObject; 034import org.opencms.file.CmsResourceFilter; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsLog; 037import org.opencms.main.OpenCms; 038import org.opencms.ui.CmsVaadinUtils; 039import org.opencms.ui.apps.Messages; 040import org.opencms.ui.components.fileselect.CmsPathSelectField; 041import org.opencms.ui.components.fileselect.CmsResourceSelectDialog; 042import org.opencms.ui.components.fileselect.CmsResourceSelectDialog.Options; 043import org.opencms.util.CmsStringUtil; 044 045import java.util.Arrays; 046 047import org.apache.commons.logging.Log; 048 049import com.vaadin.server.AbstractErrorMessage.ContentMode; 050import com.vaadin.server.ErrorMessage; 051import com.vaadin.server.UserError; 052import com.vaadin.shared.ui.ErrorLevel; 053import com.vaadin.ui.Component; 054import com.vaadin.v7.data.Item; 055import com.vaadin.v7.data.Property; 056import com.vaadin.v7.data.util.IndexedContainer; 057 058/** 059 * A widget for selecting a module resource.<p> 060 */ 061public class CmsModuleResourceSelectField extends CmsPathSelectField { 062 063 /** The logger instance for this class. */ 064 private static final Log LOG = CmsLog.getLog(CmsModuleResourceSelectField.class); 065 066 /** The serial version id. */ 067 private static final long serialVersionUID = 1L; 068 069 /** 070 * Creates a new instance.<p> 071 */ 072 public CmsModuleResourceSelectField() { 073 074 addValueChangeListener(new ValueChangeListener() { 075 076 /** Serial version id. */ 077 private static final long serialVersionUID = 1L; 078 079 @SuppressWarnings("synthetic-access") 080 public void valueChange(Property.ValueChangeEvent event) { 081 082 updateValidation(); 083 } 084 085 }); 086 } 087 088 /** 089 * @see org.opencms.ui.components.fileselect.A_CmsFileSelectField#setCmsObject(org.opencms.file.CmsObject) 090 */ 091 @Override 092 public void setCmsObject(CmsObject cms) { 093 094 m_cms = cms; 095 } 096 097 /** 098 * Updates the site root.<p> 099 * 100 * @param siteRoot the site root 101 */ 102 public void updateSite(String siteRoot) { 103 104 try { 105 CmsObject cloneCms = OpenCms.initCmsObject(m_cms); 106 if (siteRoot == null) { 107 siteRoot = "/system"; 108 } 109 cloneCms.getRequestContext().setSiteRoot(siteRoot); 110 m_cms = cloneCms; 111 } catch (CmsException e1) { 112 LOG.error(e1.getLocalizedMessage(), e1); 113 } 114 updateValidation(); 115 } 116 117 /** 118 * @see org.opencms.ui.components.fileselect.A_CmsFileSelectField#getOptions() 119 */ 120 @Override 121 protected Options getOptions() { 122 123 Options options = new Options(); 124 125 CmsSiteSelectorOptionBuilder optBuilder = new CmsSiteSelectorOptionBuilder(m_cms); 126 optBuilder.addNormalSites(true, (new CmsUserSettings(m_cms)).getStartFolder()); 127 optBuilder.addSharedSite(); 128 optBuilder.addSystemFolder(); 129 IndexedContainer availableSites = new IndexedContainer(); 130 availableSites.addContainerProperty(CmsResourceSelectDialog.PROPERTY_SITE_CAPTION, String.class, null); 131 for (CmsSiteSelectorOption option : optBuilder.getOptions()) { 132 String siteRoot = option.getSiteRoot(); 133 boolean matches = false; 134 for (String candidate : Arrays.asList( 135 m_cms.getRequestContext().getSiteRoot(), 136 "/system", 137 OpenCms.getSiteManager().getSharedFolder())) { 138 if (CmsStringUtil.comparePaths(candidate, siteRoot)) { 139 matches = true; 140 break; 141 } 142 } 143 if (matches) { 144 Item siteItem = availableSites.addItem(option.getSiteRoot()); 145 siteItem.getItemProperty(CmsResourceSelectDialog.PROPERTY_SITE_CAPTION).setValue(option.getMessage()); 146 } 147 } 148 options.setSiteSelectionContainer(availableSites); 149 return options; 150 151 } 152 153 /** 154 * Updates the validation status.<p> 155 */ 156 private void updateValidation() { 157 158 boolean changed = false; 159 changed |= CmsVaadinUtils.updateComponentError(m_textField, null); 160 String path = getValue(); 161 if (!CmsStringUtil.isEmptyOrWhitespaceOnly(path)) { 162 if (!m_cms.existsResource(path, CmsResourceFilter.IGNORE_EXPIRATION)) { 163 ErrorMessage error = new UserError( 164 CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_MODULE_RESOURCE_NOT_FOUND_0), 165 ContentMode.TEXT, 166 ErrorLevel.WARNING); 167 changed |= CmsVaadinUtils.updateComponentError(m_textField, error); 168 } 169 } 170 if (changed) { 171 fireEvent(new Component.ErrorEvent(null, this)); 172 } 173 } 174 175}