aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/uimodel/UiTextAttributeNode.java
blob: 504ac3122ac5ce296a40785bd77bdf8336b9debf (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * 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.AndroidXmlEditor;
import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
import com.android.ide.eclipse.adt.internal.editors.descriptors.DescriptorsUtils;
import com.android.ide.eclipse.adt.internal.editors.descriptors.TextAttributeDescriptor;
import com.android.ide.eclipse.adt.internal.editors.ui.SectionHelper;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.TableWrapData;

/**
 * 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 class UiTextAttributeNode extends UiAbstractTextAttributeNode {

    /** Text field */
    private Text mText;
    /** The managed form, set only once createUiControl has been called. */
    private IManagedForm mManagedForm;

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

    /* (non-java doc)
     * Creates a label widget and an associated text field.
     * <p/>
     * As most other parts of the android manifest editor, this assumes the
     * parent uses a table layout with 2 columns.
     */
    @Override
    public void createUiControl(Composite parent, IManagedForm managedForm) {
        setManagedForm(managedForm);
        TextAttributeDescriptor desc = (TextAttributeDescriptor) getDescriptor();
        Text text = SectionHelper.createLabelAndText(parent, managedForm.getToolkit(),
                desc.getUiName(), getCurrentValue(),
                DescriptorsUtils.formatTooltip(desc.getTooltip()));

        setTextWidget(text);
    }

    /**
     * No completion values for this UI attribute.
     *
     * {@inheritDoc}
     */
    @Override
    public String[] getPossibleValues(String prefix) {
        return null;
    }

    /**
     * Sets the internal managed form.
     * This is usually set by createUiControl.
     */
    protected void setManagedForm(IManagedForm managedForm) {
         mManagedForm = managedForm;
    }

    /**
     * @return The managed form, set only once createUiControl has been called.
     */
    protected IManagedForm getManagedForm() {
        return mManagedForm;
    }

    /* (non-java doc)
     * Returns if the attribute node is valid, and its UI has been created.
     */
    @Override
    public boolean isValid() {
        return mText != null;
    }

    @Override
    public String getTextWidgetValue() {
        if (mText != null) {
            return mText.getText();
        }

        return null;
    }

    @Override
    public void setTextWidgetValue(String value) {
        if (mText != null) {
            mText.setText(value);
        }
    }

    /**
     * Sets the Text widget object, and prepares it to handle modification and synchronization
     * with the XML node.
     * @param textWidget
     */
    protected final void setTextWidget(Text textWidget) {
        mText = textWidget;

        if (textWidget != null) {
            // Sets the with hint for the text field. Derived classes can always override it.
            // This helps the grid layout to resize correctly on smaller screen sizes.
            Object data = textWidget.getLayoutData();
            if (data == null) {
            } else if (data instanceof GridData) {
                ((GridData)data).widthHint = AndroidXmlEditor.TEXT_WIDTH_HINT;
            } else if (data instanceof TableWrapData) {
                ((TableWrapData)data).maxWidth = 100;
            }

            mText.addModifyListener(new ModifyListener() {
                /**
                 * Sent when the text is modified, whether by the user via manual
                 * input or programmatic input via setText().
                 * <p/>
                 * Simply mark the attribute as dirty if it really changed.
                 * The container SectionPart will collect these flag and manage them.
                 */
                @Override
                public void modifyText(ModifyEvent e) {
                    if (!isInInternalTextModification() &&
                            !isDirty() &&
                            mText != null &&
                            getCurrentValue() != null &&
                            !mText.getText().equals(getCurrentValue())) {
                        setDirty(true);
                    }
                }
            });

            // Remove self-reference when the widget is disposed
            mText.addDisposeListener(new DisposeListener() {
                @Override
                public void widgetDisposed(DisposeEvent e) {
                    mText = null;
                }
            });
        }

        onAddValidators(mText);
    }

    /**
     * Called after the text widget as been created.
     * <p/>
     * Derived classes typically want to:
     * <li> Create a new {@link ModifyListener} and attach it to the given {@link Text} widget.
     * <li> In the modify listener, call getManagedForm().getMessageManager().addMessage()
     *      and getManagedForm().getMessageManager().removeMessage() as necessary.
     * <li> Call removeMessage in a new text.addDisposeListener.
     * <li> Call the validator once to setup the initial messages as needed.
     * <p/>
     * The base implementation does nothing.
     *
     * @param text The {@link Text} widget to validate.
     */
    protected void onAddValidators(Text text) {
    }

    /**
     * Returns the text widget.
     */
    protected final Text getTextWidget() {
        return mText;
    }
}