Example usage for org.eclipse.jdt.internal.core CompilationUnit getCorrespondingResource

List of usage examples for org.eclipse.jdt.internal.core CompilationUnit getCorrespondingResource

Introduction

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

Prototype

@Override
public IResource getCorrespondingResource() throws JavaModelException 

Source Link

Document

A compilation unit has a corresponding resource unless it is contained in a jar.

Usage

From source file:de.akra.idocit.ui.actions.HTMLExport.java

License:Apache License

/**
 * @see IActionDelegate#run(IAction)//from   w ww . j a  va  2 s .co  m
 */
public void run(final IAction action) {
    final ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
            .getSelectionService();
    final IStructuredSelection structuredSelection = (IStructuredSelection) selectionService.getSelection();

    if (structuredSelection != null) {
        IFile file = null;
        if (structuredSelection.getFirstElement() instanceof CompilationUnit) {
            final CompilationUnit cu = (CompilationUnit) structuredSelection.getFirstElement();
            try {
                file = (IFile) cu.getCorrespondingResource();
            } catch (final JavaModelException e) {
                final String msg = "CompilationUnit \"" + cu.getPath().toOSString()
                        + "\" has no corresponding resource.";
                logger.log(Level.SEVERE, msg, e);
                MessageBoxUtils.openErrorBox(shell,
                        msg + StringUtils.NEW_LINE + StringUtils.NEW_LINE + e.getMessage());
            }

        } else if (structuredSelection.getFirstElement() instanceof IFile) {
            file = (IFile) structuredSelection.getFirstElement();
        }

        if (file != null) {
            // Get the interface as file ...
            final IFile interfaceIFile = new FileEditorInput(file).getFile();
            final File interfaceFile = interfaceIFile.getLocation().toFile();

            if (interfaceFile.exists()) {
                // get target file
                final FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
                fileDialog.setText("Export Documentation as HTML-file");
                final String[] filterExt = { "*.html", "*.htm" };
                fileDialog.setFilterExtensions(filterExt);

                final String preselectPath = buildPreselectedPath(interfaceIFile);

                fileDialog.setFileName(preselectPath);
                String selectedFileName = fileDialog.open();

                boolean stored = false;

                while ((selectedFileName != null) && !stored) {
                    final File destFile = new File(selectedFileName);
                    UIGlobals.setLastSelectedPathInFileDialog(destFile.getParentFile().getAbsolutePath());

                    final boolean exists = destFile.exists();
                    final boolean overwrite = exists && MessageBoxUtils.openQuestionDialogBox(shell,
                            "The file " + selectedFileName + " already exists. Do you want to overwrite it?");

                    if (overwrite || !exists) {
                        // parse interface file.
                        try {
                            logger.log(Level.INFO, "Start parsing");
                            final InterfaceArtifact interfaceArtifact = ServiceManager.getInstance()
                                    .getPersistenceService().loadInterface(interfaceIFile);
                            logger.log(Level.INFO, "End parsing");
                            logger.log(Level.INFO, "Start converting");
                            final HTMLDocGenerator docGen = new HTMLDocGenerator(interfaceArtifact);
                            final String html = docGen.generateHTML();
                            logger.log(Level.INFO, "End converting");

                            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                                    new FileOutputStream(destFile), Charset.forName(Misc.DEFAULT_CHARSET)));
                            writer.write(html);
                            writer.close();

                            // copy css file
                            BufferedReader reader = new BufferedReader(new InputStreamReader(
                                    ResourceUtils.getResourceInputStream("stylesheet.css"),
                                    Charset.forName(Misc.DEFAULT_CHARSET)));
                            final String cssFileName = selectedFileName.substring(0,
                                    selectedFileName.lastIndexOf(System.getProperty("file.separator")) + 1)
                                    + "stylesheet.css";
                            writer = new BufferedWriter(new OutputStreamWriter(
                                    new FileOutputStream(cssFileName), Charset.forName(Misc.DEFAULT_CHARSET)));
                            String line = reader.readLine();
                            while (line != null) {
                                writer.write(line + "\n");
                                line = reader.readLine();
                            }
                            reader.close();
                            writer.close();

                        } catch (final Exception ex) {
                            final String msg = "Could not export documentation for "
                                    + interfaceIFile.getFullPath();
                            logger.log(Level.SEVERE, msg, ex);
                            MessageBoxUtils.openErrorBox(shell, msg);
                        }

                        stored = true;
                    } else {
                        selectedFileName = fileDialog.open();
                    }
                }
            } else {
                final String msg = "File is no longer available: " + interfaceFile.getAbsolutePath();
                logger.log(Level.WARNING, msg);
                MessageBoxUtils.openErrorBox(shell, msg);
            }

        }
    }
}

From source file:de.akra.idocit.ui.actions.OpenEditorAction.java

License:Apache License

/**
 * @see IActionDelegate#run(IAction)//  ww  w  . j  a v a 2s.c om
 */
public void run(IAction action) {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IEditorDescriptor editorDescriptor = PlatformUI.getWorkbench().getEditorRegistry().findEditor(EDITOR_ID);

    ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
            .getSelectionService();
    IStructuredSelection structuredSelection = (IStructuredSelection) selectionService.getSelection();

    if (structuredSelection != null) {
        IFile file = null;
        if (structuredSelection.getFirstElement() instanceof CompilationUnit) {
            final CompilationUnit cu = (CompilationUnit) structuredSelection.getFirstElement();
            try {
                file = (IFile) cu.getCorrespondingResource();
            } catch (final JavaModelException e) {
                final String msg = "CompilationUnit \"" + cu.getPath().toOSString()
                        + "\" has no corresponding resource.";
                logger.log(Level.SEVERE, msg, e);
                MessageBoxUtils.openErrorBox(shell,
                        msg + StringUtils.NEW_LINE + StringUtils.NEW_LINE + e.getMessage());
            }

        } else if (structuredSelection.getFirstElement() instanceof IFile) {
            file = (IFile) structuredSelection.getFirstElement();
        }

        if (file != null) {
            try {
                page.openEditor(new FileEditorInput(file), editorDescriptor.getId());
            } catch (PartInitException e) {
                logger.log(Level.SEVERE, e.getMessage());
                MessageBoxUtils.openErrorBox(shell, e.getMessage());
            }
        }
    }
}

From source file:sidecarviz.core.MonitorEclipse.java

License:BSD License

private void openCompilationUnit(CompilationUnit compilationUnit) {
    try {/*from   w  w  w.j  a v  a 2s. c o  m*/
        IResource correspondingResource = compilationUnit.getCorrespondingResource();
        if (correspondingResource instanceof org.eclipse.core.internal.resources.File) {
            File eclipseFile = (File) correspondingResource;

            IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            if (workbenchWindow == null) {
                // TODO: DO NOT DO THIS
                // This opens another Eclipse Window...
                // workbenchWindow = PlatformUI.getWorkbench().openWorkbenchWindow(null);
                DebugUtils.println("Workbench Window is Null");
                return;
            }

            IWorkbenchPage activePage = workbenchWindow.getActivePage();
            if (activePage == null) {
                DebugUtils.println("Active Page is Null");
                try {
                    activePage = workbenchWindow.openPage(null);
                } catch (WorkbenchException e) {
                    e.printStackTrace();
                }
            }
            IDE.openEditor(activePage, eclipseFile);
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (PartInitException e) {
        e.printStackTrace();
    }
}