Example usage for org.eclipse.jdt.core IPackageFragmentRoot getElementName

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getElementName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getElementName.

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

From source file:nz.ac.massey.cs.barrio.srcgraphbuilder.ExtractDependencyGraphJob.java

License:Apache License

@Override
protected IStatus run(IProgressMonitor monitor) {
    try {/*ww w .  j  a  va  2  s.  co m*/
        int SCALE = 100000;
        monitor.beginTask("extracting dependency graph from Java project", SCALE);
        monitor.subTask("gathering classes");
        try {
            for (IJavaElement e : project.getChildren()) {
                //System.out.println("child"+e+" - "+e.getClass());
                if (e instanceof IPackageFragmentRoot) {
                    // sources
                    IPackageFragmentRoot pfr = (IPackageFragmentRoot) e;
                    if (pfr.getKind() == pfr.K_SOURCE) {
                        ContainerRef c = new ContainerRef();
                        c.setSourceContainer(true);
                        c.setName(pfr.getElementName());
                        c.setArchive(pfr.isArchive());
                        this.containers.add(c);
                        gatherSources(c, pfr);
                        // System.out.println("Added container " + c.getName() + " - contains " + c.getClassCount() + " sources");
                    } else if (pfr.getKind() == pfr.K_BINARY) {
                        ContainerRef c = new ContainerRef();
                        c.setSourceContainer(false);
                        c.setName(pfr.getElementName());
                        this.containers.add(c);
                        gatherBinaries(c, pfr);
                        // System.out.println("Added container " + c.getName() + " - contains " + c.getClassCount() + " bin classes");
                    } else {
                        System.err.println("Unhandled Java element type: " + e);
                    }
                }
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
        // STEP2 - parse sources
        monitor.worked((int) (0.08 * SCALE));
        monitor.subTask("parsing classes");
        List<PackageRef> packages = new ArrayList<PackageRef>();
        for (ContainerRef c : containers) {
            if (c.isSourceContainer()) {
                for (PackageRef p : c.getPackages()) {
                    if (p.getClassCount() > 0)
                        packages.add(p);
                }
            }
        }
        monitor.internalWorked((int) (0.1 * SCALE));
        int delta = (int) (SCALE * 0.5 / packages.size());
        for (PackageRef p : packages) {
            monitor.subTask("parsing classes in package " + p.getName());

            monitor.worked(delta);
            for (ClassRef c : p.getClasses()) {
                SourceRef src = (SourceRef) c;
                ExtractTypeInfoVisitor v = new ExtractTypeInfoVisitor(src);
                ASTParser parser = ASTParser.newParser(AST.JLS3);
                parser.setSource(src.getCompilationUnit());
                parser.setResolveBindings(false);
                ASTNode node = parser.createAST(null);
                node.accept(v);
            }
        }

        // STEP3 - resolve references

        for (PackageRef p : packages) {
            monitor.subTask("resolving references " + p.getName());
            monitor.worked(delta);
            // add inner classes
            List<ClassRef> classlist = new ArrayList<ClassRef>(); // to avoid concurrent modification exception
            classlist.addAll(p.getClasses());
            for (ClassRef c : classlist) {
                addInnerClasses((SourceRef) c, p);
            }

            // resolve references
            for (ClassRef c : p.getClasses()) {
                SourceRef src = (SourceRef) c;
                resolveReferences(src);
            }
        }

        // STEP5: testing
        for (ContainerRef c : containers) {
            if (c.hasTestCases())
                c.test();
        }

        // STEP6 export graph
        monitor.subTask("exporting dependency graph");
        exportGraph();

        monitor.done();
        return Status.OK_STATUS;
    } finally {

    }
}

From source file:org.apache.tapestrytools.ui.internal.tcc.editor.PackagesPart.java

License:Open Source License

private void collectCustomComponents(IProject project) {
    List<ComponentInstance> componentList = new ArrayList<ComponentInstance>();
    try {//from   w w w .j a v a  2 s  .  c  o m
        IPackageFragmentRoot[] roots = JavaCore.create(project).getAllPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            IPackageFragmentRoot root = roots[i];
            String srcPath = root.getElementName();
            for (ComponentPackage pi : model.getCustomPackageList()) {
                if (pi.isArchive() == root.isArchive() && srcPath.equals(pi.getFragmentRoot())) {
                    if (!root.isArchive()) {
                        //Load custom components from source directory
                        String wholePath = "/" + srcPath + "/" + pi.getPath().replace(".", "/");
                        IFolder folder = project.getFolder(wholePath);
                        if (folder.exists()) {
                            IResource[] fileList = folder.members();
                            for (IResource file : fileList) {
                                String fullPath = file.getFullPath().toString();
                                String classFile = fullPath.replace(".tml", ".java").substring(1);
                                classFile = classFile.substring(classFile.indexOf("/"));
                                if (fullPath.endsWith(".tml") && file.getType() == IResource.FILE
                                        && project.getFile(classFile).exists()) {
                                    IFile componentFile = (IFile) file;
                                    Element rootElement = getRootElementOfXML(componentFile.getContents());
                                    if (rootElement.getNodeName().trim().equals("t:container")) {
                                        String componentName = classFile.substring(classFile.lastIndexOf("/"));
                                        componentName = componentName.substring(0, componentName.indexOf("."));
                                        if (componentName.startsWith("/"))
                                            componentName = componentName.substring(1);
                                        ComponentInstance ci = new ComponentInstance();
                                        ci.setId(componentName);
                                        ci.setName(pi.getPrefix() + ":" + componentName);
                                        ci.setPath(pi.getPath());
                                        ci.setPrefix(pi.getPrefix());
                                        ci.setText(componentName);
                                        componentList.add(ci);
                                    }
                                }
                            }
                        }
                    } else {
                        // Load custom components from jar files
                        for (IJavaElement pack : root.getChildren()) {
                            if (pack != null && pack instanceof PackageFragment
                                    && pack.getElementName().equals(pi.getPath())) {
                                for (Object packo : ((PackageFragment) pack)
                                        .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                                    ClassFile packi = (ClassFile) packo;
                                    String itemName = packi.getElementName();
                                    if (itemName.indexOf('$') < 0 && itemName.endsWith(".class")) {
                                        ComponentInstance ci = new ComponentInstance();
                                        String componentName = itemName.substring(0, itemName.length() - 6);
                                        ci.setId(componentName);
                                        ci.setName(pi.getPrefix() + ":" + componentName);
                                        ci.setPath(pi.getPath());
                                        ci.setPrefix(pi.getPrefix());
                                        ci.setText(componentName);
                                        componentList.add(ci);
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        model.setComponentList(componentList);
        markDirty();
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.bonitasoft.studio.businessobject.core.repository.BusinessObjectModelRepositoryStore.java

License:Open Source License

private Predicate<IPackageFragmentRoot> withElementName(final String elementName) {
    return new Predicate<IPackageFragmentRoot>() {

        @Override//from w w w  .ja v  a2  s . com
        public boolean apply(final IPackageFragmentRoot input) {
            return elementName.equals(input.getElementName());
        }
    };
}

From source file:org.dyno.visual.swing.base.JavaUtil.java

License:Open Source License

public static IFile getIconFile(Icon icon) {
    if (icon != null && icon instanceof ResourceIcon) {
        String text = ((ResourceIcon) icon).toString();
        if (text != null && text.trim().length() > 0) {
            IJavaProject javaProject = VisualSwingPlugin.getCurrentProject();
            if (javaProject != null) {
                IPackageFragmentRoot src_root = getSourceRoot(javaProject);
                if (src_root != null) {
                    String srcName = src_root.getElementName();
                    IProject prj = javaProject.getProject();
                    IFile file = prj.getFolder(srcName).getFile(text);
                    if (file != null && file.exists())
                        return file;
                }/*from   ww  w  . j a v  a2  s.  com*/
            }
        }
    }
    return null;
}

From source file:org.dyno.visual.swing.base.ResourceIcon.java

License:Open Source License

public ResourceIcon(String p) {
    this.path = p;
    IJavaProject prj = VisualSwingPlugin.getCurrentProject();
    IProject project = prj.getProject();
    IResource resource = project.findMember(new Path(p));
    if (resource == null) {
        IPackageFragmentRoot[] roots;//from w  w w  .  j ava2s  .c o m
        try {
            roots = prj.getPackageFragmentRoots();
            for (IPackageFragmentRoot root : roots) {
                if (!root.isArchive()) {
                    String src = root.getElementName();
                    src = "/" + src + p;
                    resource = project.findMember(new Path(src));
                    if (resource != null) {
                        String ext = resource.getFileExtension();
                        if (ext.equals("gif") || ext.equals("png") || ext.equals("jpg")) {
                            IPath fullPath = project.getWorkspace().getRoot().getRawLocation()
                                    .append(resource.getFullPath());
                            String fullpath = fullPath.toString();
                            Image image = Toolkit.getDefaultToolkit().getImage(fullpath);
                            icon = new ImageIcon(image);
                        } else {
                            break;
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            VisualSwingPlugin.getLogger().error(e);
        }
    }
}

From source file:org.dyno.visual.swing.base.ResourceImage.java

License:Open Source License

public ResourceImage(String p) {
    this.path = p;
    IJavaProject prj = VisualSwingPlugin.getCurrentProject();
    IProject project = prj.getProject();
    IResource resource = project.findMember(new Path(p));
    if (resource == null) {
        IPackageFragmentRoot[] roots;//from w  w w.  j  av  a  2 s. co  m
        try {
            roots = prj.getPackageFragmentRoots();
            for (IPackageFragmentRoot root : roots) {
                if (!root.isArchive()) {
                    String src = root.getElementName();
                    src = "/" + src + p; //$NON-NLS-1$
                    resource = project.findMember(new Path(src));
                    if (resource != null) {
                        String ext = resource.getFileExtension();
                        if (ext.equals("gif") || ext.equals("png") || ext.equals("jpg")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                            IPath fullPath = project.getWorkspace().getRoot().getRawLocation()
                                    .append(resource.getFullPath());
                            String fullpath = fullPath.toString();
                            image = Toolkit.getDefaultToolkit().getImage(fullpath);
                        } else {
                            break;
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            VisualSwingPlugin.getLogger().error(e);
        }
    }
    if (image == null)
        throw new IllegalArgumentException(Messages.RESOURCE_IMAGE_CANNOT_FIND_IMG_PATH + p);
}

From source file:org.dyno.visual.swing.types.IconCellEditor.java

License:Open Source License

protected Object openDialogBox(Control cellEditorWindow) {
    try {//from  w  ww  . jav  a 2s  . c om
        ImageSelectionDialog isd = new ImageSelectionDialog(cellEditorWindow.getShell());
        String text = iconText.getText();
        if (text != null && text.trim().length() > 0) {
            IJavaProject javaProject = VisualSwingPlugin.getCurrentProject();
            if (javaProject != null) {
                IPackageFragmentRoot src_root = getSourceRoot(javaProject);
                if (src_root != null) {
                    String srcName = src_root.getElementName();
                    IProject prj = javaProject.getProject();
                    IFile file = prj.getFolder(srcName).getFile(text);
                    if (file != null && file.exists())
                        isd.setImageFile(file);
                }
            }
        }
        int ret = isd.open();
        if (ret == Window.OK) {
            IFile file = isd.getImageFile();
            if (file == null) {
                return "";
            } else {
                IPath location = file.getFullPath();
                location = location.removeFirstSegments(2);
                String path = location.toString();
                if (path.startsWith("/"))
                    return path;
                else
                    return "/" + path;
            }
        }
    } catch (Exception e) {
        VisualSwingPlugin.getLogger().error(e);
    }
    return null;
}

From source file:org.dyno.visual.swing.types.ImageIconValidator.java

License:Open Source License

public String isValid(Object value) {
    if (value == null || value.equals("")) //$NON-NLS-1$
        return null;
    StringTokenizer tokenizer = new StringTokenizer((String) value, "/"); //$NON-NLS-1$
    if (!tokenizer.hasMoreTokens()) {
        return Messages.ImageIconValidator_Incorrect_Icon_Image_Format;
    }//from  ww w.  j a v  a2s .  c o m
    do {
        String token = tokenizer.nextToken().trim();
        if (token.length() == 0)
            return Messages.ImageIconValidator_Incorrect_Icon_Image_Format;
        if (tokenizer.hasMoreTokens()) {
            char c = token.charAt(0);
            if (!Character.isJavaIdentifierStart(c)) {
                return Messages.ImageIconValidator_Incorrect_Icon_Image_Format_Segment_Id;
            }
            int i = 0;
            while (true) {
                c = token.charAt(i++);
                if (!Character.isJavaIdentifierPart(c) && c != '.')
                    return Messages.ImageIconValidator_Incorrect_Icon_Image_Format_Segment_Id;
                if (i >= token.length())
                    break;
            }
        }
    } while (tokenizer.hasMoreTokens());
    IJavaProject prj = VisualSwingPlugin.getCurrentProject();
    IProject project = prj.getProject();
    IResource resource = project.findMember(new Path((String) value));
    if (resource == null) {
        IPackageFragmentRoot[] roots;
        try {
            roots = prj.getPackageFragmentRoots();
            for (IPackageFragmentRoot root : roots) {
                if (!root.isArchive()) {
                    String src = root.getElementName();
                    src = "/" + src + value; //$NON-NLS-1$
                    resource = project.findMember(new Path(src));
                    if (resource != null) {
                        String ext = resource.getFileExtension();
                        if (ext != null && (ext.equalsIgnoreCase("gif") || ext.equalsIgnoreCase("png") //$NON-NLS-1$//$NON-NLS-2$
                                || ext.equalsIgnoreCase("jpg"))) //$NON-NLS-1$
                            return null;
                        else
                            return Messages.ImageIconValidator_Not_Image_File + value;
                    }
                }
            }
        } catch (JavaModelException e) {
            VisualSwingPlugin.getLogger().error(e);
            return e.getLocalizedMessage();
        }
        return Messages.ImageIconValidator_Cannot_Find_Such_Image_File + value + "!"; //$NON-NLS-2$
    }
    return null;
}

From source file:org.eclipse.ajdt.internal.buildpath.AddToAspectpathAction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {/*from   www .j a v  a  2s.c o  m*/
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                jarFile = null;
                fileName = root.getElementName();
                enable = !AspectJCorePreferences.isOnAspectpath(cpEntry);
            } else {
                jarFile = getJARFile(selection);
                if (jarFile != null) {
                    cpEntry = null;
                    project = jarFile.getProject();
                    enable = (!AspectJCorePreferences.isOnAspectpath(project,
                            jarFile.getFullPath().toPortableString()) && !checkIfAddingOutjar(project));
                    fileName = jarFile.getName();
                }
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}

From source file:org.eclipse.ajdt.internal.buildpath.AddToInpathAction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {//from  ww  w  .  j a va2 s .c  o m
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                jarFile = null;
                fileName = root.getElementName();
                enable = !AspectJCorePreferences.isOnInpath(cpEntry);
            } else {
                jarFile = getJARFile(selection);
                if (jarFile != null) {
                    cpEntry = null;
                    project = jarFile.getProject();
                    enable = (!AspectJCorePreferences.isOnInpath(project,
                            jarFile.getFullPath().toPortableString()) && !checkIfAddingOutjar(project));
                    fileName = jarFile.getName();
                }
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}