Search in sources :

Example 1 with GUIErrorHandler

use of org.eclipse.titanium.error.GUIErrorHandler in project titan.EclipsePlug-ins by eclipse.

the class GraphHandler method saveToImage.

/**
 * Exports the graph set for this class to a PNG file
 *
 * @param path
 *            : The PNG file's path
 * @param mode
 *            : The way of export, see {@link GraphHandler}
 *            <code>public static</code> fields for possible values (EXPORT_
 *            named fields)
 * @param size
 *            : This parameter sets the size of the exported image in pixels
 * @throws Exception
 *             on file handling error
 */
public void saveToImage(final String path, final ImageExportType mode) throws BadLayoutException {
    if (layout == null || actVisualisator == null) {
        throw new BadLayoutException("Either the layout or the visuaizer is not set (is null)", ErrorType.NO_OBJECT);
    }
    VisualizationViewer<NodeDescriptor, EdgeDescriptor> tempVisualisator = null;
    Dimension size = null;
    switch(mode) {
        case EXPORT_SEEN_GRAPH:
            {
                tempVisualisator = actVisualisator;
                size = actVisualisator.getPreferredSize();
            }
            break;
        case EXPORT_WHOLE_GRAPH:
            {
                layout = actVisualisator.getGraphLayout();
                if (size == null) {
                    size = new Dimension(layout.getSize().width, layout.getSize().height);
                }
                final Function<NodeDescriptor, Point2D> trf = new Function<NodeDescriptor, Point2D>() {

                    @Override
                    public Point2D apply(final NodeDescriptor v) {
                        return layout.apply(v);
                    }
                };
                tempVisualisator = new VisualizationViewer<NodeDescriptor, EdgeDescriptor>(new LayoutBuilder(g, Layouts.LAYOUT_STATIC, size).transformer(trf).build());
                tempVisualisator.setPreferredSize(size);
                tempVisualisator.setSize(size);
                tempVisualisator.getRenderContext().setVertexLabelTransformer(NODE_LABELER);
                final GraphRenderer<NodeDescriptor, EdgeDescriptor> rnd = new GraphRenderer<NodeDescriptor, EdgeDescriptor>(NODE_LABELER, tempVisualisator.getPickedVertexState(), tempVisualisator.getPickedEdgeState());
                setNodeRenderer(rnd, tempVisualisator);
                tempVisualisator.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
                tempVisualisator.setBackground(Color.white);
                tempVisualisator.setDoubleBuffered(false);
            }
            break;
        case EXPORT_SATELLITE:
            {
                tempVisualisator = satView;
                size = tempVisualisator.getSize();
            }
            break;
        default:
            ErrorReporter.logError("Unexpected image export type " + mode);
            return;
    }
    BufferedImage image;
    final GUIErrorHandler errorHandler = new GUIErrorHandler();
    try {
        image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    } catch (OutOfMemoryError e) {
        final long needed = (long) size.width * (long) size.height * 4;
        String temp;
        if (needed < 1024) {
            temp = needed + " bytes";
        } else if (needed < 1024 * 1024) {
            temp = needed / 1024 + " Kbytes";
        } else {
            temp = needed / 1024 / 1024 + " Mbytes";
        }
        final String errorText = "Could not save an image of " + size.width + "*" + size.height + " size as there was not enough free memory (" + temp + ")";
        errorHandler.reportErrorMessage(errorText);
        ErrorReporter.logExceptionStackTrace(errorText, e);
        return;
    }
    final Graphics2D g2 = image.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    tempVisualisator.paint(g2);
    g2.dispose();
    try {
        ImageIO.write(image, "png", new File(path));
    } catch (IOException e) {
        final String message = "Error while writing to file" + path;
        ErrorReporter.logExceptionStackTrace(message, e);
        errorHandler.reportException(message, e);
    }
}
Also used : GUIErrorHandler(org.eclipse.titanium.error.GUIErrorHandler) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor) Dimension(java.awt.Dimension) IOException(java.io.IOException) EdgeDescriptor(org.eclipse.titanium.graph.components.EdgeDescriptor) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Function(com.google.common.base.Function) Point2D(java.awt.geom.Point2D) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) CustomVisualizationViewer(org.eclipse.titanium.graph.gui.common.CustomVisualizationViewer) File(java.io.File)

Example 2 with GUIErrorHandler

use of org.eclipse.titanium.error.GUIErrorHandler in project titan.EclipsePlug-ins by eclipse.

the class ComponentGraphAction method doOpenGraphForProject.

/**
 * @param project the project whose graph is to be displayed
 */
private void doOpenGraphForProject(final IProject project) {
    try {
        // looking for a file inside the selected
        // project
        final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IFile input = null;
        IResource[] members = project.members();
        for (final IResource res : members) {
            if (res.getType() == IResource.FILE) {
                input = (IFile) res;
                break;
            }
            if (res.getType() == IResource.FOLDER) {
                members = ((IFolder) res).members();
            }
        }
        final IEditorPart editor = page.findEditor(new FileEditorInput(input));
        if (editor instanceof ComponentGraphEditor) {
            ((ComponentGraphEditor) editor).refreshGraph();
        } else {
            page.openEditor(new FileEditorInput(input), ComponentGraphEditor.ID, true, IWorkbenchPage.MATCH_ID | IWorkbenchPage.MATCH_INPUT);
        }
    } catch (Exception exc) {
        final ErrorHandler errorHandler = new GUIErrorHandler();
        errorHandler.reportException("Error while parsing the project", exc);
    }
}
Also used : GUIErrorHandler(org.eclipse.titanium.error.GUIErrorHandler) ErrorHandler(org.eclipse.titanium.error.ErrorHandler) IFile(org.eclipse.core.resources.IFile) GUIErrorHandler(org.eclipse.titanium.error.GUIErrorHandler) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ComponentGraphEditor(org.eclipse.titanium.graph.gui.windows.ComponentGraphEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) IResource(org.eclipse.core.resources.IResource) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

GUIErrorHandler (org.eclipse.titanium.error.GUIErrorHandler)2 Function (com.google.common.base.Function)1 VisualizationViewer (edu.uci.ics.jung.visualization.VisualizationViewer)1 Dimension (java.awt.Dimension)1 Graphics2D (java.awt.Graphics2D)1 Point2D (java.awt.geom.Point2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 ErrorHandler (org.eclipse.titanium.error.ErrorHandler)1 EdgeDescriptor (org.eclipse.titanium.graph.components.EdgeDescriptor)1 NodeDescriptor (org.eclipse.titanium.graph.components.NodeDescriptor)1 CustomVisualizationViewer (org.eclipse.titanium.graph.gui.common.CustomVisualizationViewer)1 ComponentGraphEditor (org.eclipse.titanium.graph.gui.windows.ComponentGraphEditor)1 IEditorPart (org.eclipse.ui.IEditorPart)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 FileEditorInput (org.eclipse.ui.part.FileEditorInput)1