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

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

Introduction

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

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

From source file:org.gw4e.eclipse.test.fwk.ProjectHelper.java

License:Open Source License

public static IJavaProject getOrCreateSharedGW4Project(String name, boolean createModel)
        throws CoreException, InvocationTargetException, InterruptedException, TimeoutException {

    boolean autoBuilding = ResourceManager.isAutoBuilding();
    ResourceManager.setAutoBuilding(false);
    try {/*from   www .  ja  v a2 s.co  m*/
        IJavaProject p = getProject(name);
        if (p == null || !p.exists()) {
            p = createProject(name);
        }
        GraphWalkerContextManager.configureProject(p.getProject());
        GW4ENature.setGW4ENature(p.getProject());
        if (createModel) {
            p = getProject(name);
            SharedTemplate provider = new SharedTemplate();

            IFolder folder = p.getProject().getFolder("src/test/resources");

            FolderConditionExists fce = new FolderConditionExists(p.getProject(), "src/test/resources");
            Waiter.waitUntil(fce);

            String[] resources = provider.getResources();
            for (String resource : resources) {
                try {
                    IFile file = provider.create(folder, resource, null, new NullProgressMonitor());
                    provider.addCreatedResources(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Display.getDefault().syncExec(() -> {
                provider.openInEditor(PlatformUI.getWorkbench());
            });

            Waiter.waitUntil(new ICondition() {
                IFile fA, fB;

                @Override
                public boolean checkCondition() throws Exception {
                    IJavaProject p = getProject(name);
                    fA = (IFile) ResourceManager.getResource(
                            p.getProject().getFullPath().append("src/test/resources/Model_A.json").toString());
                    fB = (IFile) ResourceManager.getResource(
                            p.getProject().getFullPath().append("src/test/resources/Model_B.json").toString());
                    return fA != null && fA.exists() && fB != null && fB.exists();
                }

                @Override
                public String getFailureMessage() {
                    return "File " + fA + " not found or " + " File " + fB + " not found ";
                }

            });

            IWorkbenchWindow iww = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            IFile fA = (IFile) ResourceManager.getResource(
                    p.getProject().getFullPath().append("src/test/resources/Model_A.json").toString());
            GraphWalkerContextManager.generateDefaultGraphConversion(iww, fA, new NullProgressMonitor());

            IFile fB = (IFile) ResourceManager.getResource(
                    p.getProject().getFullPath().append("src/test/resources/Model_B.json").toString());
            GraphWalkerContextManager.generateDefaultGraphConversion(iww, fB, new NullProgressMonitor());

            Waiter.waitUntil(new ICondition() {
                IFile interfA;
                IFile interfB;

                @Override
                public boolean checkCondition() throws Exception {
                    IJavaProject p = getProject(name);
                    interfA = (IFile) ResourceManager.getResource(p.getProject().getFullPath()
                            .append("target/generated-test-sources/Model_A.java").toString());
                    interfB = (IFile) ResourceManager.getResource(p.getProject().getFullPath()
                            .append("target/generated-test-sources/Model_B.java").toString());
                    return interfA != null && interfA.exists() && interfB != null && interfB.exists();
                }

                @Override
                public String getFailureMessage() {
                    return "File " + interfA + " not found or " + " File " + interfB + " not found ";
                }

            });

            Waiter.waitUntil(new ICondition() {
                IFile implA;
                IFile implB;

                @Override
                public boolean checkCondition() throws Exception {
                    IJavaProject p = getProject(name);
                    implA = (IFile) ResourceManager.getResource(
                            p.getProject().getFullPath().append("src/test/java/Model_AImpl.java").toString());
                    implB = (IFile) ResourceManager.getResource(
                            p.getProject().getFullPath().append("src/test/java/Model_BImpl.java").toString());
                    return implA != null && implA.exists() && implB != null && implB.exists();
                }

                @Override
                public String getFailureMessage() {
                    return "File " + implA + " not found or " + " File " + implB + " not found ";
                }
            });
        }
        ResourceManager.setAutoBuilding(true);
        waitBuild();
        p = getProject(name);
        return p;
    } finally {
        ResourceManager.setAutoBuilding(autoBuilding);
    }

}

From source file:org.hibernate.eclipse.console.EclipseConsoleConfigurationPreferences.java

License:Open Source License

public URL[] getCustomClassPathURLS() {
    try {//from  ww  w. j  av  a  2 s . c  o  m
        IJavaProject project = ProjectUtils.findJavaProject(getProjectName());
        String[] additonal = new String[0];
        if (project != null && useProjectClasspath() && project.exists()) {
            try {
                additonal = JavaRuntime.computeDefaultRuntimeClassPath(project);
            } catch (CoreException e) {
                throw new HibernateConsoleRuntimeException(
                        HibernateConsoleMessages.EclipseConsoleConfigurationPreferences_could_not_compute_def_classpath
                                + project);
            }

        }
        URL[] rawLocationsURLForResources = ClassLoaderHelper.getRawLocationsURLForResources(customClasspath);
        URL[] result = new URL[rawLocationsURLForResources.length + additonal.length];
        for (int i = 0; i < rawLocationsURLForResources.length; i++) {
            result[i] = rawLocationsURLForResources[i];
        }
        for (int i = 0; i < additonal.length; i++) {
            String url = additonal[i];
            result[i + rawLocationsURLForResources.length] = new File(url).toURI().toURL();
        }
        return result;
    } catch (MalformedURLException mue) {
        throw new HibernateConsoleRuntimeException(
                HibernateConsoleMessages.EclipseConsoleConfigurationPreferences_could_not_resolve_classpaths,
                mue);
    }
}

From source file:org.hibernate.eclipse.console.wizards.ConsoleConfigurationWizardPage.java

License:Open Source License

/**
 * Init launch config parameters from proper selection
 * //ww  w  .  j a v a 2 s  .  c o m
 * @param currentSelection
 */
static public void initialize(ILaunchConfigurationWorkingCopy launchConfig, ISelection currentSelection)
        throws CoreException {
    BestGuessConsoleConfigurationVisitor v = new BestGuessConsoleConfigurationVisitor();
    // use selection to build configuration from it...
    if (currentSelection != null && currentSelection.isEmpty() == false
            && currentSelection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) currentSelection;
        if (ssel.size() > 1) {
            return;
        }
        Object obj = ssel.getFirstElement();

        IContainer container = null;
        if (obj instanceof IJavaElement) {
            v.setJavaProject(((IJavaElement) obj).getJavaProject());
            if (v.getJavaProject() != null) {
                container = v.getJavaProject().getProject();
            }
        }
        if (obj instanceof IResource) {
            IResource res = (IResource) obj;
            if (obj instanceof IContainer) {
                container = (IContainer) res;
            } else {
                container = res.getParent();
            }
            if (res.getProject() != null) {
                IJavaProject project = JavaCore.create(res.getProject());
                if (project.exists()) {
                    v.setJavaProject(project);
                }
            }
        }
        if (container != null && v.getJavaProject() == null) {
            IProject project = container.getProject();
            v.setJavaProject(JavaCore.create(project));
        }
        if (container != null) {
            container.accept(v, IResource.NONE);
            if (v.getJavaProject() == null) {
                IProject project = container.getProject();
                v.setJavaProject(JavaCore.create(project));
            }
            setProjAttribute(launchConfig, IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                    v.getJavaProject());

            if (v.getJavaProject() != null) {
                ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
                String uniqName = lm.generateLaunchConfigurationName(v.getJavaProject().getElementName());
                launchConfig.rename(uniqName);
            }
            setPathAttribute(launchConfig, IConsoleConfigurationLaunchConstants.PROPERTY_FILE,
                    v.getPropertyFile());
            setPathAttribute(launchConfig, IConsoleConfigurationLaunchConstants.CFG_XML_FILE,
                    v.getConfigFile());
            if (v.getPersistencexml() != null) {
                setStrAttribute(launchConfig, IConsoleConfigurationLaunchConstants.CONFIGURATION_FACTORY,
                        ConfigurationMode.JPA.toString());
            } else {
                setStrAttribute(launchConfig, IConsoleConfigurationLaunchConstants.CONFIGURATION_FACTORY,
                        ConfigurationMode.CORE.toString());
            }
            if (!v.getMappings().isEmpty() && v.getConfigFile() == null && v.getPersistencexml() == null) {
                IPath[] mappings = v.getMappings().toArray(new IPath[] {});
                List<String> l = new ArrayList<String>();
                for (int i = 0; i < mappings.length; i++) {
                    IPath path = mappings[i];
                    l.add(path.toPortableString());
                }

                launchConfig.setAttribute(IConsoleConfigurationLaunchConstants.FILE_MAPPINGS, l);
            } else {
                launchConfig.setAttribute(IConsoleConfigurationLaunchConstants.FILE_MAPPINGS,
                        (List<String>) null);
            }
            if (!v.getClasspath().isEmpty()) {
                launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);
                IPath[] custClasspath = v.getClasspath().toArray(new IPath[] {});
                List<String> mementos = new ArrayList<String>(custClasspath.length);
                for (int i = 0; i < custClasspath.length; i++) {
                    mementos.add(custClasspath[i].toOSString());
                }
                launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, mementos);
            } else {
                launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, true);
                launchConfig.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH,
                        (List<String>) null);
            }
        } else if (obj instanceof EclipseConsoleConfiguration) {
            throw new IllegalStateException("This should never happen!"); //$NON-NLS-1$
        }
    }
}

From source file:org.hibernate.eclipse.console.workbench.ProjectCompilerVersionChecker.java

License:Open Source License

/**
 * //from  www . ja  v a  2s  . c o  m
 * @param ccfg
 * @return false if Projects jdk version is bigger than Eclipse jdk version
 */
public static boolean validateProjectComplianceLevel(final ConsoleConfiguration ccfg) {
    IJavaProject[] javaProjects = ProjectUtils.findJavaProjects(ccfg);
    if (javaProjects.length > 0) {
        for (final IJavaProject iJavaProject : javaProjects) {
            if (iJavaProject.exists()) {
                String projectTarget = iJavaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);
                String eclipseCompilerVersion = System.getProperty("java.specification.version"); //$NON-NLS-1$
                long projectJdkLevel = versionToJdkLevel(projectTarget);
                long eclipseJdkLevel = versionToJdkLevel(eclipseCompilerVersion);
                if (eclipseJdkLevel < projectJdkLevel) {
                    Display.getDefault().syncExec(new Runnable() {
                        @Override
                        public void run() {
                            MessageDialog.openWarning(null, Messages.ProjectCompilerVersionChecker_title,
                                    NLS.bind(Messages.ProjectCompilerVersionChecker_message,
                                            iJavaProject.getElementName()));
                        }
                    });
                    return false;
                }
            }
        }
    }
    return true;
}

From source file:org.hyperic.hypclipse.internal.util.HQDEJavaHelper.java

License:Open Source License

/**
 * Precedence order from high to low:  (1) Project specific option;
 * (2) General preference option; (3) Default option; (4) Java 1.3
 * @param project/*from   w w  w.ja  v a 2  s . c  o m*/
 * @param optionName
 * @return
 */
public static String getJavaLevel(IProject project, String optionName) {
    // Returns the corresponding java project
    // No need to check for null, will return null      
    IJavaProject javaProject = JavaCore.create(project);
    String value = null;
    // Preferred to use the project
    if ((javaProject != null) && javaProject.exists()) {
        // Get the project specific option if one exists. Rolls up to the 
        // general preference option if no project specific option exists.
        value = javaProject.getOption(optionName, true);
        if (value != null) {
            return value;
        }
    }
    // Get the general preference option
    value = JavaCore.getJavaCore().getPluginPreferences().getString(optionName);
    if (value != null) {
        return value;
    }
    // Get the default option
    value = JavaCore.getOption(optionName);
    if (value != null) {
        return value;
    }
    // Return the default
    return JavaCore.VERSION_1_3;
}

From source file:org.jboss.ide.eclipse.as.ui.xpl.JavaMainTabClone.java

License:Open Source License

/**
 * Show a dialog that lists all main types
 *///from  w w  w.ja  va2s. com
protected void handleSearchButtonSelected() {
    IJavaProject project = getJavaProject();
    IJavaElement[] elements = null;
    if ((project == null) || !project.exists()) {
        IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        if (model != null) {
            try {
                elements = model.getJavaProjects();
            } catch (JavaModelException e) {
                JDIDebugUIPlugin.log(e);
            }
        }
    } else {
        elements = new IJavaElement[] { project };
    }
    if (elements == null) {
        elements = new IJavaElement[] {};
    }
    int constraints = IJavaSearchScope.SOURCES;
    constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;
    if (fSearchExternalJarsCheckButton.getSelection()) {
        constraints |= IJavaSearchScope.SYSTEM_LIBRARIES;
    }
    IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(elements, constraints);
    MainMethodSearchEngine engine = new MainMethodSearchEngine();
    IType[] types = null;
    try {
        types = engine.searchMainMethods(getLaunchConfigurationDialog(), searchScope,
                fConsiderInheritedMainButton.getSelection());
    } catch (InvocationTargetException e) {
        setErrorMessage(e.getMessage());
        return;
    } catch (InterruptedException e) {
        setErrorMessage(e.getMessage());
        return;
    }
    DebugTypeSelectionDialog mmsd = new DebugTypeSelectionDialog(getShell(), types,
            LauncherMessages.JavaMainTab_Choose_Main_Type_11);
    if (mmsd.open() == Window.CANCEL) {
        return;
    }
    Object[] results = mmsd.getResult();
    IType type = (IType) results[0];
    if (type != null) {
        fMainText.setText(type.getFullyQualifiedName());
        fProjText.setText(type.getJavaProject().getElementName());
    }
}

From source file:org.jboss.tools.arquillian.core.internal.classpath.ArquillianClassLoader.java

License:Open Source License

private static void addProject(Set<URL> urls, IClasspathEntry entry, Set<IJavaProject> dependentProjects) {
    IClasspathEntry projectEntry = JavaCore.getResolvedClasspathEntry(entry);
    IPath path = projectEntry.getPath();
    String name = path.segment(0);
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
    if (project.exists()) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject.exists() && !dependentProjects.contains(javaProject)) {
            dependentProjects.add(javaProject);
            urls.addAll(getURLSet(javaProject));
        }//w w w. j a  v a2 s. co m
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.launcher.ArquillianLaunchConfigurationTab.java

License:Open Source License

private void testModeChanged() {
    boolean isSingleTestMode = fTestRadioButton.getSelection();
    setEnableSingleTestGroup(isSingleTestMode);
    setEnableContainerTestGroup(!isSingleTestMode);
    if (!isSingleTestMode && fContainerText.getText().length() == 0) {
        String projText = fProjText.getText();
        if (Path.EMPTY.isValidSegment(projText)) {
            IJavaProject javaProject = getJavaModel().getJavaProject(projText);
            if (javaProject != null && javaProject.exists())
                setContainerElement(javaProject);
        }//from   w w  w.  j  av a  2s  .  c  o m
    }
    validatePage();
    updateLaunchConfigurationDialog();
}

From source file:org.jboss.tools.arquillian.ui.internal.launcher.ArquillianLaunchShortcut.java

License:Open Source License

public IJavaProject getJavaProject(ILaunchConfiguration configuration) throws CoreException {
    String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
            (String) null);//from   ww  w. j  a  v a2  s .  c  o  m
    if (projectName != null) {
        projectName = projectName.trim();
        if (projectName.length() > 0) {
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            IJavaProject javaProject = JavaCore.create(project);
            if (javaProject != null && javaProject.exists()) {
                return javaProject;
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.arquillian.ui.internal.wizards.NewArquillianJUnitTestCasePageOne.java

License:Open Source License

private void updateBuildPathMessage() {
    if (fLink == null || fLink.isDisposed()) {
        return;//  ww  w . jav  a 2s  .  c  o  m
    }

    String message = null;
    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root != null) {
        IJavaProject project = root.getJavaProject();
        if (project.exists()) {
            if (!JUnitStubUtility.is50OrHigher(project)) {
                message = WizardMessages.NewTestCaseWizardPageOne_linkedtext_java5required;
            }
        }
    }
    fLink.setVisible(message != null);
    fImage.setVisible(message != null);

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