Ejemplo n.º 1
0
    private void setProject(ProjectState project) {
      mProjectState = project;
      mPath = project.getProject().getLocation().toOSString();
      mProjectState.addParentProject(getMainProjectState());

      getMainProjectState().updateFullLibraryList();
    }
  /* (non-Javadoc)
   * @see org.eclipse.debug.ui.ILaunchShortcut#launch(
   * org.eclipse.jface.viewers.ISelection, java.lang.String)
   */
  @Override
  public void launch(ISelection selection, String mode) {
    if (selection instanceof IStructuredSelection) {

      // get the object and the project from it
      IStructuredSelection structSelect = (IStructuredSelection) selection;
      Object o = structSelect.getFirstElement();

      // get the first (and normally only) element
      if (o instanceof IAdaptable) {
        IResource r = (IResource) ((IAdaptable) o).getAdapter(IResource.class);

        // get the project from the resource
        if (r != null) {
          IProject project = r.getProject();

          if (project != null) {
            ProjectState state = Sdk.getProjectState(project);
            if (state != null && state.isLibrary()) {

              MessageDialog.openError(
                  PlatformUI.getWorkbench().getDisplay().getActiveShell(),
                  "Android Launch",
                  "Android library projects cannot be launched.");
            } else {
              // and launch
              launch(project, mode);
            }
          }
        }
      }
    }
  }
  @Override
  public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    if (pm.isCanceled()) {
      return null;
    }
    if (!getArguments().getUpdateReferences()) {
      return null;
    }
    CompositeChange result = new CompositeChange(getName());
    result.markAsSynthetic();

    addManifestFileChanges(result);

    // Update layout files; we don't just need to react to custom view
    // changes, we need to update fragment references and even tool:context activity
    // references
    addLayoutFileChanges(mProject, result);

    // Also update in dependent projects
    ProjectState projectState = Sdk.getProjectState(mProject);
    if (projectState != null) {
      Collection<ProjectState> parentProjects = projectState.getFullParentProjects();
      for (ProjectState parentProject : parentProjects) {
        IProject project = parentProject.getProject();
        addLayoutFileChanges(project, result);
      }
    }

    return (result.getChildren().length == 0) ? null : result;
  }
Ejemplo n.º 4
0
  /**
   * Returns the {@link LibraryState} object for a given <var>name</var>. This can only return a
   * non-null object if the link between the main project's {@link IProject} and the library's
   * {@link IProject} was done.
   *
   * @return the matching LibraryState or <code>null</code>
   * @see #needs(IProject)
   */
  public LibraryState getLibrary(String name) {
    synchronized (mLibraries) {
      for (LibraryState state : mLibraries) {
        ProjectState ps = state.getProjectState();
        if (ps != null && ps.getProject().getName().equals(name)) {
          return state;
        }
      }
    }

    return null;
  }
Ejemplo n.º 5
0
  /**
   * Returns the {@link LibraryState} object for a given {@link IProject}. This can only return a
   * non-null object if the link between the main project's {@link IProject} and the library's
   * {@link IProject} was done.
   *
   * @return the matching LibraryState or <code>null</code>
   * @see #needs(ProjectState)
   */
  public LibraryState getLibrary(IProject library) {
    synchronized (mLibraries) {
      for (LibraryState state : mLibraries) {
        ProjectState ps = state.getProjectState();
        if (ps != null && ps.getProject().equals(library)) {
          return state;
        }
      }
    }

    return null;
  }
Ejemplo n.º 6
0
    /**
     * Closes the library. This resets the IProject from this object ({@link #getProjectState()}
     * will return <code>null</code>), and updates the main project data so that the library {@link
     * IProject} object does not show up in the return value of {@link
     * ProjectState#getFullLibraryProjects()}.
     */
    public void close() {
      mProjectState.removeParentProject(getMainProjectState());
      mProjectState = null;
      mPath = null;

      getMainProjectState().updateFullLibraryList();
    }
Ejemplo n.º 7
0
  /**
   * Returns whether a given library project is needed by the receiver.
   *
   * <p>If the library is needed, this finds the matching {@link LibraryState}, initializes it so
   * that it contains the library's {@link IProject} object (so that {@link
   * LibraryState#getProjectState()} does not return null) and then returns it.
   *
   * @param libraryProject the library project to check.
   * @return a non null object if the project is a library dependency, <code>null</code> otherwise.
   * @see LibraryState#getProjectState()
   */
  public LibraryState needs(ProjectState libraryProject) {
    // compute current location
    File projectFile = mProject.getLocation().toFile();

    // get the location of the library.
    File libraryFile = libraryProject.getProject().getLocation().toFile();

    // loop on all libraries and check if the path match
    synchronized (mLibraries) {
      for (LibraryState state : mLibraries) {
        if (state.getProjectState() == null) {
          File library = new File(projectFile, state.getRelativePath());
          try {
            File absPath = library.getCanonicalFile();
            if (absPath.equals(libraryFile)) {
              state.setProject(libraryProject);
              return state;
            }
          } catch (IOException e) {
            // ignore this library
          }
        }
      }
    }

    return null;
  }
Ejemplo n.º 8
0
  /**
   * Resolves a given list of libraries, finds out if they depend on other libraries, and returns a
   * full list of all the direct and indirect dependencies in the proper order (first is higher
   * priority when calling aapt).
   *
   * @param inLibraries the libraries to resolve
   * @param outLibraries where to store all the libraries.
   */
  private void buildFullLibraryDependencies(
      List<LibraryState> inLibraries, ArrayList<IProject> outLibraries) {
    // loop in the inverse order to resolve dependencies on the libraries, so that if a library
    // is required by two higher level libraries it can be inserted in the correct place
    for (int i = inLibraries.size() - 1; i >= 0; i--) {
      LibraryState library = inLibraries.get(i);

      // get its libraries if possible
      ProjectState libProjectState = library.getProjectState();
      if (libProjectState != null) {
        List<LibraryState> dependencies = libProjectState.getLibraries();

        // build the dependencies for those libraries
        buildFullLibraryDependencies(dependencies, outLibraries);

        // and add the current library (if needed) in front (higher priority)
        if (outLibraries.contains(libProjectState.getProject()) == false) {
          outLibraries.add(0, libProjectState.getProject());
        }
      }
    }
  }
Ejemplo n.º 9
0
  /**
   * Returns whether the project depends on a given <var>library</var>
   *
   * @param library the library to check.
   * @return true if the project depends on the library. This is not affected by whether the link
   *     was done through {@link #needs(ProjectState)}.
   */
  public boolean dependsOn(ProjectState library) {
    synchronized (mLibraries) {
      for (LibraryState state : mLibraries) {
        if (state != null
            && state.getProjectState() != null
            && library.getProject().equals(state.getProjectState().getProject())) {
          return true;
        }
      }
    }

    return false;
  }
Ejemplo n.º 10
0
    @Override
    public boolean equals(Object obj) {
      if (obj instanceof LibraryState) {
        // the only thing that's always non-null is the relative path.
        LibraryState objState = (LibraryState) obj;
        return mRelativePath.equals(objState.mRelativePath)
            && getMainProjectState().equals(objState.getMainProjectState());
      } else if (obj instanceof ProjectState || obj instanceof IProject) {
        return mProjectState != null && mProjectState.equals(obj);
      } else if (obj instanceof String) {
        return normalizePath(mRelativePath).equals(normalizePath((String) obj));
      }

      return false;
    }
Ejemplo n.º 11
0
  private Pair<List<String>, List<String>> findViews(final boolean layoutsOnly) {
    final Set<String> customViews = new HashSet<String>();
    final Set<String> thirdPartyViews = new HashSet<String>();

    ProjectState state = Sdk.getProjectState(mProject);
    final List<IProject> libraries =
        state != null ? state.getFullLibraryProjects() : Collections.<IProject>emptyList();

    SearchRequestor requestor =
        new SearchRequestor() {
          @Override
          public void acceptSearchMatch(SearchMatch match) throws CoreException {
            // Ignore matches in comments
            if (match.isInsideDocComment()) {
              return;
            }

            Object element = match.getElement();
            if (element instanceof ResolvedBinaryType) {
              // Third party view
              ResolvedBinaryType type = (ResolvedBinaryType) element;
              IPackageFragment fragment = type.getPackageFragment();
              IPath path = fragment.getPath();
              String last = path.lastSegment();
              // Filter out android.jar stuff
              if (last.equals(FN_FRAMEWORK_LIBRARY)) {
                return;
              }
              if (!isValidView(type, layoutsOnly)) {
                return;
              }

              IProject matchProject = match.getResource().getProject();
              if (mProject == matchProject || libraries.contains(matchProject)) {
                String fqn = type.getFullyQualifiedName();
                thirdPartyViews.add(fqn);
              }
            } else if (element instanceof ResolvedSourceType) {
              // User custom view
              IProject matchProject = match.getResource().getProject();
              if (mProject == matchProject || libraries.contains(matchProject)) {
                ResolvedSourceType type = (ResolvedSourceType) element;
                if (!isValidView(type, layoutsOnly)) {
                  return;
                }
                String fqn = type.getFullyQualifiedName();
                fqn = fqn.replace('$', '.');
                customViews.add(fqn);
              }
            }
          }
        };
    try {
      IJavaProject javaProject = BaseProjectHelper.getJavaProject(mProject);
      if (javaProject != null) {
        String className = layoutsOnly ? CLASS_VIEWGROUP : CLASS_VIEW;
        IType viewType = javaProject.findType(className);
        if (viewType != null) {
          IJavaSearchScope scope = SearchEngine.createHierarchyScope(viewType);
          SearchParticipant[] participants =
              new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};
          int matchRule = SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE;

          SearchPattern pattern =
              SearchPattern.createPattern(
                  "*", IJavaSearchConstants.CLASS, IJavaSearchConstants.IMPLEMENTORS, matchRule);
          SearchEngine engine = new SearchEngine();
          engine.search(pattern, participants, scope, requestor, new NullProgressMonitor());
        }
      }
    } catch (CoreException e) {
      AdtPlugin.log(e, null);
    }

    List<String> custom = new ArrayList<String>(customViews);
    List<String> thirdParty = new ArrayList<String>(thirdPartyViews);

    if (!layoutsOnly) {
      // Update our cached answers (unless we were filtered on only layouts)
      mCustomViews = custom;
      mThirdPartyViews = thirdParty;
    }

    return Pair.of(custom, thirdParty);
  }