• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AttributeDescriptor;
20 
21 import org.w3c.dom.Node;
22 
23 /**
24  * Represents an XML attribute in that can be modified using a simple text field
25  * in the XML editor's user interface.
26  * <p/>
27  * The XML attribute has no default value. When unset, the text field is blank.
28  * When updating the XML, if the field is empty, the attribute will be removed
29  * from the XML element.
30  * <p/>
31  * See {@link UiAttributeNode} for more information.
32  */
33 public abstract class UiAbstractTextAttributeNode extends UiAttributeNode
34     implements IUiSettableAttributeNode {
35 
36     protected static final String DEFAULT_VALUE = "";  //$NON-NLS-1$
37 
38     /** Prevent internal listener from firing when internally modifying the text */
39     private boolean mInternalTextModification;
40     /** Last value read from the XML model. Cannot be null. */
41     private String mCurrentValue = DEFAULT_VALUE;
42 
UiAbstractTextAttributeNode(AttributeDescriptor attributeDescriptor, UiElementNode uiParent)43     public UiAbstractTextAttributeNode(AttributeDescriptor attributeDescriptor,
44             UiElementNode uiParent) {
45         super(attributeDescriptor, uiParent);
46     }
47 
48     /** Returns the current value of the node. */
49     @Override
getCurrentValue()50     public final String getCurrentValue() {
51         return mCurrentValue;
52     }
53 
54     /** Sets the current value of the node. Cannot be null (use an empty string). */
setCurrentValue(String value)55     public final void setCurrentValue(String value) {
56         mCurrentValue = value;
57     }
58 
59     /** Returns if the attribute node is valid, and its UI has been created. */
isValid()60     public abstract boolean isValid();
61 
62     /** Returns the text value present in the UI. */
getTextWidgetValue()63     public abstract String getTextWidgetValue();
64 
65     /** Sets the text value to be displayed in the UI. */
setTextWidgetValue(String value)66     public abstract void setTextWidgetValue(String value);
67 
68 
69     /**
70      * Updates the current text field's value when the XML has changed.
71      * <p/>
72      * The caller doesn't really know if attributes have changed,
73      * so it will call this to refresh the attribute anyway. The value
74      * is only set if it has changed.
75      * <p/>
76      * This also resets the "dirty" flag.
77     */
78     @Override
updateValue(Node xml_attribute_node)79     public void updateValue(Node xml_attribute_node) {
80         mCurrentValue = DEFAULT_VALUE;
81         if (xml_attribute_node != null) {
82             mCurrentValue = xml_attribute_node.getNodeValue();
83         }
84 
85         if (isValid() && !getTextWidgetValue().equals(mCurrentValue)) {
86             try {
87                 mInternalTextModification = true;
88                 setTextWidgetValue(mCurrentValue);
89                 setDirty(false);
90             } finally {
91                 mInternalTextModification = false;
92             }
93         }
94     }
95 
96     /* (non-java doc)
97      * Called by the user interface when the editor is saved or its state changed
98      * and the modified attributes must be committed (i.e. written) to the XML model.
99      */
100     @Override
commit()101     public void commit() {
102         UiElementNode parent = getUiParent();
103         if (parent != null && isValid() && isDirty()) {
104             String value = getTextWidgetValue();
105             if (parent.commitAttributeToXml(this, value)) {
106                 mCurrentValue = value;
107                 setDirty(false);
108             }
109         }
110     }
111 
isInInternalTextModification()112     protected final boolean isInInternalTextModification() {
113         return mInternalTextModification;
114     }
115 
setInInternalTextModification(boolean internalTextModification)116     protected final void setInInternalTextModification(boolean internalTextModification) {
117         mInternalTextModification = internalTextModification;
118     }
119 }
120