001: /**
002: * org/ozone-db/xml/dom/html/HTMLOptionElementImpl.java
003: *
004: * The contents of this file are subject to the OpenXML Public
005: * License Version 1.0; you may not use this file except in compliance
006: * with the License. You may obtain a copy of the License at
007: * http://www.openxml.org/license.html
008: *
009: * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
010: * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
011: * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
012: * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
013: * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
014: * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
015: *
016: * The Initial Developer of this code under the License is Assaf Arkin.
017: * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
018: * All Rights Reserved.
019: */package org.ozoneDB.xml.dom.html;
020:
021: import org.ozoneDB.xml.dom.*;
022: import org.w3c.dom.*;
023: import org.w3c.dom.html.*;
024:
025: /**
026: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
027: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
028: * @see org.w3c.dom.html.HTMLOptionElement
029: * @see ElementImpl
030: */
031: public final class HTMLOptionElementImpl extends HTMLElementImpl
032: implements HTMLOptionElement {
033:
034: public boolean getDefaultSelected() {
035: // ! NOT FULLY IMPLEMENTED !
036: return getAttribute("default-selected") != null;
037: }
038:
039: public void setDefaultSelected(boolean defaultSelected) {
040: // ! NOT FULLY IMPLEMENTED !
041: setAttribute("default-selected", defaultSelected ? "" : null);
042: }
043:
044: public String getText() {
045: Node child;
046: String text;
047:
048: // Find the Text nodes contained within this element and return their
049: // concatenated value. Required to go around comments, entities, etc.
050: child = getFirstChild();
051: text = "";
052: while (child != null) {
053: if (child instanceof Text) {
054: text = text + ((Text) child).getData();
055: }
056: child = child.getNextSibling();
057: }
058: return text;
059: }
060:
061: public void setText(String text) {
062: Node child;
063: Node next;
064:
065: // Delete all the nodes and replace them with a single Text node.
066: // This is the only approach that can handle comments and other nodes.
067: child = getFirstChild();
068: while (child != null) {
069: next = child.getNextSibling();
070: removeChild(child);
071: child = next;
072: }
073: insertBefore(getOwnerDocument().createTextNode(text),
074: getFirstChild());
075: }
076:
077: public int getIndex() {
078: Node parent;
079: NodeList options;
080: int i;
081:
082: // Locate the parent SELECT. Note that this OPTION might be inside a
083: // OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
084: // Everything is possible. If no parent is found, return -1.
085: parent = getParentNode();
086: while (parent != null && !(parent instanceof HTMLSelectElement)) {
087: parent = parent.getParentNode();
088: }
089: if (parent != null) {
090: // Use getElementsByTagName() which creates a snapshot of all the
091: // OPTION elements under the SELECT. Access to the returned NodeList
092: // is very fast and the snapshot solves many synchronization problems.
093: options = ((HTMLElement) parent)
094: .getElementsByTagName("OPTION");
095: for (i = 0; i < options.getLength(); ++i) {
096: if (options.item(i) == this ) {
097: return i;
098: }
099: }
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: }
116: if (parent != null) {
117: // Use getElementsByTagName() which creates a snapshot of all the
118: // OPTION elements under the SELECT. Access to the returned NodeList
119: // is very fast and the snapshot solves many synchronization problems.
120: // Make sure this OPTION is not replacing itself.
121: options = ((HTMLElement) parent)
122: .getElementsByTagName("OPTION");
123: if (options.item(index) != this ) {
124: // Remove this OPTION from its parent. Place this OPTION right
125: // before indexed OPTION underneath it's direct parent (might
126: // be an OPTGROUP).
127: getParentNode().removeChild(this );
128: item = options.item(index);
129: item.getParentNode().insertBefore(this , item);
130: }
131: }
132: }
133:
134: public boolean getDisabled() {
135: return getAttribute("disabled") != null;
136: }
137:
138: public void setDisabled(boolean disabled) {
139: setAttribute("disabled", disabled ? "" : null);
140: }
141:
142: public String getLabel() {
143: return capitalize(getAttribute("label"));
144: }
145:
146: public void setLabel(String label) {
147: setAttribute("label", label);
148: }
149:
150: public boolean getSelected() {
151: return getAttribute("selected") != null;
152: }
153:
154: public void setSelected(boolean selected) {
155: setAttribute("selected", selected ? "" : null);
156: }
157:
158: public String getValue() {
159: return getAttribute("value");
160: }
161:
162: public void setValue(String value) {
163: setAttribute("value", value);
164: }
165:
166: /**
167: * Constructor requires owner document.
168: *
169: * @param owner The owner HTML document
170: */
171: public HTMLOptionElementImpl(HTMLDocumentImpl owner, String name) {
172: super (owner, "OPTION");
173: }
174:
175: }
|