Method from javax.swing.text.html.FormView Detail: |
public void actionPerformed(ActionEvent evt) {
Element element = getElement();
StringBuilder dataBuffer = new StringBuilder();
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();
}
}
}
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. |
protected Component createComponent() {
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;
}
Create the component. This is basically a
big switch statement based upon the tag type
and html attributes of the associated element. |
public float getMaximumSpan(int axis) {
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);
}
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. |
protected void imageSubmit(String imageData) {
StringBuilder dataBuffer = new StringBuilder();
Element elem = getElement();
HTMLDocument hdoc = (HTMLDocument)elem.getDocument();
getFormData(dataBuffer);
if (dataBuffer.length() > 0) {
dataBuffer.append('&');
}
dataBuffer.append(imageData);
submitData(dataBuffer.toString());
return;
}
This method is called to submit a form in response
to a click on an image -- an <INPUT> form
element of type "image". |
boolean isLastTextOrPasswordField() {
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;
}
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. |
void resetForm() {
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);
}
}
}
}
}
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 |
protected void submitData(String data) {
Element form = getFormElement();
AttributeSet attrs = form.getAttributes();
HTMLDocument doc = (HTMLDocument) form.getDocument();
URL base = doc.getBase();
String target = (String) attrs.getAttribute(HTML.Attribute.TARGET);
if (target == null) {
target = "_self";
}
String method = (String) attrs.getAttribute(HTML.Attribute.METHOD);
if (method == null) {
method = "GET";
}
method = method.toLowerCase();
boolean isPostMethod = method.equals("post");
if (isPostMethod) {
storePostData(doc, target, data);
}
String action = (String) attrs.getAttribute(HTML.Attribute.ACTION);
URL actionURL;
try {
actionURL = (action == null)
? new URL(base.getProtocol(), base.getHost(),
base.getPort(), base.getFile())
: new URL(base, action);
if (!isPostMethod) {
String query = data.toString();
actionURL = new URL(actionURL + "?" + query);
}
} catch (MalformedURLException e) {
actionURL = null;
}
final JEditorPane c = (JEditorPane) getContainer();
HTMLEditorKit kit = (HTMLEditorKit) c.getEditorKit();
FormSubmitEvent formEvent = null;
if (!kit.isAutoFormSubmission() || doc.isFrameDocument()) {
FormSubmitEvent.MethodType methodType = isPostMethod
? FormSubmitEvent.MethodType.POST
: FormSubmitEvent.MethodType.GET;
formEvent = new FormSubmitEvent(
FormView.this, HyperlinkEvent.EventType.ACTIVATED,
actionURL, form, target, methodType, data);
}
// setPage() may take significant time so schedule it to run later.
final FormSubmitEvent fse = formEvent;
final URL url = actionURL;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (fse != null) {
c.fireHyperlinkUpdate(fse);
} else {
try {
c.setPage(url);
} catch (IOException e) {
UIManager.getLookAndFeel().provideErrorFeedback(c);
}
}
}
});
}
This method is responsible for submitting the form data.
A thread is forked to undertake the submission. |