Example usage for org.eclipse.jdt.core IPackageFragmentRoot getPath

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.apache.tapestrytools.ui.internal.wizards.NewTapestryComponentPage.java

License:Open Source License

protected void handlePackageButtonPressed() {
    IPackageFragmentRoot packRoot = (IPackageFragmentRoot) model.get("JAVA_PACKAGE_FRAGMENT_ROOT");
    if (packRoot == null && !this.folderText.getText().isEmpty()) {
        String projectName = this.projectNameCombo.getText();
        String folderName = this.folderText.getText();
        if (projectName != null && projectName.length() > 0) {
            IProject targetProject = ResourcesPlugin.getWorkspace().getRoot().findMember(projectName)
                    .getProject();//from w w  w .ja  va 2 s  .c om
            try {
                IPackageFragmentRoot[] roots = JavaCore.create(targetProject).getAllPackageFragmentRoots();
                for (IPackageFragmentRoot root : roots) {
                    if (folderName.toString().replace('\\', '/').equals(root.getPath().toString())) {
                        packRoot = root;
                        break;
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }
    if (packRoot == null)
        return;
    IJavaElement[] packages = null;
    try {
        packages = packRoot.getChildren();
    } catch (JavaModelException e) {
        // Do nothing
    }
    if (packages == null)
        packages = new IJavaElement[0];
    ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(),
            new JavaElementLabelProvider(1));
    dialog.setTitle(WizardConstants.PACKAGE_SELECTION_DIALOG_TITLE);
    dialog.setMessage(WizardConstants.PACKAGE_SELECTION_DIALOG_DESC);
    dialog.setEmptyListMessage(WizardConstants.PACKAGE_SELECTION_DIALOG_MSG_NONE);
    dialog.setElements(packages);
    if (dialog.open() == Window.OK) {
        IPackageFragment fragment = (IPackageFragment) dialog.getFirstResult();
        if (fragment != null) {
            packageText.setText(fragment.getElementName());
        } else {
            packageText.setText("EMPTY_STRING");
        }
    }
}

From source file:org.apache.tapestrytools.ui.internal.wizards.NewTapestryComponentPage.java

License:Open Source License

/**
 * Add folder group to composite/*from  w  w w. ja v a 2s  . c o m*/
 */
private void addFolderGroup(Composite composite) {
    // folder
    Label folderLabel = new Label(composite, SWT.LEFT);
    folderLabel.setText("Source folder:");
    folderLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    folderText = new Text(composite, SWT.SINGLE | SWT.BORDER);
    folderText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    IPackageFragmentRoot root = getSelectedPackageFragmentRoot();
    String projectName = projectNameCombo.getText();
    if (projectName != null && projectName.length() > 0) {
        IProject targetProject = ResourcesPlugin.getWorkspace().getRoot().findMember(projectName).getProject();
        if (root == null || !root.getJavaProject().getProject().equals(targetProject)) {
            IFolder folder = getDefaultJavaSourceFolder(targetProject);
            if (folder != null)
                folderText.setText(folder.getFullPath().toString());
        } else {
            folderText.setText(root.getPath().toString());
        }
    }

    folderButton = new Button(composite, SWT.PUSH);
    folderButton.setText("Browse");
    folderButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    folderButton.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            handleFolderButtonPressed();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            // Do nothing
        }
    });
}

From source file:org.apache.tapestrytools.ui.internal.wizards.NewTapestryPageClassWizard.java

License:Open Source License

/**
 * Add folder group to composite/*from  w  ww. j ava  2  s  .  c o  m*/
 */
private void addFolderGroup(Composite composite) {
    // folder
    Label folderLabel = new Label(composite, SWT.LEFT);
    folderLabel.setText("Source folder:");
    folderLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    folderText = new Text(composite, SWT.SINGLE | SWT.BORDER);
    folderText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    IPackageFragmentRoot root = getSelectedPackageFragmentRoot();
    String projectName = projectNameCombo.getText();
    if (projectName != null && projectName.length() > 0) {
        IProject targetProject = ResourcesPlugin.getWorkspace().getRoot().findMember(projectName).getProject();
        if (root == null || !root.getJavaProject().getProject().equals(targetProject)) {
            IFolder folder = getDefaultJavaSourceFolder(targetProject);
            if (folder != null) {
                folderText.setText(folder.getFullPath().toString());
            }

        } else {
            folderText.setText(root.getPath().toString());
        }
    }

    folderButton = new Button(composite, SWT.PUSH);
    folderButton.setText("Browse");
    folderButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
    folderButton.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            handleFolderButtonPressed();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            // Do nothing
        }
    });
}

From source file:org.autorefactor.refactoring.rules.JavaCoreHelper.java

License:Open Source License

private static List<IClasspathEntry> getClasspathEntries(final IPackageFragmentRoot root) throws Exception {
    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IClasspathEntry srcEntry = JavaCore.newSourceEntry(root.getPath(), EMPTY_PATHS, EMPTY_PATHS, null);
    final IClasspathEntry rtJarEntry = JavaCore.newLibraryEntry(getPathToRtJar(), null, null);
    entries.add(srcEntry);//from w w  w .  jav a  2 s.co m
    entries.add(rtJarEntry);

    extractClasspathEntries(entries, "../samples/pom.xml");
    return entries;
}

From source file:org.autorefactor.refactoring.rules.JavaCoreHelper.java

License:Open Source License

private static IPackageFragmentRoot addSourceContainer(IJavaProject javaProject, String containerName)
        throws Exception {
    final IProject project = javaProject.getProject();
    final IFolder folder = project.getFolder(containerName);
    createFolder(folder);//  ww  w . java2s.c o  m

    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    IClasspathEntry cpe = JavaCore.newSourceEntry(root.getPath(), EMPTY_PATHS, EMPTY_PATHS, null);
    addToClasspath(javaProject, Arrays.asList(cpe));
    return root;
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.application.JavaCloudFoundryArchiver.java

License:Open Source License

protected void bootRepackage(final IPackageFragmentRoot[] roots, File packagedFile) throws CoreException {
    Repackager bootRepackager = new Repackager(packagedFile);
    try {//from ww w  .ja  va 2  s. c  om
        bootRepackager.repackage(new Libraries() {

            public void doWithLibraries(LibraryCallback callBack) throws IOException {
                for (IPackageFragmentRoot root : roots) {

                    if (root.isArchive()) {

                        File rootFile = new File(root.getPath().toOSString());
                        if (rootFile.exists()) {
                            callBack.library(new Library(rootFile, LibraryScope.COMPILE));
                        }
                    }
                }
            }
        });
    } catch (IOException e) {
        handleApplicationDeploymentFailure(
                NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_REPACKAGE_SPRING, e.getMessage()));
    }
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.application.JavaPackageFragmentRootHandler.java

License:Open Source License

/**
 * //from  w w  w . j ava 2 s . co m
 * Determines if the given package fragment root corresponds to the class
 * path entry path.
 * <p/>
 * Note that different package fragment roots may point to the same class
 * path entry.
 * <p/>
 * Example:
 * <p/>
 * A Java project may have the following package fragment roots:
 * <p/>
 * - src/main/java
 * <p/>
 * - src/main/resources
 * <p/>
 * Both may be using the same output folder:
 * <p/>
 * target/classes.
 * <p/>
 * In this case, the output folder will have a class path entry -
 * target/classes - and it will be the same for both roots, and this method
 * will return true for both roots if passed the entry for target/classes
 * 
 * @param root
 *            to check if it corresponds to the given class path entry path
 * @param entry
 * @return true if root is at the given entry
 */
private static boolean isRootAtEntry(IPackageFragmentRoot root, IPath entry) {
    try {
        IClasspathEntry cpe = root.getRawClasspathEntry();
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = cpe.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = root.getJavaProject().getOutputLocation();
            }

            IPath location = ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
            if (entry.equals(location)) {
                return true;
            }
        }
    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    }

    IResource resource = root.getResource();
    if (resource != null && entry.equals(resource.getLocation())) {
        return true;
    }

    IPath path = root.getPath();
    if (path != null && entry.equals(path)) {
        return true;
    }

    return false;
}

From source file:org.drools.eclipse.wizard.project.NewDroolsProjectWizard.java

License:Apache License

private void addSourceFolder(IJavaProject project, List list, String s, IProgressMonitor monitor)
        throws CoreException {
    IFolder folder = project.getProject().getFolder(s);
    createFolder(folder, monitor);//from  w  w w .j ava2s  .  com
    IPackageFragmentRoot ipackagefragmentroot = project.getPackageFragmentRoot(folder);
    list.add(JavaCore.newSourceEntry(ipackagefragmentroot.getPath()));
}

From source file:org.eclim.plugin.jdt.command.search.SearchCommand.java

License:Open Source License

/**
 * Creates a Position from the supplied SearchMatch.
 *
 * @param project The project searching from.
 * @param match The SearchMatch./*from ww  w .  ja  v a  2  s .  c  o m*/
 * @return The Position.
 */
protected Position createPosition(IProject project, SearchMatch match) throws Exception {
    IJavaElement element = (IJavaElement) match.getElement();
    IJavaElement parent = JavaUtils.getPrimaryElement(element);

    String file = null;
    String elementName = JavaUtils.getFullyQualifiedName(parent);
    if (parent.getElementType() == IJavaElement.CLASS_FILE) {
        IResource resource = parent.getResource();
        // occurs with a referenced project as a lib with no source and class
        // files that are not archived in that project
        if (resource != null && resource.getType() == IResource.FILE && !isJarArchive(resource.getLocation())) {
            file = resource.getLocation().toOSString();

        } else {
            IPath path = null;
            IPackageFragmentRoot root = (IPackageFragmentRoot) parent.getParent().getParent();
            resource = root.getResource();
            if (resource != null) {
                if (resource.getType() == IResource.PROJECT) {
                    path = ProjectUtils.getIPath((IProject) resource);
                } else {
                    path = resource.getLocation();
                }
            } else {
                path = root.getPath();
            }

            String classFile = elementName.replace('.', File.separatorChar);
            if (isJarArchive(path)) {
                file = "jar:file://" + path.toOSString() + '!' + classFile + ".class";
            } else {
                file = path.toOSString() + '/' + classFile + ".class";
            }

            // android injects its jdk classes, so filter those out if the project
            // doesn't have the android nature.
            if (ANDROID_JDK_URL.matcher(file).matches() && project != null
                    && !project.hasNature(ANDROID_NATURE)) {
                return null;
            }

            // if a source path attachment exists, use it.
            IPath srcPath = root.getSourceAttachmentPath();
            if (srcPath != null) {
                String rootPath;
                IProject elementProject = root.getJavaProject().getProject();

                // determine if src path is project relative or file system absolute.
                if (srcPath.isAbsolute() && elementProject.getName().equals(srcPath.segment(0))) {
                    rootPath = ProjectUtils.getFilePath(elementProject, srcPath.toString());
                } else {
                    rootPath = srcPath.toOSString();
                }
                String srcFile = FileUtils.toUrl(rootPath + File.separator + classFile + ".java");

                // see if source file exists at source path.
                FileSystemManager fsManager = VFS.getManager();
                FileObject fileObject = fsManager.resolveFile(srcFile.replace("%", "%25"));
                if (fileObject.exists()) {
                    file = srcFile;

                    // jdk sources on osx are under a "src/" dir in the jar
                } else if (Os.isFamily(Os.FAMILY_MAC)) {
                    srcFile = FileUtils
                            .toUrl(rootPath + File.separator + "src" + File.separator + classFile + ".java");
                    fileObject = fsManager.resolveFile(srcFile.replace("%", "%25"));
                    if (fileObject.exists()) {
                        file = srcFile;
                    }
                }
            }
        }
    } else {
        IPath location = match.getResource().getLocation();
        file = location != null ? location.toOSString() : null;
    }

    elementName = JavaUtils.getFullyQualifiedName(element);
    return Position.fromOffset(file.replace('\\', '/'), elementName, match.getOffset(), match.getLength());
}

From source file:org.eclipse.acceleo.internal.ide.ui.wizards.project.AcceleoProjectWizard.java

License:Open Source License

/**
 * Convert the empty project to an Acceleo project.
 * /*from  w w w  . ja v  a2s .  c o  m*/
 * @param project
 *            The newly created project.
 * @param selectedJVM
 *            The name of the selected JVM (J2SE-1.5 or JavaSE-1.6 recommended).
 * @param allModules
 *            The description of the module that need to be created.
 * @param shouldGenerateModules
 *            Indicates if we should generate the modules in the project or not. The wizard container to
 *            display the progress monitor
 * @param monitor
 *            The monitor.
 */
public static void convert(IProject project, String selectedJVM, List<AcceleoModule> allModules,
        boolean shouldGenerateModules, IProgressMonitor monitor) {
    String generatorName = computeGeneratorName(project.getName());
    AcceleoProject acceleoProject = AcceleowizardmodelFactory.eINSTANCE.createAcceleoProject();
    acceleoProject.setName(project.getName());
    acceleoProject.setGeneratorName(generatorName);

    // Default JRE value
    acceleoProject.setJre(selectedJVM);
    if (acceleoProject.getJre() == null || acceleoProject.getJre().length() == 0) {
        acceleoProject.setJre("J2SE-1.5"); //$NON-NLS-1$         
    }

    if (shouldGenerateModules) {
        for (AcceleoModule acceleoModule : allModules) {
            String parentFolder = acceleoModule.getParentFolder();
            IProject moduleProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(acceleoModule.getProjectName());
            if (moduleProject.exists() && moduleProject.isAccessible()
                    && acceleoModule.getModuleElement() != null
                    && acceleoModule.getModuleElement().isIsMain()) {
                IPath parentFolderPath = new Path(parentFolder);
                IFolder folder = moduleProject.getFolder(parentFolderPath.removeFirstSegments(1));
                acceleoProject.getExportedPackages()
                        .add(folder.getProjectRelativePath().removeFirstSegments(1).toString().replaceAll("/", //$NON-NLS-1$
                                "\\.")); //$NON-NLS-1$
            }
            // Calculate project dependencies
            List<String> metamodelURIs = acceleoModule.getMetamodelURIs();
            for (String metamodelURI : metamodelURIs) {
                // Find the project containing this metamodel and add a dependency to it.
                EPackage ePackage = AcceleoPackageRegistry.INSTANCE.getEPackage(metamodelURI);
                if (ePackage != null && !(ePackage instanceof EcorePackage)) {
                    Bundle bundle = AcceleoWorkspaceUtil.getBundle(ePackage.getClass());
                    acceleoProject.getPluginDependencies().add(bundle.getSymbolicName());
                }
            }
        }
    }

    try {
        IProjectDescription description = project.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID, IBundleProjectDescription.PLUGIN_NATURE,
                IAcceleoConstants.ACCELEO_NATURE_ID, });
        project.setDescription(description, monitor);

        IJavaProject iJavaProject = JavaCore.create(project);

        // Compute the JRE
        List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
        IExecutionEnvironmentsManager executionEnvironmentsManager = JavaRuntime
                .getExecutionEnvironmentsManager();
        IExecutionEnvironment[] executionEnvironments = executionEnvironmentsManager.getExecutionEnvironments();
        for (IExecutionEnvironment iExecutionEnvironment : executionEnvironments) {
            if (acceleoProject.getJre().equals(iExecutionEnvironment.getId())) {
                entries.add(JavaCore.newContainerEntry(JavaRuntime.newJREContainerPath(iExecutionEnvironment)));
                break;
            }
        }

        // PDE Entry (will not be generated anymore)
        entries.add(JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins"))); //$NON-NLS-1$

        // Sets the input / output folders
        IFolder target = project.getFolder("src"); //$NON-NLS-1$
        if (!target.exists()) {
            target.create(true, true, monitor);
        }

        IFolder classes = project.getFolder("bin"); //$NON-NLS-1$
        if (!classes.exists()) {
            classes.create(true, true, monitor);
        }

        iJavaProject.setOutputLocation(classes.getFullPath(), monitor);
        IPackageFragmentRoot packageRoot = iJavaProject.getPackageFragmentRoot(target);
        entries.add(JavaCore.newSourceEntry(packageRoot.getPath(), new Path[] {}, new Path[] {},
                classes.getFullPath()));

        iJavaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
        iJavaProject.open(monitor);

        AcceleoProjectUtils.generateFiles(acceleoProject, allModules, project, shouldGenerateModules, monitor);

        // Default settings
        AcceleoBuilderSettings settings = new AcceleoBuilderSettings(project);
        settings.setCompilationKind(AcceleoBuilderSettings.COMPILATION_PLATFORM_RESOURCE);
        settings.setResourceKind(AcceleoBuilderSettings.BUILD_XMI_RESOURCE);
        settings.save();
    } catch (CoreException e) {
        AcceleoUIActivator.log(e, true);
    }
}