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