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.gwt.client.I_CmsHasInit;
031import org.opencms.gwt.client.ui.I_CmsAutoHider;
032import org.opencms.gwt.client.ui.contextmenu.I_CmsStringSelectHandler;
033import org.opencms.gwt.client.ui.input.form.CmsWidgetFactoryRegistry;
034import org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory;
035import org.opencms.gwt.client.util.CmsEmbeddedDialogHandler;
036import org.opencms.gwt.shared.CmsLinkBean;
037import org.opencms.util.CmsStringUtil;
038
039import java.util.HashMap;
040import java.util.Map;
041
042import com.google.common.base.Optional;
043import com.google.gwt.event.dom.client.BlurEvent;
044import com.google.gwt.event.dom.client.BlurHandler;
045import com.google.gwt.event.dom.client.MouseUpEvent;
046import com.google.gwt.event.dom.client.MouseUpHandler;
047import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
048import com.google.gwt.event.logical.shared.ValueChangeEvent;
049import com.google.gwt.event.logical.shared.ValueChangeHandler;
050import com.google.gwt.event.shared.HandlerRegistration;
051import com.google.gwt.user.client.Command;
052import com.google.gwt.user.client.ui.Composite;
053import com.google.gwt.user.client.ui.FlowPanel;
054import com.google.gwt.user.client.ui.Panel;
055
056/**
057 * Basic gallery widget for forms.<p>
058 *
059 * @since 8.0.0
060 *
061 */
062public class CmsPrincipalSelection extends Composite
063implements I_CmsFormWidget, I_CmsHasInit, HasValueChangeHandlers<String> {
064
065    /** The dialog id. */
066    public static final String DIALOG_ID = "principalselect";
067
068    /** A counter used for giving text box widgets ids. */
069    private static int idCounter;
070
071    /** The widget type identifier for this widget. */
072    private static final String WIDGET_TYPE = "groupselection";
073
074    /** The old value. */
075    protected String m_oldValue = "";
076
077    /** The popup frame. */
078    protected CmsFramePopup m_popup;
079
080    /** The handler registration. */
081    protected HandlerRegistration m_previewHandlerRegistration;
082
083    /** The default rows set. */
084    int m_defaultRows;
085
086    /** The root panel containing the other components of this widget. */
087    Panel m_panel = new FlowPanel();
088
089    /** The container for the text area. */
090    CmsSelectionInput m_selectionInput;
091
092    /** The configuration parameters. */
093    private Map<String, String> m_configuration;
094
095    /** The error display for this widget. */
096    private CmsErrorWidget m_error = new CmsErrorWidget();
097
098    /** The field id. */
099    private String m_id;
100
101    /**
102     * VsfSelection widget to open the gallery selection.<p>
103     * @param config the configuration for this widget
104     */
105    public CmsPrincipalSelection(String config) {
106
107        initWidget(m_panel);
108        parseConfiguration(config);
109        m_selectionInput = new CmsSelectionInput(null);
110        m_id = "CmsVfsSelection_" + (idCounter++);
111        m_selectionInput.m_textbox.getElement().setId(m_id);
112
113        m_panel.add(m_selectionInput);
114        m_panel.add(m_error);
115
116        m_selectionInput.m_textbox.addMouseUpHandler(new MouseUpHandler() {
117
118            public void onMouseUp(MouseUpEvent event) {
119
120                m_selectionInput.hideFader();
121                setTitle("");
122                if (m_popup == null) {
123                    open();
124                } else if (m_popup.isShowing()) {
125                    close();
126                } else {
127                    open();
128                }
129
130            }
131
132        });
133        m_selectionInput.m_textbox.addBlurHandler(new BlurHandler() {
134
135            public void onBlur(BlurEvent event) {
136
137                if ((m_selectionInput.m_textbox.getValue().length()
138                    * 6.88) > m_selectionInput.m_textbox.getOffsetWidth()) {
139                    setTitle(m_selectionInput.m_textbox.getValue());
140                }
141                m_selectionInput.showFader();
142            }
143        });
144        m_selectionInput.setOpenCommand(new Command() {
145
146            public void execute() {
147
148                if (m_popup == null) {
149                    open();
150                } else if (m_popup.isShowing()) {
151                    close();
152                } else {
153                    open();
154                }
155
156            }
157        });
158    }
159
160    /**
161     * Initializes this class.<p>
162     */
163    public static void initClass() {
164
165        // registers a factory for creating new instances of this widget
166        CmsWidgetFactoryRegistry.instance().registerFactory(WIDGET_TYPE, new I_CmsFormWidgetFactory() {
167
168            /**
169             * @see org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetFactory#createWidget(java.util.Map, com.google.common.base.Optional)
170             */
171            public I_CmsFormWidget createWidget(Map<String, String> widgetParams, Optional<String> defaultValue) {
172
173                return new CmsPrincipalSelection("type=groupwidget");
174            }
175        });
176    }
177
178    /**
179     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
180     */
181    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
182
183        return m_selectionInput.m_textbox.addValueChangeHandler(handler);
184    }
185
186    /**
187     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue()
188     */
189    public String getApparentValue() {
190
191        return getFormValueAsString();
192    }
193
194    /**
195     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFieldType()
196     */
197    public FieldType getFieldType() {
198
199        return I_CmsFormWidget.FieldType.STRING;
200    }
201
202    /**
203     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue()
204     */
205    public Object getFormValue() {
206
207        if (m_selectionInput.m_textbox.getText() == null) {
208            return "";
209        }
210        return m_selectionInput.m_textbox.getValue();
211    }
212
213    /**
214     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
215     */
216    public String getFormValueAsString() {
217
218        return (String)getFormValue();
219    }
220
221    /**
222     * Returns the selected link as a bean.<p>
223     *
224     * @return the selected link as a bean
225     */
226    public CmsLinkBean getLinkBean() {
227
228        String link = m_selectionInput.m_textbox.getValue();
229        if (CmsStringUtil.isEmptyOrWhitespaceOnly(link)) {
230            return null;
231        }
232        return new CmsLinkBean(m_selectionInput.m_textbox.getText(), true);
233    }
234
235    /**
236     * Returns the text contained in the text area.<p>
237     *
238     * @return the text in the text area
239     */
240    public String getText() {
241
242        return m_selectionInput.m_textbox.getValue();
243    }
244
245    /**
246     * Returns the text box container of this widget.<p>
247     *
248     * @return the text box container
249     */
250    public CmsSelectionInput getTextAreaContainer() {
251
252        return m_selectionInput;
253    }
254
255    /**
256     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#isEnabled()
257     */
258    public boolean isEnabled() {
259
260        return m_selectionInput.m_textbox.isEnabled();
261    }
262
263    /**
264     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#reset()
265     */
266    public void reset() {
267
268        m_selectionInput.m_textbox.setText("");
269    }
270
271    /**
272     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider)
273     */
274    public void setAutoHideParent(I_CmsAutoHider autoHideParent) {
275
276        // nothing to do
277    }
278
279    /**
280     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setEnabled(boolean)
281     */
282    public void setEnabled(boolean enabled) {
283
284        m_selectionInput.m_textbox.setEnabled(enabled);
285    }
286
287    /**
288     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setErrorMessage(java.lang.String)
289     */
290    public void setErrorMessage(String errorMessage) {
291
292        m_error.setText(errorMessage);
293    }
294
295    /**
296     * Sets the value of the widget.<p>
297     *
298     * @param value the new value
299     */
300    public void setFormValue(Object value) {
301
302        if (value == null) {
303            value = "";
304        }
305        if (value instanceof String) {
306            String strValue = (String)value;
307            m_selectionInput.m_textbox.setText(strValue);
308            setTitle(strValue);
309        }
310
311    }
312
313    /**
314     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String)
315     */
316    public void setFormValueAsString(String newValue) {
317
318        setFormValue(newValue);
319    }
320
321    /**
322     * Sets the link from a bean.<p>
323     *
324     * @param link the link bean
325     */
326    public void setLinkBean(CmsLinkBean link) {
327
328        if (link == null) {
329            link = new CmsLinkBean("", true);
330        }
331        m_selectionInput.m_textbox.setValue(link.getLink());
332    }
333
334    /**
335     * Sets the name of the input field.<p>
336     *
337     * @param name of the input field
338     * */
339    public void setName(String name) {
340
341        m_selectionInput.m_textbox.setName(name);
342
343    }
344
345    /**
346     * Sets the text in the text area.<p>
347     *
348     * @param text the new text
349     */
350    public void setText(String text) {
351
352        m_selectionInput.m_textbox.setValue(text);
353    }
354
355    /**
356     * @see com.google.gwt.user.client.ui.UIObject#setTitle(java.lang.String)
357     */
358    @Override
359    public void setTitle(String title) {
360
361        m_selectionInput.m_textbox.getElement().setTitle(title);
362    }
363
364    /**
365     * Close the popup of this widget.<p>
366     * */
367    protected void close() {
368
369        m_popup.hideDelayed();
370        m_selectionInput.m_textbox.setFocus(true);
371        m_selectionInput.m_textbox.setCursorPos(m_selectionInput.m_textbox.getText().length());
372    }
373
374    /**
375     * Opens the popup of this widget.<p>
376     * */
377    protected void open() {
378
379        m_oldValue = m_selectionInput.m_textbox.getValue();
380        CmsEmbeddedDialogHandler handler = new CmsEmbeddedDialogHandler();
381        handler.setStringSelectHandler(new I_CmsStringSelectHandler() {
382
383            public void selectString(String principal) {
384
385                if (!getFormValue().equals(principal)) {
386                    setFormValueAsString(principal);
387                    fireValueChange(principal);
388                }
389            }
390        });
391        handler.openDialog(DIALOG_ID, null, null, m_configuration);
392    }
393
394    /**
395     * Fires the value change event.<p>
396     *
397     * @param value the changed value
398     */
399    void fireValueChange(String value) {
400
401        ValueChangeEvent.<String> fire(m_selectionInput.m_textbox, value);
402    }
403
404    /**
405     * Parses the configuration string.<p>
406     *
407     * @param config the configuration string
408     */
409    private void parseConfiguration(String config) {
410
411        m_configuration = new HashMap<String, String>();
412        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(config)) {
413            for (String param : config.split(",")) {
414                int index = param.indexOf("=");
415                if ((index > 0) && (param.length() > (index + 1))) {
416                    m_configuration.put(param.substring(0, index), param.substring(index + 1));
417                }
418            }
419        }
420    }
421}