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