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.openlegacy.ide.eclipse.util.JavaUtils.java

License:Open Source License

public static String convertSourceFolderToString(IPackageFragmentRoot sourceFolder) {

    String projectName = sourceFolder.getJavaProject().getProject().getName();

    return projectName + File.separator + sourceFolder.getResource().getProjectRelativePath();
}

From source file:org.openlegacy.ide.eclipse.util.PathsUtil.java

License:Open Source License

public static File toSourceDirectory(IPackageFragmentRoot sourceFolder) {
    IProject project = sourceFolder.getJavaProject().getProject();
    return new File(toOSString(project.getLocation()), toOSString(sourceFolder.getPath()));
}

From source file:org.openquark.cal.eclipse.ui.wizards.NewModuleWizard.java

License:Open Source License

private boolean isJavaSourceFolder(final IPackageFragmentRoot folder) {
    final IJavaProject jProject = folder.getJavaProject();

    IClasspathEntry[] classPathEntries;/*from ww  w  .  j a v a  2s . c  o  m*/
    try {
        classPathEntries = jProject.getRawClasspath();
    } catch (JavaModelException e) {
        // not in class path
        return false;
    }
    boolean alreadyExists = false;
    for (int iClassPathEntries = 0; iClassPathEntries < classPathEntries.length; ++iClassPathEntries) {
        final IClasspathEntry cpe = classPathEntries[iClassPathEntries];
        if (cpe.getPath().equals(folder.getPath())) {
            alreadyExists = true;
            break;
        }
    }
    return alreadyExists;
}

From source file:org.seasar.doma.extension.domax.util.JavaProjectUtil.java

License:Apache License

public static List<IResource> getSourceFolders(IJavaProject javaProject) {
    List<IResource> results = new ArrayList<IResource>();
    try {//from w  ww  .  j  av  a2  s.c  om
        IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            if (root.getJavaProject() == javaProject && root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                results.add(root.getCorrespondingResource());
            }
        }
    } catch (JavaModelException e) {
        Logger.error(e);
    }
    return results;
}

From source file:org.seasar.s2daoplugin.cache.deployment.deployer.JarComponentAutoRegisterDeployer.java

License:Apache License

protected void doDeploy(IHandler handler) throws CoreException {
    if (referenceClass == null) {
        return;// w w w.ja  v a 2 s. co  m
    }
    IPackageFragmentRoot jar = JavaProjectUtil.findPackageFragmentRoot(referenceClass.getType());
    if (!jar.isArchive()) {
        return;
    }
    IContainer base = jar.getResource().getParent();
    IResource[] archives = archives = base.members();
    IJavaProject project = jar.getJavaProject();
    for (int i = 0; i < archives.length; i++) {
        if (!"jar".equalsIgnoreCase(archives[i].getFileExtension())) {
            continue;
        }
        jar = project.getPackageFragmentRoot(archives[i].getFullPath().toString());
        if (!jar.exists()) {
            continue;
        }
        if (isAppliedJar(jar)) {
            handler.processPackageFragmentRoot(jar);
        }
    }
}

From source file:org.seasar.s2daoplugin.util.JavaProjectUtil.java

License:Apache License

public static IPackageFragmentRoot[] findPackageFragmentRootsSharedOutputLocation(IPackageFragmentRoot root) {
    if (root == null) {
        return new IPackageFragmentRoot[0];
    }/*w  ww . ja v  a  2s  .c o  m*/
    IJavaProject project = root.getJavaProject();
    Set result = new HashSet();
    try {
        IPath output1 = root.getRawClasspathEntry().getOutputLocation();
        if (output1 == null) {
            output1 = project.getOutputLocation();
        }
        IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() == IPackageFragmentRoot.K_BINARY) {
                continue;
            }
            IPath output2 = roots[i].getRawClasspathEntry().getOutputLocation();
            if (output2 == null) {
                output2 = project.getOutputLocation();
            }
            if (output1.equals(output2)) {
                result.add(roots[i]);
            }
        }
    } catch (JavaModelException e) {
        S2DaoPlugin.log(e);
    }
    return (IPackageFragmentRoot[]) result.toArray(new IPackageFragmentRoot[result.size()]);
}

From source file:org.seasar.s2junit4plugin.wizard.NewS2JUnit4TestCaseWizardPageOne.java

License:Apache License

private void performBuildpathConfiguration(Object data) {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root == null) {
        return; // should not happen. Link shouldn't be visible
    }/* www.  j av a 2  s.com*/
    IJavaProject javaProject = root.getJavaProject();

    if ("a3".equals(data)) { // add and configure JUnit 3 //$NON-NLS-1$
        String id = BUILD_PATH_PAGE_ID;
        Map input = new HashMap();
        IClasspathEntry newEntry = BuildPathSupport.getJUnit3ClasspathEntry();
        input.put(BUILD_PATH_KEY_ADD_ENTRY, newEntry);
        input.put(BUILD_PATH_BLOCK, Boolean.TRUE);
        PreferencesUtil.createPropertyDialogOn(getShell(), javaProject, id, new String[] { id }, input).open();
    } else if ("a4".equals(data)) { // add and configure JUnit 4 //$NON-NLS-1$
        String id = BUILD_PATH_PAGE_ID;
        Map input = new HashMap();
        IClasspathEntry newEntry = BuildPathSupport.getJUnit4ClasspathEntry();
        input.put(BUILD_PATH_KEY_ADD_ENTRY, newEntry);
        input.put(BUILD_PATH_BLOCK, Boolean.TRUE);
        PreferencesUtil.createPropertyDialogOn(getShell(), javaProject, id, new String[] { id }, input).open();
    } else if ("b".equals(data)) { // open build path //$NON-NLS-1$
        String id = BUILD_PATH_PAGE_ID;
        Map input = new HashMap();
        input.put(BUILD_PATH_BLOCK, Boolean.TRUE);
        PreferencesUtil.createPropertyDialogOn(getShell(), javaProject, id, new String[] { id }, input).open();
    } else if ("c".equals(data)) { // open compliance //$NON-NLS-1$
        String buildPath = BUILD_PATH_PAGE_ID;
        String complianceId = COMPLIANCE_PAGE_ID;
        Map input = new HashMap();
        input.put(BUILD_PATH_BLOCK, Boolean.TRUE);
        input.put(KEY_NO_LINK, Boolean.TRUE);
        PreferencesUtil.createPropertyDialogOn(getShell(), javaProject, complianceId,
                new String[] { buildPath, complianceId }, data).open();
    }

    updateBuildPathMessage();
}

From source file:org.seasar.s2junit4plugin.wizard.NewS2JUnit4TestCaseWizardPageOne.java

License:Apache License

private void updateBuildPathMessage() {
    if (fLink == null || fLink.isDisposed()) {
        return;//from  w  ww. ja v  a2  s. co m
    }

    String message = null;
    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root != null) {
        try {
            IJavaProject project = root.getJavaProject();
            if (project.exists()) {
                if (isJUnit4()) {
                    if (!S2JUnit4StubUtility.is50OrHigher(project)) {
                        message = WizardMessages.NewTestCaseWizardPageOne_linkedtext_java5required;
                    } else if (project.findType(JUnitPlugin.JUNIT4_ANNOTATION_NAME) == null) {
                        message = Messages.format(
                                WizardMessages.NewTestCaseWizardPageOne_linkedtext_junit4_notonbuildpath,
                                project.getElementName());
                    }
                } else {
                    if (project.findType(JUnitPlugin.TEST_SUPERCLASS_NAME) == null) {
                        message = Messages.format(
                                WizardMessages.NewTestCaseWizardPageOne_linkedtext_junit3_notonbuildpath,
                                project.getElementName());
                    }
                }
            }
        } catch (JavaModelException e) {
        }
    }
    fLink.setVisible(message != null);
    fImage.setVisible(message != null);

    if (message != null) {
        fLink.setText(message);
    }
}

From source file:org.seasar.s2junit4plugin.wizard.NewS2JUnit4TestCaseWizardPageOne.java

License:Apache License

private IType chooseClassToTestType() {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root == null)
        return null;

    IJavaElement[] elements = new IJavaElement[] { root.getJavaProject() };
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);

    try {/*from w  w  w  . java  2 s  .c  o m*/
        SelectionDialog dialog = JavaUI.createTypeDialog(getShell(), getWizard().getContainer(), scope,
                IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS, false, getClassUnderTestText());
        dialog.setTitle(WizardMessages.NewTestCaseWizardPageOne_class_to_test_dialog_title);
        dialog.setMessage(WizardMessages.NewTestCaseWizardPageOne_class_to_test_dialog_message);
        if (dialog.open() == Window.OK) {
            Object[] resultArray = dialog.getResult();
            if (resultArray != null && resultArray.length > 0)
                return (IType) resultArray[0];
        }
    } catch (JavaModelException e) {
        Logger.error(e, this);
    }
    return null;
}

From source file:org.seasar.s2junit4plugin.wizard.NewS2JUnit4TestCaseWizardPageOne.java

License:Apache License

/**
 * Hook method that gets called when the class under test has changed. The method class under test
 * returns the status of the validation.
 * <p>/*from   w  w w .  ja  v  a2s . c o  m*/
 * Subclasses may extend this method to perform their own validation.
 * </p>
 * 
 * @return the status of the validation
 */
protected IStatus classUnderTestChanged() {
    JUnitStatus status = new JUnitStatus();

    fClassUnderTest = null;

    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root == null) {
        return status;
    }

    String classToTestName = getClassUnderTestText();
    if (classToTestName.length() == 0) {
        return status;
    }
    IJavaProject javaProject = root.getJavaProject();
    String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

    IStatus val = JavaConventions.validateJavaTypeName(classToTestName, sourceLevel, compliance);
    if (val.getSeverity() == IStatus.ERROR) {
        status.setError(WizardMessages.NewTestCaseWizardPageOne_error_class_to_test_not_valid);
        return status;
    }

    IPackageFragment pack = getPackageFragment(); // can be null
    try {
        IType type = resolveClassNameToType(root.getJavaProject(), pack, classToTestName);
        if (type == null) {
            status.setError(WizardMessages.NewTestCaseWizardPageOne_error_class_to_test_not_exist);
            return status;
        }
        if (type.isInterface()) {
            status.setWarning(
                    Messages.format(WizardMessages.NewTestCaseWizardPageOne_warning_class_to_test_is_interface,
                            classToTestName));
        }

        if (pack != null && !S2JUnit4StubUtility.isVisible(type, pack)) {
            status.setWarning(
                    Messages.format(WizardMessages.NewTestCaseWizardPageOne_warning_class_to_test_not_visible,
                            classToTestName));
        }
        fClassUnderTest = type;
        fPage2.setClassUnderTest(fClassUnderTest);
    } catch (JavaModelException e) {
        status.setError(WizardMessages.NewTestCaseWizardPageOne_error_class_to_test_not_valid);
    }
    return status;
}