Search in sources :

Example 1 with Graph

use of org.eclipse.zest.core.widgets.Graph in project hale by halestudio.

the class HtmlMappingExporter method saveImageToFile.

private void saveImageToFile(final Cell cell, File filesDir) {
    Display display;
    if (Display.getCurrent() != null) {
        // use the current display if available
        display = Display.getCurrent();
    } else {
        try {
            // use workbench display if available
            display = PlatformUI.getWorkbench().getDisplay();
        } catch (Throwable e) {
            // use a dedicated display thread if no workbench is
            // available
            display = DisplayThread.getInstance().getDisplay();
        }
    }
    // creates a unique id for each cell
    String cellId = cellIds.getId(cell);
    final File file = new File(filesDir, "img_" + cellId + ".png");
    display.syncExec(new Runnable() {

        @Override
        public void run() {
            OffscreenGraph offscreenGraph = new OffscreenGraph(600, 200) {

                @Override
                protected void configureViewer(GraphViewer viewer) {
                    IContentProvider contentProvider = new CellGraphContentProvider();
                    GraphLabelProvider labelProvider = new GraphLabelProvider(null, HaleUI.getServiceProvider());
                    viewer.setContentProvider(contentProvider);
                    viewer.setLabelProvider(labelProvider);
                    viewer.setInput(cell);
                }
            };
            Graph graph = offscreenGraph.getGraph();
            Dimension dimension = computeSize(graph);
            // minimum width = 600
            offscreenGraph.resize(dimension.width > 600 ? dimension.width : 600, dimension.height);
            try {
                offscreenGraph.saveImage(new BufferedOutputStream(new FileOutputStream(file)), null);
            } catch (Exception e) {
                reporter.error(new IOMessageImpl("Can not create image", e));
            } finally {
                offscreenGraph.dispose();
            }
        }
    });
}
Also used : IContentProvider(org.eclipse.jface.viewers.IContentProvider) GraphLabelProvider(eu.esdihumboldt.hale.ui.common.graph.labels.GraphLabelProvider) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) CellGraphContentProvider(eu.esdihumboldt.hale.ui.common.graph.content.CellGraphContentProvider) Dimension(java.awt.Dimension) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GraphViewer(org.eclipse.zest.core.viewers.GraphViewer) OffscreenGraph(eu.esdihumboldt.hale.ui.util.graph.OffscreenGraph) Graph(org.eclipse.zest.core.widgets.Graph) FileOutputStream(java.io.FileOutputStream) OffscreenGraph(eu.esdihumboldt.hale.ui.util.graph.OffscreenGraph) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Display(org.eclipse.swt.widgets.Display)

Example 2 with Graph

use of org.eclipse.zest.core.widgets.Graph in project hale by halestudio.

the class ExportGraphAction method run.

/**
 * @see Action#run()
 */
@Override
public void run() {
    FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
    // XXX if called from TTreeExporter during transformation, the active
    // shell may be null!
    dialog.setOverwrite(true);
    dialog.setText("Export graph to file");
    String[] imageExtensions = ImageIO.getWriterFileSuffixes();
    StringBuffer extensions = new StringBuffer("*.svg;*.gv;*.dot");
    for (String imageExt : imageExtensions) {
        extensions.append(";*.");
        extensions.append(imageExt);
    }
    dialog.setFilterExtensions(new String[] { extensions.toString() });
    dialog.setFilterNames(new String[] { "Image, SVG or dot file (" + extensions + ")" });
    String fileName = dialog.open();
    if (fileName != null) {
        final File file = new File(fileName);
        // //XXX use an off-screen graph (testing)
        // OffscreenGraph graph = new OffscreenGraph(1000, 1000) {
        // 
        // @Override
        // protected void configureViewer(GraphViewer viewer) {
        // viewer.setContentProvider(RenderAction.this.viewer.getContentProvider());
        // viewer.setLabelProvider(RenderAction.this.viewer.getLabelProvider());
        // viewer.setInput(RenderAction.this.viewer.getInput());
        // viewer.setLayoutAlgorithm(new TreeLayoutAlgorithm(TreeLayoutAlgorithm.LEFT_RIGHT), false);
        // }
        // };
        // get the graph
        final Graph graph = viewer.getGraphControl();
        final String ext = FilenameUtils.getExtension(file.getAbsolutePath());
        final IFigure root = graph.getRootLayer();
        ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
        try {
            progress.run(false, false, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                        if (ext.equalsIgnoreCase("gv") || ext.equalsIgnoreCase("dot")) {
                            OffscreenGraph.saveDot(graph, out);
                        } else // else if (ext.equalsIgnoreCase("svg")) {
                        // OffscreenGraph.saveSVG(root, out);
                        // }
                        {
                            OffscreenGraph.saveImage(root, out, ext);
                        }
                    } catch (Throwable e) {
                        log.userError("Error saving graph to file", e);
                    }
                }
            });
        } catch (Throwable e) {
            log.error("Error launching graph export", e);
        }
    }
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Graph(org.eclipse.zest.core.widgets.Graph) FileOutputStream(java.io.FileOutputStream) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IFigure(org.eclipse.draw2d.IFigure)

Example 3 with Graph

use of org.eclipse.zest.core.widgets.Graph in project hale by halestudio.

the class ImageContent method getImageContent.

/**
 * Get the function image for the function with the given identifier.
 *
 * @param func_id the function identifier
 * @param tempDir the temporary directory where the function image may be
 *            stored
 * @return the function image input stream or <code>null</code>
 * @throws Exception if an error occurs generating the image
 */
public static InputStream getImageContent(String func_id, File tempDir) throws Exception {
    final FunctionDefinition<?> function = FunctionUtil.getFunction(func_id, null);
    if (function == null) {
        log.warn("Unknown function " + func_id);
        return null;
    }
    final File _functionFile = new File(tempDir, func_id + ".png");
    if (!_functionFile.exists()) {
        Display display;
        if (Display.getCurrent() != null) {
            // use the current display if available
            display = Display.getCurrent();
        } else {
            try {
                // use workbench display if available
                display = PlatformUI.getWorkbench().getDisplay();
            } catch (Throwable e) {
                // use a dedicated display thread if no workbench is
                // available
                display = DisplayThread.getInstance().getDisplay();
            }
        }
        display.syncExec(new Runnable() {

            @Override
            public void run() {
                // create an initial off-screen graph with fixed values;
                // resize the graph after computing the figures width and
                // height
                OffscreenGraph off_graph = new OffscreenGraph(300, 200) {

                    @Override
                    protected void configureViewer(GraphViewer viewer) {
                        LayoutAlgorithm algo = new FunctionTreeLayoutAlgorithm();
                        FunctionGraphContentProvider stcp = new FunctionGraphContentProvider();
                        // XXX no service provider given
                        FunctionGraphLabelProvider fglp = new FunctionGraphLabelProvider(null, false);
                        viewer.setContentProvider(stcp);
                        viewer.setLabelProvider(fglp);
                        viewer.setInput(function);
                        viewer.setLayoutAlgorithm(algo);
                    }
                };
                Graph graph = off_graph.getGraph();
                Dimension dim = computeSize(graph);
                int width;
                if (dim.width > 450) {
                    width = dim.width;
                } else {
                    // minimum width = 450
                    width = 450;
                }
                int height = dim.height;
                off_graph.resize(width, height);
                try {
                    off_graph.saveImage(new BufferedOutputStream(new FileOutputStream(_functionFile)), null);
                } catch (IOException e) {
                    log.warn("Conversion from Graph to Image failed!");
                } finally {
                    off_graph.dispose();
                }
            }
        });
    }
    if (_functionFile.exists()) {
        return new FileInputStream(_functionFile);
    }
    return null;
}
Also used : FunctionTreeLayoutAlgorithm(eu.esdihumboldt.hale.ui.common.graph.layout.FunctionTreeLayoutAlgorithm) Dimension(java.awt.Dimension) IOException(java.io.IOException) FunctionGraphLabelProvider(eu.esdihumboldt.hale.ui.common.graph.labels.FunctionGraphLabelProvider) FileInputStream(java.io.FileInputStream) GraphViewer(org.eclipse.zest.core.viewers.GraphViewer) OffscreenGraph(eu.esdihumboldt.hale.ui.util.graph.OffscreenGraph) Graph(org.eclipse.zest.core.widgets.Graph) FunctionTreeLayoutAlgorithm(eu.esdihumboldt.hale.ui.common.graph.layout.FunctionTreeLayoutAlgorithm) LayoutAlgorithm(org.eclipse.zest.layouts.LayoutAlgorithm) FileOutputStream(java.io.FileOutputStream) OffscreenGraph(eu.esdihumboldt.hale.ui.util.graph.OffscreenGraph) FunctionGraphContentProvider(eu.esdihumboldt.hale.ui.common.graph.content.FunctionGraphContentProvider) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Display(org.eclipse.swt.widgets.Display)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 Graph (org.eclipse.zest.core.widgets.Graph)3 OffscreenGraph (eu.esdihumboldt.hale.ui.util.graph.OffscreenGraph)2 Dimension (java.awt.Dimension)2 IOException (java.io.IOException)2 Display (org.eclipse.swt.widgets.Display)2 GraphViewer (org.eclipse.zest.core.viewers.GraphViewer)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 CellGraphContentProvider (eu.esdihumboldt.hale.ui.common.graph.content.CellGraphContentProvider)1 FunctionGraphContentProvider (eu.esdihumboldt.hale.ui.common.graph.content.FunctionGraphContentProvider)1 FunctionGraphLabelProvider (eu.esdihumboldt.hale.ui.common.graph.labels.FunctionGraphLabelProvider)1 GraphLabelProvider (eu.esdihumboldt.hale.ui.common.graph.labels.GraphLabelProvider)1 FunctionTreeLayoutAlgorithm (eu.esdihumboldt.hale.ui.common.graph.layout.FunctionTreeLayoutAlgorithm)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1