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.manifest.pages;
19 import com.android.ide.eclipse.adt.AdtPlugin;
20 import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
21 import com.android.ide.eclipse.adt.internal.editors.descriptors.XmlnsAttributeDescriptor;
22 import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestEditor;
23 import com.android.ide.eclipse.adt.internal.editors.ui.UiElementPart;
24 import com.android.ide.eclipse.adt.internal.editors.uimodel.IUiUpdateListener;
25 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiAttributeNode;
26 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.ui.forms.IManagedForm;
32 import org.eclipse.ui.forms.widgets.FormToolkit;
33 import org.eclipse.ui.forms.widgets.Section;
36 * Application's attributes section part for Application page.
38 * This part is displayed at the top of the application page and displays all the possible
39 * attributes of an application node in the AndroidManifest (icon, class name, label, etc.)
41 final class ApplicationAttributesPart extends UiElementPart {
43 /** Listen to changes to the UI node for <application> and updates the UI */
44 private AppNodeUpdateListener mAppNodeUpdateListener;
45 /** ManagedForm needed to create the UI controls */
46 private IManagedForm mManagedForm;
48 public ApplicationAttributesPart(Composite body, FormToolkit toolkit, ManifestEditor editor,
49 UiElementNode applicationUiNode) {
50 super(body, toolkit, editor, applicationUiNode,
51 "Application Attributes", // section title
52 "Defines the attributes specific to the application.", // section description
53 Section.TWISTIE | Section.EXPANDED);
57 * Changes and refreshes the Application UI node handle by the this part.
60 public void setUiElementNode(UiElementNode uiElementNode) {
61 super.setUiElementNode(uiElementNode);
63 createUiAttributes(mManagedForm);
67 * Create the controls to edit the attributes for the given ElementDescriptor.
69 * This MUST not be called by the constructor. Instead it must be called from
70 * <code>initialize</code> (i.e. right after the form part is added to the managed form.)
72 * Derived classes can override this if necessary.
74 * @param managedForm The owner managed form
77 protected void createFormControls(final IManagedForm managedForm) {
78 mManagedForm = managedForm;
79 setTable(createTableLayout(managedForm.getToolkit(), 4 /* numColumns */));
81 mAppNodeUpdateListener = new AppNodeUpdateListener();
82 getUiElementNode().addUpdateListener(mAppNodeUpdateListener);
84 createUiAttributes(mManagedForm);
88 public void dispose() {
90 if (getUiElementNode() != null && mAppNodeUpdateListener != null) {
91 getUiElementNode().removeUpdateListener(mAppNodeUpdateListener);
92 mAppNodeUpdateListener = null;
97 protected void createUiAttributes(IManagedForm managedForm) {
98 Composite table = getTable();
99 if (table == null || managedForm == null) {
103 // Remove any old UI controls
104 for (Control c : table.getChildren()) {
108 UiElementNode uiElementNode = getUiElementNode();
109 AttributeDescriptor[] attr_desc_list = uiElementNode.getAttributeDescriptors();
111 // Display the attributes in 2 columns:
116 // that is we have to fill the grid in order 0, 4, 1, 5, 2, 6, 3, 7
117 // thus index = i/2 + (i is odd * n/2)
118 int n = attr_desc_list.length;
119 int n2 = (int) Math.ceil(n / 2.0);
120 for (int i = 0; i < n; i++) {
121 AttributeDescriptor attr_desc = attr_desc_list[i / 2 + (i & 1) * n2];
122 if (attr_desc instanceof XmlnsAttributeDescriptor) {
123 // Do not show hidden attributes
127 UiAttributeNode ui_attr = uiElementNode.findUiAttribute(attr_desc);
128 if (ui_attr != null) {
129 ui_attr.createUiControl(table, managedForm);
131 // The XML has an extra attribute which wasn't declared in
132 // AndroidManifestDescriptors. This is not a problem, we just ignore it.
133 AdtPlugin.log(IStatus.WARNING,
134 "Attribute %1$s not declared in node %2$s, ignored.", //$NON-NLS-1$
135 attr_desc.getXmlLocalName(),
136 uiElementNode.getDescriptor().getXmlName());
141 createLabel(table, managedForm.getToolkit(),
142 "No attributes to display, waiting for SDK to finish loading...",
143 null /* tooltip */ );
146 // Initialize the enabled/disabled state
147 if (mAppNodeUpdateListener != null) {
148 mAppNodeUpdateListener.uiElementNodeUpdated(uiElementNode, null /* state, not used */);
151 // Tell the section that the layout has changed.
156 * This listener synchronizes the UI with the actual presence of the application XML node.
158 private class AppNodeUpdateListener implements IUiUpdateListener {
159 public void uiElementNodeUpdated(UiElementNode ui_node, UiUpdateState state) {
160 // The UiElementNode for the application XML node always exists, even
161 // if there is no corresponding XML node in the XML file.
163 // We enable the UI here if the XML node is not null.
164 Composite table = getTable();
165 boolean exists = (ui_node.getXmlNode() != null);
166 if (table != null && table.getEnabled() != exists) {
167 table.setEnabled(exists);
168 for (Control c : table.getChildren()) {
169 c.setEnabled(exists);