aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/uimodel/UiAbstractTextAttributeNode.java
blob: 4f795904d3d765b315ac0793006a53dc5cc26330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.uimodel;

import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;

import org.w3c.dom.Node;

/**
 * Represents an XML attribute in that can be modified using a simple text field
 * in the XML editor's user interface.
 * <p/>
 * The XML attribute has no default value. When unset, the text field is blank.
 * When updating the XML, if the field is empty, the attribute will be removed
 * from the XML element.
 * <p/>
 * See {@link UiAttributeNode} for more information.
 */
public abstract class UiAbstractTextAttributeNode extends UiAttributeNode
    implements IUiSettableAttributeNode {

    protected static final String DEFAULT_VALUE = "";  //$NON-NLS-1$

    /** Prevent internal listener from firing when internally modifying the text */
    private boolean mInternalTextModification;
    /** Last value read from the XML model. Cannot be null. */
    private String mCurrentValue = DEFAULT_VALUE;

    public UiAbstractTextAttributeNode(AttributeDescriptor attributeDescriptor,
            UiElementNode uiParent) {
        super(attributeDescriptor, uiParent);
    }

    /** Returns the current value of the node. */
    @Override
    public final String getCurrentValue() {
        return mCurrentValue;
    }

    /** Sets the current value of the node. Cannot be null (use an empty string). */
    @Override
    public final void setCurrentValue(String value) {
        mCurrentValue = value;
    }

    /** Returns if the attribute node is valid, and its UI has been created. */
    public abstract boolean isValid();

    /** Returns the text value present in the UI. */
    public abstract String getTextWidgetValue();

    /** Sets the text value to be displayed in the UI. */
    public abstract void setTextWidgetValue(String value);


    /**
     * Updates the current text field's value when the XML has changed.
     * <p/>
     * The caller doesn't really know if attributes have changed,
     * so it will call this to refresh the attribute anyway. The value
     * is only set if it has changed.
     * <p/>
     * This also resets the "dirty" flag.
    */
    @Override
    public void updateValue(Node xml_attribute_node) {
        mCurrentValue = DEFAULT_VALUE;
        if (xml_attribute_node != null) {
            mCurrentValue = xml_attribute_node.getNodeValue();
        }

        if (isValid() && !getTextWidgetValue().equals(mCurrentValue)) {
            try {
                mInternalTextModification = true;
                setTextWidgetValue(mCurrentValue);
                setDirty(false);
            } finally {
                mInternalTextModification = false;
            }
        }
    }

    /* (non-java doc)
     * Called by the user interface when the editor is saved or its state changed
     * and the modified attributes must be committed (i.e. written) to the XML model.
     */
    @Override
    public void commit() {
        UiElementNode parent = getUiParent();
        if (parent != null && isValid() && isDirty()) {
            String value = getTextWidgetValue();
            if (parent.commitAttributeToXml(this, value)) {
                mCurrentValue = value;
                setDirty(false);
            }
        }
    }

    protected final boolean isInInternalTextModification() {
        return mInternalTextModification;
    }

    protected final void setInInternalTextModification(boolean internalTextModification) {
        mInternalTextModification = internalTextModification;
    }
}