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.dbmanager;
029
030import org.opencms.file.CmsPropertyDefinition;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsIllegalArgumentException;
033import org.opencms.ui.A_CmsUI;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.Messages;
036import org.opencms.ui.components.CmsBasicDialog;
037import org.opencms.util.CmsStringUtil;
038
039import com.vaadin.v7.data.Validator;
040import com.vaadin.ui.Button;
041import com.vaadin.ui.Button.ClickEvent;
042import com.vaadin.ui.Button.ClickListener;
043import com.vaadin.v7.ui.TextField;
044import com.vaadin.ui.Window;
045
046/**
047 * Class for dialog to create property definition.<p>
048 */
049public class CmsAddPropertyDefinitionDialog extends CmsBasicDialog {
050
051    /**Validator for new Property name. */
052    class PropertyExistValidator implements Validator {
053
054        /**vaadin serial id. */
055        private static final long serialVersionUID = -2500052661735259675L;
056
057        /**
058         * @see com.vaadin.data.Validator#validate(java.lang.Object)
059         */
060        public void validate(Object value) throws InvalidValueException {
061
062            if (value == null) {
063                throw new InvalidValueException(
064                    CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_EMPTY_0));
065            }
066            String propName = (String)value;
067            if (CmsStringUtil.isEmptyOrWhitespaceOnly(propName)) {
068                throw new InvalidValueException(
069                    CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_EMPTY_0));
070            }
071            try {
072                if (A_CmsUI.getCmsObject().readPropertyDefinition(propName) != null) {
073                    throw new InvalidValueException(
074                        CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_ALREADY_EXIST_0));
075                }
076            } catch (CmsException e) {
077                //should not happen (non existing property leads to null, not throwing exception.)
078            }
079
080            try {
081                CmsPropertyDefinition.checkPropertyName(propName);
082            } catch (CmsIllegalArgumentException e) {
083                throw new InvalidValueException(
084                    CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_NOTVALID_0));
085            }
086        }
087
088    }
089
090    /**vaadin serial id.*/
091    private static final long serialVersionUID = -5454565964997277536L;
092
093    /**New property name field. */
094    protected TextField m_newProperty;
095
096    /**ok button. */
097    private Button m_ok;
098
099    /**cancel button. */
100    private Button m_cancel;
101
102    /**
103     * public constructor.<p>
104     * @param window to be closed
105     * @param table to be updated
106     */
107    public CmsAddPropertyDefinitionDialog(final Window window, final CmsPropertyTable table) {
108        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
109
110        m_ok.addClickListener(new ClickListener() {
111
112            private static final long serialVersionUID = -7356827828386377748L;
113
114            public void buttonClick(ClickEvent event) {
115
116                m_newProperty.removeAllValidators();
117                m_newProperty.addValidator(new PropertyExistValidator());
118                if (m_newProperty.isValid()) {
119
120                    saveProperty();
121                    table.init();
122                    window.close();
123                }
124            }
125        });
126
127        m_cancel.addClickListener(new ClickListener() {
128
129            private static final long serialVersionUID = -3198675226086758775L;
130
131            public void buttonClick(ClickEvent event) {
132
133                window.close();
134
135            }
136        });
137
138    }
139
140    /**
141     * saves the new property.<p>
142     */
143    protected void saveProperty() {
144
145        try {
146            A_CmsUI.getCmsObject().createPropertyDefinition(m_newProperty.getValue());
147        } catch (CmsException e) {
148            //
149        }
150    }
151
152}