1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.editors.uimodel; 18 19 import com.android.ide.eclipse.adt.internal.editors.descriptors.TextValueDescriptor; 20 21 import org.w3c.dom.Document; 22 import org.w3c.dom.Node; 23 import org.w3c.dom.Text; 24 25 /** 26 * Represents an XML element value in that can be modified using a simple text field 27 * in the XML editor's user interface. 28 */ 29 public class UiTextValueNode extends UiTextAttributeNode { 30 31 public UiTextValueNode(TextValueDescriptor attributeDescriptor, UiElementNode uiParent) { 32 super(attributeDescriptor, uiParent); 33 } 34 35 /** 36 * Updates the current text field's value when the XML has changed. 37 * <p/> 38 * The caller doesn't really know if value of the element has changed, 39 * so it will call this to refresh the value anyway. The value 40 * is only set if it has changed. 41 * <p/> 42 * This also resets the "dirty" flag. 43 */ 44 @Override 45 public void updateValue(Node xml_attribute_node) { 46 setCurrentValue(DEFAULT_VALUE); 47 48 // The argument xml_attribute_node is not used here. It should always be 49 // null since this is not an attribute. What we want is the "text value" of 50 // the parent element, which is actually the first text node of the element. 51 52 UiElementNode parent = getUiParent(); 53 if (parent != null) { 54 Node xml_node = parent.getXmlNode(); 55 if (xml_node != null) { 56 for (Node xml_child = xml_node.getFirstChild(); 57 xml_child != null; 58 xml_child = xml_child.getNextSibling()) { 59 if (xml_child.getNodeType() == Node.TEXT_NODE) { 60 setCurrentValue(xml_child.getNodeValue()); 61 break; 62 } 63 } 64 } 65 } 66 67 if (isValid() && !getTextWidgetValue().equals(getCurrentValue())) { 68 try { 69 setInInternalTextModification(true); 70 setTextWidgetValue(getCurrentValue()); 71 setDirty(false); 72 } finally { 73 setInInternalTextModification(false); 74 } 75 } 76 } 77 78 /* (non-java doc) 79 * Called by the user interface when the editor is saved or its state changed 80 * and the modified "attributes" must be committed (i.e. written) to the XML model. 81 */ 82 @Override 83 public void commit() { 84 UiElementNode parent = getUiParent(); 85 if (parent != null && isValid() && isDirty()) { 86 // Get (or create) the underlying XML element node that contains the value. 87 Node element = parent.prepareCommit(); 88 if (element != null) { 89 String value = getTextWidgetValue(); 90 91 // Try to find an existing text child to update. 92 boolean updated = false; 93 94 for (Node xml_child = element.getFirstChild(); 95 xml_child != null; 96 xml_child = xml_child.getNextSibling()) { 97 if (xml_child.getNodeType() == Node.TEXT_NODE) { 98 xml_child.setNodeValue(value); 99 updated = true; 100 break; 101 } 102 } 103 104 // If we didn't find a text child to update, we need to create one. 105 if (!updated) { 106 Document doc = element.getOwnerDocument(); 107 if (doc != null) { 108 Text text = doc.createTextNode(value); 109 element.appendChild(text); 110 } 111 } 112 113 setCurrentValue(value); 114 } 115 } 116 setDirty(false); 117 } 118 } 119