001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.property.definition;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.rpc.CmsRpcAction;
032import org.opencms.gwt.client.ui.CmsPushButton;
033import org.opencms.gwt.client.ui.I_CmsButton;
034import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
035import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
036import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
037
038import java.util.ArrayList;
039
040import com.google.gwt.dom.client.Style.Unit;
041import com.google.gwt.event.dom.client.ClickEvent;
042import com.google.gwt.event.dom.client.ClickHandler;
043import com.google.gwt.event.logical.shared.CloseEvent;
044import com.google.gwt.event.logical.shared.CloseHandler;
045import com.google.gwt.user.client.ui.PopupPanel;
046
047/**
048 * Button for defining new properties from the property dialog.<p>
049 */
050public class CmsPropertyDefinitionButton extends CmsPushButton {
051
052    /** The dialog instance. */
053    private CmsFormDialog m_dialog;
054
055    /**
056     * Creates a new instance of the button.<p>
057     */
058    public CmsPropertyDefinitionButton() {
059
060        super(I_CmsButton.SETTINGS_SMALL);
061        addStyleName(I_CmsLayoutBundle.INSTANCE.propertiesCss().propertyDefinitionButton());
062        setTitle(CmsPropertyDefinitionMessages.messageDialogCaption());
063        setButtonStyle(ButtonStyle.FONT_ICON, null);
064        getElement().getStyle().setMargin(4, Unit.PX);
065        addClickHandler(new ClickHandler() {
066
067            public void onClick(ClickEvent event) {
068
069                onBeforeEditPropertyDefinition();
070                editPropertyDefinition();
071            }
072        });
073    }
074
075    /**
076     * Gets the dialog which this button is used for.<p>
077     *
078     * @return the dialog for this button
079     */
080    public CmsFormDialog getDialog() {
081
082        return m_dialog;
083    }
084
085    /**
086     * Installs the button on a dialog if the user has sufficient permissions.<p>
087     *
088     * @param dialog the dialog to which the button should be added
089     */
090    public void installOnDialog(CmsFormDialog dialog) {
091
092        if (CmsCoreProvider.get().getUserInfo().isDeveloper()) {
093            setDialog(dialog);
094            dialog.addButton(this);
095        }
096    }
097
098    /**
099     * Method which is called directly before the property definition dialog is opened.<p>
100     */
101    public void onBeforeEditPropertyDefinition() {
102
103        // do nothing in the default implementation
104    }
105
106    /**
107     * Method which is called when the property definition dialog is closed.<p>
108     */
109    public void onClosePropertyDefinitionDialog() {
110
111        // do nothing in the default implementation
112    }
113
114    /**
115     * Sets the dialog instance.<p>
116     *
117     * @param dialog the dialog instance
118     */
119    public void setDialog(CmsFormDialog dialog) {
120
121        m_dialog = dialog;
122    }
123
124    /**
125     * Opens the dialog for creating new property definitions.<p>
126     */
127    protected void editPropertyDefinition() {
128
129        CmsRpcAction<ArrayList<String>> action = new CmsRpcAction<ArrayList<String>>() {
130
131            @Override
132            public void execute() {
133
134                start(200, true);
135                CmsCoreProvider.getVfsService().getDefinedProperties(this);
136            }
137
138            @Override
139            protected void onResponse(ArrayList<String> result) {
140
141                stop(false);
142                CmsPropertyDefinitionDialog dialog = new CmsPropertyDefinitionDialog(result);
143                dialog.center();
144                dialog.addCloseHandler(new CloseHandler<PopupPanel>() {
145
146                    public void onClose(CloseEvent<PopupPanel> event) {
147
148                        onClosePropertyDefinitionDialog();
149                    }
150
151                });
152            }
153
154        };
155        action.execute();
156    }
157
158}