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

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

Introduction

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

Prototype

IResource getCorrespondingResource() throws JavaModelException;

Source Link

Document

Returns the resource that corresponds directly to this element, or null if there is no resource that corresponds to this element.

Usage

From source file:org.eclipse.zest.dot.internal.ZestProjectWizard.java

License:Open Source License

private void runGeneratedZestGraphs(final IJavaElement javaElement) throws JavaModelException {
    List<IPath> graphs = pathsToGeneratedGraphs();
    for (IPath graph : graphs) {
        IProject project = (IProject) javaElement.getCorrespondingResource();
        IFile member = (IFile) project.findMember(graph);
        /* We give the builder some time to generate files, etc. */
        long waited = 0;
        long timeout = 10000;
        while ((member == null || !member.exists()) && waited < timeout) {
            try {
                int millis = 100;
                Thread.sleep(millis);
                waited += millis;/* w  w w .  j  a  v a 2 s  . c o  m*/
                member = (IFile) project.findMember(graph);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        ZestGraphWizard.launchJavaApplication(member, new NullProgressMonitor());
    }
}

From source file:org.eclipse.zest.dot.internal.ZestProjectWizard.java

License:Open Source License

private void openDotFiles(final IJavaElement javaElement) throws CoreException {
    IProject project = (IProject) javaElement.getCorrespondingResource();
    IFolder templatesFolder = (IFolder) project.findMember(new Path(TEMPLATES));
    IResource[] members = templatesFolder.members();
    for (IResource r : members) {
        IFile file = (IFile) r;// w w  w . j  a v  a 2  s.c  o  m
        /*
         * The external editor for *.dot files causes trouble in the majority of cases
         * (Microsoft Office, Open Office, NeoOffice etc.), so we only open the new files if an
         * editor is set up in Eclipse:
         */
        if (IDE.getDefaultEditor(file) != null) {
            IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file);
        } else {
            return;
        }
    }
}

From source file:org.efaps.eclipse.wizards.RestPostWizard.java

License:Apache License

@Override
public boolean performFinish() {

    final String url = this.restPage.getComboUrl().getItem(this.restPage.getComboUrl().getSelectionIndex());
    final List<File> files = new ArrayList<File>();
    final Iterator<?> iter = this.selection.iterator();
    try {// w  ww .j  a v  a2  s.c  om
        File revFile = null;
        while (iter.hasNext()) {
            final IAdaptable adapt = (IAdaptable) iter.next();
            final IResource file;
            if (adapt instanceof IJavaElement) {
                final IJavaElement comp = (IJavaElement) adapt.getAdapter(IJavaElement.class);
                file = comp.getCorrespondingResource();
            } else {
                file = (IResource) adapt.getAdapter(IResource.class);
            }
            if (file != null && file.isAccessible()) {
                final URI uri = file.getLocationURI();
                final File tmpFile = new File(uri);
                if (tmpFile.getName().equals("_revFile.txt")) {
                    revFile = tmpFile;
                } else {
                    files.add(tmpFile);
                }
            }
        }
        if (!files.isEmpty()) {
            final RestClient client = new RestClient(url);
            client.init();
            client.post(files, revFile);
        }
    } catch (final Exception e) {
        EfapsPlugin.getDefault().logError(getClass(), "Exception", e);
    }
    return true;
}

From source file:org.evosuite.eclipse.popup.actions.ClearMarkersAction.java

License:Open Source License

/**
 * @see IActionDelegate#selectionChanged(IAction, ISelection)
 *///  w ww. ja  va  2  s  .  co m
@Override
public void selectionChanged(IAction action, ISelection selection) {
    currentSelection.clear();

    if (selection instanceof IStructuredSelection) {
        IStructuredSelection sel = (IStructuredSelection) selection;

        for (Object o : sel.toList()) {
            if (o instanceof IJavaElement) {
                IJavaElement jEl = (IJavaElement) o;
                try {
                    IResource jRes = jEl.getCorrespondingResource();
                    if (jRes != null) {
                        jRes.accept(new IResourceVisitor() {
                            @Override
                            public boolean visit(IResource resource) throws CoreException {
                                if ("java".equals(resource.getFileExtension()))
                                    currentSelection.add(resource);
                                return true;
                            }
                        });
                    }
                } catch (JavaModelException e) {
                    System.err.println("Error while traversing resources!" + e);
                } catch (CoreException e) {
                    System.err.println("Error while traversing resources!" + e);
                }
            }
        }
    }
}

From source file:org.jaml.eclipse.utils.JDTUtils.java

License:Open Source License

public static String getIJavaElementPath(IJavaElement element) throws JavaModelException {
    switch (element.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
        return element.getCorrespondingResource().getFullPath().toString();
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        return element.getCorrespondingResource().getFullPath().toString();
    case IJavaElement.PACKAGE_FRAGMENT:
        return element.getCorrespondingResource().getFullPath().toString();
    case IJavaElement.COMPILATION_UNIT:
        return element.getCorrespondingResource().getFullPath().toString().replace(element.getElementName(),
                "");
    case IJavaElement.TYPE:
        return getIJavaElementPath(element.getParent());
    }// w w  w .  ja  va2 s. c  o  m
    return JavaExt.STRING_EMPTY;
}

From source file:org.jboss.tools.forge.ui.ext.context.UIContextImpl.java

License:Open Source License

public UIContextImpl(IStructuredSelection selection) {
    List<Object> selectedElements = selection == null ? Collections.EMPTY_LIST : selection.toList();
    List<Object> result = new LinkedList<Object>();
    ConverterFactory converterFactory = FurnaceService.INSTANCE.getConverterFactory();
    Converter<File, Resource> converter = converterFactory.getConverter(File.class,
            locateNativeClass(Resource.class));

    if (selectedElements.isEmpty()) {
        // Get the Workspace directory path
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        File workspaceDirectory = workspace.getRoot().getLocation().toFile();
        Object convertedObj = converter.convert(workspaceDirectory);
        result.add(Proxies.unwrap(convertedObj));
    } else {//from  w ww . ja v  a 2  s . c  om
        for (Object object : selectedElements) {
            if (object instanceof IResource) {
                IPath location = ((IResource) object).getLocation();
                if (location != null) {
                    File file = location.toFile();
                    result.add(Proxies.unwrap(converter.convert(file)));
                }
            } else if (object instanceof IJavaElement) {
                try {
                    IJavaElement javaElem = (IJavaElement) object;
                    IResource correspondingResource = javaElem.getCorrespondingResource();
                    if (correspondingResource != null) {
                        IPath location = correspondingResource.getLocation();
                        if (location != null) {
                            File file = location.toFile();
                            result.add(Proxies.unwrap(converter.convert(file)));
                        }
                    }
                } catch (JavaModelException e) {
                    ForgeUIPlugin.log(e);
                }
            }
        }
    }
    this.currentSelection = new UISelectionImpl(result, selection);
}

From source file:org.jboss.tools.forge.ui.internal.ext.context.UIContextImpl.java

License:Open Source License

public UIContextImpl(UIProvider provider, IStructuredSelection selection, ITextSelection textSelection) {
    this.provider = provider;
    List<Object> selectedElements = selection == null ? Collections.EMPTY_LIST : selection.toList();
    List<Object> result = new LinkedList<>();
    ConverterFactory converterFactory = FurnaceService.INSTANCE.getConverterFactory();
    Converter<File, Resource> converter = converterFactory.getConverter(File.class, Resource.class);

    if (selectedElements.isEmpty()) {
        // Get the Workspace directory path
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        File workspaceDirectory = workspace.getRoot().getLocation().toFile();
        Object convertedObj = converter.convert(workspaceDirectory);
        result.add(Proxies.unwrap(convertedObj));
    } else {/* w w  w.ja va 2  s  . c  o m*/
        for (Object object : selectedElements) {
            if (object instanceof Resource) {
                result.add(object);
            } else if (object instanceof File) {
                File file = (File) object;
                result.add(Proxies.unwrap(converter.convert(file)));
            } else if (object instanceof IResource) {
                IPath location = ((IResource) object).getLocation();
                if (location != null) {
                    File file = location.toFile();
                    result.add(Proxies.unwrap(converter.convert(file)));
                }
            } else if (object instanceof IJavaElement) {
                try {
                    IJavaElement javaElem = (IJavaElement) object;
                    IResource correspondingResource = javaElem.getCorrespondingResource();
                    if (correspondingResource != null) {
                        IPath location = correspondingResource.getLocation();
                        if (location != null) {
                            File file = location.toFile();
                            result.add(Proxies.unwrap(converter.convert(file)));
                        }
                    }
                } catch (JavaModelException e) {
                    ForgeUIPlugin.log(e);
                }
            }
        }
    }
    this.currentSelection = new UISelectionImpl(result, selection, textSelection);
    initialize();
}

From source file:org.jboss.tools.seam.text.ext.hyperlink.SeamComponentHyperlinkDetector.java

License:Open Source License

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region,
        boolean canShowMultipleHyperlinks) {
    ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
    if (region == null ||
    //            canShowMultipleHyperlinks || 
            !(textEditor instanceof JavaEditor))
        return null;

    int offset = region.getOffset();

    IJavaElement input = EditorUtility.getEditorInputJavaElement(textEditor, false);
    if (input == null)
        return null;

    if (input.getResource() == null || input.getResource().getProject() == null)
        return null;

    ISeamProject seamProject = SeamCorePlugin.getSeamProject(input.getResource().getProject(), true);
    if (seamProject == null) {
        return null;
    }//from   w  w  w.j av a 2 s .  c o  m
    SeamELCompletionEngine engine = new SeamELCompletionEngine();

    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    IRegion wordRegion = JavaWordFinder.findWord(document, offset);
    if (wordRegion == null)
        return null;

    IFile file = null;

    try {
        IResource resource = input.getCorrespondingResource();
        if (resource instanceof IFile)
            file = (IFile) resource;
    } catch (JavaModelException e) {
        // It is probably because of Java element's resource is not found
        SeamExtPlugin.getDefault().logError(e);
    }

    int[] range = new int[] { wordRegion.getOffset(), wordRegion.getOffset() + wordRegion.getLength() };

    IJavaElement[] elements = null;

    try {
        elements = ((ICodeAssist) input).codeSelect(wordRegion.getOffset(), wordRegion.getLength());
        if (elements == null)
            return null;
        if (elements.length != 1)
            return null;

        ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>();
        if (elements[0] instanceof IAnnotatable) {
            IAnnotatable annotatable = (IAnnotatable) elements[0];

            IAnnotation annotation = annotatable.getAnnotation("In");
            if (annotation == null || !annotation.exists())
                return null;

            String nameToSearch = elements[0].getElementName();

            IMemberValuePair[] mvPairs = annotation.getMemberValuePairs();
            if (mvPairs != null) {
                for (IMemberValuePair mvPair : mvPairs) {
                    if ("value".equals(mvPair.getMemberName()) && mvPair.getValue() != null) {
                        String name = mvPair.getValue().toString();
                        if (name != null && name.trim().length() != 0) {
                            nameToSearch = name;
                            break;
                        }
                    }
                }
            }

            if (nameToSearch == null && nameToSearch.trim().length() == 0)
                return null;

            ISeamJavaComponentDeclaration declaration = null;

            if (file != null) {
                Set<ISeamComponent> cs = seamProject.getComponentsByPath(file.getFullPath());
                for (ISeamComponent c : cs) {
                    ISeamJavaComponentDeclaration d = c.getJavaDeclaration();
                    if (d != null && file.getFullPath().equals(d.getSourcePath())
                            && !((SeamJavaComponentDeclaration) d).getImports().isEmpty()) {
                        declaration = d;
                    }
                }

            }
            Set<ISeamContextVariable> vars = seamProject.getVariables(declaration);
            if (vars != null) {
                for (ISeamContextVariable var : vars) {
                    if (nameToSearch.equals(var.getName())) {
                        while (var instanceof ISeamContextShortVariable) {
                            var = ((ISeamContextShortVariable) var).getOriginal();
                        }
                        if (var == null)
                            continue;

                        if (var instanceof ISeamXmlFactory) {
                            ISeamXmlFactory xmlFactory = (ISeamXmlFactory) var;

                            String value = xmlFactory.getValue();
                            if (value == null || value.trim().length() == 0) {
                                value = xmlFactory.getMethod();
                            }

                            if (value == null || value.trim().length() == 0)
                                continue;

                            List<IJavaElement> javaElements = null;

                            try {
                                javaElements = engine.getJavaElementsForExpression(seamProject, file, value,
                                        region.getOffset());
                            } catch (StringIndexOutOfBoundsException e) {
                                SeamExtPlugin.getDefault().logError(e);
                            } catch (BadLocationException e) {
                                SeamExtPlugin.getDefault().logError(e);
                            }
                            if (javaElements != null) {
                                for (IJavaElement javaElement : javaElements) {
                                    String resourceName = null;
                                    if (javaElement.getResource() != null) {
                                        resourceName = javaElement.getResource().getName();
                                    }
                                    hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName,
                                            javaElement, nameToSearch));
                                }
                            }
                        } else if (var instanceof ISeamComponent) {
                            String resourceName = null;
                            ISeamComponent comp = (ISeamComponent) var;
                            Set<ISeamComponentDeclaration> decls = comp.getAllDeclarations();
                            for (ISeamComponentDeclaration decl : decls) {
                                if (decl.getResource() != null) {
                                    resourceName = decl.getResource().getName();
                                    break;
                                }
                            }
                            hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName,
                                    (ISeamComponent) var, nameToSearch));
                        } else if (var instanceof IRole) {
                            String resourceName = null;
                            if (var.getResource() != null) {
                                resourceName = var.getResource().getName();
                            }
                            hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName, (IRole) var,
                                    nameToSearch));
                        } else if (var instanceof IBijectedAttribute) {
                            String resourceName = null;
                            if (var.getResource() != null) {
                                resourceName = var.getResource().getName();
                            }
                            IBijectedAttribute attr = (IBijectedAttribute) var;
                            if (attr.getSourceMember() != null) {
                                hyperlinks.add(new SeamComponentHyperlink(wordRegion, resourceName,
                                        (IBijectedAttribute) var, nameToSearch));
                            }
                        }
                    }
                }
            }
        }
        if (hyperlinks != null && !hyperlinks.isEmpty()) {
            return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]);
        }
    } catch (JavaModelException jme) {
        SeamExtPlugin.getDefault().logError(jme);
    }
    return null;
}

From source file:org.kopl.jamopp.modelextractor.handlers.ExtractionHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {

    final String[] extensions = { "*.xmi" };
    File source = null;/*w  ww . j a  v  a2  s  . co  m*/
    ISelection selection = HandlerUtil.getActiveMenuSelectionChecked(event);
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        IJavaElement obj = (IJavaElement) ssel.getFirstElement();
        try {
            source = obj.getCorrespondingResource().getLocation().toFile();
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    FileDialog fd = new FileDialog(window.getShell(), SWT.SAVE);
    fd.setFilterExtensions(extensions);
    String destinationString = fd.open();
    File destination = new File(destinationString);
    Extractor.extract(source, destination);
    return null;

}

From source file:org.kopl.jamopp.modelextractor.handlers.MultipleExtractionHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {

    File source = null;/*from   w  ww .j a  v a  2  s.  c o m*/
    ISelection selection = HandlerUtil.getActiveMenuSelectionChecked(event);
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        IJavaElement obj = (IJavaElement) ssel.getFirstElement();
        try {
            source = obj.getCorrespondingResource().getLocation().toFile();
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    DirectoryDialog dd = new DirectoryDialog(window.getShell(), SWT.SAVE);
    String destinationString = dd.open();
    File destination = new File(destinationString);
    Extractor.extract(source, destination);
    return null;

}