001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.html.dom;
018:
019: import org.w3c.dom.Node;
020: import org.w3c.dom.NodeList;
021: import org.w3c.dom.Text;
022: import org.w3c.dom.html.HTMLElement;
023: import org.w3c.dom.html.HTMLOptionElement;
024: import org.w3c.dom.html.HTMLSelectElement;
025:
026: /**
027: * @xerces.internal
028: * @version $Revision: 447255 $ $Date: 2006-09-18 01:36:42 -0400 (Mon, 18 Sep 2006) $
029: * @author <a href="mailto:arkin@openxml.org">Assaf Arkin</a>
030: * @see org.w3c.dom.html.HTMLOptionElement
031: * @see org.apache.xerces.dom.ElementImpl
032: */
033: public class HTMLOptionElementImpl extends HTMLElementImpl implements
034: HTMLOptionElement {
035:
036: private static final long serialVersionUID = -4486774554137530907L;
037:
038: public boolean getDefaultSelected() {
039: // ! NOT FULLY IMPLEMENTED !
040: return getBinary("default-selected");
041: }
042:
043: public void setDefaultSelected(boolean defaultSelected) {
044: // ! NOT FULLY IMPLEMENTED !
045: setAttribute("default-selected", defaultSelected);
046: }
047:
048: public String getText() {
049: Node child;
050: StringBuffer text = new StringBuffer();
051:
052: // Find the Text nodes contained within this element and return their
053: // concatenated value. Required to go around comments, entities, etc.
054: child = getFirstChild();
055: while (child != null) {
056: if (child instanceof Text) {
057: text.append(((Text) child).getData());
058: }
059: child = child.getNextSibling();
060: }
061: return text.toString();
062: }
063:
064: public void setText(String text) {
065: Node child;
066: Node next;
067:
068: // Delete all the nodes and replace them with a single Text node.
069: // This is the only approach that can handle comments and other nodes.
070: child = getFirstChild();
071: while (child != null) {
072: next = child.getNextSibling();
073: removeChild(child);
074: child = next;
075: }
076: insertBefore(getOwnerDocument().createTextNode(text),
077: getFirstChild());
078: }
079:
080: public int getIndex() {
081: Node parent;
082: NodeList options;
083: int i;
084:
085: // Locate the parent SELECT. Note that this OPTION might be inside a
086: // OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
087: // Everything is possible. If no parent is found, return -1.
088: parent = getParentNode();
089: while (parent != null && !(parent instanceof HTMLSelectElement))
090: parent = parent.getParentNode();
091: if (parent != null) {
092: // Use getElementsByTagName() which creates a snapshot of all the
093: // OPTION elements under the SELECT. Access to the returned NodeList
094: // is very fast and the snapshot solves many synchronization problems.
095: options = ((HTMLElement) parent)
096: .getElementsByTagName("OPTION");
097: for (i = 0; i < options.getLength(); ++i)
098: if (options.item(i) == this )
099: return i;
100: }
101: return -1;
102: }
103:
104: public void setIndex(int index) {
105: Node parent;
106: NodeList options;
107: Node item;
108:
109: // Locate the parent SELECT. Note that this OPTION might be inside a
110: // OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
111: // Everything is possible. If no parent is found, just return.
112: parent = getParentNode();
113: while (parent != null && !(parent instanceof HTMLSelectElement))
114: parent = parent.getParentNode();
115: if (parent != null) {
116: // Use getElementsByTagName() which creates a snapshot of all the
117: // OPTION elements under the SELECT. Access to the returned NodeList
118: // is very fast and the snapshot solves many synchronization problems.
119: // Make sure this OPTION is not replacing itself.
120: options = ((HTMLElement) parent)
121: .getElementsByTagName("OPTION");
122: if (options.item(index) != this ) {
123: // Remove this OPTION from its parent. Place this OPTION right
124: // before indexed OPTION underneath it's direct parent (might
125: // be an OPTGROUP).
126: getParentNode().removeChild(this );
127: item = options.item(index);
128: item.getParentNode().insertBefore(this , item);
129: }
130: }
131: }
132:
133: public boolean getDisabled() {
134: return getBinary("disabled");
135: }
136:
137: public void setDisabled(boolean disabled) {
138: setAttribute("disabled", disabled);
139: }
140:
141: public String getLabel() {
142: return capitalize(getAttribute("label"));
143: }
144:
145: public void setLabel(String label) {
146: setAttribute("label", label);
147: }
148:
149: public boolean getSelected() {
150: return getBinary("selected");
151: }
152:
153: public void setSelected(boolean selected) {
154: setAttribute("selected", selected);
155: }
156:
157: public String getValue() {
158: return getAttribute("value");
159: }
160:
161: public void setValue(String value) {
162: setAttribute("value", value);
163: }
164:
165: /**
166: * Constructor requires owner document.
167: *
168: * @param owner The owner HTML document
169: */
170: public HTMLOptionElementImpl(HTMLDocumentImpl owner, String name) {
171: super(owner, name);
172: }
173:
174: }
|