FormViewpublic class FormView extends ComponentView implements ActionListenerComponent decorator that implements the view interface
for form elements, <input>, <textarea>,
and <select>. The model for the component is stored
as an attribute of the the element (using StyleConstants.ModelAttribute),
and is used to build the component of the view. The type
of the model is assumed to of the type that would be set by
HTMLDocument.HTMLReader.FormAction . If there are
multiple views mapped over the document, they will share the
embedded component models.
The following table shows what components get built
by this view.
Element Type |
Component built |
input, type button |
JButton |
input, type checkbox |
JCheckBox |
input, type image |
JButton |
input, type password |
JPasswordField |
input, type radio |
JRadioButton |
input, type reset |
JButton |
input, type submit |
JButton |
input, type text |
JTextField |
select, size > 1 or multiple attribute defined |
JList in a JScrollPane |
select, size unspecified or 1 |
JComboBox |
textarea |
JTextArea in a JScrollPane |
input, type file |
JTextField |
|
Fields Summary |
---|
public static final String | SUBMITIf a value attribute is not specified for a FORM input element
of type "submit", then this default string is used. | public static final String | RESETIf a value attribute is not specified for a FORM input element
of type "reset", then this default string is used. | private short | maxIsPreferredUsed to indicate if the maximum span should be the same as the
preferred span. This is used so that the Component's size doesn't
change if there is extra room on a line. The first bit is used for
the X direction, and the second for the y direction. |
Constructors Summary |
---|
public FormView(Element elem)Creates a new FormView object.
super(elem);
|
Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent evt)Responsible for processeing the ActionEvent.
If the element associated with the FormView,
has a type of "submit", "reset", "text" or "password"
then the action is processed. In the case of a "submit"
the form is submitted. In the case of a "reset"
the form is reset to its original state.
In the case of "text" or "password", if the
element is the last one of type "text" or "password",
the form is submitted. Otherwise, focus is transferred
to the next component in the form.
Element element = getElement();
StringBuffer dataBuffer = new StringBuffer();
HTMLDocument doc = (HTMLDocument)getDocument();
AttributeSet attr = element.getAttributes();
String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
if (type.equals("submit")) {
getFormData(dataBuffer);
submitData(dataBuffer.toString());
} else if (type.equals("reset")) {
resetForm();
} else if (type.equals("text") || type.equals("password")) {
if (isLastTextOrPasswordField()) {
getFormData(dataBuffer);
submitData(dataBuffer.toString());
} else {
getComponent().transferFocus();
}
}
| private void | appendBuffer(java.lang.StringBuffer buffer, java.lang.String name, java.lang.String value)Appends name / value pairs into the
buffer. Both names and values are encoded using the
URLEncoder.encode() method before being added to the
buffer.
if (buffer.length() > 0) {
buffer.append('&");
}
String encodedName = URLEncoder.encode(name);
buffer.append(encodedName);
buffer.append('=");
String encodedValue = URLEncoder.encode(value);
buffer.append(encodedValue);
| protected java.awt.Component | createComponent()Create the component. This is basically a
big switch statement based upon the tag type
and html attributes of the associated element.
AttributeSet attr = getElement().getAttributes();
HTML.Tag t = (HTML.Tag)
attr.getAttribute(StyleConstants.NameAttribute);
JComponent c = null;
Object model = attr.getAttribute(StyleConstants.ModelAttribute);
if (t == HTML.Tag.INPUT) {
c = createInputComponent(attr, model);
} else if (t == HTML.Tag.SELECT) {
if (model instanceof OptionListModel) {
JList list = new JList((ListModel) model);
int size = HTML.getIntegerAttributeValue(attr,
HTML.Attribute.SIZE,
1);
list.setVisibleRowCount(size);
list.setSelectionModel((ListSelectionModel)model);
c = new JScrollPane(list);
} else {
c = new JComboBox((ComboBoxModel) model);
maxIsPreferred = 3;
}
} else if (t == HTML.Tag.TEXTAREA) {
JTextArea area = new JTextArea((Document) model);
int rows = HTML.getIntegerAttributeValue(attr,
HTML.Attribute.ROWS,
1);
area.setRows(rows);
int cols = HTML.getIntegerAttributeValue(attr,
HTML.Attribute.COLS,
20);
maxIsPreferred = 3;
area.setColumns(cols);
c = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
}
if (c != null) {
c.setAlignmentY(1.0f);
}
return c;
| private javax.swing.JComponent | createInputComponent(javax.swing.text.AttributeSet attr, java.lang.Object model)Creates a component for an <INPUT> element based on the
value of the "type" attribute.
JComponent c = null;
String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
if (type.equals("submit") || type.equals("reset")) {
String value = (String)
attr.getAttribute(HTML.Attribute.VALUE);
if (value == null) {
if (type.equals("submit")) {
value = UIManager.getString("FormView.submitButtonText");
} else {
value = UIManager.getString("FormView.resetButtonText");
}
}
JButton button = new JButton(value);
if (model != null) {
button.setModel((ButtonModel)model);
button.addActionListener(this);
}
c = button;
maxIsPreferred = 3;
} else if (type.equals("image")) {
String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC);
JButton button;
try {
URL base = ((HTMLDocument)getElement().getDocument()).getBase();
URL srcURL = new URL(base, srcAtt);
Icon icon = new ImageIcon(srcURL);
button = new JButton(icon);
} catch (MalformedURLException e) {
button = new JButton(srcAtt);
}
if (model != null) {
button.setModel((ButtonModel)model);
button.addMouseListener(new MouseEventListener());
}
c = button;
maxIsPreferred = 3;
} else if (type.equals("checkbox")) {
c = new JCheckBox();
if (model != null) {
((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model);
}
maxIsPreferred = 3;
} else if (type.equals("radio")) {
c = new JRadioButton();
if (model != null) {
((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model);
}
maxIsPreferred = 3;
} else if (type.equals("text")) {
int size = HTML.getIntegerAttributeValue(attr,
HTML.Attribute.SIZE,
-1);
JTextField field;
if (size > 0) {
field = new JTextField();
field.setColumns(size);
}
else {
field = new JTextField();
field.setColumns(20);
}
c = field;
if (model != null) {
field.setDocument((Document) model);
}
field.addActionListener(this);
maxIsPreferred = 3;
} else if (type.equals("password")) {
JPasswordField field = new JPasswordField();
c = field;
if (model != null) {
field.setDocument((Document) model);
}
int size = HTML.getIntegerAttributeValue(attr,
HTML.Attribute.SIZE,
-1);
field.setColumns((size > 0) ? size : 20);
field.addActionListener(this);
maxIsPreferred = 3;
} else if (type.equals("file")) {
JTextField field = new JTextField();
if (model != null) {
field.setDocument((Document)model);
}
int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE,
-1);
field.setColumns((size > 0) ? size : 20);
JButton browseButton = new JButton(UIManager.getString
("FormView.browseFileButtonText"));
Box box = Box.createHorizontalBox();
box.add(field);
box.add(Box.createHorizontalStrut(5));
box.add(browseButton);
browseButton.addActionListener(new BrowseFileAction(
attr, (Document)model));
c = box;
maxIsPreferred = 3;
}
return c;
| void | getFormData(java.lang.StringBuffer buffer)Iterates over the
element hierarchy, extracting data from the
models associated with the relevant form elements.
"Relevant" means the form elements that are part
of the same form whose element triggered the submit
action.
Element formE = getFormElement();
if (formE != null) {
ElementIterator it = new ElementIterator(formE);
Element next;
while ((next = it.next()) != null) {
if (isControl(next)) {
String type = (String)next.getAttributes().getAttribute
(HTML.Attribute.TYPE);
if (type != null && type.equals("submit") &&
next != getElement()) {
// do nothing - this submit isnt the trigger
} else if (type == null || !type.equals("image")) {
// images only result in data if they triggered
// the submit and they require that the mouse click
// coords be appended to the data. Hence its
// processing is handled by the view.
loadElementDataIntoBuffer(next, buffer);
}
}
}
}
| private javax.swing.text.Element | getFormElement()Returns the Element representing the FORM .
Element elem = getElement();
while (elem != null) {
if (elem.getAttributes().getAttribute
(StyleConstants.NameAttribute) == HTML.Tag.FORM) {
return elem;
}
elem = elem.getParentElement();
}
return null;
| private java.lang.String | getImageData(java.awt.Point point)Extracts the value of the name attribute
associated with the input element of type
image. If name is defined it is encoded using
the URLEncoder.encode() method and the
image data is returned in the following format:
name + ".x" +"="+ x +"&"+ name +".y"+"="+ y
otherwise,
"x="+ x +"&y="+ y
String mouseCoords = point.x + ":" + point.y;
int sep = mouseCoords.indexOf(':");
String x = mouseCoords.substring(0, sep);
String y = mouseCoords.substring(++sep);
String name = (String) getElement().getAttributes().getAttribute(HTML.Attribute.NAME);
String data;
if (name == null || name.equals("")) {
data = "x="+ x +"&y="+ y;
} else {
name = URLEncoder.encode(name);
data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y;
}
return data;
| private java.lang.String | getInputElementData(javax.swing.text.AttributeSet attr)Returns the data associated with an <INPUT> form
element. The value of "type" attributes is
used to determine the type of the model associated
with the element and then the relevant data is
extracted.
Object model = attr.getAttribute(StyleConstants.ModelAttribute);
String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
String value = null;
if (type.equals("text") || type.equals("password")) {
Document doc = (Document)model;
try {
value = doc.getText(0, doc.getLength());
} catch (BadLocationException e) {
value = null;
}
} else if (type.equals("submit") || type.equals("hidden")) {
value = (String) attr.getAttribute(HTML.Attribute.VALUE);
if (value == null) {
value = "";
}
} else if (type.equals("radio") || type.equals("checkbox")) {
ButtonModel m = (ButtonModel)model;
if (m.isSelected()) {
value = (String) attr.getAttribute(HTML.Attribute.VALUE);
if (value == null) {
value = "on";
}
}
} else if (type.equals("file")) {
Document doc = (Document)model;
String path;
try {
path = doc.getText(0, doc.getLength());
} catch (BadLocationException e) {
path = null;
}
if (path != null && path.length() > 0) {
value = path;
/*
try {
Reader reader = new BufferedReader(new FileReader(path));
StringBuffer buffer = new StringBuffer();
char[] cBuff = new char[1024];
int read;
try {
while ((read = reader.read(cBuff)) != -1) {
buffer.append(cBuff, 0, read);
}
} catch (IOException ioe) {
buffer = null;
}
try {
reader.close();
} catch (IOException ioe) {}
if (buffer != null) {
value = buffer.toString();
}
} catch (IOException ioe) {}
*/
}
}
return value;
| public float | getMaximumSpan(int axis)Determines the maximum span for this view along an
axis. For certain components, the maximum and preferred span are the
same. For others this will return the value
returned by Component.getMaximumSize along the
axis of interest.
switch (axis) {
case View.X_AXIS:
if ((maxIsPreferred & 1) == 1) {
super.getMaximumSpan(axis);
return getPreferredSpan(axis);
}
return super.getMaximumSpan(axis);
case View.Y_AXIS:
if ((maxIsPreferred & 2) == 2) {
super.getMaximumSpan(axis);
return getPreferredSpan(axis);
}
return super.getMaximumSpan(axis);
default:
break;
}
return super.getMaximumSpan(axis);
| private java.lang.String | getTextAreaData(javax.swing.text.AttributeSet attr)Returns the data associated with the <TEXTAREA> form
element. This is done by getting the text stored in the
Document model.
Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);
try {
return doc.getText(0, doc.getLength());
} catch (BadLocationException e) {
return null;
}
| protected void | imageSubmit(java.lang.String imageData)This method is called to submit a form in response
to a click on an image -- an <INPUT> form
element of type "image".
StringBuffer dataBuffer = new StringBuffer();
Element elem = getElement();
HTMLDocument hdoc = (HTMLDocument)elem.getDocument();
getFormData(dataBuffer);
if (dataBuffer.length() > 0) {
dataBuffer.append('&");
}
dataBuffer.append(imageData);
submitData(dataBuffer.toString());
return;
| private boolean | isControl(javax.swing.text.Element elem)Returns true if the Element elem represents a control.
return elem.isLeaf();
| boolean | isLastTextOrPasswordField()Iterates over the element hierarchy to determine if
the element parameter, which is assumed to be an
<INPUT> element of type password or text, is the last
one of either kind, in the form to which it belongs.
Element parent = getFormElement();
Element elem = getElement();
if (parent != null) {
ElementIterator it = new ElementIterator(parent);
Element next;
boolean found = false;
while ((next = it.next()) != null) {
if (next == elem) {
found = true;
}
else if (found && isControl(next)) {
AttributeSet elemAttr = next.getAttributes();
if (HTMLDocument.matchNameAttribute
(elemAttr, HTML.Tag.INPUT)) {
String type = (String)elemAttr.getAttribute
(HTML.Attribute.TYPE);
if ("text".equals(type) || "password".equals(type)) {
return false;
}
}
}
}
}
return true;
| private void | loadElementDataIntoBuffer(javax.swing.text.Element elem, java.lang.StringBuffer buffer)Loads the data
associated with the element into the buffer.
The format in which data is appended depends
on the type of the form element. Essentially
data is loaded in name/value pairs.
AttributeSet attr = elem.getAttributes();
String name = (String)attr.getAttribute(HTML.Attribute.NAME);
if (name == null) {
return;
}
String value = null;
HTML.Tag tag = (HTML.Tag)elem.getAttributes().getAttribute
(StyleConstants.NameAttribute);
if (tag == HTML.Tag.INPUT) {
value = getInputElementData(attr);
} else if (tag == HTML.Tag.TEXTAREA) {
value = getTextAreaData(attr);
} else if (tag == HTML.Tag.SELECT) {
loadSelectData(attr, buffer);
}
if (name != null && value != null) {
appendBuffer(buffer, name, value);
}
| private void | loadSelectData(javax.swing.text.AttributeSet attr, java.lang.StringBuffer buffer)Loads the buffer with the data associated with the Select
form element. Basically, only items that are selected
and have their name attribute set are added to the buffer.
String name = (String)attr.getAttribute(HTML.Attribute.NAME);
if (name == null) {
return;
}
Object m = attr.getAttribute(StyleConstants.ModelAttribute);
if (m instanceof OptionListModel) {
OptionListModel model = (OptionListModel)m;
for (int i = 0; i < model.getSize(); i++) {
if (model.isSelectedIndex(i)) {
Option option = (Option) model.getElementAt(i);
appendBuffer(buffer, name, option.getValue());
}
}
} else if (m instanceof ComboBoxModel) {
ComboBoxModel model = (ComboBoxModel)m;
Option option = (Option)model.getSelectedItem();
if (option != null) {
appendBuffer(buffer, name, option.getValue());
}
}
| void | resetForm()Resets the form
to its initial state by reinitializing the models
associated with each form element to their initial
values.
param elem the element that triggered the reset
Element parent = getFormElement();
if (parent != null) {
ElementIterator it = new ElementIterator(parent);
Element next;
while((next = it.next()) != null) {
if (isControl(next)) {
AttributeSet elemAttr = next.getAttributes();
Object m = elemAttr.getAttribute(StyleConstants.
ModelAttribute);
if (m instanceof TextAreaDocument) {
TextAreaDocument doc = (TextAreaDocument)m;
doc.reset();
} else if (m instanceof PlainDocument) {
try {
PlainDocument doc = (PlainDocument)m;
doc.remove(0, doc.getLength());
if (HTMLDocument.matchNameAttribute
(elemAttr, HTML.Tag.INPUT)) {
String value = (String)elemAttr.
getAttribute(HTML.Attribute.VALUE);
if (value != null) {
doc.insertString(0, value, null);
}
}
} catch (BadLocationException e) {
}
} else if (m instanceof OptionListModel) {
OptionListModel model = (OptionListModel) m;
int size = model.getSize();
for (int i = 0; i < size; i++) {
model.removeIndexInterval(i, i);
}
BitSet selectionRange = model.getInitialSelection();
for (int i = 0; i < selectionRange.size(); i++) {
if (selectionRange.get(i)) {
model.addSelectionInterval(i, i);
}
}
} else if (m instanceof OptionComboBoxModel) {
OptionComboBoxModel model = (OptionComboBoxModel) m;
Option option = model.getInitialSelection();
if (option != null) {
model.setSelectedItem(option);
}
} else if (m instanceof JToggleButton.ToggleButtonModel) {
boolean checked = ((String)elemAttr.getAttribute
(HTML.Attribute.CHECKED) != null);
JToggleButton.ToggleButtonModel model =
(JToggleButton.ToggleButtonModel)m;
model.setSelected(checked);
}
}
}
}
| protected void | submitData(java.lang.String data)This method is responsible for submitting the form data.
A thread is forked to undertake the submission.
//System.err.println("data ->"+data+"<-");
SubmitThread dataThread = new SubmitThread(getElement(), data);
dataThread.start();
|
|