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.acacia.client.widgets;
029
030import org.opencms.ade.galleries.client.CmsGalleryConfigurationJSO;
031import org.opencms.ade.galleries.client.ui.CmsGalleryField;
032import org.opencms.gwt.client.util.CmsDomUtil;
033
034import com.google.gwt.dom.client.Element;
035import com.google.gwt.event.dom.client.FocusEvent;
036import com.google.gwt.event.dom.client.FocusHandler;
037import com.google.gwt.event.logical.shared.HasResizeHandlers;
038import com.google.gwt.event.logical.shared.ResizeHandler;
039import com.google.gwt.event.logical.shared.ValueChangeEvent;
040import com.google.gwt.event.logical.shared.ValueChangeHandler;
041import com.google.gwt.event.shared.HandlerRegistration;
042import com.google.gwt.user.client.ui.Composite;
043
044/**
045 *
046 * */
047public class CmsFileWidget extends Composite implements I_CmsEditWidget, HasResizeHandlers {
048
049    /** Value of the activation. */
050    private boolean m_active = true;
051
052    /** If files should selectable. */
053    private boolean m_includeFiles;
054
055    /** The link selector. */
056    private CmsGalleryField m_linkSelect;
057
058    /** The reference path. */
059    private String m_referencePath;
060
061    /** If the site selector should be visible. */
062    private boolean m_showSiteSelector;
063
064    /** The start site. */
065    private String m_startSite;
066
067    /** The resource types. */
068    private String m_types;
069
070    /**
071     * Constructs an CmsComboWidget with the in XSD schema declared configuration.<p>
072     *
073     * @param openerTitle the gallery opener title
074     * @param config the widget configuration string
075     */
076    public CmsFileWidget(String openerTitle, String config) {
077
078        m_linkSelect = new CmsGalleryField(CmsGalleryConfigurationJSO.parseConfiguration(config), false);
079        m_linkSelect.setGalleryOpenerTitle(openerTitle);
080        m_linkSelect.addValueChangeHandler(new ValueChangeHandler<String>() {
081
082            public void onValueChange(ValueChangeEvent<String> event) {
083
084                fireChangeEvent();
085
086            }
087        });
088        // All composites must call initWidget() in their constructors.
089        initWidget(m_linkSelect);
090        m_linkSelect.addFocusHandler(new FocusHandler() {
091
092            public void onFocus(FocusEvent event) {
093
094                CmsDomUtil.fireFocusEvent(CmsFileWidget.this);
095            }
096        });
097    }
098
099    /**
100     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
101     */
102    public HandlerRegistration addFocusHandler(FocusHandler handler) {
103
104        return addDomHandler(handler, FocusEvent.getType());
105    }
106
107    /**
108     * @see com.google.gwt.event.logical.shared.HasResizeHandlers#addResizeHandler(com.google.gwt.event.logical.shared.ResizeHandler)
109     */
110    public HandlerRegistration addResizeHandler(ResizeHandler handler) {
111
112        return m_linkSelect.addResizeHandler(handler);
113    }
114
115    /**
116     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
117     */
118    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
119
120        return addHandler(handler, ValueChangeEvent.getType());
121    }
122
123    /**
124     * Represents a value change event.<p>
125     *
126     */
127    public void fireChangeEvent() {
128
129        ValueChangeEvent.fire(this, m_linkSelect.getFormValueAsString());
130    }
131
132    /**
133     * @see com.google.gwt.user.client.ui.HasValue#getValue()
134     */
135    public String getValue() {
136
137        return m_linkSelect.getFormValueAsString();
138    }
139
140    /**
141     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
142     */
143    public boolean isActive() {
144
145        return m_active;
146    }
147
148    /**
149     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
150     */
151    public void onAttachWidget() {
152
153        super.onAttach();
154    }
155
156    /**
157     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
158     */
159    public boolean owns(Element element) {
160
161        return (m_linkSelect != null)
162            && (m_linkSelect.getPopup() != null)
163            && m_linkSelect.getPopup().isShowing()
164            && m_linkSelect.getPopup().getContainer().getElement().isOrHasChild(element);
165    }
166
167    /**
168     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
169     */
170    public void setActive(boolean active) {
171
172        if (m_active == active) {
173            return;
174        }
175        m_active = active;
176        if (active) {
177            fireChangeEvent();
178        }
179
180    }
181
182    /**
183     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
184     */
185    public void setName(String name) {
186
187        m_linkSelect.setName(name);
188
189    }
190
191    /**
192     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
193     */
194    public void setValue(String value) {
195
196        setValue(value, false);
197
198    }
199
200    /**
201     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
202     */
203    public void setValue(String value, boolean fireEvents) {
204
205        m_linkSelect.setFormValueAsString(value);
206        if (fireEvents) {
207            fireChangeEvent();
208        }
209
210    }
211
212    /**
213     * Parses the configuration.<p>
214     *
215     * @param configuration the widget configuration
216     */
217    private native void parseConfiguration(String configuration)/*-{
218                                                                var config = @org.opencms.gwt.client.util.CmsDomUtil::parseJSON(Ljava/lang/String;)(configuration);
219                                                                if (config.includefiles)
220                                                                this.@org.opencms.acacia.client.widgets.CmsFileWidget::m_includeFiles = config.includefiles;
221                                                                if (config.showsiteselector)
222                                                                this.@org.opencms.acacia.client.widgets.CmsFileWidget::m_showSiteSelector = config.showsiteselector;
223                                                                if (config.startsite)
224                                                                this.@org.opencms.acacia.client.widgets.CmsFileWidget::m_startSite = config.startsite;
225                                                                if (config.referencepath)
226                                                                this.@org.opencms.acacia.client.widgets.CmsFileWidget::m_referencePath = config.referencepath;
227                                                                }-*/;
228}