com.openhtmltopdf.simple.extend.XhtmlForm Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openhtmltopdf-core Show documentation
Show all versions of openhtmltopdf-core Show documentation
Open HTML to PDF is a CSS 2.1 renderer written in Java. This artifact contains the core rendering and layout code.
/*
* {{{ header & license
* Copyright (c) 2004, 2005 Torbjoern Gannholm
* Copyright (c) 2007 Sean Bright
*
* This program 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 program 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.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* }}}
*/
package com.openhtmltopdf.simple.extend;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JRadioButton;
import com.openhtmltopdf.util.LogMessageId;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import com.openhtmltopdf.extend.UserAgentCallback;
import com.openhtmltopdf.layout.LayoutContext;
import com.openhtmltopdf.render.BlockBox;
import com.openhtmltopdf.simple.extend.form.FormField;
import com.openhtmltopdf.simple.extend.form.FormFieldFactory;
import com.openhtmltopdf.util.XRLog;
/**
* Represents a form object
*
* @author Torbjoern Gannholm
* @author Sean Bright
*/
public class XhtmlForm {
private static final String FS_DEFAULT_GROUP = "__fs_default_group_";
private static int _defaultGroupCount = 1;
private UserAgentCallback _userAgentCallback;
private Map _componentCache;
private Map _buttonGroups;
private Element _parentFormElement;
private FormSubmissionListener _formSubmissionListener;
public XhtmlForm(UserAgentCallback uac, Element e, FormSubmissionListener fsListener) {
_userAgentCallback = uac;
_buttonGroups = new HashMap();
_componentCache = new LinkedHashMap();
_parentFormElement = e;
_formSubmissionListener = fsListener;
}
public XhtmlForm(UserAgentCallback uac, Element e) {
this(uac, e, new DefaultFormSubmissionListener());
}
public UserAgentCallback getUserAgentCallback() {
return _userAgentCallback;
}
public void addButtonToGroup(String groupName, AbstractButton button) {
if (groupName == null) {
groupName = createNewDefaultGroupName();
}
ButtonGroupWrapper group = (ButtonGroupWrapper) _buttonGroups.get(groupName);
if (group == null) {
group = new ButtonGroupWrapper();
_buttonGroups.put(groupName, group);
}
group.add(button);
}
private static String createNewDefaultGroupName() {
return FS_DEFAULT_GROUP + ++_defaultGroupCount;
}
private static boolean isFormField(Element e) {
String nodeName = e.getNodeName();
return nodeName.equals("input") || nodeName.equals("select") || nodeName.equals("textarea");
}
public FormField addComponent(Element e, LayoutContext context, BlockBox box) {
FormField field = null;
if (_componentCache.containsKey(e)) {
field = (FormField) _componentCache.get(e);
} else {
if (!isFormField(e)) {
return null;
}
field = FormFieldFactory.create(this, context, box);
if (field == null) {
XRLog.log(Level.INFO, LogMessageId.LogMessageId1Param.LAYOUT_UNKNOWN_FIELD_TYPE, e.getNodeName());
return null;
}
_componentCache.put(e, field);
}
return field;
}
public void reset() {
Iterator buttonGroups = _buttonGroups.values().iterator();
while (buttonGroups.hasNext()) {
((ButtonGroupWrapper) buttonGroups.next()).clearSelection();
}
Iterator fields = _componentCache.values().iterator();
while (fields.hasNext()) {
((FormField) fields.next()).reset();
}
}
public static String collectText(Element e) {
StringBuilder result = new StringBuilder();
Node node = e.getFirstChild();
if (node != null) {
do {
short nodeType = node.getNodeType();
if (nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE) {
Text text = (Text) node;
result.append(text.getData());
}
} while ((node = node.getNextSibling()) != null);
}
return result.toString().trim();
}
public void submit(JComponent source) {
// If we don't have a