Home | History | Annotate | Download | only in project
      1 /*
      2  * Copyright (C) 2010 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 
     18 package com.android.ide.eclipse.adt.internal.project;
     19 
     20 import com.android.ide.eclipse.adt.AdtConstants;
     21 
     22 import org.eclipse.core.resources.IProject;
     23 import org.eclipse.core.resources.ResourcesPlugin;
     24 import org.eclipse.core.runtime.IPath;
     25 import org.eclipse.core.runtime.IStatus;
     26 import org.eclipse.core.runtime.Path;
     27 import org.eclipse.jdt.core.IClasspathEntry;
     28 import org.eclipse.jdt.core.IJavaProject;
     29 import org.eclipse.jdt.core.JavaCore;
     30 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
     31 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
     32 import org.eclipse.jdt.ui.wizards.IClasspathContainerPage;
     33 import org.eclipse.jdt.ui.wizards.IClasspathContainerPageExtension;
     34 import org.eclipse.jface.wizard.WizardPage;
     35 import org.eclipse.swt.SWT;
     36 import org.eclipse.swt.layout.GridData;
     37 import org.eclipse.swt.layout.GridLayout;
     38 import org.eclipse.swt.widgets.Combo;
     39 import org.eclipse.swt.widgets.Composite;
     40 import org.eclipse.swt.widgets.Label;
     41 
     42 import java.util.Arrays;
     43 
     44 public class AndroidClasspathContainerPage extends WizardPage implements IClasspathContainerPage,
     45         IClasspathContainerPageExtension {
     46 
     47     private IProject mOwnerProject;
     48 
     49     private String mLibsProjectName;
     50 
     51     private Combo mProjectsCombo;
     52 
     53     private IStatus mCurrStatus;
     54 
     55     private boolean mPageVisible;
     56 
     57     public AndroidClasspathContainerPage() {
     58         super("AndroidClasspathContainerPage"); //$NON-NLS-1$
     59         mPageVisible = false;
     60         mCurrStatus = new StatusInfo();
     61         setTitle("Android Libraries");
     62         setDescription("This container manages classpath entries for Android container");
     63     }
     64 
     65     @Override
     66     public IClasspathEntry getSelection() {
     67         IPath path = new Path(AdtConstants.CONTAINER_FRAMEWORK);
     68 
     69         final int index = this.mProjectsCombo.getSelectionIndex();
     70         if (index != -1) {
     71             final String selectedProjectName = this.mProjectsCombo.getItem(index);
     72 
     73             if (this.mOwnerProject == null
     74                     || !selectedProjectName.equals(this.mOwnerProject.getName())) {
     75                 path = path.append(selectedProjectName);
     76             }
     77         }
     78 
     79         return JavaCore.newContainerEntry(path);
     80     }
     81 
     82     @Override
     83     public void setSelection(final IClasspathEntry cpentry) {
     84         final IPath path = cpentry == null ? null : cpentry.getPath();
     85 
     86         if (path == null || path.segmentCount() == 1) {
     87             if (this.mOwnerProject != null) {
     88                 this.mLibsProjectName = this.mOwnerProject.getName();
     89             }
     90         } else {
     91             this.mLibsProjectName = path.segment(1);
     92         }
     93     }
     94 
     95     @Override
     96     public void createControl(final Composite parent) {
     97         final Composite composite = new Composite(parent, SWT.NONE);
     98         composite.setLayout(new GridLayout(2, false));
     99 
    100         final Label label = new Label(composite, SWT.NONE);
    101         label.setText("Project:");
    102 
    103         final String[] androidProjects = getAndroidProjects();
    104 
    105         this.mProjectsCombo = new Combo(composite, SWT.READ_ONLY);
    106         this.mProjectsCombo.setItems(androidProjects);
    107 
    108         final int index;
    109 
    110         if (this.mOwnerProject != null) {
    111             index = indexOf(androidProjects, this.mLibsProjectName);
    112         } else {
    113             if (this.mProjectsCombo.getItemCount() > 0) {
    114                 index = 0;
    115             } else {
    116                 index = -1;
    117             }
    118         }
    119 
    120         if (index != -1) {
    121             this.mProjectsCombo.select(index);
    122         }
    123 
    124         final GridData gd = new GridData();
    125         gd.grabExcessHorizontalSpace = true;
    126         gd.minimumWidth = 100;
    127 
    128         this.mProjectsCombo.setLayoutData(gd);
    129 
    130         setControl(composite);
    131     }
    132 
    133     @Override
    134     public boolean finish() {
    135         return true;
    136     }
    137 
    138     @Override
    139     public void setVisible(boolean visible) {
    140         super.setVisible(visible);
    141         mPageVisible = visible;
    142         // policy: wizards are not allowed to come up with an error message
    143         if (visible && mCurrStatus.matches(IStatus.ERROR)) {
    144             StatusInfo status = new StatusInfo();
    145             status.setError(""); //$NON-NLS-1$
    146             mCurrStatus = status;
    147         }
    148         updateStatus(mCurrStatus);
    149     }
    150 
    151     /**
    152      * Updates the status line and the OK button according to the given status
    153      *
    154      * @param status status to apply
    155      */
    156     protected void updateStatus(IStatus status) {
    157         mCurrStatus = status;
    158         setPageComplete(!status.matches(IStatus.ERROR));
    159         if (mPageVisible) {
    160             StatusUtil.applyToStatusLine(this, status);
    161         }
    162     }
    163 
    164     /**
    165      * Updates the status line and the OK button according to the status
    166      * evaluate from an array of status. The most severe error is taken. In case
    167      * that two status with the same severity exists, the status with lower
    168      * index is taken.
    169      *
    170      * @param status the array of status
    171      */
    172     protected void updateStatus(IStatus[] status) {
    173         updateStatus(StatusUtil.getMostSevere(status));
    174     }
    175 
    176     @Override
    177     public void initialize(final IJavaProject project, final IClasspathEntry[] currentEntries) {
    178         this.mOwnerProject = (project == null ? null : project.getProject());
    179     }
    180 
    181     private static String[] getAndroidProjects() {
    182         IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    183         final String[] names = new String[projects.length];
    184         for (int i = 0; i < projects.length; i++) {
    185             names[i] = projects[i].getName();
    186         }
    187         Arrays.sort(names);
    188         return names;
    189     }
    190 
    191     private static int indexOf(final String[] array, final String str) {
    192         for (int i = 0; i < array.length; i++) {
    193             if (array[i].equals(str)) {
    194                 return i;
    195             }
    196         }
    197         return -1;
    198     }
    199 
    200 }
    201