diff options
author | Roman Kennke <roman@kennke.org> | 2006-11-02 21:36:29 +0000 |
---|---|---|
committer | Roman Kennke <roman@kennke.org> | 2006-11-02 21:36:29 +0000 |
commit | 4c09f7d17324679eeff2a7aa7ccf4d5c6a3e8761 (patch) | |
tree | d012a4ef8176e10f5ceb051835b4cec72e256f9a /javax | |
parent | 37e3d40b79a799c55767b898c52f75ea9aa9df0a (diff) | |
download | classpath-4c09f7d17324679eeff2a7aa7ccf4d5c6a3e8761.tar.gz |
2006-11-02 Roman Kennke <kennke@aicas.com>
* javax/swing/text/html/FormView.java
(maxIsPreferred): New field.
(createComponent): Initialize components correctly.
(getMaximumSpan): Return the preferred span for components
that need this. The maxIsPreferred flag is set accordingly
in createComponent.
* javax/swing/text/html/HTMLDocument.java
(HTMLReader.FormAction.start): Implemented to set the
correct model as attribute.
(HTMLReader.FormAction.setModel): New helper method.
(HTMLReader.FormAction.end): Call super to finish the element.
Added TODO about things left to do.
(HTMLReader.handleComment): Use SimpleAttributeSet rather
than htmlAttributeSet.
* javax/swing/text/html/HTMLEditorKit.java
(HTMLFactory.create): Create BlockView for FORM tags.
Create FormView for INPUT, TEXTAREA and SELECT tags.
Diffstat (limited to 'javax')
-rw-r--r-- | javax/swing/text/html/FormView.java | 152 | ||||
-rw-r--r-- | javax/swing/text/html/HTMLDocument.java | 64 | ||||
-rw-r--r-- | javax/swing/text/html/HTMLEditorKit.java | 9 |
3 files changed, 189 insertions, 36 deletions
diff --git a/javax/swing/text/html/FormView.java b/javax/swing/text/html/FormView.java index d54021066..103252c47 100644 --- a/javax/swing/text/html/FormView.java +++ b/javax/swing/text/html/FormView.java @@ -44,15 +44,21 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.net.MalformedURLException; +import java.net.URL; +import javax.swing.ButtonModel; +import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField; +import javax.swing.JToggleButton; import javax.swing.UIManager; import javax.swing.text.AttributeSet; import javax.swing.text.ComponentView; +import javax.swing.text.Document; import javax.swing.text.Element; import javax.swing.text.StyleConstants; @@ -125,6 +131,11 @@ public class FormView UIManager.getString("FormView.resetButtonText"); /** + * If this is true, the maximum size is set to the preferred size. + */ + private boolean maxIsPreferred; + + /** * Creates a new <code>FormView</code>. * * @param el the element that is displayed by this view. @@ -141,37 +152,135 @@ public class FormView { Component comp = null; Element el = getElement(); - Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute); + AttributeSet atts = el.getAttributes(); + Object tag = atts.getAttribute(StyleConstants.NameAttribute); + Object model = atts.getAttribute(StyleConstants.ModelAttribute); if (tag.equals(HTML.Tag.INPUT)) { - AttributeSet atts = el.getAttributes(); String type = (String) atts.getAttribute(HTML.Attribute.TYPE); - String value = (String) atts.getAttribute(HTML.Attribute.VALUE); if (type.equals("button")) - comp = new JButton(value); + { + String value = (String) atts.getAttribute(HTML.Attribute.VALUE); + JButton b = new JButton(value); + if (model != null) + { + b.setModel((ButtonModel) model); + b.addActionListener(this); + } + comp = b; + maxIsPreferred = true; + } else if (type.equals("checkbox")) - comp = new JCheckBox(value); + { + JCheckBox c = new JCheckBox(); + if (model != null) + { + boolean sel = atts.getAttribute(HTML.Attribute.CHECKED) != null; + ((JToggleButton.ToggleButtonModel) model).setSelected(sel); + c.setModel((ButtonModel) model); + } + comp = c; + maxIsPreferred = true; + } else if (type.equals("image")) - comp = new JButton(value); // FIXME: Find out how to fetch the image. + { + String src = (String) atts.getAttribute(HTML.Attribute.SRC); + JButton b; + try + { + URL base = ((HTMLDocument) el.getDocument()).getBase(); + URL srcURL = new URL(base, src); + ImageIcon icon = new ImageIcon(srcURL); + b = new JButton(icon); + } + catch (MalformedURLException ex) + { + b = new JButton(src); + } + if (model != null) + { + b.setModel((ButtonModel) model); + b.addActionListener(this); + } + comp = b; + maxIsPreferred = true; + } else if (type.equals("password")) - comp = new JPasswordField(value); + { + int size = HTML.getIntegerAttributeValue(atts, HTML.Attribute.SIZE, + -1); + JTextField tf = new JPasswordField(); + if (size > 0) + tf.setColumns(size); + else + tf.setColumns(20); + if (model != null) + tf.setDocument((Document) model); + String value = (String) atts.getAttribute(HTML.Attribute.VALUE); + if (value != null) + tf.setText(value); + tf.addActionListener(this); + comp = tf; + maxIsPreferred = true; + } else if (type.equals("radio")) - comp = new JRadioButton(value); + { + JRadioButton c = new JRadioButton(); + if (model != null) + { + boolean sel = atts.getAttribute(HTML.Attribute.CHECKED) != null; + ((JToggleButton.ToggleButtonModel) model).setSelected(sel); + c.setModel((ButtonModel) model); + } + comp = c; + maxIsPreferred = true; + } else if (type.equals("reset")) { - if (value == null || value.equals("")) - value = RESET; - comp = new JButton(value); + String value = (String) atts.getAttribute(HTML.Attribute.VALUE); + if (value == null) + value = UIManager.getString("FormView.resetButtonText"); + JButton b = new JButton(value); + if (model != null) + { + b.setModel((ButtonModel) model); + b.addActionListener(this); + } + comp = b; + maxIsPreferred = true; } else if (type.equals("submit")) { - if (value == null || value.equals("")) - value = SUBMIT; - comp = new JButton(value); + String value = (String) atts.getAttribute(HTML.Attribute.VALUE); + if (value == null) + value = UIManager.getString("FormView.submitButtonText"); + JButton b = new JButton(value); + if (model != null) + { + b.setModel((ButtonModel) model); + b.addActionListener(this); + } + comp = b; + maxIsPreferred = true; } else if (type.equals("text")) - comp = new JTextField(value); - + { + int size = HTML.getIntegerAttributeValue(atts, HTML.Attribute.SIZE, + -1); + JTextField tf = new JTextField(); + if (size > 0) + tf.setColumns(size); + else + tf.setColumns(20); + if (model != null) + tf.setDocument((Document) model); + String value = (String) atts.getAttribute(HTML.Attribute.VALUE); + if (value != null) + tf.setText(value); + tf.addActionListener(this); + comp = tf; + maxIsPreferred = true; + } } // FIXME: Implement the remaining components. return comp; @@ -188,16 +297,11 @@ public class FormView */ public float getMaximumSpan(int axis) { - // FIXME: The specs say that for some components the maximum span == the - // preferred span of the component. This should be figured out and - // implemented accordingly. float span; - if (axis == X_AXIS) - span = getComponent().getMaximumSize().width; - else if (axis == Y_AXIS) - span = getComponent().getMaximumSize().height; + if (maxIsPreferred) + span = getPreferredSpan(axis); else - throw new IllegalArgumentException("Invalid axis parameter"); + span = super.getMaximumSpan(axis); return span; } diff --git a/javax/swing/text/html/HTMLDocument.java b/javax/swing/text/html/HTMLDocument.java index bb6679928..2ff0fd097 100644 --- a/javax/swing/text/html/HTMLDocument.java +++ b/javax/swing/text/html/HTMLDocument.java @@ -48,7 +48,9 @@ import java.util.HashMap; import java.util.Stack; import java.util.Vector; +import javax.swing.DefaultButtonModel; import javax.swing.JEditorPane; +import javax.swing.JToggleButton; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; @@ -57,6 +59,7 @@ import javax.swing.text.Element; import javax.swing.text.ElementIterator; import javax.swing.text.GapContent; import javax.swing.text.MutableAttributeSet; +import javax.swing.text.PlainDocument; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.html.HTML.Tag; @@ -639,7 +642,11 @@ public class HTMLDocument extends DefaultStyledDocument popCharacterStyle(); } } - + + /** + * Processes elements that make up forms: <input>, <textarea>, + * <select> and <option>. + */ public class FormAction extends SpecialAction { /** @@ -647,10 +654,21 @@ public class HTMLDocument extends DefaultStyledDocument * of tags associated with this Action. */ public void start(HTML.Tag t, MutableAttributeSet a) - throws NotImplementedException { - // FIXME: Implement. - print ("FormAction.start not implemented"); + if (t == HTML.Tag.INPUT) + { + String type = (String) a.getAttribute(HTML.Attribute.TYPE); + if (type == null) + { + type = "text"; // Default to 'text' when nothing was specified. + a.addAttribute(HTML.Attribute.TYPE, type); + } + setModel(type, a); + } + // TODO: Handle textarea, select and option tags. + + // Build the element. + super.start(t, a); } /** @@ -658,11 +676,41 @@ public class HTMLDocument extends DefaultStyledDocument * with this Action. */ public void end(HTML.Tag t) - throws NotImplementedException { - // FIXME: Implement. - print ("FormAction.end not implemented"); - } + // TODO: Handle textarea, select and option tags. + + // Finish the element. + super.end(t); + } + + private void setModel(String type, MutableAttributeSet attrs) + { + if (type.equals("submit") || type.equals("reset") + || type.equals("image")) + { + // Create button. + attrs.addAttribute(StyleConstants.ModelAttribute, + new DefaultButtonModel()); + } + else if (type.equals("text") || type.equals("password")) + { + // TODO: Handle fixed length input fields. + attrs.addAttribute(StyleConstants.ModelAttribute, + new PlainDocument()); + } + else if (type.equals("file")) + { + attrs.addAttribute(StyleConstants.ModelAttribute, + new PlainDocument()); + } + else if (type.equals("checkbox") || type.equals("radio")) + { + JToggleButton.ToggleButtonModel model = + new JToggleButton.ToggleButtonModel(); + // TODO: Handle radio button via ButtonGroups. + attrs.addAttribute(StyleConstants.ModelAttribute, model); + } + } } /** diff --git a/javax/swing/text/html/HTMLEditorKit.java b/javax/swing/text/html/HTMLEditorKit.java index fa879bc49..d4bfbbce5 100644 --- a/javax/swing/text/html/HTMLEditorKit.java +++ b/javax/swing/text/html/HTMLEditorKit.java @@ -640,7 +640,8 @@ public class HTMLEditorKit || tag.equals(HTML.Tag.HTML) || tag.equals(HTML.Tag.CENTER) || tag.equals(HTML.Tag.DIV) || tag.equals(HTML.Tag.BLOCKQUOTE) - || tag.equals(HTML.Tag.PRE)) + || tag.equals(HTML.Tag.PRE) + || tag.equals(HTML.Tag.FORM)) view = new BlockView(element, View.Y_AXIS); else if (tag.equals(HTML.Tag.IMG)) view = new ImageView(element); @@ -658,14 +659,14 @@ public class HTMLEditorKit view = new HRuleView(element); else if (tag.equals(HTML.Tag.BR)) view = new BRView(element); + else if (tag.equals(HTML.Tag.INPUT) || tag.equals(HTML.Tag.SELECT) + || tag.equals(HTML.Tag.TEXTAREA)) + view = new FormView(element); /* else if (tag.equals(HTML.Tag.MENU) || tag.equals(HTML.Tag.DIR) || tag.equals(HTML.Tag.UL) || tag.equals(HTML.Tag.OL)) view = new ListView(element); - else if (tag.equals(HTML.Tag.INPUT) || tag.equals(HTML.Tag.SELECT) - || tag.equals(HTML.Tag.TEXTAREA)) - view = new FormView(element); else if (tag.equals(HTML.Tag.OBJECT)) view = new ObjectView(element); else if (tag.equals(HTML.Tag.FRAMESET)) |