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

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

Introduction

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

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:org.jboss.tools.cdi.ui.wizard.NewStereotypeWizardPage.java

License:Open Source License

void setScopes(IPackageFragmentRoot root) {
    if (root != null) {
        IJavaProject jp = root.getJavaProject();
        ICDIProject cdi = getCDIProject(jp);
        if (cdi != null) {
            setScopes(cdi.getScopeNames().toArray(new String[0]));
        } else {/*from   w ww  . ja va  2  s. c  om*/
            setScopes(new String[] { "" });
        }
    } else {
        setScopes(new String[] { "" });
    }
}

From source file:org.jboss.tools.cdi.ui.wizard.NewStereotypeWizardPage.java

License:Open Source License

void setStereotypes(IPackageFragmentRoot root) {
    stereotypesProvider.setProject(null);
    if (root != null) {
        IJavaProject jp = root.getJavaProject();
        ICDIProject cdi = CDICorePlugin.getCDIProject(jp.getProject(), true);
        if (cdi != null)
            stereotypesProvider.setProject(cdi);
    }//from  www . j  a v a2 s  .  c om
}

From source file:org.jboss.tools.common.core.resources.XModelObjectEditorInput.java

License:Open Source License

private static IEditorInput convertStorageEditorInput(IStorageEditorInput input) {
    if (input instanceof JarEntryEditorInput) {
        IProject project = null;/*from www .  j  a v a 2  s. co m*/
        JarEntryEditorInput j = (JarEntryEditorInput) input;
        if (j.getStorage() instanceof IJarEntryResource) {
            IJarEntryResource file = (IJarEntryResource) j.getStorage();
            String jarFile = file.getPackageFragmentRoot().getPath().toString();
            if (file.getPackageFragmentRoot().getResource() != null) {
                jarFile = file.getPackageFragmentRoot().getResource().getLocation().toString();
            }
            String entry = file.getName();
            IJarEntryResource r = file;
            while (r != null && r.getParent() instanceof IJarEntryResource) {
                r = (IJarEntryResource) r.getParent();
                entry = r.getName() + "/" + entry;
            }
            if (r != null && r.getParent() instanceof IPackageFragment) {
                IPackageFragment pf = (IPackageFragment) r.getParent();
                IJavaProject jp = pf.getJavaProject();
                if (jp != null)
                    project = jp.getProject();
                while (pf != null) {
                    String p = pf.getElementName();
                    entry = p + "/" + entry;
                    if (pf.getParent() instanceof IPackageFragment) {
                        pf = (IPackageFragment) pf.getParent();
                    } else {
                        pf = null;
                    }
                }
            } else if (r != null && r.getPackageFragmentRoot() != null) {
                IPackageFragmentRoot root = r.getPackageFragmentRoot();
                if (root.getJavaProject() != null) {
                    project = root.getJavaProject().getProject();
                }
            }
            IEditorInput result = createJarEntryEditorInput(project, jarFile, entry);
            if (result != null)
                return result;
        }
    }
    String[] entryInfo = parseJarEntryFileInput(input);
    if (entryInfo == null)
        return input;
    String jarFile = entryInfo[0];
    String entry = entryInfo[1];
    IEditorInput result = createJarEntryEditorInput(null, jarFile, entry);
    return (result == null || result instanceof NullEditorInput) ? input : result;
}

From source file:org.jboss.tools.common.model.ui.attribute.editor.JavaHyperlinkLineFieldEditor.java

License:Open Source License

private IJavaSearchScope getScope(String javaProjectName) {
    IJavaElement[] elements = new IJavaElement[0];
    if (javaProjectName != null) {
        IPath path = new Path(javaProjectName);
        IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
        IProject proj = res.getProject();
        IJavaProject jproject = JavaCore.create(proj);
        IPackageFragmentRoot fCurrRoot = jproject.getPackageFragmentRoot(res);
        elements = new IJavaElement[] { fCurrRoot.getJavaProject() };
    } else {//w  ww .  ja  va  2s .c o  m
        IProject[] ps = ModelPlugin.getWorkspace().getRoot().getProjects();
        ArrayList<IJavaElement> l = new ArrayList<IJavaElement>();
        for (int i = 0; i < ps.length; i++) {
            if (EclipseResourceUtil.getJavaProject(ps[i]) != null)
                l.add(JavaCore.create(ps[i]));
        }
        elements = l.toArray(new IJavaElement[0]);
    }
    return SearchEngine.createJavaSearchScope(elements);
}

From source file:org.jboss.tools.maven.sourcelookup.ui.internal.util.SourceLookupUtil.java

License:Open Source License

public static void attachSource(final IPackageFragmentRoot fragment, final IPath newSourcePath,
        boolean displayDialog) {
    try {//from  ww  w  .  j  av  a  2s .c  om
        if (fragment == null || fragment.getKind() != IPackageFragmentRoot.K_BINARY) {
            return;
        }
        String value = SourceLookupActivator.getDefault().getAutoAddSourceAttachment();
        if (SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_NEVER.equals(value)) {
            return;
        }
        if (displayDialog && SourceLookupActivator.AUTO_ADD_JBOSS_SOURCE_ATTACHMENT_PROMPT.equals(value)) {
            final boolean[] attach = new boolean[1];
            Display.getDefault().syncExec(new Runnable() {

                @Override
                public void run() {
                    attach[0] = promptToAddSourceAttachment(fragment.getElementName(),
                            newSourcePath.toString());
                }
            });
            if (!attach[0]) {
                return;
            }
            ;
        }
        IPath containerPath = null;
        IJavaProject jproject = fragment.getJavaProject();
        IClasspathEntry entry = fragment.getRawClasspathEntry();
        if (entry == null) {
            entry = JavaCore.newLibraryEntry(fragment.getPath(), null, null);
        } else {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                containerPath = entry.getPath();
                ClasspathContainerInitializer initializer = JavaCore
                        .getClasspathContainerInitializer(containerPath.segment(0));
                IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
                if (initializer == null || container == null) {
                    return;
                }
                IStatus status = initializer.getSourceAttachmentStatus(containerPath, jproject);
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
                    return;
                }
                if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
                    return;
                }
                entry = JavaModelUtil.findEntryInContainer(container, fragment.getPath());
                if (entry == null) {
                    return;
                }
            }
        }
        IClasspathEntry entry1;
        CPListElement elem = CPListElement.createFromExisting(entry, null);
        elem.setAttribute(CPListElement.SOURCEATTACHMENT, newSourcePath);
        entry1 = elem.getClasspathEntry();
        if (entry1.equals(entry)) {
            return;
        }
        IClasspathEntry newEntry = entry1;
        String[] changedAttributes = { CPListElement.SOURCEATTACHMENT };
        BuildPathSupport.modifyClasspathEntry(null, newEntry, changedAttributes, jproject, containerPath,
                newEntry.getReferencingEntry() != null, new NullProgressMonitor());
    } catch (CoreException e) {
        // ignore
    }
}

From source file:org.jboss.tools.portlet.ui.internal.wizard.xpl.NewJavaClassWizardPageEx.java

License:Open Source License

/**
 * Add folder group to composite/*w  w  w.  ja va 2s  . c o m*/
 */
private void addFolderGroup(Composite composite) {
    // folder
    Label folderLabel = new Label(composite, SWT.LEFT);
    folderLabel.setText(J2EEUIMessages.FOLDER_LABEL);
    folderLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

    folderText = new Text(composite, SWT.SINGLE | SWT.BORDER);
    folderText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    synchHelper.synchText(folderText, INewJavaClassDataModelProperties.SOURCE_FOLDER, null);

    IPackageFragmentRoot root = getSelectedPackageFragmentRoot();
    String projectName = model.getStringProperty(IArtifactEditOperationDataModelProperties.PROJECT_NAME);
    if (projectName != null && projectName.length() > 0) {
        IProject targetProject = ProjectUtilities.getProject(projectName);
        if (root == null || !root.getJavaProject().getProject().equals(targetProject)) {
            IFolder folder = getDefaultJavaSourceFolder(targetProject);
            if (folder != null)
                folderText.setText(folder.getFullPath().toOSString());
        } else {
            folderText.setText(root.getPath().toString());
        }
    }

    folderButton = new Button(composite, SWT.PUSH);
    folderButton.setText(J2EEUIMessages.BROWSE_BUTTON_LABEL);
    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.jboss.tools.ws.jaxrs.ui.wizards.JaxrsApplicationCreationWizardPage.java

License:Open Source License

/**
 * Sets the extra default values from the given 'packageFragmentRoot' argument. In particular, 
 * selects the default package to set the default values.
 * /*from w ww  .  j a v  a  2s.  c o  m*/
 * @param packageFragmentRoot
 *            the selected {@link IPackageFragmentRoot}.
 * @throws JavaModelException 
 */
private void setDefaultValues(final IPackageFragmentRoot packageFragmentRoot) {
    setPackageFragmentRoot(packageFragmentRoot, true);
    setDefaultValues(packageFragmentRoot.getJavaProject());
}

From source file:org.jboss.tools.ws.jaxrs.ui.wizards.JaxrsElementCreationUtils.java

License:Open Source License

/**
 * Retrieves the top-level package in the given {@link IPackageFragmentRoot} of an {@link IJavaProject}, ie, the deepest {@link IPackageFragment} which has no {@link ICompilationUnit} element.
 * @param packageFragmentRoot the base packageFragmentRoot
 * @return the top-level {@link IPackageFragment}
 * @throws CoreException //from w  ww .  jav a 2 s. c o m
 */
public static IPackageFragment getProjectTopLevelPackage(final IPackageFragmentRoot packageFragmentRoot)
        throws CoreException {
    IFolder currentFolder = (IFolder) packageFragmentRoot.getResource();
    while (hasUniqueChildFolder(currentFolder)) {
        currentFolder = (IFolder) currentFolder.members()[0];
    }
    if (currentFolder != null) {
        return (IPackageFragment) packageFragmentRoot.getJavaProject()
                .findPackageFragment(currentFolder.getFullPath());
    }

    return null;
}

From source file:org.jboss.tools.ws.jaxws.ui.utils.JBossWSUIUtils.java

License:Open Source License

public static IStatus validatePackageName(String name, IJavaElement context) {
    IStatus status = null;/* www.  j a  v  a  2s .  co  m*/
    if (context == null || !context.exists()) {
        status = JavaConventions.validatePackageName(name, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
        if (status != null && !status.isOK()) {
            return status;
        }
    }
    String[] sourceComplianceLevels = getSourceComplianceLevels(context);
    status = JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
    if (status != null && status.getSeverity() == IStatus.ERROR) {
        return status;
    }

    IPackageFragmentRoot[] roots = null;
    try {
        IResource[] srcFolders = JBossWSCreationUtils.getJavaSourceRoots(context.getJavaProject());
        roots = new IPackageFragmentRoot[srcFolders.length];
        int i = 0;
        for (IResource src : srcFolders) {
            roots[i] = context.getJavaProject().getPackageFragmentRoot(src);
            i++;
        }
    } catch (JavaModelException e) {
        JBossWSUIPlugin.log(e);
    }
    for (IPackageFragmentRoot root : roots) {
        if (root != null) {
            IPackageFragment pack = root.getPackageFragment(name);
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = root.getJavaProject().getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow
                    // to name a package
                    // like the bin folder
                    IPath packagePath = pack.getPath();
                    if (outputPath.isPrefixOf(packagePath)) {
                        status = StatusUtils.warningStatus(
                                JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_IsOutputFolder);
                        return status;
                    }
                }
                if (pack.exists()) {
                    if (pack.containsJavaResources() || !pack.hasSubpackages()) {
                        status = StatusUtils
                                .warningStatus(JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_PackageExists);
                    } else {
                        status = StatusUtils.warningStatus(
                                JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_PackageNotShown);
                    }
                    return status;
                } else {
                    if (pack.getResource() == null) {
                        continue;
                    }
                    URI location = pack.getResource().getLocationURI();
                    if (location != null) {
                        IFileStore store = EFS.getStore(location);
                        if (store.fetchInfo().exists()) {
                            status = StatusUtils.warningStatus(
                                    JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_PackageExistsDifferentCase);
                            return status;
                        }
                    }
                }
            } catch (CoreException e) {
                JBossWSUIPlugin.log(e);
            }
        }
    }
    return status;
}

From source file:org.jboss.tools.ws.ui.utils.JBossWSUIUtils.java

License:Open Source License

public static IStatus validatePackageName(String name, IJavaElement context) {
    IStatus status = null;/*from   w  w  w . java  2s. com*/
    if (context == null || !context.exists()) {
        status = JavaConventions.validatePackageName(name, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
        if (status != null && !status.isOK()) {
            return status;
        }
    }
    String[] sourceComplianceLevels = getSourceComplianceLevels(context);
    status = JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
    if (status != null && status.getSeverity() == IStatus.ERROR) {
        return status;
    }

    IPackageFragmentRoot[] roots = null;
    try {
        IResource[] srcFolders = JBossWSCreationUtils.getJavaSourceRoots(context.getJavaProject());
        roots = new IPackageFragmentRoot[srcFolders.length];
        int i = 0;
        for (IResource src : srcFolders) {
            roots[i] = context.getJavaProject().getPackageFragmentRoot(src);
            i++;
        }
    } catch (JavaModelException e) {
        JBossWSUIPlugin.log(e);
    }
    for (IPackageFragmentRoot root : roots) {
        if (root != null) {
            IPackageFragment pack = root.getPackageFragment(name);
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = root.getJavaProject().getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow
                    // to name a package
                    // like the bin folder
                    IPath packagePath = pack.getPath();
                    if (outputPath.isPrefixOf(packagePath)) {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_IsOutputFolder);
                        return status;
                    }
                }
                if (pack.exists()) {
                    if (pack.containsJavaResources() || !pack.hasSubpackages()) {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageExists);
                    } else {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageNotShown);
                    }
                    return status;
                } else {
                    if (pack.getResource() == null) {
                        continue;
                    }
                    URI location = pack.getResource().getLocationURI();
                    if (location != null) {
                        IFileStore store = EFS.getStore(location);
                        if (store.fetchInfo().exists()) {
                            status = StatusUtils.warningStatus(
                                    JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageExistsDifferentCase);
                            return status;
                        }
                    }
                }
            } catch (CoreException e) {
                JBossWSUIPlugin.log(e);
            }
        }
    }
    return status;
}