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.lists;
029
030import org.opencms.ade.containerpage.shared.CmsDialogOptions;
031import org.opencms.ade.containerpage.shared.CmsDialogOptions.Option;
032import org.opencms.file.CmsResource;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.components.CmsBasicDialog;
035
036import java.util.Collections;
037
038import com.vaadin.v7.shared.ui.label.ContentMode;
039import com.vaadin.ui.Button;
040import com.vaadin.ui.Button.ClickEvent;
041import com.vaadin.ui.Button.ClickListener;
042import com.vaadin.v7.ui.Label;
043import com.vaadin.v7.ui.OptionGroup;
044import com.vaadin.v7.ui.VerticalLayout;
045import com.vaadin.ui.Window;
046import com.vaadin.ui.Window.CloseEvent;
047import com.vaadin.ui.Window.CloseListener;
048
049/**
050 * Option dialog.<p>
051 */
052public class CmsOptionDialog extends CmsBasicDialog {
053
054    /**
055     * Dialog handler to handle the selected option.<p>
056     */
057    public interface I_OptionHandler {
058
059        /**
060         * Handles the selected option.<p>
061         *
062         * @param option the selected option
063         */
064        void handleOption(String option);
065    }
066
067    /** The serial version id. */
068    private static final long serialVersionUID = 8398169182381160373L;
069
070    /**
071     * Constructor.<p>
072     *
073     * @param resource the handled resource
074     * @param options the available options
075     * @param handler the option handler
076     * @param onClose called on dialog close or cancel
077     * @param window the dialog window
078     */
079    public CmsOptionDialog(
080        CmsResource resource,
081        CmsDialogOptions options,
082        final I_OptionHandler handler,
083        final Runnable onClose,
084        final Window window) {
085        if (resource != null) {
086            displayResourceInfo(Collections.singletonList(resource));
087        }
088
089        VerticalLayout layout = new VerticalLayout();
090        layout.setMargin(true);
091        layout.setSpacing(true);
092        layout.addComponent(new Label(options.getInfo(), ContentMode.HTML));
093        final OptionGroup opt = new OptionGroup();
094        for (Option option : options.getOptions()) {
095            opt.addItem(option.getValue());
096            opt.setItemCaption(option.getValue(), option.getLabel());
097        }
098        opt.setValue(options.getOptions().get(0).getValue());
099        layout.addComponent(opt);
100        setContent(layout);
101        Button ok = new Button(CmsVaadinUtils.getMessageText(org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_OK_0));
102        ok.addClickListener(new ClickListener() {
103
104            private static final long serialVersionUID = 1L;
105
106            public void buttonClick(ClickEvent event) {
107
108                String option = (String)opt.getValue();
109                if (window != null) {
110                    window.close();
111                }
112                handler.handleOption(option);
113            }
114
115        });
116        addButton(ok);
117        Button cancel = new Button(
118            CmsVaadinUtils.getMessageText(org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_CANCEL_0));
119        cancel.addClickListener(new ClickListener() {
120
121            private static final long serialVersionUID = 1L;
122
123            public void buttonClick(ClickEvent event) {
124
125                if (window != null) {
126                    window.close();
127                }
128                if (onClose != null) {
129                    onClose.run();
130                }
131            }
132
133        });
134        addButton(cancel);
135        if ((window != null) && (onClose != null)) {
136            window.addCloseListener(new CloseListener() {
137
138                private static final long serialVersionUID = 1L;
139
140                public void windowClose(CloseEvent e) {
141
142                    onClose.run();
143                }
144            });
145        }
146    }
147
148}