Home | History | Annotate | Download | only in sourcelookup
      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.sourcelookup;
     19 
     20 import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
     21 import com.android.ide.eclipse.adt.internal.sdk.Sdk;
     22 import com.android.sdklib.IAndroidTarget;
     23 
     24 import org.eclipse.core.resources.IProject;
     25 import org.eclipse.core.resources.ResourcesPlugin;
     26 import org.eclipse.core.runtime.CoreException;
     27 import org.eclipse.core.runtime.IPath;
     28 import org.eclipse.debug.core.ILaunchConfiguration;
     29 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
     30 import org.eclipse.debug.core.sourcelookup.containers.DefaultSourceContainer;
     31 import org.eclipse.debug.core.sourcelookup.containers.DirectorySourceContainer;
     32 import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
     33 import org.eclipse.jdt.core.IClasspathEntry;
     34 import org.eclipse.jdt.core.IJavaProject;
     35 import org.eclipse.jdt.core.JavaCore;
     36 import org.eclipse.jdt.internal.launching.JavaSourceLookupDirector;
     37 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
     38 
     39 import java.io.File;
     40 
     41 public class AdtSourceLookupDirector extends JavaSourceLookupDirector {
     42 
     43     @Override
     44     public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
     45         dispose();
     46         setLaunchConfiguration(configuration);
     47         String projectName =
     48             configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
     49                 ""); //$NON-NLS-1$
     50         if (projectName != null && projectName.length() > 0) {
     51             IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
     52             if (project != null && project.isOpen()) {
     53                 ProjectState state = Sdk.getProjectState(project);
     54                 if (state == null) {
     55                     initDefaults();
     56                     return;
     57                 }
     58                 IAndroidTarget target = state.getTarget();
     59                 if (target == null) {
     60                     initDefaults();
     61                     return;
     62                 }
     63                 String path = target.getPath(IAndroidTarget.ANDROID_JAR);
     64                 if (path == null) {
     65                     initDefaults();
     66                     return;
     67                 }
     68                 IJavaProject javaProject = JavaCore.create(project);
     69                 if (javaProject != null && javaProject.isOpen()) {
     70                     IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
     71                     IClasspathEntry androidEntry = null;
     72                     for (int i = 0; i < entries.length; i++) {
     73                         IClasspathEntry entry = entries[i];
     74                         if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
     75                                 && path.equals(entry.getPath().toString())) {
     76                             androidEntry = entry;
     77                             break;
     78                         }
     79                     }
     80                     if (androidEntry != null) {
     81                         IPath sourceAttachmentPath = androidEntry.getSourceAttachmentPath();
     82                         if (sourceAttachmentPath != null) {
     83                             String androidSrc = sourceAttachmentPath.toString();
     84                             if (androidSrc != null && androidSrc.trim().length() > 0) {
     85                                 File srcFile = new File(androidSrc);
     86                                 ISourceContainer adtContainer = null;
     87                                 if (srcFile.isFile()) {
     88                                     adtContainer = new ExternalArchiveSourceContainer(androidSrc,
     89                                             true);
     90                                 }
     91                                 if (srcFile.isDirectory()) {
     92                                     adtContainer = new DirectorySourceContainer(srcFile, false);
     93                                 }
     94                                 if (adtContainer != null) {
     95                                     ISourceContainer defaultContainer =
     96                                         new DefaultSourceContainer();
     97                                     setSourceContainers(new ISourceContainer[] {
     98                                             adtContainer, defaultContainer
     99                                     });
    100                                     initializeParticipants();
    101                                     return;
    102                                 }
    103                             }
    104                         }
    105                     }
    106                 }
    107             }
    108         }
    109         initDefaults();
    110     }
    111 
    112     private void initDefaults() {
    113         setSourceContainers(new ISourceContainer[] {
    114             new DefaultSourceContainer()
    115         });
    116         initializeParticipants();
    117     }
    118 
    119 }
    120