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

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

Introduction

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

Prototype

@Override
public IPath getPath() 

Source Link

Usage

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

License:Apache License

/**
 * @see IActionDelegate#run(IAction)//from  w w  w . ja  v  a2s. c om
 */
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)/*from   www  .  ja  va  2 s .  com*/
 */
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

/**
 * When the developer selects an item in the package explorer, we monitor it and save the item in a
 * hashmap for later access./*from w  w w  .  j a  v a 2  s  . com*/
 * 
 * @param selection
 */
public void gotPackageExplorerSelectionChanged(TreeSelection selection) {
    // if we cast it to a string, the selection is between two brackets [....]
    // the stuff after the selection gives us more information
    Object[] selected = selection.toArray();
    for (Object o : selected) {
        Class<?> selectedItemType = o.getClass();
        if (selectedItemType.equals(PackageFragment.class)) {
            PackageFragment frag = (PackageFragment) o;
            String fragName = frag.getElementName();

            DebugUtils.println("Package: " + fragName);
            // store the element in a hashmap, for later access
            packageExplorerSelectedPackages.put(fragName, frag);

            // TODO: forward to Flash
        } else if (selectedItemType.equals(CompilationUnit.class)) {
            CompilationUnit comp = (CompilationUnit) o;
            String compName = comp.getElementName();
            IPath compPath = comp.getPath();

            DebugUtils.println("Class: " + compName + "   " + compPath);
            // store the element in a hashmap, for later access
            packageExplorerSelectedClasses.put(compName, comp);

            // TODO: forward to Flash
        } else {
            // DebugUtils.println(o.getClass());
            // unhandled
        }
    }
}