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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT.

Prototype

int COMPILATION_UNIT

To view the source code for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT.

Click Source Link

Document

Constant representing a Java compilation unit.

Usage

From source file:org.eclipse.jst.jsf.ui.internal.jspeditor.BeanSuffixHyperlink.java

License:Open Source License

public String getHyperlinkText() {
    //Bug 254452 - JSF hyperlink provider shows "unknown hyperlink" when in list
    final IJavaElement element = determineJavaElement();
    if (element != null) {
        final ICompilationUnit compilationUnit = (ICompilationUnit) element
                .getAncestor(IJavaElement.COMPILATION_UNIT);
        if (compilationUnit != null) {
            return NLS.bind(Messages.Hyperlink_Open_JavaMethod, compilationUnit.getElementName(),
                    element.getElementName());
        }/*from   w w w.  jav a2s  .  co m*/
        return Messages.Hyperlink_Open_JavaFile;
    }
    return Messages.Hyperlink_Open_JavaElement;
}

From source file:org.eclipse.jst.jsf.ui.internal.jspeditor.JavaElementHyperlink.java

License:Open Source License

public String getHyperlinkText() {
    //Bug 254452 - JSF hyperlink provider shows "unknown hyperlink" when in list
    final IJavaElement element = determineJavaElement();
    if (element != null) {
        final ICompilationUnit compilationUnit = (ICompilationUnit) element
                .getAncestor(IJavaElement.COMPILATION_UNIT);
        if (compilationUnit != null) {
            return NLS.bind(Messages.Hyperlink_Open_JavaType, compilationUnit.getElementName());
        }/*from w ww. j a  va  2s .co m*/
        return Messages.Hyperlink_Open_JavaFile;
    }
    return Messages.Hyperlink_Open_JavaElement;
}

From source file:org.eclipse.jst.jsp.core.internal.taglib.TaglibHelperManager.java

License:Open Source License

/**
 * Update classpath for appropriate loader.
 * @see org.eclipse.jdt.core.IElementChangedListener#elementChanged(org.eclipse.jdt.core.ElementChangedEvent)
 *//*  www .j  a  va 2 s .c  om*/
public void elementChanged(ElementChangedEvent event) {
    // handle classpath changes
    IJavaElementDelta delta = event.getDelta();
    if (delta.getElement().getElementType() == IJavaElement.JAVA_MODEL) {
        IJavaElementDelta[] changed = delta.getChangedChildren();
        for (int i = 0; i < changed.length; i++) {
            if ((changed[i].getFlags() & IJavaElementDelta.F_CLASSPATH_CHANGED) != 0
                    || (changed[i].getFlags() & IJavaElementDelta.F_REORDER) != 0
                    || (changed[i].getFlags() & IJavaElementDelta.F_RESOLVED_CLASSPATH_CHANGED) != 0
                    || (changed[i].getFlags() & IJavaElementDelta.F_PRIMARY_RESOURCE) != 0) {
                IJavaElement proj = changed[i].getElement();
                handleClasspathChange(changed, i, proj);
            }
        }
    } else if (delta.getElement().getElementType() == IJavaElement.COMPILATION_UNIT) {
        IJavaElementDelta[] changed = delta.getChangedChildren();
        for (int i = 0; i < changed.length; i++) {
            if ((changed[i].getFlags() & IJavaElementDelta.F_SUPER_TYPES) != 0) {
                IJavaElement element = changed[i].getElement();
                handleSuperTypeChange(element);
            }
        }
    }
}

From source file:org.eclipse.jst.jsp.ui.internal.hyperlink.JSPJavaHyperlinkDetector.java

License:Open Source License

private IHyperlink createHyperlink(IJavaElement element, IRegion region, IDocument document) {
    IHyperlink link = null;/* www  .j ava2  s  . com*/
    if (region != null) {
        // open local variable in the JSP file...
        boolean isInTranslationCU = false;
        if (element instanceof ISourceReference) {
            IFile file = null;
            int jspOffset = 0;

            // try to locate the file in the workspace
            ITextFileBuffer textFileBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
            if (textFileBuffer != null && textFileBuffer.getLocation() != null) {
                file = getFile(textFileBuffer.getLocation().toString());
            }

            // get Java range and translate to JSP range
            try {
                ISourceRange range = null;
                IJSPTranslation jspTranslation = getJSPTranslation(document);
                if (jspTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        Object cu = ((ILocalVariable) element).getAncestor(IJavaElement.COMPILATION_UNIT);
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit()))
                            isInTranslationCU = true;
                    }
                    // linking to fields of the same compilation unit
                    else if (element.getElementType() == IJavaElement.FIELD) {
                        Object cu = ((IField) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    }
                    // linking to methods of the same compilation unit
                    else if (element.getElementType() == IJavaElement.METHOD) {
                        Object cu = ((IMethod) element).getCompilationUnit();
                        if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                            isInTranslationCU = true;
                        }
                    }
                }

                if (jspTranslation != null && range != null && file != null) {
                    jspOffset = jspTranslation.getJspOffset(range.getOffset());
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file,
                                new Region(jspOffset, range.getLength()));
                    }
                }
            } catch (JavaModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null && !isInTranslationCU) { // Don't try to open the translation CU
            link = new JSPJavaHyperlink(region, element);
        }
    }
    return link;
}

From source file:org.eclipse.linuxtools.changelog.parsers.java.JavaParser.java

License:Open Source License

/**
 * @see IParserChangeLogContrib#parseCurrentFunction(IEditorPart)
 *//* w  w w .j a v a 2  s  . com*/
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {

    String currentElementName;
    int elementType;

    // Get the working copy and connect to input.
    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    manager.connect(input);

    // Retrieve the Java Element in question.
    // The following internal access is done because the getWorkingCopy() method
    // for the WorkingCopyManager returns null for StorageEditorInput, however,
    // there is a working copy available through the ICompilationUnitDocumentProvider.
    //      ICompilationUnit workingCopy = manager.getWorkingCopy(input);
    ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider();
    // Retrieve the Java Element in question.
    ICompilationUnit workingCopy = x.getWorkingCopy(input);

    if (workingCopy == null)
        return "";

    IJavaElement method = workingCopy.getElementAt(offset);

    manager.disconnect(input);

    // no element selected
    if (method == null)
        return "";

    // Get the current element name, to test it.
    currentElementName = method.getElementName();

    // Element doesn't have a name. Can go no further.
    if (currentElementName == null)
        return "";

    // Get the Element Type to test.
    elementType = method.getElementType();

    switch (elementType) {
    case IJavaElement.METHOD:
    case IJavaElement.FIELD:
        break;
    case IJavaElement.COMPILATION_UNIT:
        return "";
    case IJavaElement.INITIALIZER:
        return STATIC_INITIALIZER_NAME;

    // So it's not a method, field, type, or static initializer. Where are we?
    default:
        IJavaElement tmpMethodType;
        if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null)
                && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) {
            return "";
        } else {
            // In a class, but not in a method. Return class name instead.
            method = tmpMethodType;
            currentElementName = method.getElementName();
        }
    }

    // Build all ancestor classes.
    // Append all ancestor class names to string

    IJavaElement tmpParent = method.getParent();
    boolean firstLoop = true;

    while (tmpParent != null) {
        IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE);
        if (tmpParentClass != null) {
            String tmpParentClassName = tmpParentClass.getElementName();
            if (tmpParentClassName == null)
                return "";
            currentElementName = tmpParentClassName + "." + currentElementName;
        } else {
            // cut root class name
            int rootClassPos = currentElementName.indexOf(".");
            if (rootClassPos >= 0)
                currentElementName = currentElementName.substring(rootClassPos + 1);
            if (firstLoop)
                return "";
            else
                return currentElementName;
        }
        tmpParent = tmpParentClass.getParent();
        firstLoop = false;

    }

    return "";
}

From source file:org.eclipse.linuxtools.internal.changelog.parsers.java.JavaParser.java

License:Open Source License

@Override
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {

    String currentElementName;/*w w w. j a  va2s  . c o  m*/
    int elementType;

    // Get the working copy and connect to input.
    IWorkingCopyManager manager = JavaUI.getWorkingCopyManager();
    manager.connect(input);

    // Retrieve the Java Element in question.
    // The following internal access is done because the getWorkingCopy()
    // method
    // for the WorkingCopyManager returns null for StorageEditorInput,
    // however,
    // there is a working copy available through the
    // ICompilationUnitDocumentProvider.
    ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider();
    // Retrieve the Java Element in question.
    ICompilationUnit workingCopy = x.getWorkingCopy(input);

    if (workingCopy == null) {
        return "";
    }

    IJavaElement method = workingCopy.getElementAt(offset);

    manager.disconnect(input);

    // no element selected
    if (method == null) {
        return "";
    }

    // Get the current element name, to test it.
    currentElementName = method.getElementName();

    // Element doesn't have a name. Can go no further.
    if (currentElementName == null) {
        return "";
    }

    // Get the Element Type to test.
    elementType = method.getElementType();

    switch (elementType) {
    case IJavaElement.METHOD:
    case IJavaElement.FIELD:
        break;
    case IJavaElement.COMPILATION_UNIT:
        return "";
    case IJavaElement.INITIALIZER:
        return STATIC_INITIALIZER_NAME;

    // So it's not a method, field, type, or static initializer. Where
    // are we?
    default:
        IJavaElement tmpMethodType;
        if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null)
                && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) {
            return "";
        } else {
            // In a class, but not in a method. Return class name instead.
            method = tmpMethodType;
            currentElementName = method.getElementName();
        }
    }

    // Build all ancestor classes.
    // Append all ancestor class names to string

    IJavaElement tmpParent = method.getParent();
    boolean firstLoop = true;

    while (tmpParent != null) {
        IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE);
        if (tmpParentClass != null) {
            String tmpParentClassName = tmpParentClass.getElementName();
            if (tmpParentClassName == null) {
                return "";
            }
            currentElementName = tmpParentClassName + "." + currentElementName;
        } else {
            // cut root class name
            int rootClassPos = currentElementName.indexOf('.');
            if (rootClassPos >= 0) {
                currentElementName = currentElementName.substring(rootClassPos + 1);
            }
            if (firstLoop) {
                return "";
            } else {
                return currentElementName;
            }
        }
        tmpParent = tmpParentClass.getParent();
        firstLoop = false;

    }

    return "";
}

From source file:org.eclipse.ltk.internal.ui.refactoring.CompilationUnitChangeNode.java

License:Open Source License

private IJavaElement getModifiedJavaElement(TextEditBasedChangeGroup edit, ICompilationUnit cunit)
        throws JavaModelException {
    IRegion range = edit.getRegion();//from   w w  w .j a v  a 2s.  com
    if (range.getOffset() == 0 && range.getLength() == 0)
        return cunit;
    IJavaElement result = cunit.getElementAt(range.getOffset());
    if (result == null)
        return cunit;

    try {
        while (true) {
            ISourceReference ref = (ISourceReference) result;
            IRegion sRange = new Region(ref.getSourceRange().getOffset(), ref.getSourceRange().getLength());
            if (result.getElementType() == IJavaElement.COMPILATION_UNIT || result.getParent() == null
                    || coveredBy(edit, sRange))
                break;
            result = result.getParent();
        }
    } catch (JavaModelException e) {
        // Do nothing, use old value.
    } catch (ClassCastException e) {
        // Do nothing, use old value.
    }
    return result;
}

From source file:org.eclipse.mylyn.internal.java.ui.JavaStructureBridge.java

License:Open Source License

/**
 * Some copying from://w  w  w .  j  av  a2 s .co m
 * 
 * @see org.eclipse.jdt.ui.ProblemsLabelDecorator
 */
public boolean containsProblem(IInteractionElement node) {
    try {
        IJavaElement element = (IJavaElement) getObjectForHandle(node.getHandleIdentifier());
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_INFINITE, null);
        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.CLASS_FILE:
            return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_ONE, null);
        case IJavaElement.PACKAGE_DECLARATION:
        case IJavaElement.IMPORT_DECLARATION:
        case IJavaElement.IMPORT_CONTAINER:
        case IJavaElement.TYPE:
        case IJavaElement.INITIALIZER:
        case IJavaElement.METHOD:
        case IJavaElement.FIELD:
        case IJavaElement.LOCAL_VARIABLE:
            ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null) {
                return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_ONE, null);
            }
        }
    } catch (CoreException e) {
        // ignore
    }
    return false;
}

From source file:org.eclipse.mylyn.internal.java.ui.LandmarkMarkerManager.java

License:Open Source License

private LandmarkUpdateOperation createRemoveLandmarkMarkerOperation(final IInteractionElement node) {
    if (node == null) {
        return null;
    }//w  ww . jav a 2s . c  o m
    if (JavaStructureBridge.CONTENT_TYPE.equals(node.getContentType())) {
        IJavaElement element = JavaCore.create(node.getHandleIdentifier());
        if (!element.exists()) {
            return null;
        }
        if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null // stuff
                // from .class files
                && element instanceof ISourceReference) {
            try {
                IResource resource = element.getUnderlyingResource();
                LandmarkUpdateOperation runnable = new LandmarkUpdateOperation(resource) {
                    public void run(IProgressMonitor monitor) throws CoreException {
                        if (getResource() != null) {
                            try {
                                if (markerMap.containsKey(node)) {
                                    long id = markerMap.get(node);
                                    IMarker marker = getResource().getMarker(id);
                                    if (marker != null) {
                                        marker.delete();
                                    }
                                }
                            } catch (NullPointerException e) {
                                // FIXME avoid NPE
                                StatusHandler.log(new Status(IStatus.ERROR, JavaUiBridgePlugin.ID_PLUGIN,
                                        "Could not update marker", e)); //$NON-NLS-1$
                            }
                        }
                    }
                };
                return runnable;
            } catch (JavaModelException e) {
                // ignore the Java Model errors
            }
        }
    }
    return null;
}

From source file:org.eclipse.mylyn.java.ui.editor.AutoFoldingStructureProvider.java

License:Open Source License

private boolean isInnerType(IType type) {

    try {//from w w  w . ja  va 2 s.c  o m
        return type.isMember();
    } catch (JavaModelException x) {
        IJavaElement parent = type.getParent();
        if (parent != null) {
            int parentType = parent.getElementType();
            return (parentType != IJavaElement.COMPILATION_UNIT && parentType != IJavaElement.CLASS_FILE);
        }
    }

    return false;
}