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 GmbH & Co. KG, 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.workplace.list;
029
030import java.io.IOException;
031
032import javax.servlet.ServletException;
033import javax.servlet.jsp.JspException;
034import javax.servlet.jsp.JspWriter;
035
036/**
037 * Helper class for managing two lists on the same dialog.<p>
038 *
039 * @since 6.0.0
040 */
041public class CmsTwoListsDialog {
042
043    /** the workplace instance for the active list. */
044    private A_CmsListDialog m_activeWp;
045
046    /** the workplace instance for the first list. */
047    private A_CmsListDialog m_firstWp;
048
049    /** the workplace instance for the passive list. */
050    private A_CmsListDialog m_passiveWp;
051
052    /** the workplace instance for the second list. */
053    private A_CmsListDialog m_secondWp;
054
055    /**
056     * Default constructor.<p>
057     *
058     * @param wp1 the workplace instance for the first list
059     * @param wp2 the workplace instance for the second list
060     */
061    public CmsTwoListsDialog(A_CmsListDialog wp1, A_CmsListDialog wp2) {
062
063        m_activeWp = (wp1.isActive() ? wp1 : wp2);
064        m_passiveWp = (!wp1.isActive() ? wp1 : wp2);
065        m_firstWp = wp1;
066        m_secondWp = wp2;
067    }
068
069    /**
070     * Generates the dialog starting html code.<p>
071     *
072     * @return html code
073     */
074    protected String defaultActionHtml() {
075
076        StringBuffer result = new StringBuffer(2048);
077        result.append(defaultActionHtmlStart());
078        result.append(defaultActionHtmlContent());
079        result.append(defaultActionHtmlEnd());
080        return result.toString();
081    }
082
083    /**
084     * Returns the html code for the default action content.<p>
085     *
086     * @return html code
087     */
088    protected String defaultActionHtmlContent() {
089
090        StringBuffer result = new StringBuffer(2048);
091        result.append("<table id='twolists' cellpadding='0' cellspacing='0' align='center' width='100%'>\n");
092        result.append("\t<tr>\n");
093        result.append("\t\t<td width='50%' valign='top'>\n");
094        result.append("\t\t\t").append(m_firstWp.defaultActionHtmlContent()).append("\n");
095        result.append("\t\t</td>\n");
096        result.append("\t\t<td width='20'>&nbsp;</td>");
097        result.append("\t\t<td width='50%' valign='top'>\n");
098        result.append("\t\t\t").append(m_secondWp.defaultActionHtmlContent()).append("\n");
099        result.append("\t\t</td>\n");
100        result.append("\t</tr>\n");
101        result.append("</table>\n");
102        return result.toString();
103    }
104
105    /**
106     * Generates the dialog ending html code.<p>
107     *
108     * @return html code
109     */
110    protected String defaultActionHtmlEnd() {
111
112        return m_activeWp.defaultActionHtmlEnd();
113    }
114
115    /**
116     * Generates the dialog starting html code.<p>
117     *
118     * @return html code
119     */
120    protected String defaultActionHtmlStart() {
121
122        return m_activeWp.defaultActionHtmlStart();
123    }
124
125    /**
126     * Display method for two list dialogs.<p>
127     *
128     * @throws JspException if dialog actions fail
129     * @throws IOException if writing to the JSP out fails, or in case of errors forwarding to the required result page
130     * @throws ServletException in case of errors forwarding to the required result page
131     */
132    public void displayDialog() throws JspException, IOException, ServletException {
133
134        displayDialog(false);
135    }
136
137    /**
138     * Writes the dialog html code, only if the <code>{@link org.opencms.workplace.CmsDialog#ACTION_DEFAULT}</code> is set.<p>
139     *
140     * @throws IOException if writing to the JSP out fails, or in case of errors forwarding to the required result page
141     */
142    public void writeDialog() throws IOException {
143
144        if (m_activeWp.isForwarded() || m_passiveWp.isForwarded()) {
145            return;
146        }
147
148        JspWriter out = m_activeWp.getJsp().getJspContext().getOut();
149        out.print(defaultActionHtml());
150    }
151
152    /**
153     * Display method for two list dialogs, executes actions, but only displays if needed.<p>
154     *
155     * @param writeLater if <code>true</code> no output is written,
156     *                   you have to call manually the <code>{@link #defaultActionHtml()}</code> method.
157     *
158     * @throws JspException if dialog actions fail
159     * @throws IOException if writing to the JSP out fails, or in case of errors forwarding to the required result page
160     * @throws ServletException in case of errors forwarding to the required result page
161     */
162    public void displayDialog(boolean writeLater) throws JspException, IOException, ServletException {
163
164        // perform the active list actions
165        m_activeWp.actionDialog();
166        if (m_activeWp.isForwarded()) {
167            return;
168        }
169
170        m_activeWp.refreshList();
171        m_passiveWp.refreshList();
172
173        if (writeLater) {
174            return;
175        }
176        JspWriter out = m_activeWp.getJsp().getJspContext().getOut();
177        out.print(defaultActionHtml());
178    }
179
180    /**
181     * Returns the workplace instance of the active list.<p>
182     *
183     * @return the workplace instance of the active list
184     */
185    public A_CmsListDialog getActiveWp() {
186
187        return m_activeWp;
188    }
189
190    /**
191     * Returns the workplace instance of the first list.<p>
192     *
193     * @return the workplace instance of the first list
194     */
195    public A_CmsListDialog getFirstWp() {
196
197        return m_firstWp;
198    }
199
200    /**
201     * Returns the workplace instance of the passive list.<p>
202     *
203     * @return the workplace instance of the passive list
204     */
205    public A_CmsListDialog getPassiveWp() {
206
207        return m_passiveWp;
208    }
209
210    /**
211     * Returns the workplace instance of the second list.<p>
212     *
213     * @return the workplace instance of the second list
214     */
215    public A_CmsListDialog getSecondWp() {
216
217        return m_secondWp;
218    }
219}