Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/xml/dom/html2/DomHTMLSelectElement.java


1   /* DomHTMLSelectElement.java -- 
2      Copyright (C) 2005 Free Software Foundation, Inc.
3   
4   This file is part of GNU Classpath.
5   
6   GNU Classpath is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10  
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20  
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25  
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37  
38  package gnu.xml.dom.html2;
39  
40  import gnu.xml.dom.DomDOMException;
41  import org.w3c.dom.DOMException;
42  import org.w3c.dom.html2.HTMLElement;
43  import org.w3c.dom.html2.HTMLFormElement;
44  import org.w3c.dom.html2.HTMLOptionElement;
45  import org.w3c.dom.html2.HTMLOptionsCollection;
46  import org.w3c.dom.html2.HTMLSelectElement;
47  
48  /**
49   * An HTML 'SELECT' element node.
50   *
51   * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
52   */
53  public class DomHTMLSelectElement
54    extends DomHTMLElement
55    implements HTMLSelectElement
56  {
57  
58    protected DomHTMLSelectElement(DomHTMLDocument owner, String namespaceURI,
59                                   String name)
60    {
61      super(owner, namespaceURI, name);
62    }
63    
64    public String getType()
65    {
66      return getHTMLAttribute("type");
67    }
68  
69    public int getSelectedIndex()
70    {
71      HTMLOptionsCollection options = getOptions();
72      int len = options.getLength();
73      for (int i = 0; i < len; i++)
74        {
75          HTMLOptionElement option = (HTMLOptionElement) options.item(i);
76          if (option.getSelected())
77            {
78              return i;
79            }
80        }
81      return -1;
82    }
83  
84    public void setSelectedIndex(int selectedIndex)
85    {
86      HTMLOptionsCollection options = getOptions();
87      int len = options.getLength();
88      if (selectedIndex < 0 || selectedIndex >= len)
89        {
90          throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
91        }
92      for (int i = 0; i < len; i++)
93        {
94          HTMLOptionElement option = (HTMLOptionElement) options.item(i);
95          option.setSelected(i == selectedIndex);
96        }
97    }
98    
99    public String getValue()
100   {
101     return getHTMLAttribute("value");
102   }
103 
104   public void setValue(String value)
105   {
106     setHTMLAttribute("value", value);
107   }
108   
109   public int getLength()
110   {
111     return getIntHTMLAttribute("length");
112   }
113 
114   public void setLength(int length)
115   {
116     setIntHTMLAttribute("length", length);
117   }
118 
119   public HTMLFormElement getForm()
120   {
121     return (HTMLFormElement) getParentElement("form");
122   }
123 
124   public HTMLOptionsCollection getOptions()
125   {
126     DomHTMLCollection ret =
127       new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
128     ret.addNodeName("option");
129     ret.evaluate();
130     return ret;
131   }
132   
133   public boolean getDisabled()
134   {
135     return getBooleanHTMLAttribute("disabled");
136   }
137 
138   public void setDisabled(boolean disabled)
139   {
140     setBooleanHTMLAttribute("disabled", disabled);
141   }
142   
143   public boolean getMultiple()
144   {
145     return getBooleanHTMLAttribute("multiple");
146   }
147 
148   public void setMultiple(boolean multiple)
149   {
150     setBooleanHTMLAttribute("multiple", multiple);
151   }
152   
153   public String getName()
154   {
155     return getHTMLAttribute("name");
156   }
157 
158   public void setName(String name)
159   {
160     setHTMLAttribute("name", name);
161   }
162   
163   public int getSize()
164   {
165     return getIntHTMLAttribute("size");
166   }
167 
168   public void setSize(int size)
169   {
170     setIntHTMLAttribute("size", size);
171   }
172   
173   public int getTabIndex()
174   {
175     return getIntHTMLAttribute("tabindex");
176   }
177 
178   public void setTabIndex(int tabIndex)
179   {
180     setIntHTMLAttribute("tabindex", tabIndex);
181   }
182 
183   public void add(HTMLElement element, HTMLElement before)
184   {
185     insertBefore(before, element);
186   }
187 
188   public void remove(int index)
189   {
190     HTMLOptionsCollection options = getOptions();
191     int len = options.getLength();
192     if (index < 0 || index >= len)
193       {
194         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
195       }
196     HTMLOptionElement option = (HTMLOptionElement) options.item(index);
197     option.getParentNode().removeChild(option);
198   }
199 
200   public void blur()
201   {
202     dispatchUIEvent("blur");
203   }
204 
205   public void focus()
206   {
207     dispatchUIEvent("focus");
208   }
209   
210 }
211