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.preferences;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.rpc.CmsRpcAction;
032import org.opencms.gwt.client.ui.CmsPopup;
033import org.opencms.gwt.client.ui.CmsScrollPanel;
034import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
035import org.opencms.gwt.client.ui.input.CmsSelectBox;
036import org.opencms.gwt.client.ui.input.I_CmsFormField;
037import org.opencms.gwt.client.ui.input.I_CmsFormWidget;
038import org.opencms.gwt.client.ui.input.form.CmsBasicFormField;
039import org.opencms.gwt.client.ui.input.form.CmsDialogFormHandler;
040import org.opencms.gwt.client.ui.input.form.CmsForm;
041import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
042import org.opencms.gwt.client.ui.input.form.CmsWidgetFactoryRegistry;
043import org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler;
044import org.opencms.gwt.client.ui.input.form.I_CmsFormWidgetMultiFactory;
045import org.opencms.gwt.client.util.CmsDomUtil;
046import org.opencms.gwt.client.util.CmsDomUtil.Style;
047import org.opencms.gwt.shared.CmsUserSettingsBean;
048import org.opencms.xml.content.CmsXmlContentProperty;
049
050import java.util.Collections;
051import java.util.Map;
052import java.util.Set;
053
054import com.google.common.base.Optional;
055import com.google.gwt.core.client.Scheduler;
056import com.google.gwt.core.client.Scheduler.RepeatingCommand;
057import com.google.gwt.dom.client.Element;
058
059/**
060 * Dialog used for changing the user settings.<p>
061 */
062public class CmsUserSettingsDialog extends CmsFormDialog implements I_CmsFormSubmitHandler {
063
064    /** The action to execute after the user has changed their preferences. */
065    Runnable m_finishAction;
066
067    /** The panel used to edit the preferences. */
068    CmsUserSettingsFormFieldPanel m_panel;
069
070    /** The old tab index. */
071    private int m_oldTabIndex;
072
073    /**
074     * Creates a new widget instance.<p>
075     *
076     * @param userSettings the current user settings
077     * @param finishAction the action to execute when the user has edited the user settings
078     */
079    public CmsUserSettingsDialog(CmsUserSettingsBean userSettings, Runnable finishAction) {
080
081        super("User settings", new CmsForm(false), -1);
082        m_finishAction = finishAction;
083        m_panel = new CmsUserSettingsFormFieldPanel(userSettings);
084
085        getForm().setWidget(m_panel);
086        for (Map.Entry<String, CmsXmlContentProperty> entry : userSettings.getConfiguration().entrySet()) {
087            String key = entry.getKey();
088            CmsXmlContentProperty settingDef = entry.getValue();
089            I_CmsFormWidgetMultiFactory factory = new I_CmsFormWidgetMultiFactory() {
090
091                public I_CmsFormWidget createFormWidget(
092                    String widgetKey,
093                    Map<String, String> widgetParams,
094                    Optional<String> defaultValue) {
095
096                    if (CmsSelectBox.WIDGET_TYPE.equals(widgetKey)) {
097                        widgetKey = CmsSelectBox.WIDGET_TYPE_NOTNULL;
098                    }
099                    return CmsWidgetFactoryRegistry.instance().createFormWidget(widgetKey, widgetParams, defaultValue);
100                }
101            };
102            I_CmsFormField field = CmsBasicFormField.createField(
103                settingDef,
104                settingDef.getName(),
105                factory,
106                Collections.<String, String> emptyMap(),
107                false);
108            if (userSettings.isBasic(settingDef.getName())) {
109                field.getLayoutData().put("basic", "true");
110            }
111            String initialValue = userSettings.getValue(key);
112            if (initialValue == null) {
113                initialValue = settingDef.getDefault();
114            }
115
116            getForm().addField(field, initialValue);
117        }
118        CmsDialogFormHandler handler = new CmsDialogFormHandler();
119        handler.setDialog(this);
120        handler.setSubmitHandler(this);
121        getForm().setFormHandler(handler);
122        getForm().render();
123        getElement().addClassName(I_CmsLayoutBundle.INSTANCE.dialogCss().hideCaption());
124        setMainContent(m_panel);
125        setModal(true);
126        setGlassEnabled(true);
127        removePadding();
128    }
129
130    /**
131     * Loads the user settings dialog.<p>
132     *
133     * @param finishAction  the action to execute after the user has changed his preferences
134     */
135    public static void loadAndShow(final Runnable finishAction) {
136
137        CmsRpcAction<CmsUserSettingsBean> action = new CmsRpcAction<CmsUserSettingsBean>() {
138
139            @Override
140            public void execute() {
141
142                start(200, false);
143                CmsCoreProvider.getService().loadUserSettings(this);
144
145            }
146
147            @Override
148            protected void onResponse(CmsUserSettingsBean result) {
149
150                stop(false);
151                CmsUserSettingsDialog dlg = new CmsUserSettingsDialog(result, finishAction);
152                dlg.centerHorizontally(50);
153            }
154        };
155
156        action.execute();
157
158    }
159
160    /**
161     * @see org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler#onSubmitForm(org.opencms.gwt.client.ui.input.form.CmsForm, java.util.Map, java.util.Set)
162     */
163    public void onSubmitForm(CmsForm form, final Map<String, String> fieldValues, final Set<String> editedFields) {
164
165        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
166
167            @Override
168            public void execute() {
169
170                start(200, false);
171                CmsCoreProvider.getService().saveUserSettings(fieldValues, editedFields, this);
172
173            }
174
175            @Override
176            protected void onResponse(Void result) {
177
178                stop(false);
179                if (m_finishAction != null) {
180                    m_finishAction.run();
181                }
182            }
183        };
184        action.execute();
185    }
186
187    /**
188     * @see com.google.gwt.user.client.ui.Widget#onLoad()
189     */
190    @Override
191    protected void onLoad() {
192
193        super.onLoad();
194        Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
195
196            public boolean execute() {
197
198                if (!CmsUserSettingsDialog.this.isAttached() || !CmsUserSettingsDialog.this.isVisible()) {
199                    return false;
200                }
201                updateHeight();
202                return true;
203            }
204
205        }, 200);
206    }
207
208    /**
209     * Updates the panel height depending on the content of the current tab.<p>
210     */
211    protected void updateHeight() {
212
213        CmsPopup dialog = this;
214        int tabIndex = m_panel.getTabPanel().getSelectedIndex();
215        boolean changedTab = tabIndex != m_oldTabIndex;
216        m_oldTabIndex = tabIndex;
217        CmsScrollPanel tabWidget = m_panel.getTabPanel().getWidget(tabIndex);
218        Element innerElement = tabWidget.getWidget().getElement();
219        int contentHeight = CmsDomUtil.getCurrentStyleInt(innerElement, Style.height);
220        int spaceLeft = dialog.getAvailableHeight(0);
221        int newHeight = Math.min(spaceLeft, contentHeight + 47);
222        boolean changedHeight = m_panel.getTabPanel().getOffsetHeight() != newHeight;
223        if (changedHeight || changedTab) {
224            m_panel.getTabPanel().setHeight(newHeight + "px");
225            int selectedIndex = m_panel.getTabPanel().getSelectedIndex();
226            CmsScrollPanel widget = m_panel.getTabPanel().getWidget(selectedIndex);
227            widget.setHeight((newHeight - 34) + "px");
228            widget.onResizeDescendant();
229        }
230    }
231}