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