Example usage for org.eclipse.jdt.core IJavaProject getElementName

List of usage examples for org.eclipse.jdt.core IJavaProject getElementName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getElementName.

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 * @param workingJavaProject/*from w  w  w  .  j ava2 s.co  m*/
 * @param defaultPath
 * @return
 * @throws Exception
 */
public static IPackageFragment[] getPackagesInProject(IJavaProject workingJavaProject, String defaultPath,
        final String type) throws Exception {
    final List<IPackageFragment> allPackages = new ArrayList<IPackageFragment>();
    if (workingJavaProject.getElementName().equals(FC_PLUGIN)) {
        workingJavaProject = getWorkingJavaProjectFromUser();
        defaultPath = getDefaultPathFromProject(workingJavaProject, type, EMPTY_STR);
    }
    final IPackageFragmentRoot[] packageFragmentRootArray = workingJavaProject.getJavaProject()
            .getAllPackageFragmentRoots();
    getPackagesForFragment(workingJavaProject, packageFragmentRootArray, allPackages, defaultPath);
    final String reqdPrjs[] = workingJavaProject.getRequiredProjectNames();

    for (final String rqdPrj : reqdPrjs) {
        final IJavaProject javaPrj = getJavaProject(rqdPrj);
        if (javaPrj == null) {
            MessageDialog.openInformation(new Shell(), "Dependent Project Missing",
                    "Dependent project is missing, going ahead with the available ones.");
            continue;
        }
        final IPackageFragmentRoot[] pkgFrgmRoots = javaPrj.getAllPackageFragmentRoots();
        getPackagesForFragment(getJavaProject(rqdPrj), pkgFrgmRoots, allPackages, defaultPath);
    }
    final GlobalSettings globalSettings = getInstance();
    if (allPackages.isEmpty()) {
        if (globalSettings.isUseDefaultForPath()) {
            MessageDialog.openInformation(new Shell(), "No Packages Found",
                    "There are no packages in the path " + globalSettings.getSourcePathJava());
            return new IPackageFragment[0];
        } else {
            MessageDialog.openInformation(new Shell(), "No Packages Found",
                    "There are no packages found in the project " + workingJavaProject.getElementName());
            return new IPackageFragment[0];
        }
    }

    return allPackages.toArray(new IPackageFragment[0]);
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 * @param javaProject/*  w  w  w. j av a 2  s . c o m*/
 * @param type
 * @return
 */
public static String getDefaultPathFromProject(final IJavaProject javaProject, final String type,
        final String path) {
    final GlobalSettings globalSettings = getInstance();
    String defaultPath = path;
    if (defaultPath.equals(EMPTY_STR)) {
        if (type.equals("test")) {
            defaultPath = globalSettings.getSourcePathTest();
        } else if (type.equals("resources")) {
            defaultPath = globalSettings.getSourcePathResources();
        } else {
            defaultPath = globalSettings.getSourcePathJava();
        }
    }
    final String[][] entryNamesAndValues = getSourcePathsForProject(javaProject.getElementName());
    final String[] defatPathArray = defaultPath.split(SPACE);
    for (int i = 0; i < entryNamesAndValues.length; i++) {
        if (!entryNamesAndValues[i][0].trim().equals(EMPTY_STR)) {
            for (final String str : defatPathArray) {
                if (str.equalsIgnoreCase(entryNamesAndValues[i][0])) {
                    defaultPath = str;
                    return defaultPath;

                }
            }
        }
    }
    return defaultPath;

}

From source file:org.fuin.srcmixins4j.plugin.SrcMixins4JCompilationParticipant.java

License:Open Source License

@Override
public int aboutToBuild(final IJavaProject project) {
    LOG.info("aboutToBuild: " + project.getElementName());
    this.project = project;

    // Is this a SrcMixins4J project?
    try {/*ww w . j  a v a2s. c o  m*/
        srcMixins4JProject = false;
        final String[] natureIds = project.getProject().getDescription().getNatureIds();
        for (final String natureId : natureIds) {
            if (SrcMixins4JNature.NATURE_ID.endsWith(natureId)) {
                srcMixins4JProject = true;
            }
        }
    } catch (final CoreException ex) {
        LOG.error("Couldn't get project's nature IDs", ex);
    }

    return READY_FOR_BUILD;
}

From source file:org.fuin.srcmixins4j.plugin.SrcMixins4JCompilationParticipant.java

License:Open Source License

@Override
public final void buildFinished(final IJavaProject project) {
    if (LOG.isDebugEnabled()) {
        final ResourceSet resourceSet = SrcMixins4JPlugin.getDefault().getResourceSet(project);
        dumpClassPath(project, resourceSet);
    }//from  w  ww .  ja v  a 2s.  co m
    LOG.info("buildFinished: project=" + project.getElementName());
}

From source file:org.fuin.srcmixins4j.plugin.SrcMixins4JCompilationParticipant.java

License:Open Source License

private static void dumpClassPath(final IJavaProject project, final ResourceSet resourceSet) {
    LOG.debug("PROJECT: " + project.getElementName());
    if (resourceSet == null) {
        LOG.info("RESOURCE SET: null");
        return;//from   ww  w .  ja  v  a  2  s  .c o  m
    }
    LOG.debug("RESOURCE SET: " + resourceSet.eAdapters().size() + " eAdapters");
    for (final Adapter a : resourceSet.eAdapters()) {
        LOG.debug("ADAPTER: " + a);
        if (a instanceof JavaClasspath) {
            final JavaClasspath cp = (JavaClasspath) a;
            final Map<String, List<String>> pcMap = cp.getPackageClassifierMap();
            LOG.debug("JavaClasspath: " + pcMap.size() + " keys");
            final Iterator<String> it = pcMap.keySet().iterator();
            while (it.hasNext()) {
                final String key = it.next();
                if (LOG.isTraceEnabled() || !isExcluded(key)) {
                    final StringBuilder sb = new StringBuilder(key + ": ");
                    final List<String> values = pcMap.get(key);
                    for (int i = 0; i < values.size(); i++) {
                        if (i > 0) {
                            sb.append(", ");
                        }
                        sb.append("[" + i + "]=" + values.get(i));
                    }
                    LOG.debug("CP KEY: " + sb.toString());
                }
            }
        }
    }
}

From source file:org.fuin.srcmixins4j.plugin.SrcMixins4JPlugin.java

License:Open Source License

/**
 * Returns a resource set for a given project. The resource set is cached
 * until a resource set for another class path is requested.
 * //  w ww .  ja v a 2 s .c om
 * @param project
 *            Project to return a resource set for.
 * 
 * @return Resource set.
 */
public final ResourceSet getResourceSet(final IJavaProject project) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("BEGIN getResourceSet(IJavaProject)");
        LOG.trace("project: " + project.getElementName());
    }

    if (resourceSet == null) {
        LOG.info("Create new ResourceSet");
        resourceSet = new ResourceSetImpl();
    }

    if (!project.equals(currentProject)) {
        LOG.info("project != currentProject [project=" + asString(project) + ", currentProject="
                + asString(currentProject) + "]");
        resetClasspath();
    }
    currentProject = project;

    if (LOG.isTraceEnabled()) {
        LOG.trace("resourceSet: " + resourceSet);
        LOG.trace("END getResourceSet(IJavaProject)");
    }
    return resourceSet;
}

From source file:org.fuin.srcmixins4j.plugin.SrcMixins4JPlugin.java

License:Open Source License

private static String asString(final IJavaProject project) {
    if (project == null) {
        return "null";
    }//w ww  . ja  va  2 s.c  o m
    return project.getElementName() + "@" + Integer.toHexString(System.identityHashCode(project));
}

From source file:org.fusesource.ide.camel.editor.propertysheet.PropertiesUtils.java

License:Open Source License

/**
 * Checks if the package field has to be pre-filled in this page and returns the package
 * fragment to be used for that. The package fragment has the name of the project if the source
 * folder does not contain any package and if the project name is a valid package name. If the
 * source folder contains exactly one package then the name of that package is used as the
 * package fragment's name. <code>null</code> is returned if none of the above is applicable.
 * /*from  ww w .  jav a 2  s.c o  m*/
 * @param javaProject the containing Java project of the selection used to initialize this page
 * 
 * @return the package fragment to be pre-filled in this page or <code>null</code> if no
 *         suitable package can be suggested for the given project
 * 
 * @since 3.9
 */
public static IPackageFragment getPackage(IJavaProject javaProject,
        final IPackageFragmentRoot pkgFragmentRoot) {
    String packName = null;
    IJavaElement[] packages = null;
    try {
        if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) {
            packages = pkgFragmentRoot.getChildren();
            if (packages.length == 1) { // only default package -> use Project name
                packName = javaProject.getElementName();
                // validate package name
                IStatus status = validatePackageName(packName, javaProject);
                if (status.getSeverity() == IStatus.OK) {
                    return pkgFragmentRoot.getPackageFragment(packName);
                }
            } else {
                int noOfPackages = 0;
                IPackageFragment thePackage = null;
                for (final IJavaElement pack : packages) {
                    IPackageFragment pkg = (IPackageFragment) pack;
                    // ignoring empty parent packages and default package
                    if ((!pkg.hasSubpackages() || pkg.hasChildren()) && !pkg.isDefaultPackage()) {
                        noOfPackages++;
                        thePackage = pkg;
                        if (noOfPackages > 1) {
                            return null;
                        }
                    }
                }
                if (noOfPackages == 1) { // use package name
                    packName = thePackage.getElementName();
                    return pkgFragmentRoot.getPackageFragment(packName);
                }
            }
        }
    } catch (JavaModelException e) {
        // fall through
    }
    return null;
}

From source file:org.fusesource.ide.camel.model.service.core.util.PropertiesUtils.java

License:Open Source License

/**
 * Checks if the package field has to be pre-filled in this page and returns
 * the package fragment to be used for that. The package fragment has the
 * name of the project if the source folder does not contain any package and
 * if the project name is a valid package name. If the source folder
 * contains exactly one package then the name of that package is used as the
 * package fragment's name. <code>null</code> is returned if none of the
 * above is applicable./*w ww  .  j  a  va2  s.c  o m*/
 * 
 * @param javaProject
 *            the containing Java project of the selection used to
 *            initialize this page
 * 
 * @return the package fragment to be pre-filled in this page or
 *         <code>null</code> if no suitable package can be suggested for the
 *         given project
 * 
 * @since 3.9
 */
public static IPackageFragment getPackage(IJavaProject javaProject,
        final IPackageFragmentRoot pkgFragmentRoot) {
    String packName = null;
    IJavaElement[] packages = null;
    try {
        if (pkgFragmentRoot != null && pkgFragmentRoot.exists()) {
            packages = pkgFragmentRoot.getChildren();
            if (packages.length == 1) { // only default package -> use
                // Project name
                packName = javaProject.getElementName();
                // validate package name
                IStatus status = validatePackageName(packName, javaProject);
                if (status.getSeverity() == IStatus.OK) {
                    return pkgFragmentRoot.getPackageFragment(packName);
                }
            } else {
                int noOfPackages = 0;
                IPackageFragment thePackage = null;
                for (final IJavaElement pack : packages) {
                    IPackageFragment pkg = (IPackageFragment) pack;
                    // ignoring empty parent packages and default package
                    if ((!pkg.hasSubpackages() || pkg.hasChildren()) && !pkg.isDefaultPackage()) {
                        noOfPackages++;
                        thePackage = pkg;
                        if (noOfPackages > 1) {
                            return null;
                        }
                    }
                }
                if (noOfPackages == 1) { // use package name
                    packName = thePackage.getElementName();
                    return pkgFragmentRoot.getPackageFragment(packName);
                }
            }
        }
    } catch (JavaModelException e) {
        // fall through
    }
    return null;
}

From source file:org.grails.ide.eclipse.commands.GrailsCommandUtils.java

License:Open Source License

/**
 * Recompute the Grails class path container. Essentially this performs the
 * same action as the "Refresh Dependencies" Grails menu command.
 * <p>/*w w  w .jav a  2s  .  c o m*/
 * This is a potentially long running process and so it should not be called
 * directly from the UI thread. When running in the UI thread you should
 * wrap calls to this (and other work you are possibly doing alongside with
 * this in some type of background Job.).
 */
public static void refreshDependencies(final IJavaProject javaProject, boolean showOutput)
        throws CoreException {
    debug("Refreshing dependencies for " + javaProject.getElementName() + " ...");

    // This job is a no-op for maven projects since maven handles the source folders
    if (isMavenProject(javaProject)) {
        // don't do refresh dependencies on maven projects.  This is handled by project configurator
        debug("Not refreshing dependencies because this is a maven project.");
        return;
    }

    GroovyCompilerVersionCheck.check(javaProject);
    deleteOutOfSynchPlugins(javaProject.getProject());

    // Create the external process part and launch it synchronously...
    GrailsCommand refreshFileCmd = GrailsCommandFactory.refreshDependencyFile(javaProject.getProject());
    refreshFileCmd.setShowOutput(showOutput);
    ILaunchResult result = refreshFileCmd.synchExec();
    debug(result.toString());

    //TODO: KDV: (depend) if we do it right, we should be able to remove the call to the refreshFileCmd below. However, this
    //   assumes that 
    //    a) we ensure that any command that may change the state of the dependencies also forces
    //      the regeneration of the data file as part of its own execution. (Currently this isn't the case)
    //    b) RefreshGrailsDependencyActionDelegate also forces the data file to be regenerated somehow
    // Making this change is desirable (executing the command below takes a long time).
    // Making this change is difficult at the moment because many commands do not go via the GrailsCommand
    // class. In particular, commands executed via the command prompt still directly use the old GrailsLaunchConfigurationDelegate,

    //      ILaunchConfiguration configuration = GrailsDependencyLaunchConfigurationDelegate
    //      .getLaunchConfiguration(javaProject.getProject());
    //      SynchLaunch sl = new SynchLaunch(configuration, GrailsCoreActivator.getDefault().getGrailsCommandTimeOut());
    //      sl.setShowOutput(showOutput);
    //      sl.synchExec();

    // ensure that this operation runs without causing multiple builds
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    workspace.run(new IWorkspaceRunnable() {

        public void run(IProgressMonitor monitor) throws CoreException {
            // Grails "compile" command may have changed resources...?
            // TODO: KDV: (depend) find out why this refresh is necessary. See STS-1263.
            // Note: if this line is removed, it *will* break STS-1270. We should revisit
            // where calls are being made to refresh the resource tree. Suspect we may doing this more than 
            // once in some cases.
            javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);

            // Now that we got here the data file should be available and 
            // we can ask GrailsClasspathContainer to refresh its dependencies.
            GrailsClasspathContainer container = GrailsClasspathUtils.getClasspathContainer(javaProject);
            // reparse classpath entries from dependencies file on next request
            if (container != null) {
                container.invalidate();
            }

            // ensure that the dependency and plugin data is forgotten
            GrailsCore.get().connect(javaProject.getProject(), PerProjectDependencyDataCache.class)
                    .refreshData();
            GrailsCore.get().connect(javaProject.getProject(), PerProjectPluginCache.class)
                    .refreshDependencyCache();

            // recompute source folders now
            SourceFolderJob updateSourceFolders = new SourceFolderJob(javaProject);
            updateSourceFolders.refreshSourceFolders(new NullProgressMonitor());

            // This will force the JDT to re-resolve the CP, even if only the "contents" of class path container changed see STS-1347
            javaProject.setRawClasspath(javaProject.getRawClasspath(), monitor);
        }

    }, new NullProgressMonitor());
    debug("Refreshing dependencies for " + javaProject.getElementName() + " DONE");
}