Home | History | Annotate | Download | only in pages
      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.manifest.pages;
     18 
     19 import com.android.ide.eclipse.adt.AdtPlugin;
     20 import com.android.ide.eclipse.adt.internal.editors.IPageImageProvider;
     21 import com.android.ide.eclipse.adt.internal.editors.IconFactory;
     22 import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
     23 import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestEditor;
     24 import com.android.ide.eclipse.adt.internal.editors.manifest.descriptors.AndroidManifestDescriptors;
     25 import com.android.ide.eclipse.adt.internal.editors.ui.tree.UiTreeBlock;
     26 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
     27 
     28 import org.eclipse.swt.SWT;
     29 import org.eclipse.swt.graphics.Image;
     30 import org.eclipse.swt.layout.GridData;
     31 import org.eclipse.swt.widgets.Composite;
     32 import org.eclipse.ui.forms.IManagedForm;
     33 import org.eclipse.ui.forms.editor.FormPage;
     34 import org.eclipse.ui.forms.widgets.FormToolkit;
     35 import org.eclipse.ui.forms.widgets.ScrolledForm;
     36 
     37 import java.util.ArrayList;
     38 import java.util.HashSet;
     39 
     40 
     41 /**
     42  * Page for overview settings, part of the AndroidManifest form editor.
     43  * <p/>
     44  * Useful reference:
     45  * <a href="http://www.eclipse.org/articles/Article-Forms/article.html">
     46  *   http://www.eclipse.org/articles/Article-Forms/article.html</a>
     47  */
     48 public final class OverviewPage extends FormPage implements IPageImageProvider {
     49 
     50     /** Page id used for switching tabs programmatically */
     51     final static String PAGE_ID = "overview_page"; //$NON-NLS-1$
     52 
     53     /** Container editor */
     54     ManifestEditor mEditor;
     55     /** Overview part (attributes for manifest) */
     56     private OverviewInfoPart mOverviewPart;
     57     /** Overview link part */
     58     private OverviewLinksPart mOverviewLinkPart;
     59 
     60     private UiTreeBlock mTreeBlock;
     61 
     62     public OverviewPage(ManifestEditor editor) {
     63         super(editor, PAGE_ID, "Manifest");  // tab's label, user visible, keep it short
     64         mEditor = editor;
     65     }
     66 
     67     @Override
     68     public Image getPageImage() {
     69         return IconFactory.getInstance().getIcon("editor_page_design");  //$NON-NLS-1$
     70     }
     71 
     72     /**
     73      * Creates the content in the form hosted in this page.
     74      *
     75      * @param managedForm the form hosted in this page.
     76      */
     77     @Override
     78     protected void createFormContent(IManagedForm managedForm) {
     79         super.createFormContent(managedForm);
     80         ScrolledForm form = managedForm.getForm();
     81         form.setText("Android Manifest");
     82         form.setImage(AdtPlugin.getAndroidLogo());
     83 
     84         Composite body = form.getBody();
     85         FormToolkit toolkit = managedForm.getToolkit();
     86 
     87         // Usually we would set a ColumnLayout on body here. However the presence of the
     88         // UiTreeBlock forces a GridLayout with one column so we comply with it.
     89 
     90         mOverviewPart = new OverviewInfoPart(body, toolkit, mEditor);
     91         mOverviewPart.getSection().setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
     92         managedForm.addPart(mOverviewPart);
     93 
     94         newManifestExtrasPart(managedForm);
     95 
     96         OverviewExportPart exportPart = new OverviewExportPart(this, body, toolkit, mEditor);
     97         exportPart.getSection().setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
     98         managedForm.addPart(exportPart);
     99 
    100         mOverviewLinkPart = new OverviewLinksPart(body, toolkit, mEditor);
    101         mOverviewLinkPart.getSection().setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    102         managedForm.addPart(mOverviewLinkPart);
    103     }
    104 
    105     private void newManifestExtrasPart(IManagedForm managedForm) {
    106         UiElementNode manifest = mEditor.getUiRootNode();
    107         mTreeBlock = new UiTreeBlock(mEditor, manifest,
    108                 true /* autoCreateRoot */,
    109                 computeManifestExtraFilters(),
    110                 "Manifest Extras",
    111                 "Extra manifest elements");
    112         mTreeBlock.createContent(managedForm);
    113     }
    114 
    115     /**
    116      * Changes and refreshes the Application UI node handled by the sub parts.
    117      */
    118     public void refreshUiApplicationNode() {
    119         if (mOverviewPart != null) {
    120             mOverviewPart.onSdkChanged();
    121         }
    122 
    123         if (mOverviewLinkPart != null) {
    124             mOverviewLinkPart.onSdkChanged();
    125         }
    126 
    127         if (mTreeBlock != null) {
    128             UiElementNode manifest = mEditor.getUiRootNode();
    129             mTreeBlock.changeRootAndDescriptors(manifest,
    130                     computeManifestExtraFilters(),
    131                     true /* refresh */);
    132         }
    133     }
    134 
    135     private ElementDescriptor[] computeManifestExtraFilters() {
    136         UiElementNode manifest = mEditor.getUiRootNode();
    137         AndroidManifestDescriptors manifestDescriptor = mEditor.getManifestDescriptors();
    138 
    139         if (manifestDescriptor == null) {
    140             return null;
    141         }
    142 
    143         // get the elements we want to exclude
    144         HashSet<ElementDescriptor> excludes = new HashSet<ElementDescriptor>();
    145         excludes.add(manifestDescriptor.getApplicationElement());
    146         excludes.add(manifestDescriptor.getInstrumentationElement());
    147         excludes.add(manifestDescriptor.getPermissionElement());
    148         excludes.add(manifestDescriptor.getPermissionGroupElement());
    149         excludes.add(manifestDescriptor.getPermissionTreeElement());
    150         excludes.add(manifestDescriptor.getUsesPermissionElement());
    151 
    152         // walk through the known children of the manifest descriptor and keep what's not excluded
    153         ArrayList<ElementDescriptor> descriptorFilters = new ArrayList<ElementDescriptor>();
    154         for (ElementDescriptor child : manifest.getDescriptor().getChildren()) {
    155             if (!excludes.contains(child)) {
    156                 descriptorFilters.add(child);
    157             }
    158         }
    159 
    160         if (descriptorFilters.size() == 0) {
    161             return null;
    162         }
    163         return descriptorFilters.toArray(new ElementDescriptor[descriptorFilters.size()]);
    164     }
    165 }
    166