2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.eclipse.org/org/documents/epl-v10.php
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.
17 package com.android.ide.eclipse.adt.internal.editors.uimodel;
19 import com.android.ide.eclipse.adt.internal.editors.AndroidEditor;
20 import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
21 import com.android.ide.eclipse.adt.internal.editors.descriptors.DescriptorsUtils;
22 import com.android.ide.eclipse.adt.internal.editors.descriptors.TextAttributeDescriptor;
23 import com.android.ide.eclipse.adt.internal.editors.ui.SectionHelper;
25 import org.eclipse.swt.events.DisposeEvent;
26 import org.eclipse.swt.events.DisposeListener;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.ui.forms.IManagedForm;
33 import org.eclipse.ui.forms.widgets.TableWrapData;
36 * Represents an XML attribute in that can be modified using a simple text field
37 * in the XML editor's user interface.
39 * The XML attribute has no default value. When unset, the text field is blank.
40 * When updating the XML, if the field is empty, the attribute will be removed
41 * from the XML element.
43 * See {@link UiAttributeNode} for more information.
45 public class UiTextAttributeNode extends UiAbstractTextAttributeNode {
49 /** The managed form, set only once createUiControl has been called. */
50 private IManagedForm mManagedForm;
52 public UiTextAttributeNode(AttributeDescriptor attributeDescriptor, UiElementNode uiParent) {
53 super(attributeDescriptor, uiParent);
57 * Creates a label widget and an associated text field.
59 * As most other parts of the android manifest editor, this assumes the
60 * parent uses a table layout with 2 columns.
63 public void createUiControl(Composite parent, IManagedForm managedForm) {
64 setManagedForm(managedForm);
65 TextAttributeDescriptor desc = (TextAttributeDescriptor) getDescriptor();
66 Text text = SectionHelper.createLabelAndText(parent, managedForm.getToolkit(),
67 desc.getUiName(), getCurrentValue(),
68 DescriptorsUtils.formatTooltip(desc.getTooltip()));
74 * No completion values for this UI attribute.
79 public String[] getPossibleValues(String prefix) {
84 * Sets the internal managed form.
85 * This is usually set by createUiControl.
87 protected void setManagedForm(IManagedForm managedForm) {
88 mManagedForm = managedForm;
92 * @return The managed form, set only once createUiControl has been called.
94 protected IManagedForm getManagedForm() {
99 * Returns if the attribute node is valid, and its UI has been created.
102 public boolean isValid() {
103 return mText != null;
107 public String getTextWidgetValue() {
109 return mText.getText();
116 public void setTextWidgetValue(String value) {
118 mText.setText(value);
123 * Sets the Text widget object, and prepares it to handle modification and synchronization
127 protected final void setTextWidget(Text textWidget) {
130 if (textWidget != null) {
131 // Sets the with hint for the text field. Derived classes can always override it.
132 // This helps the grid layout to resize correctly on smaller screen sizes.
133 Object data = textWidget.getLayoutData();
135 } else if (data instanceof GridData) {
136 ((GridData)data).widthHint = AndroidEditor.TEXT_WIDTH_HINT;
137 } else if (data instanceof TableWrapData) {
138 ((TableWrapData)data).maxWidth = 100;
141 mText.addModifyListener(new ModifyListener() {
143 * Sent when the text is modified, whether by the user via manual
144 * input or programmatic input via setText().
146 * Simply mark the attribute as dirty if it really changed.
147 * The container SectionPart will collect these flag and manage them.
149 public void modifyText(ModifyEvent e) {
150 if (!isInInternalTextModification() &&
153 getCurrentValue() != null &&
154 !mText.getText().equals(getCurrentValue())) {
160 // Remove self-reference when the widget is disposed
161 mText.addDisposeListener(new DisposeListener() {
162 public void widgetDisposed(DisposeEvent e) {
168 onAddValidators(mText);
172 * Called after the text widget as been created.
174 * Derived classes typically want to:
175 * <li> Create a new {@link ModifyListener} and attach it to the given {@link Text} widget.
176 * <li> In the modify listener, call getManagedForm().getMessageManager().addMessage()
177 * and getManagedForm().getMessageManager().removeMessage() as necessary.
178 * <li> Call removeMessage in a new text.addDisposeListener.
179 * <li> Call the validator once to setup the initial messages as needed.
181 * The base implementation does nothing.
183 * @param text The {@link Text} widget to validate.
185 protected void onAddValidators(Text text) {
189 * Returns the text widget.
191 protected final Text getTextWidget() {