This adds support for the textarea tag.
2006-11-03 Roman Kennke <[EMAIL PROTECTED]>
* javax/swing/text/html/HTMLDocument.java
(HTMLReader.FormAction.start): Added support for textarea.
(HTMLReader.FormAction.end): Added support for textarea.
(HTMLReader.HeadAction.end): Call super to actually close the
block.
(HTMLReader.inTextArea): New field.
(HTMLReader.textAreaDocument): New field.
(HTMLReader.handleText): Call textAreaContent when inside
a textarea tag.
(HTMLReader.textAreaContent): Implemented to initialize
the text area's model.
* javax/swing/text/html/FormView.java
(createComponent): Added support for textarea tag.
/Roman
Index: javax/swing/text/html/FormView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/FormView.java,v
retrieving revision 1.4
diff -u -1 -5 -r1.4 FormView.java
--- javax/swing/text/html/FormView.java 2 Nov 2006 21:36:29 -0000 1.4
+++ javax/swing/text/html/FormView.java 3 Nov 2006 14:00:07 -0000
@@ -41,30 +41,32 @@
import java.awt.Component;
import java.awt.Point;
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.JScrollPane;
+import javax.swing.JTextArea;
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;
/**
* A View that renders HTML form elements like buttons and input fields.
* This is implemented as a [EMAIL PROTECTED] ComponentView} that creates different Swing
* component depending on the type and setting of the different form elements.
*
* Namely, this view creates the following components:
@@ -270,30 +272,42 @@
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;
}
}
+ else if (tag == HTML.Tag.TEXTAREA)
+ {
+ JTextArea textArea = new JTextArea((Document) model);
+ int rows = HTML.getIntegerAttributeValue(atts, HTML.Attribute.ROWS, 1);
+ textArea.setRows(rows);
+ int cols = HTML.getIntegerAttributeValue(atts, HTML.Attribute.COLS, 20);
+ textArea.setColumns(cols);
+ maxIsPreferred = true;
+ comp = new JScrollPane(textArea,
+ JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
+ JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
+ }
// FIXME: Implement the remaining components.
return comp;
}
/**
* Determines the maximum span for this view on the specified axis.
*
* @param axis the axis along which to determine the span
*
* @return the maximum span for this view on the specified axis
*
* @throws IllegalArgumentException if the axis is invalid
*/
public float getMaximumSpan(int axis)
{
Index: javax/swing/text/html/HTMLDocument.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/html/HTMLDocument.java,v
retrieving revision 1.45
diff -u -1 -5 -r1.45 HTMLDocument.java
--- javax/swing/text/html/HTMLDocument.java 3 Nov 2006 13:20:15 -0000 1.45
+++ javax/swing/text/html/HTMLDocument.java 3 Nov 2006 14:00:07 -0000
@@ -44,30 +44,31 @@
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
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;
import javax.swing.text.DefaultStyledDocument;
+import javax.swing.text.Document;
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;
/**
* Represents the HTML document that is constructed by defining the text and
* other components (images, buttons, etc) in HTML language. This class can
* becomes the default document for [EMAIL PROTECTED] JEditorPane} after setting its
* content type to "text/html". HTML document also serves as an intermediate
* data structure when it is needed to parse HTML and then obtain the content of
@@ -566,36 +567,51 @@
/**
* This is true when we are inside a pre tag.
*/
boolean inPreTag = false;
/**
* This is true when we are inside a style tag. This will add text
* content inside this style tag beeing parsed as CSS.
*
* This is package private to avoid accessor methods.
*/
boolean inStyleTag = false;
/**
+ * This is true when we are inside a <textarea> tag. Any text
+ * content will then be added to the text area.
+ *
+ * This is package private to avoid accessor methods.
+ */
+ boolean inTextArea = false;
+
+ /**
* This contains all stylesheets that are somehow read, either
* via embedded style tags, or via linked stylesheets. The
* elements will be String objects containing a stylesheet each.
*/
ArrayList styles;
+ /**
+ * The document model for a textarea.
+ *
+ * This is package private to avoid accessor methods.
+ */
+ Document textAreaDocument;
+
void print (String line)
{
if (debug)
System.out.println (line);
}
public class TagAction
{
/**
* This method is called when a start tag is seen for one of the types
* of tags associated with this Action. By default this does nothing.
*/
public void start(HTML.Tag t, MutableAttributeSet a)
{
// Nothing to do here.
@@ -669,43 +685,54 @@
* This method is called when a start tag is seen for one of the types
* of tags associated with this Action.
*/
public void start(HTML.Tag t, MutableAttributeSet a)
{
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.
+ else if (t == HTML.Tag.TEXTAREA)
+ {
+ inTextArea = true;
+ textAreaDocument = new PlainDocument();
+ a.addAttribute(StyleConstants.ModelAttribute, textAreaDocument);
+ }
+ // TODO: Handle select and option tags.
// Build the element.
super.start(t, a);
}
/**
* Called when an end tag is seen for one of the types of tags associated
* with this Action.
*/
public void end(HTML.Tag t)
{
- // TODO: Handle textarea, select and option tags.
+ if (t == HTML.Tag.TEXTAREA)
+ {
+ inTextArea = false;
+ }
+
+ // TODO: Handle 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"))
@@ -937,44 +964,44 @@
* of tags associated with this Action.
*/
public void start(HTML.Tag t, MutableAttributeSet a)
throws NotImplementedException
{
// FIXME: Implement.
print ("HeadAction.start not implemented: "+t);
super.start(t, a);
}
/**
* Called when an end tag is seen for one of the types of tags associated
* with this Action.
*/
public void end(HTML.Tag t)
- throws NotImplementedException
{
// We read in all the stylesheets that are embedded or referenced
// inside the header.
if (styles != null)
{
int numStyles = styles.size();
for (int i = 0; i < numStyles; i++)
{
String style = (String) styles.get(i);
getStyleSheet().addRule(style);
}
}
- }
+ super.end(t);
+ }
}
class LinkAction extends TagAction
{
/**
* This method is called when a start tag is seen for one of the types
* of tags associated with this Action.
*/
public void start(HTML.Tag t, MutableAttributeSet a)
throws NotImplementedException
{
// FIXME: Implement.
print ("LinkAction.start not implemented");
}
@@ -1249,31 +1276,33 @@
offset += HTMLDocument.this.getLength() - offset;
}
/**
* This method is called by the parser to indicate a block of
* text was encountered. Should insert the text appropriately.
*
* @param data the text that was inserted
* @param pos the position at which the text was inserted
*/
public void handleText(char[] data, int pos)
{
if (shouldInsert() && data != null && data.length > 0)
{
- if (inPreTag)
+ if (inTextArea)
+ textAreaContent(data);
+ else if (inPreTag)
preContent(data);
else if (inStyleTag)
{
if (styles == null)
styles = new ArrayList();
styles.add(new String(data));
}
else
addContent(data, 0, data.length);
}
}
/**
* Checks if the HTML tag should be inserted. The tags before insert tag (if
@@ -1383,34 +1412,42 @@
* @since 1.3
*/
public void handleEndOfLineString(String eol)
{
// FIXME: Implement.
print ("HTMLReader.handleEndOfLineString not implemented yet");
}
/**
* Adds the given text to the textarea document. Called only when we are
* within a textarea.
*
* @param data the text to add to the textarea
*/
protected void textAreaContent(char[] data)
- throws NotImplementedException
{
- // FIXME: Implement.
- print ("HTMLReader.textAreaContent not implemented yet");
+ try
+ {
+ int offset = textAreaDocument.getLength();
+ textAreaDocument.insertString(offset, new String(data), null);
+ }
+ catch (BadLocationException ex)
+ {
+ // Must not happen as we insert at a model location that we
+ // got from the document itself.
+ assert false;
+ }
}
/**
* Adds the given text that was encountered in a <PRE> element.
* This adds synthesized lines to hold the text runs.
*
* @param data the text
*/
protected void preContent(char[] data)
{
int start = 0;
for (int i = 0; i < data.length; i++)
{
if (data[i] == '\n')
{