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.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(new ValueChangeListener() {
056
057            private static final long serialVersionUID = 1L;
058
059            @SuppressWarnings("synthetic-access")
060            public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
061
062                String value = (String)(event.getProperty().getValue());
063                if (!m_settingInternalValue) {
064                    setInternalValue(value);
065                    fireValueChange(false);
066                }
067
068            }
069
070        });
071
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 if only root paths should be used.<p>
106     *
107     * @param useRootPaths <code>true</code> to use root paths only
108     */
109    public void setUseRootPaths(boolean useRootPaths) {
110
111        m_useRootPaths = useRootPaths;
112    }
113
114    /**
115     * Sets the value.<p>
116     *
117     * @param value the new value
118     */
119    @Override
120    public void setValue(String value) {
121
122        setValue(false, value);
123    }
124
125    /**
126     * @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object)
127     */
128    @Override
129    protected void setInternalValue(String newValue) {
130
131        m_settingInternalValue = true;
132        try {
133            super.setInternalValue(newValue);
134            m_textField.setValue(newValue);
135        } finally {
136            m_settingInternalValue = false;
137        }
138    }
139
140    /**
141     * @see org.opencms.ui.components.fileselect.A_CmsFileSelectField#setResourceValue(org.opencms.file.CmsResource)
142     */
143    @Override
144    protected void setResourceValue(CmsResource resource) {
145
146        CmsObject cms = m_cms == null ? A_CmsUI.getCmsObject() : m_cms;
147
148        CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(resource.getRootPath());
149        if (!m_useRootPaths && (site != null) && cms.getRequestContext().getSiteRoot().equals(site.getSiteRoot())) {
150            setValue(true, cms.getSitePath(resource));
151        } else {
152            setValue(true, resource.getRootPath());
153        }
154    }
155
156    /**
157     * Sets the value.<p>
158     *
159     * @param fireChange <code>true</code> to fire the value change event
160     * @param value the value to set
161     */
162    protected void setValue(boolean fireChange, String value) {
163
164        m_textField.setValue(value);
165        fireValueChange(false);
166    }
167}