📄 cmswidgetdialog.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsWidgetDialog.java,v $
* Date : $Date: 2006/07/20 12:11:04 $
* Version: $Revision: 1.61 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.workplace;
import org.opencms.i18n.CmsEncoder;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.I_CmsThrowable;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.A_CmsWidget;
import org.opencms.widgets.CmsDisplayWidget;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.widgets.I_CmsWidgetDialog;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
/**
* Base class for dialogs that use the OpenCms widgets without XML content.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.61 $
*
* @since 6.0.0
*/
public abstract class CmsWidgetDialog extends CmsDialog implements I_CmsWidgetDialog {
/** Action for optional element creation. */
public static final int ACTION_ELEMENT_ADD = 152;
/** Action for optional element removal. */
public static final int ACTION_ELEMENT_REMOVE = 153;
/** Value for the action: error in the form validation. */
public static final int ACTION_ERROR = 303;
/** Value for the action: save the dialog. */
public static final int ACTION_SAVE = 300;
/** Request parameter value for the action: save the dialog. */
public static final String DIALOG_SAVE = "save";
/** Indicates an optional element should be created. */
public static final String EDITOR_ACTION_ELEMENT_ADD = "addelement";
/** Indicates an optional element should be removed. */
public static final String EDITOR_ACTION_ELEMENT_REMOVE = "removeelement";
/** Prefix for "hidden" parameters, required since these must be unescaped later. */
public static final String HIDDEN_PARAM_PREFIX = "hidden.";
/** The errors thrown by commit actions. */
protected List m_commitErrors;
/** The object edited with this widget dialog. */
protected Object m_dialogObject;
/** The allowed pages for this dialog in a List. */
protected List m_pages;
/** Controls which page is currently displayed in the dialog. */
protected String m_paramPage;
/** The validation errors for the input form. */
protected List m_validationErrorList;
/** Contains all parameter value of this dialog. */
protected Map m_widgetParamValues;
/** The list of widgets used on the dialog. */
protected List m_widgets;
/** The set of help message ids that have already been used. */
private Set m_helpMessageIds;
/**
* Parameter stores the index of the element to add or remove.<p>
*
* This must not be <code>null</code>, because it must be available
* when calling <code>{@link org.opencms.workplace.CmsWorkplace#paramsAsHidden()}</code>.<p>
*/
private String m_paramElementIndex = "0";
/**
* Parameter stores the name of the element to add or remove.<p>
*
* This must not be <code>null</code>, because it must be available
* when calling <code>{@link org.opencms.workplace.CmsWorkplace#paramsAsHidden()}</code>.<p>
*/
private String m_paramElementName = "undefined";
/** Optional localized key prefix identificator. */
private String m_prefix;
/**
* Public constructor with JSP action element.<p>
*
* @param jsp an initialized JSP action element
*/
public CmsWidgetDialog(CmsJspActionElement jsp) {
super(jsp);
}
/**
* Public constructor with JSP variables.<p>
*
* @param context the JSP page context
* @param req the JSP request
* @param res the JSP response
*/
public CmsWidgetDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
this(new CmsJspActionElement(context, req, res));
}
/**
* Deletes the edited dialog object from the session.<p>
*/
public void actionCancel() {
clearDialogObject();
}
/**
* Commits the edited object after pressing the "OK" button.<p>
*
* @throws IOException in case of errros forwarding to the required result page
* @throws ServletException in case of errros forwarding to the required result page
*/
public abstract void actionCommit() throws IOException, ServletException;
/**
* Adds or removes an optional element.<p>
*
* Depends on the value stored in the <code>{@link CmsDialog#getAction()}</code> method.<p>
*/
public void actionToggleElement() {
// get the necessary parameters to add/remove the element
int index = 0;
try {
index = Integer.parseInt(getParamElementIndex());
} catch (Exception e) {
// ignore, should not happen
}
String name = getParamElementName();
// get the base parameter definition
CmsWidgetDialogParameter base = getParameterDefinition(name);
if (base != null) {
// the requested parameter is valid for this dialog
List params = (List)getParameters().get(name);
if (getAction() == ACTION_ELEMENT_REMOVE) {
// remove the value
params.remove(index);
} else {
List sequence = (List)getParameters().get(base.getName());
if (sequence.size() > 0) {
// add the new value after the clicked element
index = index + 1;
}
CmsWidgetDialogParameter newParam = new CmsWidgetDialogParameter(base, index);
params.add(index, newParam);
}
// reset all index value in the parameter list
for (int i = 0; i < params.size(); i++) {
CmsWidgetDialogParameter param = (CmsWidgetDialogParameter)params.get(i);
param.setindex(i);
}
}
}
/**
* Returns the html for a button to add an optional element.<p>
*
* @param elementName name of the element
* @param insertAfter the index of the element after which the new element should be created
* @param enabled if true, the button to add an element is shown, otherwise a spacer is returned
* @return the html for a button to add an optional element
*/
public String buildAddElement(String elementName, int insertAfter, boolean enabled) {
if (enabled) {
StringBuffer href = new StringBuffer(4);
href.append("javascript:addElement('");
href.append(elementName);
href.append("', ");
href.append(insertAfter);
href.append(");");
return button(href.toString(), null, "new.png", Messages.GUI_DIALOG_BUTTON_ADDNEW_0, 0);
} else {
return "";
}
}
/**
* Builds the HTML for the dialog form.<p>
*
* @return the HTML for the dialog form
*/
public String buildDialogForm() {
// create the dialog HTML
return createDialogHtml(getParamPage());
}
/**
* Returns the html for a button to remove an optional element.<p>
*
* @param elementName name of the element
* @param index the element index of the element to remove
* @param enabled if true, the button to remove an element is shown, otherwise a spacer is returned
* @return the html for a button to remove an optional element
*/
public String buildRemoveElement(String elementName, int index, boolean enabled) {
if (enabled) {
StringBuffer href = new StringBuffer(4);
href.append("javascript:removeElement('");
href.append(elementName);
href.append("', ");
href.append(index);
href.append(");");
return button(href.toString(), null, "deletecontent.png", Messages.GUI_DIALOG_BUTTON_DELETE_0, 0);
} else {
return "";
}
}
/**
* Clears the "dialog object" for this widget dialog by removing it from the current users session.<p>
*/
public void clearDialogObject() {
setDialogObject(null);
}
/**
* Builds the end HTML for a block with 3D border in the dialog content area.<p>
*
* @return 3D block start / end segment
*/
public String dialogBlockEnd() {
StringBuffer result = new StringBuffer(8);
result.append(super.dialogBlockEnd());
result.append(dialogSpacer());
result.append("</td></tr>\n");
return result.toString();
}
/**
* Builds the start HTML for a block with 3D border and optional subheadline in the dialog content area.<p>
*
* @param headline the headline String for the block
* @return 3D block start / end segment
*/
public String dialogBlockStart(String headline) {
StringBuffer result = new StringBuffer(8);
result.append("<tr><td colspan=\"5\">\n");
result.append(super.dialogBlockStart(headline));
return result.toString();
}
/**
* Creates the HTML for the buttons on the dialog.<p>
*
* @return the HTML for the buttons on the dialog.<p>
*/
public String dialogButtonsCustom() {
if (getPages().size() > 1) {
// this is a multi page dialog, create buttons according to current page
int pageIndex = getPages().indexOf(getParamPage());
if (pageIndex == getPages().size() - 1) {
// this is the last dialog page
return dialogButtons(new int[] {BUTTON_OK, BUTTON_BACK, BUTTON_CANCEL}, new String[3]);
} else if (pageIndex > 0) {
// this is a dialog page between first and last page
return dialogButtons(new int[] {BUTTON_BACK, BUTTON_CONTINUE, BUTTON_CANCEL}, new String[3]);
} else {
// this is the first dialog page
return dialogButtons(new int[] {BUTTON_CONTINUE, BUTTON_CANCEL}, new String[2]);
}
}
boolean onlyDisplay = true;
Iterator it = m_widgets.iterator();
while (it.hasNext()) {
CmsWidgetDialogParameter wdp = (CmsWidgetDialogParameter)it.next();
if (!(wdp.getWidget() instanceof CmsDisplayWidget)) {
onlyDisplay = false;
break;
}
}
if (!onlyDisplay) {
// this is a single page dialog, create common buttons
return dialogButtons(new int[] {BUTTON_OK, BUTTON_CANCEL}, new String[2]);
}
// this is a display only dialog
return "";
}
/**
* Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
*
* @throws JspException if dialog actions fail
* @throws IOException if writing to the JSP out fails, or in case of errros forwarding to the required result page
* @throws ServletException in case of errros forwarding to the required result page
*/
public void displayDialog() throws JspException, IOException, ServletException {
displayDialog(false);
}
/**
* Performs the dialog actions depending on the initialized action and displays the dialog form if needed.<p>
*
* @param writeLater if <code>true</code> no output is written,
* you have to call manually the <code>{@link #defaultActionHtml()}</code> method.
*
* @throws JspException if dialog actions fail
* @throws IOException if writing to the JSP out fails, or in case of errros forwarding to the required result page
* @throws ServletException in case of errros forwarding to the required result page
*/
public void displayDialog(boolean writeLater) throws JspException, IOException, ServletException {
if (isForwarded()) {
return;
}
switch (getAction()) {
case ACTION_CANCEL:
// ACTION: cancel button pressed
actionCancel();
actionCloseDialog();
break;
case ACTION_ERROR:
// ACTION: an error occured (display nothing)
break;
case ACTION_SAVE:
// ACTION: save edited values
setParamAction(DIALOG_OK);
actionCommit();
if (closeDialogOnCommit()) {
setAction(ACTION_CANCEL);
actionCloseDialog();
break;
}
setAction(ACTION_DEFAULT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -