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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement 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:com.technophobia.substeps.junit.launcher.SubstepsLaunchConfigurationDelegate.java

License:Open Source License

/**
 * Evaluates all test elements selected by the given launch configuration.
 * The elements are of type {@link IType} or {@link IMethod}. At the moment
 * it is only possible to run a single method or a set of types, but not
 * mixed or more than one method at a time.
 * /*from  ww  w . j a  v a2 s  .c  o  m*/
 * @param configuration
 *            the launch configuration to inspect
 * @param monitor
 *            the progress monitor
 * @return returns all types or methods that should be ran
 * @throws CoreException
 *             an exception is thrown when the search for tests failed
 */
protected IMember[] evaluateTests(final ILaunchConfiguration configuration, final IProgressMonitor monitor)
        throws CoreException {
    final IJavaProject javaProject = getJavaProject(configuration);

    final IJavaElement testTarget = getTestTarget(configuration, javaProject);
    final String testMethodName = configuration
            .getAttribute(SubstepsLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, ""); //$NON-NLS-1$
    if (testMethodName.length() > 0) {
        if (testTarget instanceof IType) {
            return new IMember[] { ((IType) testTarget).getMethod(testMethodName, new String[0]) };
        }
    }
    final HashSet<IMember> result = new HashSet<IMember>();
    result.add(testTarget.getJavaProject().findType(SubstepsLaunchConfigWorkingCopyDecorator.FEATURE_TEST));
    return result.toArray(new IMember[result.size()]);
}

From source file:com.versant.core.jdo.tools.plugins.eclipse.popup.actions.AddPersistentClass.java

License:Open Source License

public void setISelection(ISelection iSelection) {
    className = null;/*from   w ww  . ja  v  a2s .  co  m*/
    iProject = null;
    IProject proj = null;
    if (iSelection instanceof StructuredSelection) {
        StructuredSelection sS = (StructuredSelection) iSelection;
        Object el = sS.getFirstElement();
        if (el instanceof IJavaElement) {
            IJavaElement javaElement = ((IJavaElement) el);
            proj = javaElement.getJavaProject().getProject();
            IType selectedClass = (IType) javaElement.getAncestor(IJavaElement.TYPE);
            if (selectedClass != null) {
                className = selectedClass.getFullyQualifiedName();
            }
        }
    }
    if (proj != null) {
        iProject = proj;
    }
}

From source file:com.versant.core.jdo.tools.plugins.eclipse.popup.actions.TogglePersistentField.java

License:Open Source License

public void setISelection(ISelection iSelection) {
    fieldName = null;/*from   w ww . j  ava2 s  .com*/
    className = null;
    iProject = null;
    IProject proj = null;
    if (iSelection instanceof StructuredSelection) {
        StructuredSelection sS = (StructuredSelection) iSelection;
        Object el = sS.getFirstElement();
        if (el instanceof IJavaElement) {
            IJavaElement javaElement = ((IJavaElement) el);
            proj = javaElement.getJavaProject().getProject();
            IType selectedClass = (IType) javaElement.getAncestor(IJavaElement.TYPE);
            if (selectedClass != null) {
                className = selectedClass.getFullyQualifiedName();
            }
            IField selectedField = (IField) javaElement.getAncestor(IJavaElement.FIELD);
            if (selectedField != null) {
                fieldName = selectedField.getElementName();
            }
        }
    }
    if (proj != null) {
        iProject = proj;
    }
}

From source file:com.windowtester.eclipse.ui.convert.WTAPIUsage.java

License:Open Source License

/**
 * Recursively iterate over the specified java element and their children to convert
 * each compilation to use the new WindowTester API.
 * /*www  .  j  av a2s  .c  o m*/
 * @param elem the java element (not <code>null</code>)
 * @param monitor the progress monitor (not <code>null</code>)
 */
private void scanElement(IJavaElement elem, IProgressMonitor monitor) throws JavaModelException {
    projects.add(elem.getJavaProject());
    switch (elem.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
        scanProject((IJavaProject) elem, monitor);
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        scanPackageRoot((IPackageFragmentRoot) elem, monitor);
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        scanPackage((IPackageFragment) elem, monitor);
        break;
    case IJavaElement.COMPILATION_UNIT:
        scanCompilationUnit((ICompilationUnit) elem);
        break;
    default:
        break;
    }
}

From source file:de.cau.cs.kieler.klassviz.handlers.RestoreSelectionHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    // Get the Java project of the first item in the selection.
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    Object selectedElement = ((IStructuredSelection) selection).getFirstElement();
    IPath projectPath = null;/*from   w  ww .j  av  a 2  s. c  om*/
    IJavaProject javaProject = null;
    try {
        if (selectedElement instanceof IProject) {
            IProject project = (IProject) selectedElement;
            projectPath = project.getFullPath();
            if (project.hasNature(JavaCore.NATURE_ID)) {
                javaProject = JavaCore.create(project);
            }
        } else if (selectedElement instanceof IJavaElement) {
            IJavaElement javaElement = (IJavaElement) selectedElement;
            projectPath = javaElement.getJavaProject().getPath();
            javaProject = javaElement.getJavaProject();
        }
        if (projectPath == null) {
            return null;
        }

        // Load the class data of the URI referred to the project.
        ResourceSet resourceSet = new ResourceSetImpl();
        URI platformURI = getStoredSelectionURI(projectPath);
        EObject modelObject;
        try {
            Resource resource = resourceSet.getResource(platformURI, true);
            modelObject = resource.getContents().get(0);
        } catch (RuntimeException exception) {
            IStatus status = new Status(IStatus.ERROR, PLUGIN_ID,
                    "There is no stored class selection for this project.", exception);
            StatusManager.getManager().handle(status, StatusManager.SHOW);
            return null;
        }

        if (modelObject instanceof KClassModel) {
            KClassModel classDataSelection = (KClassModel) modelObject;
            List<Object> toBeSelectedElements = new ArrayList<Object>();
            // Restore the full data of the previously selected types,
            // including their fields and methods in the model. Also add the
            // the previously selected types, fields and methods to
            // 'toBeSelectedElements'.
            for (KPackage kPackage : classDataSelection.getPackages()) {
                for (KType kType : kPackage.getTypes()) {
                    if (kType.isSelected()) {
                        IType jdtType = javaProject.findType(getQualifiedName(kType));
                        if (jdtType != null) {
                            toBeSelectedElements.add(jdtType);
                            for (KField kField : kType.getFields()) {
                                // If field was previously selected add it to
                                // 'toBeSelectedElements'.
                                if (kField.isSelected()) {
                                    toBeSelectedElements.add(jdtType.getField(kField.getName()));
                                }
                            }
                            for (KMethod kMethod : kType.getMethods()) {
                                // Get method based on name and parameter types.
                                // If method was previously selected add it to
                                // 'toBeSelectedElements'.
                                if (kMethod.isSelected()) {
                                    toBeSelectedElements.add(
                                            jdtType.getMethod(kMethod.getName(), getMethodParameters(kMethod)));
                                }
                            }
                        }
                    }
                }
            }
            ISelection newSelection = new StructuredSelection(toBeSelectedElements.toArray());

            // restore the selection respectively which view is focused
            // right now (package explorer or project explorer)
            IWorkbenchPage wbp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
            String id = wbp.getActivePartReference().getId();
            IViewPart view = wbp.showView(id);

            view.getSite().getSelectionProvider().setSelection(newSelection);

            // Start the synthesis with Xtend and visualize with KlighD.
            DiagramViewManager.createView("de.cau.cs.kieler.klassviz.ClassDataDiagramSynthesis",
                    javaProject.getElementName(), classDataSelection, null);
        }
    } catch (JavaModelException exception) {
        IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, "Error while restoring previous selection.",
                exception);
        StatusManager.getManager().handle(status, StatusManager.SHOW);
    } catch (PartInitException exception) {
        IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, "Error while activating view.", exception);
        StatusManager.getManager().handle(status, StatusManager.SHOW);
    } catch (CoreException exception) {
        StatusManager.getManager().handle(exception, PLUGIN_ID);
    }

    return null;
}

From source file:de.enough.polish.plugin.eclipse.polishEditor.editor.PolishEditor.java

License:Open Source License

public IJavaProject getJavaProject() {

    IJavaElement element = null;
    IEditorInput input = getEditorInput();
    IDocumentProvider provider = getDocumentProvider();
    if (provider instanceof ICompilationUnitDocumentProvider) {
        ICompilationUnitDocumentProvider cudp = (ICompilationUnitDocumentProvider) provider;
        element = cudp.getWorkingCopy(input);
    } else if (input instanceof IClassFileEditorInput) {
        IClassFileEditorInput cfei = (IClassFileEditorInput) input;
        element = cfei.getClassFile();/* w w  w  .j a  va2 s.  com*/
    }

    if (element == null)
        return null;

    return element.getJavaProject();
}

From source file:de.enough.polish.plugin.eclipse.polishEditor.editor.PolishSourceViewerConfiguration.java

License:Open Source License

protected IJavaProject getJavaProject() {
    ITextEditor editor2 = getEditor();/*from w ww  . j  av  a2 s. c o  m*/
    if (editor2 == null)
        return null;

    IJavaElement element = null;
    IEditorInput input = editor2.getEditorInput();
    IDocumentProvider provider = editor2.getDocumentProvider();
    if (provider instanceof ICompilationUnitDocumentProvider) {
        ICompilationUnitDocumentProvider cudp = (ICompilationUnitDocumentProvider) provider;
        element = cudp.getWorkingCopy(input);
    } else if (input instanceof IClassFileEditorInput) {
        IClassFileEditorInput cfei = (IClassFileEditorInput) input;
        element = cfei.getClassFile();
    }

    if (element == null)
        return null;

    return element.getJavaProject();
}

From source file:de.fu_berlin.inf.jtourbus.utility.Utilities.java

License:Open Source License

public static IJavaProject getProject(IStructuredSelection selection) {

    // First try by using the selection
    // ISelection selection = window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        Object o = structured.getFirstElement();
        IJavaProject newProject = null;//from  w w  w  .  ja  va 2s.  c  o m
        if (o != null && o instanceof IJavaElement
                && null != (newProject = ((IJavaElement) o).getJavaProject())) {
            return newProject;
        }
    }

    // Otherwise use the editor
    IWorkbenchWindow window = JTourBusPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
    if (window != null) {

        // Try to determine the project from the editor
        IWorkbenchPage page = window.getActivePage();
        if (page != null) {

            IEditorPart part = page.getActiveEditor();
            if (part != null) {
                IEditorInput editorInput = part.getEditorInput();
                if (editorInput != null) {
                    IJavaElement input = (IJavaElement) editorInput.getAdapter(IJavaElement.class);
                    if (input != null) {
                        return input.getJavaProject();
                    }
                }
            }
        }
    }
    return null;
}

From source file:de.fu_berlin.inf.jtourbus.view.TourChangeListener.java

License:Open Source License

void visitInternal(IProgressMonitor pm, IJavaElementDelta delta) {

    IJavaElement elem = delta.getElement();
    IJavaElementDelta[] children = delta.getAffectedChildren();

    if (pm != null)
        pm.beginTask("", children.length + 2);

    if ((delta.getFlags() & IJavaElementDelta.F_CHILDREN) != 0) {

        if (delta.getKind() != IJavaElementDelta.CHANGED) {
            throw new RuntimeException("ASSERT: CHILDREN should always be CHANGE");
        }//  www. j av  a2 s  .  c om

        for (int i = 0; i < children.length; i++) {
            visit(pm, children[i]);
        }
        return;
    }

    switch (delta.getKind()) {
    case IJavaElementDelta.ADDED: {
        if (!((delta.getFlags() & IJavaElementDelta.F_CHILDREN) == 0))
            throw new RuntimeException("ASSERT: ADDED has no children");

        switch (elem.getElementType()) {
        case IJavaElement.JAVA_MODEL:
            throw new RuntimeException("ASSERT: Adding Java Model not possible");
        case IJavaElement.JAVA_PROJECT:
            throw new RuntimeException("ASSERT: Adding Java Project not possible");
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            // The user added a source folder

            for (BusStop bs : Utilities.findJTourBusStops(pm, elem.getResource()))
                fContentProvider.fTourPlan.add(bs);
            return;
        case IJavaElement.PACKAGE_FRAGMENT:
            // The user inserted a packaged

            for (BusStop bs : Utilities.findJTourBusStops(pm, elem.getResource()))
                fContentProvider.fTourPlan.add(bs);
            return;
        case IJavaElement.COMPILATION_UNIT: {
            ICompilationUnit cu = (ICompilationUnit) elem;
            if (cu.getPrimary().equals(cu)) {
                for (BusStop bs : Utilities.findJTourBusStops(pm, elem.getResource()))
                    fContentProvider.fTourPlan.add(bs);
            }
            return;
        }
        default:
            ICompilationUnit cu = (ICompilationUnit) delta.getElement()
                    .getAncestor(IJavaElement.COMPILATION_UNIT);

            redoCU(cu, pm);
            return;
        }
    }
    case IJavaElementDelta.REMOVED:
        if (!((delta.getFlags() & IJavaElementDelta.F_CHILDREN) == 0))
            throw new RuntimeException("REMOVED has children");

        switch (elem.getElementType()) {
        case IJavaElement.JAVA_MODEL:
            throw new RuntimeException("ASSERT: Java Model not possible");
        case IJavaElement.JAVA_PROJECT:
            fContentProvider.fTourPlan.removeAll();
            return;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        case IJavaElement.PACKAGE_FRAGMENT:
            fContentProvider.fTourPlan.removeAll();

            for (BusStop bs : Utilities.findJTourBusStops(pm, elem.getJavaProject().getResource()))
                fContentProvider.fTourPlan.add(bs);

            return;
        case IJavaElement.COMPILATION_UNIT: {
            ICompilationUnit cu = (ICompilationUnit) elem;
            if (cu.getPrimary().equals(cu)) {
                fContentProvider.fTourPlan.removeCU(cu);
            }
            return;
        }
        default: {
            ICompilationUnit cu = (ICompilationUnit) delta.getElement()
                    .getAncestor(IJavaElement.COMPILATION_UNIT);

            redoCU(cu, pm);
            return;
        }
        }
    case IJavaElementDelta.CHANGED:
        // F_CONTENT && F_FINE_GRAINED
        if ((delta.getFlags() & IJavaElementDelta.F_CONTENT) != 0
                && (delta.getFlags() & IJavaElementDelta.F_FINE_GRAINED) != 0) {

            ICompilationUnit cu = (ICompilationUnit) delta.getElement()
                    .getAncestor(IJavaElement.COMPILATION_UNIT);

            redoCU(cu, pm);
        }

        // Closing without saving will trigger this event. We thus re-read
        // the file.
        if ((delta.getFlags() & IJavaElementDelta.F_PRIMARY_WORKING_COPY) != 0) {

            ICompilationUnit cu = (ICompilationUnit) delta.getElement();

            /* FIXME */

            redoCU(cu, pm);
        }
        break;
    }
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * Modified copy from JavaModelUtil./*from   w w  w  .  j a v  a  2s .c  o  m*/
 * @param javaElt
 * @return true, if corresponding java project has compiler setting to generate
 * bytecode for jdk 1.5 and above
 */
public static boolean is50OrHigher(IJavaElement javaElt) {
    IJavaProject project = javaElt.getJavaProject();
    String option = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    boolean result = JavaCore.VERSION_1_5.equals(option);
    if (result) {
        return result;
    }
    // probably > 1.5?
    result = JavaCore.VERSION_1_4.equals(option);
    if (result) {
        return false;
    }
    result = JavaCore.VERSION_1_3.equals(option);
    if (result) {
        return false;
    }
    result = JavaCore.VERSION_1_2.equals(option);
    if (result) {
        return false;
    }
    result = JavaCore.VERSION_1_1.equals(option);
    if (result) {
        return false;
    }
    // unknown = > 1.5
    return true;
}