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

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

Introduction

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

Prototype

IJavaElement getParent();

Source Link

Document

Returns the element directly containing this element, or null if this element has no parent.

Usage

From source file:net.atos.optimus.common.tools.jdt.PackageFragmentHelper.java

License:Open Source License

/**
 * Return the IPackageFragment corresponding to the Path
 * //from  w  w  w. ja v  a2s .c  o  m
 * @param Path
 * @return
 */
public static IPackageFragment getPackageFragment(Path filePath) throws JavaModelException {

    IFile f = getFileForLocation(ResourcesPlugin.getWorkspace().getRoot(), filePath);

    if (f != null) {
        ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(f);
        IPackageFragment packageFragment = null;
        IJavaElement temp = compilationUnit;
        while (packageFragment == null && temp != null) {
            if (temp instanceof IPackageFragment)
                packageFragment = (IPackageFragment) temp;
            else
                temp = temp.getParent();
        }
        return packageFragment;
    }
    return null;
}

From source file:net.hillsdon.testlink.model.impl.JavaSelection.java

License:Open Source License

/**
 * We bias towards the outermost type for two reasons:
 *   1) It won't be anonymous/*w w w  .  j av  a 2 s .  c  o  m*/
 *   2) If you're going to test it it probably deserves its own file and test class...
 *   
 * @param javaElement Any java element.
 * @return
 */
private IType getOutermostType(final IJavaElement javaElement) {
    IJavaElement outermost = javaElement;
    while (true) {
        IType better = (IType) outermost.getParent().getAncestor(IJavaElement.TYPE);
        if (better == null || better.equals(outermost)) {
            break;
        }
        outermost = better;
    }
    return outermost instanceof IType ? (IType) outermost : null;
}

From source file:net.officefloor.eclipse.classpath.ClasspathUtil.java

License:Open Source License

/**
 * Obtains the location on the class path for the input {@link IPath}.
 * // w ww . j  a  v  a 2s .  c om
 * @param path
 *            {@link IPath}.
 * @return Location on the class path for the input {@link IPath}.
 */
public static String getClassPathLocation(IPath path) {

    // Obtain the resource for the path
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IResource pathResource = workspaceRoot.findMember(path);
    IResource resource = pathResource;

    // Obtain the java element
    IJavaElement javaElement = null;
    do {

        // Ensure have the resource
        if (resource == null) {
            // Did not find java element for resource
            return null;
        }

        // Obtain the java element from the resource
        javaElement = JavaCore.create(resource);

        // Obtain the parent resource
        resource = resource.getParent();

    } while (javaElement == null);

    // Obtain the package fragment root for the java element
    IPackageFragmentRoot fragmentRoot = null;
    do {

        // Determine if package fragment root
        if (javaElement instanceof IPackageFragmentRoot) {
            fragmentRoot = (IPackageFragmentRoot) javaElement;
        }

        // Obtain the parent java element
        javaElement = javaElement.getParent();

    } while ((fragmentRoot == null) && (javaElement != null));

    // Determine if have fragment root
    if (fragmentRoot == null) {
        // Return path as is
        return path.toString();
    }

    // Obtain the fragment root full path
    String fragmentPath = fragmentRoot.getResource().getFullPath().toString() + "/";

    // Obtain the class path location (by removing fragment root path)
    String fullPath = pathResource.getFullPath().toString();
    String location = fullPath.substring(fragmentPath.length());

    // Return the location
    return location;
}

From source file:net.rim.ejde.internal.ui.editors.locale.ResourceEditor.java

License:Open Source License

public void doSave(IProgressMonitor monitor) {
    try {/*from   w  w w  .  j  av  a2 s .c  o  m*/
        _resources.save();

        // Check if package was changed.
        String rrhPackage = PackageUtils.getRRHPackageID(rrhFile);
        if (!_originalPackage.equals(rrhPackage)) {
            // Attempt to find a non linked rrhIFile
            IFile files[] = ResourcesPlugin.getWorkspace().getRoot()
                    .findFilesForLocation(new Path(rrhFile.getAbsolutePath()));
            IFile rrhIFile = null;
            for (IFile file : files) {
                if (!file.isLinked()) {
                    rrhIFile = file;
                    break;
                }
            }
            if (files.length != 0 && rrhIFile == null) {
                rrhIFile = files[0];
            }

            IJavaElement packageFolder = JavaCore.create(rrhIFile.getParent());
            IPackageFragmentRoot sourceFolder = (IPackageFragmentRoot) packageFolder.getParent();
            try {
                moveResources(rrhIFile,
                        sourceFolder.createPackageFragment(rrhPackage, true, new NullProgressMonitor()));
            } catch (Exception e) {
                _logger.error("doSave: error moving resources", e); //$NON-NLS-1$
            }
        }

        if (_wasDirty) {
            _wasDirty = false;
            firePropertyChange(IEditorPart.PROP_DIRTY);
        }

        // get eclipse workspace
        final IWorkspace workspace = ResourcesPlugin.getWorkspace();

        // get eclipse workspace description
        final IWorkspaceDescription workspaceDescription = workspace.getDescription();

        // get autoBuildFlag
        final boolean autoBuild = workspaceDescription.isAutoBuilding();
        IProject project = ((FileEditorInput) (this.getEditorInput())).getFile().getProject();

        // if autobuild is checked build eclipse project
        if (autoBuild) {
            try {
                project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }

        // We must refresh the project because we are using a JAR that uses File objects to save, instead of eclipse IFiles.
        project.refreshLocal(IProject.DEPTH_INFINITE, monitor);

    } catch (IOException e) {
        TableViewer viewer = getActiveResourceEditorPage().getTableViewer();
        Status status = new Status(IStatus.ERROR, ContextManager.PLUGIN_ID, IStatus.OK,
                "Unable to open file for writing.", e);
        ErrorDialog.openError(viewer.getControl().getShell(), "Error - Resource Editor", "Error saving file.",
                status);
        monitor.setCanceled(true);
    } catch (CoreException e) {
        _logger.error("doSave: error getting package", e); //$NON-NLS-1$
    }
}

From source file:net.rim.ejde.internal.ui.editors.locale.ResourceEditor.java

License:Open Source License

/**
 * This method will find all links pointing to oldLinkLocation and will point them to the newLinkLocation. The method also
 * moves the links to a package specified by the passed in packageID.
 *
 * @param oldLinkLocation/* w ww  . ja va 2s . co m*/
 *            - The location existing links will be pointing to.
 * @param newLinkLocation
 *            - The location the new links will be pointing to, null if location doesn't change.
 * @param packageID
 *            - The package to place the new links.
 */
private void updateLinksLocationAndPackage(IPath oldLinkLocation, IPath newLinkLocation, String packageID) {
    if (newLinkLocation == null) {
        // Link location will not change
        newLinkLocation = oldLinkLocation;
    }
    IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(oldLinkLocation);
    for (IFile file : files) {
        if (file.isLinked()) {
            IJavaElement packageFolder = JavaCore.create(file.getParent());
            IPackageFragmentRoot sourceFolder = (IPackageFragmentRoot) packageFolder.getParent();
            try {
                IPackageFragment newPackage = sourceFolder.createPackageFragment(packageID, true,
                        new NullProgressMonitor());
                if (newPackage.exists()) {
                    IContainer parentFolder = ((IFolder) newPackage.getResource());
                    IFile newLocation = parentFolder.getFile(new Path(file.getName()));
                    newLocation.createLink(newLinkLocation, IResource.NONE, new NullProgressMonitor());
                    file.delete(true, new NullProgressMonitor());
                }
            } catch (Exception e) {
                _logger.error("Error updating links", e); //$NON-NLS-1$
            }
        }
    }
}

From source file:net.sf.bddbddb.util.EclipseUtil.java

License:LGPL

/**
 * Return the fully-qualified class name corresponding to the given Eclipse
 * type. Example: java.util.Hashtable$Entry
 * /*from   w  w  w  .j  a va 2  s .  c om*/
 * @param t  IType object
 * @return  class name, or null if unknown
 */
public static String getFullyQualifiedClassName(IType t) {
    StringBuffer sb = new StringBuffer();
    IJavaElement e = t;
    e = e.getParent();
    if (e instanceof IClassFile) {
        // .class file
        String classFileName = e.getElementName();
        sb.append(classFileName.substring(0, classFileName.length() - 6));
    } else {
        while (e instanceof IType) {
            sb.insert(0, e.getElementName() + "$");
            e = e.getParent();
        }
        if (e instanceof ICompilationUnit) {
            // .java file
            sb.append(t.getElementName());
        } else {
            // unknown!
            return null;
        }
    }
    e = e.getParent();
    if (e instanceof IPackageFragment) {
        String packageName = e.getElementName();
        if (packageName.length() > 0)
            sb.insert(0, packageName + ".");
    } else {
        // unknown!
        return null;
    }
    return sb.toString();
}

From source file:net.sf.j2s.ui.actions.UnitJavaScriptUtil.java

License:Open Source License

protected static String getRelativeJSPath(ICompilationUnit unit) {
    if (unit == null) {
        return null;
    }/*from  w  ww  .j  av a2 s  . c  o m*/
    IJavaProject javaProject = unit.getJavaProject();
    if (javaProject != null) {
        String relativePath = null;
        IJavaElement parent = unit.getParent();
        while (parent != null) {
            if (parent instanceof PackageFragmentRoot) {
                relativePath = unit.getPath().toPortableString()
                        .substring(parent.getPath().toPortableString().length());
                break;
            }
            parent = parent.getParent();
        }
        IPath outputLocation = null;
        try {
            outputLocation = javaProject.getOutputLocation();
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
        if (outputLocation != null && relativePath != null) {
            relativePath = outputLocation + relativePath.substring(0, relativePath.lastIndexOf('.')) + ".js";
            return relativePath;
        }
    }
    return null;
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

protected String getNotBlankName(String currentName, IJavaElement element) {
    String l_return = currentName;
    if ("".equals(l_return)) {
        if (element instanceof IType) {
            IJavaElement parentType = element.getParent().getAncestor(IJavaElement.TYPE);
            String handle = element.getHandleIdentifier();
            int start = handle.lastIndexOf(parentType.getElementName());
            if (start != -1) {
                handle = handle.substring(start + parentType.getElementName().length());
            }//from  w  w w.ja v a 2 s. c o m
            l_return = "anonymous#" + handle;
        } else {
            l_return = "(default package)";
        }
    }
    return l_return;
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

protected String buildName(IJavaElement element) {
    String l_return = element.getElementName();
    if (element instanceof IType) {
        IJavaElement container = element.getParent();
        if (container != null && container.getAncestor(IJavaElement.TYPE) != null) {
            l_return = buildParentTypeNamePart(element);
        }//  www  .ja v  a 2  s.  c  om
    } else if (element instanceof IMethod) {
        IJavaElement container = element.getAncestor(IJavaElement.TYPE);
        if (container != null && container.getParent() != null
                && container.getParent().getAncestor(IJavaElement.TYPE) != null) {
            l_return = buildParentTypeNamePart(container) + "#" + element.getElementName();
        }
    }
    return l_return;
}

From source file:net.sourceforge.metrics.internal.xml.MetricsFirstExporter.java

License:Open Source License

protected String buildParentTypeNamePart(IJavaElement element) {
    StringBuffer l_strBuffer = new StringBuffer(getNotBlankName(element.getElementName(), element));
    IJavaElement l_current = element.getParent().getAncestor(IJavaElement.TYPE);
    while (l_current != null) {
        l_strBuffer.insert(0, '.');
        l_strBuffer.insert(0, getNotBlankName(l_current.getElementName(), l_current));
        l_current = l_current.getParent();
        if (l_current != null) {
            l_current = l_current.getAncestor(IJavaElement.TYPE);
        }//from  ww w.  ja  v a 2s  .  c  o m
    }
    return l_strBuffer.toString();
}