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.gwt.client.ui.input;
029
030import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
031import org.opencms.gwt.client.CmsCoreProvider;
032import org.opencms.gwt.client.ui.I_CmsAutoHider;
033import org.opencms.gwt.shared.CmsLinkBean;
034import org.opencms.util.CmsStringUtil;
035
036import com.google.gwt.event.dom.client.BlurEvent;
037import com.google.gwt.event.dom.client.BlurHandler;
038import com.google.gwt.event.dom.client.MouseUpEvent;
039import com.google.gwt.event.dom.client.MouseUpHandler;
040import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
041import com.google.gwt.event.logical.shared.ValueChangeHandler;
042import com.google.gwt.event.shared.HandlerRegistration;
043import com.google.gwt.user.client.Command;
044import com.google.gwt.user.client.DOM;
045import com.google.gwt.user.client.Event;
046import com.google.gwt.user.client.Event.NativePreviewEvent;
047import com.google.gwt.user.client.Event.NativePreviewHandler;
048import com.google.gwt.user.client.ui.Composite;
049import com.google.gwt.user.client.ui.FlowPanel;
050import com.google.gwt.user.client.ui.Panel;
051
052/**
053 * Basic gallery widget for forms.<p>
054 *
055 * @since 8.0.0
056 *
057 */
058public class CmsVfsSelection extends Composite implements I_CmsFormWidget, HasValueChangeHandlers<String> {
059
060    /**
061     * Event preview handler.<p>
062     *
063     * To be used while popup open.<p>
064     */
065    protected class CloseEventPreviewHandler implements NativePreviewHandler {
066
067        /**
068         * @see com.google.gwt.user.client.Event.NativePreviewHandler#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent)
069         */
070        public void onPreviewNativeEvent(NativePreviewEvent event) {
071
072            Event nativeEvent = Event.as(event.getNativeEvent());
073            switch (DOM.eventGetType(nativeEvent)) {
074                case Event.ONMOUSEMOVE:
075                    break;
076                case Event.ONMOUSEUP:
077                    break;
078                case Event.ONMOUSEDOWN:
079                    break;
080                case Event.ONKEYUP:
081                    if (m_selectionInput.m_textbox.getValue().length() > 0) {
082                        close();
083                    } else {
084                        if (m_popup == null) {
085                            open();
086                        } else if (m_popup.isShowing()) {
087                            close();
088                        } else {
089                            open();
090                        }
091                    }
092                    break;
093                case Event.ONMOUSEWHEEL:
094                    close();
095                    break;
096                default:
097                    // do nothing
098            }
099        }
100
101    }
102
103    /** The download mode of this widget. */
104    public static final String DOWNLOAD = "download";
105
106    /** The download link mode of this widget. */
107    public static final String DOWNLOAD_LINK = "download_link";
108
109    /** The file link mode of this widget. */
110    public static final String FILE_LINK = "file_link";
111
112    /** The image link mode of this widget. */
113    public static final String IMAGE_LINK = "image_link";
114
115    /** The link mode of this widget. */
116    public static final String LINK = "link";
117
118    /** A counter used for giving text box widgets ids. */
119    private static int idCounter;
120
121    /** The old value. */
122    protected String m_oldValue = "";
123
124    /** The popup frame. */
125    protected CmsFramePopup m_popup;
126
127    /** The handler registration. */
128    protected HandlerRegistration m_previewHandlerRegistration;
129
130    /** The default rows set. */
131    int m_defaultRows;
132
133    /** The root panel containing the other components of this widget. */
134    Panel m_panel = new FlowPanel();
135
136    /** The container for the text area. */
137    CmsSelectionInput m_selectionInput;
138
139    /** The configuration string. */
140    private String m_config;
141
142    /** The error display for this widget. */
143    private CmsErrorWidget m_error = new CmsErrorWidget();
144
145    /** The field id. */
146    private String m_id;
147
148    /** The selection type. */
149    private String m_type;
150
151    /**
152     * VsfSelection widget to open the gallery selection.<p>
153     * @param iconImage the image of the icon shown in the
154     * @param type the type of this widget
155     * @param config the configuration for this widget
156     */
157    public CmsVfsSelection(String iconImage, String type, String config) {
158
159        initWidget(m_panel);
160        m_type = type;
161        m_config = config;
162        m_selectionInput = new CmsSelectionInput(iconImage);
163        m_id = "CmsVfsSelection_" + (idCounter++);
164        m_selectionInput.m_textbox.getElement().setId(m_id);
165
166        m_panel.add(m_selectionInput);
167        m_panel.add(m_error);
168
169        m_selectionInput.m_textbox.addMouseUpHandler(new MouseUpHandler() {
170
171            public void onMouseUp(MouseUpEvent event) {
172
173                m_selectionInput.hideFader();
174                setTitle("");
175                if (m_popup == null) {
176                    open();
177                } else if (m_popup.isShowing()) {
178                    close();
179                } else {
180                    open();
181                }
182
183            }
184
185        });
186        m_selectionInput.m_textbox.addBlurHandler(new BlurHandler() {
187
188            public void onBlur(BlurEvent event) {
189
190                if ((m_selectionInput.m_textbox.getValue().length()
191                    * 6.88) > m_selectionInput.m_textbox.getOffsetWidth()) {
192                    setTitle(m_selectionInput.m_textbox.getValue());
193                }
194                m_selectionInput.showFader();
195            }
196        });
197        m_selectionInput.setOpenCommand(new Command() {
198
199            public void execute() {
200
201                if (m_popup == null) {
202                    open();
203                } else if (m_popup.isShowing()) {
204                    close();
205                } else {
206                    open();
207                }
208
209            }
210        });
211    }
212
213    /**
214     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
215     */
216    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
217
218        return m_selectionInput.m_textbox.addValueChangeHandler(handler);
219    }
220
221    /**
222     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue()
223     */
224    public String getApparentValue() {
225
226        return getFormValueAsString();
227    }
228
229    /**
230     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFieldType()
231     */
232    public FieldType getFieldType() {
233
234        return I_CmsFormWidget.FieldType.STRING;
235    }
236
237    /**
238     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue()
239     */
240    public Object getFormValue() {
241
242        if (m_selectionInput.m_textbox.getText() == null) {
243            return "";
244        }
245        return m_selectionInput.m_textbox.getValue();
246    }
247
248    /**
249     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
250     */
251    public String getFormValueAsString() {
252
253        return (String)getFormValue();
254    }
255
256    /**
257     * Returns the selected link as a bean.<p>
258     *
259     * @return the selected link as a bean
260     */
261    public CmsLinkBean getLinkBean() {
262
263        String link = m_selectionInput.m_textbox.getValue();
264        if (CmsStringUtil.isEmptyOrWhitespaceOnly(link)) {
265            return null;
266        }
267        return new CmsLinkBean(m_selectionInput.m_textbox.getText(), true);
268    }
269
270    /**
271     * Returns the text contained in the text area.<p>
272     *
273     * @return the text in the text area
274     */
275    public String getText() {
276
277        return m_selectionInput.m_textbox.getValue();
278    }
279
280    /**
281     * Returns the text box container of this widget.<p>
282     *
283     * @return the text box container
284     */
285    public CmsSelectionInput getTextAreaContainer() {
286
287        return m_selectionInput;
288    }
289
290    /**
291     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#isEnabled()
292     */
293    public boolean isEnabled() {
294
295        return m_selectionInput.m_textbox.isEnabled();
296    }
297
298    /**
299     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#reset()
300     */
301    public void reset() {
302
303        m_selectionInput.m_textbox.setText("");
304    }
305
306    /**
307     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider)
308     */
309    public void setAutoHideParent(I_CmsAutoHider autoHideParent) {
310
311        // nothing to do
312    }
313
314    /**
315     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setEnabled(boolean)
316     */
317    public void setEnabled(boolean enabled) {
318
319        m_selectionInput.m_textbox.setEnabled(enabled);
320    }
321
322    /**
323     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setErrorMessage(java.lang.String)
324     */
325    public void setErrorMessage(String errorMessage) {
326
327        m_error.setText(errorMessage);
328    }
329
330    /**
331     * Sets the value of the widget.<p>
332     *
333     * @param value the new value
334     */
335    public void setFormValue(Object value) {
336
337        if (value == null) {
338            value = "";
339        }
340        if (value instanceof String) {
341            String strValue = (String)value;
342            m_selectionInput.m_textbox.setText(strValue);
343            setTitle(strValue);
344        }
345
346    }
347
348    /**
349     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String)
350     */
351    public void setFormValueAsString(String newValue) {
352
353        setFormValue(newValue);
354    }
355
356    /**
357     * Sets the link from a bean.<p>
358     *
359     * @param link the link bean
360     */
361    public void setLinkBean(CmsLinkBean link) {
362
363        if (link == null) {
364            link = new CmsLinkBean("", true);
365        }
366        m_selectionInput.m_textbox.setValue(link.getLink());
367    }
368
369    /**
370     * Sets the name of the input field.<p>
371     *
372     * @param name of the input field
373     * */
374    public void setName(String name) {
375
376        m_selectionInput.m_textbox.setName(name);
377
378    }
379
380    /**
381     * Sets the text in the text area.<p>
382     *
383     * @param text the new text
384     */
385    public void setText(String text) {
386
387        m_selectionInput.m_textbox.setValue(text);
388    }
389
390    /**
391     * @see com.google.gwt.user.client.ui.UIObject#setTitle(java.lang.String)
392     */
393    @Override
394    public void setTitle(String title) {
395
396        m_selectionInput.m_textbox.getElement().setTitle(title);
397    }
398
399    /**
400     * Creates the URL for the gallery dialog IFrame.<p>
401     *
402     * @return the URL for the gallery dialog IFrame
403     */
404    protected String buildGalleryUrl() {
405
406        String basePath = "";
407        basePath = "/system/workplace/commons/gallery.jsp";
408        basePath += "?dialogmode=widget&fieldid=" + m_id;
409        if (LINK.equals(m_type)) {
410            basePath += "&" + I_CmsGalleryProviderConstants.CONFIG_RESOURCE_TYPES + "=pointer";
411        }
412
413        String pathparameter = m_selectionInput.m_textbox.getText();
414        if (pathparameter.indexOf("/") > -1) {
415            basePath += "&currentelement=" + pathparameter;
416        }
417        basePath += m_config;
418        return CmsCoreProvider.get().link(basePath);
419    }
420
421    /**
422     * Close the popup of this widget.<p>
423     * */
424    protected void close() {
425
426        m_popup.hideDelayed();
427        m_selectionInput.m_textbox.setFocus(true);
428        m_selectionInput.m_textbox.setCursorPos(m_selectionInput.m_textbox.getText().length());
429    }
430
431    /**
432     * Opens the popup of this widget.<p>
433     * */
434    protected void open() {
435
436        m_oldValue = m_selectionInput.m_textbox.getValue();
437        if (m_popup == null) {
438            String title = org.opencms.gwt.client.Messages.get().key(
439                org.opencms.gwt.client.Messages.GUI_GALLERY_SELECT_DIALOG_TITLE_0);
440            m_popup = new CmsFramePopup(title, buildGalleryUrl());
441
442            m_popup.setCloseHandler(new Runnable() {
443
444                public void run() {
445
446                    String textboxValue = m_selectionInput.m_textbox.getText();
447
448                    if (!m_oldValue.equals(textboxValue)) {
449                        m_selectionInput.m_textbox.setValue("", true);
450                        m_selectionInput.m_textbox.setValue(textboxValue, true);
451                    }
452
453                    if (m_previewHandlerRegistration != null) {
454                        m_previewHandlerRegistration.removeHandler();
455                        m_previewHandlerRegistration = null;
456                    }
457                    m_selectionInput.m_textbox.setFocus(true);
458                    m_selectionInput.m_textbox.setCursorPos(m_selectionInput.m_textbox.getText().length());
459                }
460            });
461            m_popup.setModal(false);
462            m_popup.setId(m_id);
463            m_popup.setWidth(717);
464            m_popup.getFrame().setSize("705px", "640px");
465            m_popup.addDialogClose(new Command() {
466
467                public void execute() {
468
469                    close();
470
471                }
472            });
473        } else {
474            m_popup.getFrame().setUrl(buildGalleryUrl());
475        }
476        m_popup.setAutoHideEnabled(true);
477        m_popup.center();
478        if (m_previewHandlerRegistration == null) {
479            m_previewHandlerRegistration = Event.addNativePreviewHandler(new CloseEventPreviewHandler());
480        }
481    }
482
483    /**
484     * Exporting the set principal function to the window scope.<p>
485     */
486    private native void exportSetPrincipalFunction()/*-{
487                var self = this;
488                $wnd.setPrincipalFormValue = function(typeFlag, principal) {
489                        self.@org.opencms.gwt.client.ui.input.CmsVfsSelection::setFormValueAsString(Ljava/lang/String;)(principal);
490                        self.@org.opencms.gwt.client.ui.input.CmsVfsSelection::close()();
491                }
492    }-*/;
493}