001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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 jakarta.servlet.ServletException;
033import jakarta.servlet.jsp.JspException;
034import jakarta.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     * Display method for two list dialogs.<p>
071     *
072     * @throws JspException if dialog actions fail
073     * @throws IOException if writing to the JSP out fails, or in case of errors forwarding to the required result page
074     * @throws ServletException in case of errors forwarding to the required result page
075     */
076    public void displayDialog() throws JspException, IOException, ServletException {
077
078        displayDialog(false);
079    }
080
081    /**
082     * Display method for two list dialogs, executes actions, but only displays if needed.<p>
083     *
084     * @param writeLater if <code>true</code> no output is written,
085     *                   you have to call manually the <code>{@link #defaultActionHtml()}</code> method.
086     *
087     * @throws JspException if dialog actions fail
088     * @throws IOException if writing to the JSP out fails, or in case of errors forwarding to the required result page
089     * @throws ServletException in case of errors forwarding to the required result page
090     */
091    public void displayDialog(boolean writeLater) throws JspException, IOException, ServletException {
092
093        // perform the active list actions
094        m_activeWp.actionDialog();
095        if (m_activeWp.isForwarded()) {
096            return;
097        }
098
099        m_activeWp.refreshList();
100        m_passiveWp.refreshList();
101
102        if (writeLater) {
103            return;
104        }
105        JspWriter out = m_activeWp.getJsp().getJspContext().getOut();
106        out.print(defaultActionHtml());
107    }
108
109    /**
110     * Returns the workplace instance of the active list.<p>
111     *
112     * @return the workplace instance of the active list
113     */
114    public A_CmsListDialog getActiveWp() {
115
116        return m_activeWp;
117    }
118
119    /**
120     * Returns the workplace instance of the first list.<p>
121     *
122     * @return the workplace instance of the first list
123     */
124    public A_CmsListDialog getFirstWp() {
125
126        return m_firstWp;
127    }
128
129    /**
130     * Returns the workplace instance of the passive list.<p>
131     *
132     * @return the workplace instance of the passive list
133     */
134    public A_CmsListDialog getPassiveWp() {
135
136        return m_passiveWp;
137    }
138
139    /**
140     * Returns the workplace instance of the second list.<p>
141     *
142     * @return the workplace instance of the second list
143     */
144    public A_CmsListDialog getSecondWp() {
145
146        return m_secondWp;
147    }
148
149    /**
150     * Writes the dialog html code, only if the <code>{@link org.opencms.workplace.CmsDialog#ACTION_DEFAULT}</code> is set.<p>
151     *
152     * @throws IOException if writing to the JSP out fails, or in case of errors forwarding to the required result page
153     */
154    public void writeDialog() throws IOException {
155
156        if (m_activeWp.isForwarded() || m_passiveWp.isForwarded()) {
157            return;
158        }
159
160        JspWriter out = m_activeWp.getJsp().getJspContext().getOut();
161        out.print(defaultActionHtml());
162    }
163
164    /**
165     * Generates the dialog starting html code.<p>
166     *
167     * @return html code
168     */
169    protected String defaultActionHtml() {
170
171        StringBuffer result = new StringBuffer(2048);
172        result.append(defaultActionHtmlStart());
173        result.append(defaultActionHtmlContent());
174        result.append(defaultActionHtmlEnd());
175        return result.toString();
176    }
177
178    /**
179     * Returns the html code for the default action content.<p>
180     *
181     * @return html code
182     */
183    protected String defaultActionHtmlContent() {
184
185        StringBuffer result = new StringBuffer(2048);
186        result.append("<table id='twolists' cellpadding='0' cellspacing='0' align='center' width='100%'>\n");
187        result.append("\t<tr>\n");
188        result.append("\t\t<td width='50%' valign='top'>\n");
189        result.append("\t\t\t").append(m_firstWp.defaultActionHtmlContent()).append("\n");
190        result.append("\t\t</td>\n");
191        result.append("\t\t<td width='20'>&nbsp;</td>");
192        result.append("\t\t<td width='50%' valign='top'>\n");
193        result.append("\t\t\t").append(m_secondWp.defaultActionHtmlContent()).append("\n");
194        result.append("\t\t</td>\n");
195        result.append("\t</tr>\n");
196        result.append("</table>\n");
197        return result.toString();
198    }
199
200    /**
201     * Generates the dialog ending html code.<p>
202     *
203     * @return html code
204     */
205    protected String defaultActionHtmlEnd() {
206
207        return m_activeWp.defaultActionHtmlEnd();
208    }
209
210    /**
211     * Generates the dialog starting html code.<p>
212     *
213     * @return html code
214     */
215    protected String defaultActionHtmlStart() {
216
217        return m_activeWp.defaultActionHtmlStart();
218    }
219}