From 11e0b4379965e75fce42833a155724a8f0990151 Mon Sep 17 00:00:00 2001 From: Roman Kennke Date: Thu, 7 Dec 2006 14:42:46 +0000 Subject: 2006-12-06 Roman Kennke * javax/swing/text/html/FormView.java (SubmitThread.postData): Implemented. (SubmitThread.run): Pass data to postData(). (actionPerformed): Reset form when reset button is activated. (createComponent): Add support for select lists and comboboxes. Don't set value of text and password fields here, this is done now in HTMLDocument for consistency. (getElementFormData): Add support for fetching form data from select lists and comboboxes as well as textareas. (getSelectData): New helper method. Fetches form data from select boxes. (getTextAreaData): New helper method. Fetches form data from textareas. (resetForm): New helper method. Resets the entire form. * javax/swing/text/html/HTMLDocument.java (HTMLReader.FormAction.end): Handle SELECT and OPTION tags. (HTMLReader.FormAction.start): Handle SELECT and OPTION tags. (HTMLReader.FormAction.setModel): Initialize text and password values here. Also, use the resetable special models. Group radio buttons into ButtonGroup for exclusive selection. (HTMLReader.FormTagAction): New class. Handles FORM tags. (HTMLReader.buttonGroups): New field. (HTMLReader.numOptions): New field. (HTMLReader.option): New field. (HTMLReader.selectModel): New field. (HTMLReader.textAreaDocument): Make ResetablePlainDocument. (HTMLReader.handleText): Handle OPTION text. (HTMLReader.initTags): Map FORM tags to FormTagAction. (HTMLReader.textAreaContent): Set initial content. * javax/swing/text/html/Option.java (Option): Make copy of attribute set. Initialize selected state. (getValue): Fetch value from attribute set. * javax/swing/text/html/ResetableModel.java: New interface. * javax/swing/text/html/ResetablePlainDocument.java: New class. Supports resetting the state. * javax/swing/text/html/ResetableToggleButtonModel.java: Likewise. * javax/swing/text/html/SelectComboBoxModel.java: Likewise. * javax/swing/text/html/SelectListModel.java: Likewise. --- ChangeLog | 41 +++++ javax/swing/text/html/FormView.java | 184 +++++++++++++++++---- javax/swing/text/html/HTMLDocument.java | 177 +++++++++++++++++--- javax/swing/text/html/Option.java | 12 +- javax/swing/text/html/ResetableModel.java | 50 ++++++ javax/swing/text/html/ResetablePlainDocument.java | 82 +++++++++ .../text/html/ResetableToggleButtonModel.java | 71 ++++++++ javax/swing/text/html/SelectComboBoxModel.java | 84 ++++++++++ javax/swing/text/html/SelectListModel.java | 106 ++++++++++++ 9 files changed, 748 insertions(+), 59 deletions(-) create mode 100644 javax/swing/text/html/ResetableModel.java create mode 100644 javax/swing/text/html/ResetablePlainDocument.java create mode 100644 javax/swing/text/html/ResetableToggleButtonModel.java create mode 100644 javax/swing/text/html/SelectComboBoxModel.java create mode 100644 javax/swing/text/html/SelectListModel.java diff --git a/ChangeLog b/ChangeLog index cdc699156..1080fda21 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,44 @@ +2006-12-06 Roman Kennke + + * javax/swing/text/html/FormView.java + (SubmitThread.postData): Implemented. + (SubmitThread.run): Pass data to postData(). + (actionPerformed): Reset form when reset button is activated. + (createComponent): Add support for select lists and comboboxes. + Don't set value of text and password fields here, this is done + now in HTMLDocument for consistency. + (getElementFormData): Add support for fetching form data from + select lists and comboboxes as well as textareas. + (getSelectData): New helper method. Fetches form data from + select boxes. + (getTextAreaData): New helper method. Fetches form data from + textareas. + (resetForm): New helper method. Resets the entire form. + * javax/swing/text/html/HTMLDocument.java + (HTMLReader.FormAction.end): Handle SELECT and OPTION tags. + (HTMLReader.FormAction.start): Handle SELECT and OPTION tags. + (HTMLReader.FormAction.setModel): Initialize text and password + values here. Also, use the resetable special models. + Group radio buttons into ButtonGroup for exclusive selection. + (HTMLReader.FormTagAction): New class. Handles FORM tags. + (HTMLReader.buttonGroups): New field. + (HTMLReader.numOptions): New field. + (HTMLReader.option): New field. + (HTMLReader.selectModel): New field. + (HTMLReader.textAreaDocument): Make ResetablePlainDocument. + (HTMLReader.handleText): Handle OPTION text. + (HTMLReader.initTags): Map FORM tags to FormTagAction. + (HTMLReader.textAreaContent): Set initial content. + * javax/swing/text/html/Option.java + (Option): Make copy of attribute set. Initialize selected state. + (getValue): Fetch value from attribute set. + * javax/swing/text/html/ResetableModel.java: New interface. + * javax/swing/text/html/ResetablePlainDocument.java: New class. + Supports resetting the state. + * javax/swing/text/html/ResetableToggleButtonModel.java: Likewise. + * javax/swing/text/html/SelectComboBoxModel.java: Likewise. + * javax/swing/text/html/SelectListModel.java: Likewise. + 2006-12-06 Roman Kennke * javax/swing/text/DefaultCaret.java diff --git a/javax/swing/text/html/FormView.java b/javax/swing/text/html/FormView.java index 340f85490..ef362bd3d 100644 --- a/javax/swing/text/html/FormView.java +++ b/javax/swing/text/html/FormView.java @@ -45,6 +45,8 @@ import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -54,13 +56,15 @@ import javax.swing.ButtonModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; +import javax.swing.JComboBox; import javax.swing.JEditorPane; +import javax.swing.JList; 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.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.HyperlinkEvent; @@ -165,7 +169,7 @@ public class FormView // Perform POST. url = actionURL; conn = url.openConnection(); - postData(conn); + postData(conn, data); } else { @@ -285,9 +289,26 @@ public class FormView * * @param conn the connection */ - private void postData(URLConnection conn) + private void postData(URLConnection conn, String data) { - // TODO: Implement. + conn.setDoOutput(true); + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream())); + out.print(data); + out.flush(); + } + catch (IOException ex) + { + // Deal with this! + ex.printStackTrace(); + } + finally + { + if (out != null) + out.close(); + } } /** @@ -390,15 +411,15 @@ public class FormView } else if (type.equals("checkbox")) { - JCheckBox c = new JCheckBox(); - if (model != null) + if (model instanceof ResetableToggleButtonModel) { - boolean sel = atts.getAttribute(HTML.Attribute.CHECKED) != null; - ((JToggleButton.ToggleButtonModel) model).setSelected(sel); - c.setModel((ButtonModel) model); + ResetableToggleButtonModel m = + (ResetableToggleButtonModel) model; + JCheckBox c = new JCheckBox(); + c.setModel(m); + comp = c; + maxIsPreferred = true; } - comp = c; - maxIsPreferred = true; } else if (type.equals("image")) { @@ -434,24 +455,21 @@ public class FormView 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")) { - JRadioButton c = new JRadioButton(); - if (model != null) + if (model instanceof ResetableToggleButtonModel) { - boolean sel = atts.getAttribute(HTML.Attribute.CHECKED) != null; - ((JToggleButton.ToggleButtonModel) model).setSelected(sel); - c.setModel((ButtonModel) model); + ResetableToggleButtonModel m = + (ResetableToggleButtonModel) model; + JRadioButton c = new JRadioButton(); + c.setModel(m); + comp = c; + maxIsPreferred = true; } - comp = c; - maxIsPreferred = true; } else if (type.equals("reset")) { @@ -492,9 +510,6 @@ public class FormView 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; @@ -512,7 +527,25 @@ public class FormView JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); } - // FIXME: Implement the remaining components. + else if (tag == HTML.Tag.SELECT) + { + if (model instanceof SelectListModel) + { + SelectListModel slModel = (SelectListModel) model; + JList list = new JList(slModel); + int size = HTML.getIntegerAttributeValue(atts, HTML.Attribute.SIZE, + 1); + list.setVisibleRowCount(size); + list.setSelectionModel(slModel.getSelectionModel()); + comp = new JScrollPane(list); + } + else if (model instanceof SelectComboBoxModel) + { + SelectComboBoxModel scbModel = (SelectComboBoxModel) model; + comp = new JComboBox(scbModel); + } + maxIsPreferred = true; + } return comp; } @@ -557,6 +590,8 @@ public class FormView String type = (String) atts.getAttribute(HTML.Attribute.TYPE); if (type.equals("submit")) submitData(getFormData()); + else if (type.equals("reset")) + resetForm(); } // FIXME: Implement the remaining actions. } @@ -680,16 +715,82 @@ public class FormView { String value = null; HTML.Tag tag = (HTML.Tag) atts.getAttribute(StyleConstants.NameAttribute); - if (tag == HTML.Tag.INPUT) - value = getInputFormData(atts); - // TODO: Implement textarea and select. - if (name != null && value != null) + if (tag == HTML.Tag.SELECT) + { + getSelectData(atts, b); + } + else { - addData(b, name, value); + if (tag == HTML.Tag.INPUT) + value = getInputFormData(atts); + else if (tag == HTML.Tag.TEXTAREA) + value = getTextAreaData(atts); + if (name != null && value != null) + { + addData(b, name, value); + } } } } + /** + * Fetches form data from select boxes. + * + * @param atts the attributes of the element + * + * @param b the form data string to append to + */ + private void getSelectData(AttributeSet atts, StringBuilder b) + { + String name = (String) atts.getAttribute(HTML.Attribute.NAME); + if (name != null) + { + Object m = atts.getAttribute(StyleConstants.ModelAttribute); + if (m instanceof SelectListModel) + { + SelectListModel sl = (SelectListModel) m; + ListSelectionModel lsm = sl.getSelectionModel(); + for (int i = 0; i < sl.getSize(); i++) + { + if (lsm.isSelectedIndex(i)) + { + Option o = (Option) sl.getElementAt(i); + addData(b, name, o.getValue()); + } + } + } + else if (m instanceof SelectComboBoxModel) + { + SelectComboBoxModel scb = (SelectComboBoxModel) m; + Option o = (Option) scb.getSelectedItem(); + if (o != null) + addData(b, name, o.getValue()); + } + } + } + + /** + * Fetches form data from a textarea. + * + * @param atts the attributes + * + * @return the form data + */ + private String getTextAreaData(AttributeSet atts) + { + Document doc = (Document) atts.getAttribute(StyleConstants.ModelAttribute); + String data; + try + { + data = doc.getText(0, doc.getLength()); + } + catch (BadLocationException ex) + { + data = null; + } + return data; + } + /** * Fetches form data from an input tag. * @@ -743,4 +844,27 @@ public class FormView String encValue = URLEncoder.encode(value); b.append(encValue); } + + /** + * Resets the form data to their initial state. + */ + private void resetForm() + { + Element form = getFormElement(); + if (form != null) + { + ElementIterator iter = new ElementIterator(form); + Element next; + while ((next = iter.next()) != null) + { + if (next.isLeaf()) + { + AttributeSet atts = next.getAttributes(); + Object m = atts.getAttribute(StyleConstants.ModelAttribute); + if (m instanceof ResetableModel) + ((ResetableModel) m).reset(); + } + } + } + } } diff --git a/javax/swing/text/html/HTMLDocument.java b/javax/swing/text/html/HTMLDocument.java index 8ef4c0e87..05a250da3 100644 --- a/javax/swing/text/html/HTMLDocument.java +++ b/javax/swing/text/html/HTMLDocument.java @@ -49,16 +49,16 @@ import java.util.HashMap; import java.util.Stack; import java.util.Vector; +import javax.swing.ButtonGroup; import javax.swing.DefaultButtonModel; import javax.swing.JEditorPane; -import javax.swing.JToggleButton; +import javax.swing.ListSelectionModel; import javax.swing.event.DocumentEvent; import javax.swing.event.UndoableEditEvent; 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; @@ -729,7 +729,28 @@ public class HTMLDocument extends DefaultStyledDocument * * This is package private to avoid accessor methods. */ - Document textAreaDocument; + ResetablePlainDocument textAreaDocument; + + /** + * The current model of a select tag. Can be a ComboBoxModel or a + * ListModel depending on the type of the select box. + */ + Object selectModel; + + /** + * The current option beeing read. + */ + Option option; + + /** + * The current number of options in the current select model. + */ + int numOptions; + + /** + * The current button groups mappings. + */ + HashMap buttonGroups; /** * The token threshold. This gets increased while loading. @@ -834,13 +855,59 @@ public class HTMLDocument extends DefaultStyledDocument else if (t == HTML.Tag.TEXTAREA) { inTextArea = true; - textAreaDocument = new PlainDocument(); + textAreaDocument = new ResetablePlainDocument(); a.addAttribute(StyleConstants.ModelAttribute, textAreaDocument); } - // TODO: Handle select and option tags. - - // Build the element. - super.start(t, a); + else if (t == HTML.Tag.SELECT) + { + int size = HTML.getIntegerAttributeValue(a, HTML.Attribute.SIZE, + 1); + boolean multi = a.getAttribute(HTML.Attribute.MULTIPLE) != null; + if (size > 1 || multi) + { + SelectListModel m = new SelectListModel(); + if (multi) + m.getSelectionModel().setSelectionMode(ListSelectionModel + .MULTIPLE_INTERVAL_SELECTION); + selectModel = m; + } + else + { + selectModel = new SelectComboBoxModel(); + } + a.addAttribute(StyleConstants.ModelAttribute, selectModel); + } + if (t == HTML.Tag.OPTION) + { + option = new Option(a); + if (selectModel instanceof SelectListModel) + { + SelectListModel m = (SelectListModel) selectModel; + m.addElement(option); + if (option.isSelected()) + { + m.getSelectionModel().addSelectionInterval(numOptions, + numOptions); + m.addInitialSelection(numOptions); + } + } + else if (selectModel instanceof SelectComboBoxModel) + { + SelectComboBoxModel m = (SelectComboBoxModel) selectModel; + m.addElement(option); + if (option.isSelected()) + { + m.setSelectedItem(option); + m.setInitialSelection(option); + } + } + numOptions++; + } + else + { + // Build the element. + super.start(t, a); + } } /** @@ -849,15 +916,24 @@ public class HTMLDocument extends DefaultStyledDocument */ public void end(HTML.Tag t) { - if (t == HTML.Tag.TEXTAREA) + if (t == HTML.Tag.OPTION) { - inTextArea = false; + option = null; + } + else + { + if (t == HTML.Tag.TEXTAREA) + { + inTextArea = false; + } + else if (t == HTML.Tag.SELECT) + { + selectModel = null; + numOptions = 0; + } + // Finish the element. + super.end(t); } - - // TODO: Handle select and option tags. - - // Finish the element. - super.end(t); } private void setModel(String type, MutableAttributeSet attrs) @@ -871,9 +947,22 @@ public class HTMLDocument extends DefaultStyledDocument } else if (type.equals("text") || type.equals("password")) { - // TODO: Handle fixed length input fields. - attrs.addAttribute(StyleConstants.ModelAttribute, - new PlainDocument()); + String text = (String) attrs.getAttribute(HTML.Attribute.VALUE); + ResetablePlainDocument doc = new ResetablePlainDocument(); + if (text != null) + { + doc.setInitialText(text); + try + { + doc.insertString(0, text, null); + } + catch (BadLocationException ex) + { + // Shouldn't happen. + assert false; + } + } + attrs.addAttribute(StyleConstants.ModelAttribute, doc); } else if (type.equals("file")) { @@ -882,14 +971,50 @@ public class HTMLDocument extends DefaultStyledDocument } else if (type.equals("checkbox") || type.equals("radio")) { - JToggleButton.ToggleButtonModel model = - new JToggleButton.ToggleButtonModel(); - // TODO: Handle radio button via ButtonGroups. + ResetableToggleButtonModel model = + new ResetableToggleButtonModel(); + if (attrs.getAttribute(HTML.Attribute.SELECTED) != null) + { + model.setSelected(true); + model.setInitial(true); + } + if (type.equals("radio")) + { + String name = (String) attrs.getAttribute(HTML.Attribute.NAME); + if (name != null) + { + if (buttonGroups == null) + buttonGroups = new HashMap(); + ButtonGroup group = (ButtonGroup) buttonGroups.get(name); + if (group == null) + { + group = new ButtonGroup(); + buttonGroups.put(name, group); + } + model.setGroup(group); + } + } attrs.addAttribute(StyleConstants.ModelAttribute, model); } } } - + + /** + * Called for form tags. + */ + class FormTagAction + extends BlockAction + { + /** + * Clears the button group mapping. + */ + public void end(HTML.Tag t) + { + super.end(t); + buttonGroups = null; + } + } + /** * This action indicates that the content between starting and closing HTML * elements (like script - /script) should not be visible. The content is @@ -1327,7 +1452,7 @@ public class HTMLDocument extends DefaultStyledDocument tagToAction.put(HTML.Tag.DT, paragraphAction); tagToAction.put(HTML.Tag.EM, characterAction); tagToAction.put(HTML.Tag.FONT, convertAction); - tagToAction.put(HTML.Tag.FORM, blockAction); + tagToAction.put(HTML.Tag.FORM, new FormTagAction()); tagToAction.put(HTML.Tag.FRAME, specialAction); tagToAction.put(HTML.Tag.FRAMESET, blockAction); tagToAction.put(HTML.Tag.H1, paragraphAction); @@ -1454,6 +1579,8 @@ public class HTMLDocument extends DefaultStyledDocument textAreaContent(data); else if (inPreTag) preContent(data); + else if (option != null) + option.setLabel(new String(data)); else if (inStyleTag) { if (styles == null) @@ -1588,7 +1715,9 @@ public class HTMLDocument extends DefaultStyledDocument try { int offset = textAreaDocument.getLength(); - textAreaDocument.insertString(offset, new String(data), null); + String text = new String(data); + textAreaDocument.setInitialText(text); + textAreaDocument.insertString(offset, text, null); } catch (BadLocationException ex) { diff --git a/javax/swing/text/html/Option.java b/javax/swing/text/html/Option.java index 1def51b2f..18d5c2bd8 100644 --- a/javax/swing/text/html/Option.java +++ b/javax/swing/text/html/Option.java @@ -72,10 +72,10 @@ public class Option */ public Option(AttributeSet attr) { - attributes = attr; + // Protect the attribute set. + attributes = attr.copyAttributes(); label = null; - selected = false; - // FIXME: Probably initialize something using the attributes. + selected = attr.getAttribute(HTML.Attribute.SELECTED) != null; } /** @@ -151,7 +151,9 @@ public class Option */ public String getValue() { - // FIXME: Return some attribute here if specified. - return label; + String value = (String) attributes.getAttribute(HTML.Attribute.VALUE); + if (value == null) + value = label; + return value; } } diff --git a/javax/swing/text/html/ResetableModel.java b/javax/swing/text/html/ResetableModel.java new file mode 100644 index 000000000..17f65b97d --- /dev/null +++ b/javax/swing/text/html/ResetableModel.java @@ -0,0 +1,50 @@ +/* ResetableModel.java -- Form models that can be resetted + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.swing.text.html; + +/** + * Form models that can be resetted implement this. + */ +interface ResetableModel +{ + /** + * Resets the model. + */ + void reset(); +} diff --git a/javax/swing/text/html/ResetablePlainDocument.java b/javax/swing/text/html/ResetablePlainDocument.java new file mode 100644 index 000000000..6177f9b86 --- /dev/null +++ b/javax/swing/text/html/ResetablePlainDocument.java @@ -0,0 +1,82 @@ +/* ResetablePlainDocument.java -- A plain document for use in the HTML renderer + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.swing.text.html; + +import javax.swing.text.BadLocationException; +import javax.swing.text.PlainDocument; + +/** + * A PlainDocument that can be resetted. + */ +class ResetablePlainDocument + extends PlainDocument + implements ResetableModel +{ + /** + * The initial text. + */ + private String initial; + + /** + * Stores the initial text. + * + * @param text the initial text + */ + void setInitialText(String text) + { + initial = text; + } + + /** + * Resets the model. + */ + public void reset() + { + try + { + replace(0, getLength(), initial, null); + } + catch (BadLocationException ex) + { + // Shouldn't happen. + assert false; + } + } + +} diff --git a/javax/swing/text/html/ResetableToggleButtonModel.java b/javax/swing/text/html/ResetableToggleButtonModel.java new file mode 100644 index 000000000..619c24e47 --- /dev/null +++ b/javax/swing/text/html/ResetableToggleButtonModel.java @@ -0,0 +1,71 @@ +/* ResetableToggleButtonModel.java -- A toggle button model with reset support + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.swing.text.html; + +import javax.swing.ButtonGroup; +import javax.swing.JToggleButton.ToggleButtonModel; + +class ResetableToggleButtonModel + extends ToggleButtonModel + implements ResetableModel +{ + + /** + * The initial state. + */ + private boolean initial; + + /** + * Sets the initial selection value. + * + * @param state the initial value + */ + public void setInitial(boolean state) + { + initial = state; + } + + /** + * Resets the model. + */ + public void reset() + { + setSelected(initial); + } +} diff --git a/javax/swing/text/html/SelectComboBoxModel.java b/javax/swing/text/html/SelectComboBoxModel.java new file mode 100644 index 000000000..999746413 --- /dev/null +++ b/javax/swing/text/html/SelectComboBoxModel.java @@ -0,0 +1,84 @@ +/* SelectComboBoxModel.java -- A special ComboBoxModel for use in HTML renderer + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.swing.text.html; + +import javax.swing.DefaultComboBoxModel; + +/** + * A special ComboBoxModel that supports storing the initial value so that + * the combobox can be resetted later. + */ +class SelectComboBoxModel + extends DefaultComboBoxModel + implements ResetableModel +{ + + /** + * The initial selection. + */ + private Option initial; + + /** + * Sets the initial selection. + * + * @param option the initial selection + */ + void setInitialSelection(Option option) + { + initial = option; + } + + /** + * Returns the initial selection. + * + * @return the initial selection + */ + Option getInitialSelection() + { + return initial; + } + + /** + * Resets the model. + */ + public void reset() + { + setSelectedItem(initial); + } +} diff --git a/javax/swing/text/html/SelectListModel.java b/javax/swing/text/html/SelectListModel.java new file mode 100644 index 000000000..23bfaa11b --- /dev/null +++ b/javax/swing/text/html/SelectListModel.java @@ -0,0 +1,106 @@ +/* OptionListModel.java -- A special ListModel for use in the HTML renderer + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.swing.text.html; + +import java.util.BitSet; + +import javax.swing.DefaultListModel; +import javax.swing.DefaultListSelectionModel; +import javax.swing.ListSelectionModel; + +/** + * A special list model that encapsulates its selection model and supports + * storing of the initial value so that it can be resetted. + */ +class SelectListModel + extends DefaultListModel + implements ResetableModel +{ + /** + * The selection model. + */ + private DefaultListSelectionModel selectionModel; + + /** + * The initial selection. + */ + private BitSet initialSelection; + + /** + * Creates a new SelectListModel. + */ + SelectListModel() + { + selectionModel = new DefaultListSelectionModel(); + initialSelection = new BitSet(); + } + + /** + * Sets the initial selection. + * + * @param init the initial selection + */ + void addInitialSelection(int init) + { + initialSelection.set(init); + } + + /** + * Resets the model. + */ + public void reset() + { + selectionModel.clearSelection(); + for (int i = initialSelection.size(); i >= 0; i--) + { + if (initialSelection.get(i)) + selectionModel.addSelectionInterval(i, i); + } + } + + /** + * Returns the associated selection model. + * + * @return the associated selection model + */ + ListSelectionModel getSelectionModel() + { + return selectionModel; + } +} -- cgit v1.2.1