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.ui.apps.filehistory;
029
030import org.opencms.configuration.CmsSystemConfiguration;
031import org.opencms.main.OpenCms;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.apps.Messages;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.util.CmsUUID;
036
037import java.util.ArrayList;
038import java.util.List;
039
040import com.vaadin.v7.data.Property.ValueChangeEvent;
041import com.vaadin.v7.data.Property.ValueChangeListener;
042import com.vaadin.v7.data.util.BeanItemContainer;
043import com.vaadin.v7.shared.ui.label.ContentMode;
044import com.vaadin.ui.Button;
045import com.vaadin.ui.Button.ClickEvent;
046import com.vaadin.ui.Button.ClickListener;
047import com.vaadin.v7.ui.ComboBox;
048import com.vaadin.v7.ui.Label;
049import com.vaadin.v7.ui.OptionGroup;
050import com.vaadin.ui.Panel;
051import com.vaadin.v7.ui.VerticalLayout;
052
053/**
054 * Class for the history settings dialog.<p>
055 */
056public class CmsFileHistorySettings extends VerticalLayout {
057
058    /**
059     * Bean for the elements of the combo box for number of version option.<p>
060     */
061    public class ComboBoxVersionsBean {
062
063        /**Value of item.*/
064        private int m_val;
065
066        /**
067         * public constructor.<p>
068         *
069         * @param value of item
070         */
071        public ComboBoxVersionsBean(int value) {
072            m_val = value;
073        }
074
075        /**
076         * @see java.lang.Object#equals(java.lang.Object)
077         */
078        @Override
079        public boolean equals(Object o) {
080
081            if (o instanceof ComboBoxVersionsBean) {
082                return ((ComboBoxVersionsBean)o).getValue() == m_val;
083            }
084            return false;
085        }
086
087        /**
088         * Getter for display String to be displayed in combo box.<p>
089         *
090         * @return String to be displayed
091         */
092        public String getDisplayValue() {
093
094            if (m_val == CmsFileHistoryApp.NUMBER_VERSIONS_DISABLED) {
095                return CmsVaadinUtils.getMessageText(Messages.GUI_FILEHISTORY_SETTINGS_VERSIONS_DISABLED_0);
096            }
097            if (m_val == CmsFileHistoryApp.NUMBER_VERSIONS_UNLIMITED) {
098                return CmsVaadinUtils.getMessageText(Messages.GUI_FILEHISTORY_SETTINGS_VERSIONS_UNLIMITED_0);
099            }
100            return String.valueOf(m_val);
101        }
102
103        /**
104         * Getter for (int) value of item.<p>
105         *
106         * @return int value
107         */
108        public int getValue() {
109
110            return m_val;
111        }
112
113        /**
114         * @see java.lang.Object#hashCode()
115         */
116        @Override
117        public int hashCode() {
118
119            return CmsUUID.getNullUUID().hashCode();
120        }
121    }
122
123    /**Vaadin serial id.*/
124    private static final long serialVersionUID = 5595583577732283758L;
125
126    /**Vaadin component.*/
127    private Button m_edit;
128
129    /**Vaadin component.*/
130    private OptionGroup m_mode;
131
132    /**Vaadin component.*/
133    private ComboBox m_numberVersions;
134
135    /**Vaadin component.*/
136    private Panel m_statusPanel;
137
138    /**
139     * Public constructor of class.<p>
140     *
141     * @param app instance of history app.
142     * @param state state passed from app with information if settings were edited or are not valid
143     */
144    public CmsFileHistorySettings(final CmsFileHistoryApp app, String state) {
145        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
146
147        m_edit.setEnabled(false);
148
149        if (CmsStringUtil.isEmptyOrWhitespaceOnly(state)) {
150            m_statusPanel.setVisible(false);
151        } else {
152            Label status = new Label();
153            status.setWidth("100%");
154            status.setContentMode(ContentMode.HTML);
155            if (state.startsWith(CmsFileHistoryApp.PATH_SETTINGS_INVALID)) {
156                status.setValue(
157                    "<p width='100%' align='center' style='color:#FF0000;'>"
158                        + CmsVaadinUtils.getMessageText(Messages.GUI_FILEHISTORY_SETTINGS_INVALID_0)
159                        + "</p>");
160            } else {
161                status.setValue("<p width='100%' align='center'>Settings saved</p>");
162            }
163
164            m_statusPanel.setContent(status);
165        }
166
167        setupVersionComboBox();
168        setupModeOptions();
169
170        ValueChangeListener changeListener = new ValueChangeListener() {
171
172            /**vaadin serial id.*/
173            private static final long serialVersionUID = -6003215873244541851L;
174
175            public void valueChange(ValueChangeEvent event) {
176
177                setButtonEnabled(true);
178            }
179        };
180
181        m_numberVersions.addValueChangeListener(changeListener);
182
183        m_mode.addValueChangeListener(changeListener);
184
185        m_edit.addClickListener(new ClickListener() {
186
187            private static final long serialVersionUID = 161296255232053110L;
188
189            public void buttonClick(ClickEvent event) {
190
191                if (saveOptions()) {
192                    setButtonEnabled(false);
193                } else {
194                    app.openSubView(CmsFileHistoryApp.PATH_SETTINGS_INVALID, true);
195                }
196            }
197        });
198
199    }
200
201    /**
202     * Sets the edit button enabled or disabled.<p>
203     *
204     * @param enable state of button
205     */
206    protected void setButtonEnabled(boolean enable) {
207
208        m_edit.setEnabled(enable);
209    }
210
211    /**
212     * Saves the options.<p>
213     *
214     * @return Path to be called in app.
215     */
216    boolean saveOptions() {
217
218        //Enable history?
219        boolean enabled = ((ComboBoxVersionsBean)m_numberVersions.getValue()).getValue() != CmsFileHistoryApp.NUMBER_VERSIONS_DISABLED;
220
221        //Maximal count of versions for current resources.
222        int versions = ((ComboBoxVersionsBean)m_numberVersions.getValue()).getValue();
223
224        //Maximal count of versions for deleted resources.
225        int versionsDeleted = versions;
226
227        if (m_mode.getValue().equals(CmsFileHistoryApp.MODE_DISABLED)) {
228            versionsDeleted = 0;
229        }
230        if (m_mode.getValue().equals(CmsFileHistoryApp.MODE_WITHOUTVERSIONS)) {
231            versionsDeleted = 1;
232        }
233
234        if (m_mode.getValue().equals(CmsFileHistoryApp.MODE_WITHVERSIONS)
235            && (versions == CmsFileHistoryApp.NUMBER_VERSIONS_DISABLED)) {
236            return false;
237        }
238        OpenCms.getSystemInfo().setVersionHistorySettings(enabled, versions, versionsDeleted);
239        OpenCms.writeConfiguration(CmsSystemConfiguration.class);
240
241        return true;
242    }
243
244    /**
245     * Sets up the OptionGroup for the mode.<p>
246     */
247    private void setupModeOptions() {
248
249        int versionsDeleted = OpenCms.getSystemInfo().getHistoryVersionsAfterDeletion();
250
251        String mode;
252
253        if (versionsDeleted == 0) {
254            mode = CmsFileHistoryApp.MODE_DISABLED;
255        } else if (versionsDeleted == 1) {
256            mode = CmsFileHistoryApp.MODE_WITHOUTVERSIONS;
257        } else if ((versionsDeleted > 1) || (versionsDeleted == -1)) {
258            mode = CmsFileHistoryApp.MODE_WITHVERSIONS;
259        } else {
260            mode = CmsFileHistoryApp.MODE_DISABLED;
261        }
262        m_mode.setValue(mode);
263    }
264
265    /**
266     * Fills the combo box to choose the number of versions which shouls be stored.<p>
267     */
268    private void setupVersionComboBox() {
269
270        //Setup beans
271        List<ComboBoxVersionsBean> beans = new ArrayList<ComboBoxVersionsBean>();
272
273        //Disabled
274        beans.add(new ComboBoxVersionsBean(CmsFileHistoryApp.NUMBER_VERSIONS_DISABLED));
275
276        //1-10
277        for (int i = 1; i <= 10; i++) {
278            beans.add(new ComboBoxVersionsBean(i));
279        }
280
281        //15-50
282        for (int i = 15; i <= 50; i += 5) {
283            beans.add(new ComboBoxVersionsBean(i));
284        }
285
286        //Unlimited
287        beans.add(new ComboBoxVersionsBean(CmsFileHistoryApp.NUMBER_VERSIONS_UNLIMITED));
288
289        BeanItemContainer<ComboBoxVersionsBean> objects = new BeanItemContainer<ComboBoxVersionsBean>(
290            ComboBoxVersionsBean.class,
291            beans);
292        m_numberVersions.setContainerDataSource(objects);
293        m_numberVersions.setItemCaptionPropertyId("displayValue");
294
295        m_numberVersions.setNullSelectionAllowed(false);
296        m_numberVersions.setTextInputAllowed(false);
297        m_numberVersions.setPageLength(beans.size());
298
299        int numberHistoryVersions = OpenCms.getSystemInfo().getHistoryVersions();
300
301        m_numberVersions.setValue(beans.get(beans.indexOf(new ComboBoxVersionsBean(numberHistoryVersions))));
302    }
303}