001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.components.fileselect; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.main.OpenCms; 033import org.opencms.site.CmsSite; 034import org.opencms.ui.A_CmsUI; 035 036/** 037 * File path select field. This field will also allow paths that are not pointing to any VFS resource.<p> 038 */ 039public class CmsPathSelectField extends A_CmsFileSelectField<String> { 040 041 /** Serial version id. */ 042 private static final long serialVersionUID = 1L; 043 044 /** Flag to indicate whether we are currently setting the internal value. */ 045 private boolean m_settingInternalValue; 046 047 /** Flag indicating if only root paths are used. */ 048 private boolean m_useRootPaths; 049 050 /** 051 * Creates a new instance.<p> 052 */ 053 public CmsPathSelectField() { 054 055 m_textField.addValueChangeListener(event -> { 056 057 if (!m_settingInternalValue) { 058 setInternalValue(event.getValue()); 059 fireValueChange(false); 060 } 061 }); 062 063 } 064 065 /** 066 * Returns the placeholder. 067 * @return the placeholder 068 */ 069 public String getPlaceholder() { 070 071 return m_textField.getPlaceholder(); 072 } 073 074 /** 075 * @see com.vaadin.ui.AbstractField#getType() 076 */ 077 @Override 078 public Class<? extends String> getType() { 079 080 return String.class; 081 } 082 083 /** 084 * Gets the value.<p> 085 * 086 * @return the value 087 */ 088 @Override 089 public String getValue() { 090 091 return m_textField.getValue(); 092 } 093 094 /** 095 * Returns if only root paths are used.<p> 096 * 097 * @return <code>true</code> if only root paths are used 098 */ 099 public boolean isUseRootPaths() { 100 101 return m_useRootPaths; 102 } 103 104 /** 105 * Sets the placeholder. 106 * @param placeholder the placeholder 107 */ 108 public void setPlaceholder(String placeholder) { 109 110 m_textField.setPlaceholder(placeholder); 111 } 112 113 /** 114 * Sets if only root paths should be used.<p> 115 * 116 * @param useRootPaths <code>true</code> to use root paths only 117 */ 118 public void setUseRootPaths(boolean useRootPaths) { 119 120 m_useRootPaths = useRootPaths; 121 } 122 123 /** 124 * Sets the value.<p> 125 * 126 * @param value the new value 127 */ 128 @Override 129 public void setValue(String value) { 130 131 setValue(false, value); 132 } 133 134 /** 135 * @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object) 136 */ 137 @Override 138 protected void setInternalValue(String newValue) { 139 140 m_settingInternalValue = true; 141 try { 142 super.setInternalValue(newValue); 143 m_textField.setValue(newValue); 144 } finally { 145 m_settingInternalValue = false; 146 } 147 } 148 149 /** 150 * @see org.opencms.ui.components.fileselect.A_CmsFileSelectField#setResourceValue(org.opencms.file.CmsResource) 151 */ 152 @Override 153 protected void setResourceValue(CmsResource resource) { 154 155 CmsObject cms = m_cms == null ? A_CmsUI.getCmsObject() : m_cms; 156 157 CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(resource.getRootPath()); 158 if (!m_useRootPaths && (site != null) && cms.getRequestContext().getSiteRoot().equals(site.getSiteRoot())) { 159 setValue(true, cms.getSitePath(resource)); 160 } else { 161 setValue(true, resource.getRootPath()); 162 } 163 } 164 165 /** 166 * Sets the value.<p> 167 * 168 * @param fireChange <code>true</code> to fire the value change event 169 * @param value the value to set 170 */ 171 protected void setValue(boolean fireChange, String value) { 172 173 m_textField.setValue(value); 174 fireValueChange(false); 175 } 176}