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.setup.updater.dialogs;
029
030import org.opencms.setup.CmsUpdateBean;
031import org.opencms.setup.CmsUpdateUI;
032import org.opencms.ui.components.CmsBasicDialog;
033import org.opencms.util.CmsFileUtil;
034import org.opencms.util.CmsStringUtil;
035
036import java.io.FileInputStream;
037import java.io.InputStream;
038
039import com.vaadin.shared.ui.ContentMode;
040import com.vaadin.ui.Button;
041import com.vaadin.ui.Button.ClickEvent;
042import com.vaadin.ui.Button.ClickListener;
043import com.vaadin.ui.Label;
044
045/**
046 * The abstract class for the update dialogs.<p>
047 */
048public abstract class A_CmsUpdateDialog extends CmsBasicDialog {
049
050    /**vaadin serial id.*/
051    private static final long serialVersionUID = 1L;
052
053    /**Ui. */
054    protected CmsUpdateUI m_ui;
055
056    /**Continue button. */
057    Button m_ok;
058
059    /**Back button. */
060    Button m_back;
061
062    /**
063     * Returns the back button.<p>
064     *
065     * @return Button
066     */
067    public Button getBackButton() {
068
069        return new Button("Back");
070    }
071
072    /**
073     * Returns the continue button.<p>
074     *
075     * @return Button
076     */
077    public Button getOkButton() {
078
079        return new Button("Continue");
080    }
081
082    /**
083     * Creates a new HTML-formatted label with the given content.
084     *
085     * @param html the label content
086     * @return Label
087     */
088    public Label htmlLabel(String html) {
089
090        Label label = new Label();
091        label.setContentMode(ContentMode.HTML);
092        label.setValue(html);
093        return label;
094
095    }
096
097    /**
098     * Inits the dialog.<p>
099     * (The constructor is empty)
100     *
101     * @param ui UI
102     * @return true if dialog should be displayed
103     */
104    public abstract boolean init(CmsUpdateUI ui);
105
106    /**
107     * Init called from implementations of this class.<p>
108     *
109     * @param ui ui
110     * @param hasPrev has preview dialog
111     * @param hasNext has next dialog
112     */
113    public void init(CmsUpdateUI ui, boolean hasPrev, boolean hasNext) {
114
115        m_ui = ui;
116
117        if (hasPrev) {
118            m_back = getBackButton();
119            m_back.addClickListener(new ClickListener() {
120
121                private static final long serialVersionUID = 1L;
122
123                public void buttonClick(ClickEvent event) {
124
125                    ui.displayDialog(getPreviousDialog());
126                }
127
128            });
129            addButton(m_back, true);
130        }
131
132        if (hasNext) {
133            m_ok = getOkButton();
134            m_ok.addClickListener(new ClickListener() {
135
136                private static final long serialVersionUID = 1L;
137
138                public void buttonClick(ClickEvent event) {
139
140                    if (submitDialog()) {
141                        ui.displayDialog(getNextDialog());
142                    }
143                }
144
145            });
146            addButton(m_ok, true);
147        }
148    }
149
150    /**
151     * Reads an HTML snipped with the given name.
152     * @param name name of file
153     *
154     * @return the HTML data
155     */
156    public String readSnippet(String name) {
157
158        String path = CmsStringUtil.joinPaths(
159            m_ui.getUpdateBean().getWebAppRfsPath(),
160            CmsUpdateBean.FOLDER_UPDATE,
161            "html",
162            name);
163        try (InputStream stream = new FileInputStream(path)) {
164            byte[] data = CmsFileUtil.readFully(stream, false);
165            String result = new String(data, "UTF-8");
166            return result;
167        } catch (Exception e) {
168            throw new RuntimeException(e);
169        }
170    }
171
172    /**
173     * En / Disables the continue button.<p>
174     *
175     * @param enable boolean
176     */
177    protected void enableOK(boolean enable) {
178
179        m_ok.setEnabled(enable);
180    }
181
182    /**
183     * Submit method.<p>
184     *
185     * @return true if next dialog should be loaded
186     */
187    protected boolean submitDialog() {
188
189        return true;
190    }
191
192    /**
193     * Returns next Dialog (not initialized).<p>
194     *
195     * @return A_CmsUpdateDialog
196     */
197    abstract A_CmsUpdateDialog getNextDialog();
198
199    /**
200     * Returns previous Dialog (not initialized).<p>
201     *
202     * @return A_CmsUpdateDialog
203     */
204    abstract A_CmsUpdateDialog getPreviousDialog();
205
206}