Search in sources :

Example 1 with GraphViewer

use of org.eclipse.zest.core.viewers.GraphViewer 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 GraphViewer

use of org.eclipse.zest.core.viewers.GraphViewer in project hale by halestudio.

the class AbstractMappingView method createViewControl.

@Override
public void createViewControl(Composite parent) {
    viewer = new GraphViewer(parent, SWT.BORDER);
    viewer.setContentProvider(createContentProvider());
    // viewer.setContentProvider(new CellRelationshipContentProvider());
    // viewer.setContentProvider(new NestedCellRelationshipContentProvider());
    viewer.setLabelProvider(createLabelProvider(viewer));
    viewer.setInput(null);
    LayoutAlgorithm layout = createLayout();
    viewer.setLayoutAlgorithm(layout, true);
    viewer.applyLayout();
    fillToolBar();
    // set selection provider
    selectionFacade = new SelectionProviderFacade();
    selectionFacade.setSelectionProvider(getViewer());
    getSite().setSelectionProvider(new PostSelectionSupport(selectionFacade));
    viewer.getControl().addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.keyCode == 'a' && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
                // XXX even though getSelection returns the current state, a
                // selection update is not triggered by the control
                // -> force selection change event after Ctrl+A
                ISelection sel = viewer.getSelection();
                selectionFacade.setSelection(sel);
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
        // nothing to do
        }
    });
    // create context menu
    new ViewerMenu(getSite(), getViewer()) {

        /**
         * @see eu.esdihumboldt.hale.ui.util.ViewContextMenu#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
         */
        @Override
        public void menuAboutToShow(IMenuManager manager) {
            super.menuAboutToShow(manager);
            AbstractMappingView.this.menuAboutToShow(manager);
        }
    };
}
Also used : KeyEvent(org.eclipse.swt.events.KeyEvent) GraphViewer(org.eclipse.zest.core.viewers.GraphViewer) TreeLayoutAlgorithm(org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm) LayoutAlgorithm(org.eclipse.zest.layouts.LayoutAlgorithm) SelectionProviderFacade(eu.esdihumboldt.hale.ui.util.selection.SelectionProviderFacade) ISelection(org.eclipse.jface.viewers.ISelection) KeyListener(org.eclipse.swt.events.KeyListener) IMenuManager(org.eclipse.jface.action.IMenuManager) PostSelectionSupport(eu.esdihumboldt.hale.ui.util.viewer.PostSelectionSupport) ViewerMenu(eu.esdihumboldt.hale.ui.util.viewer.ViewerMenu)

Example 3 with GraphViewer

use of org.eclipse.zest.core.viewers.GraphViewer 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)

Example 4 with GraphViewer

use of org.eclipse.zest.core.viewers.GraphViewer in project hale by halestudio.

the class GraphMLDialog method open.

/**
 * Opens the dialog for displaying the graph
 *
 * @throws IOException may be thrown if the graph string from the database
 *             can fails to convert
 */
public void open() throws IOException {
    Shell parent = super.getParent();
    final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.MIN | SWT.MODELESS);
    shell.setFocus();
    shell.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            shell.setMinimized(true);
        }

        @Override
        public void focusGained(FocusEvent e) {
        // ignore
        }
    });
    shell.setLayout(GridLayoutFactory.fillDefaults().create());
    shell.setText(getText());
    final Composite viewerContainer = new Composite(shell, SWT.EMBEDDED);
    viewerContainer.setLayout(new FillLayout());
    viewerContainer.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    TinkerGraph tgraph = new TinkerGraph();
    GraphMLReader greader = new GraphMLReader(tgraph);
    ByteArrayInputStream in;
    in = new ByteArrayInputStream(graphString.getBytes(("UTF-8")));
    greader.inputGraph(in);
    GraphViewer viewer = new GraphViewer(viewerContainer, SWT.NONE);
    TreeLayoutAlgorithm la = new TreeLayoutAlgorithm(TreeLayoutAlgorithm.RIGHT_LEFT);
    viewer.setLabelProvider(new GraphMLLabelProvider());
    viewer.setContentProvider(new GraphMLContentProvider());
    viewer.setInput(tgraph.getEdges());
    viewer.setLayoutAlgorithm(la, true);
    viewer.applyLayout();
    viewerContainer.pack();
    viewerContainer.setVisible(true);
    shell.open();
    Display display = parent.getDisplay();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
}
Also used : Composite(org.eclipse.swt.widgets.Composite) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) TreeLayoutAlgorithm(org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm) FillLayout(org.eclipse.swt.layout.FillLayout) FocusEvent(org.eclipse.swt.events.FocusEvent) Shell(org.eclipse.swt.widgets.Shell) GraphViewer(org.eclipse.zest.core.viewers.GraphViewer) ByteArrayInputStream(java.io.ByteArrayInputStream) GraphMLReader(com.tinkerpop.blueprints.util.io.graphml.GraphMLReader) FocusListener(org.eclipse.swt.events.FocusListener) Display(org.eclipse.swt.widgets.Display)

Example 5 with GraphViewer

use of org.eclipse.zest.core.viewers.GraphViewer in project hale by halestudio.

the class FunctionGraphSection method createControls.

/**
 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite,
 *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
 */
@Override
public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
    super.createControls(parent, aTabbedPropertySheetPage);
    Composite compparent = getWidgetFactory().createComposite(parent);
    compparent.setLayout(new FormLayout());
    Composite composite = getWidgetFactory().createComposite(compparent);
    composite.setLayout(new FillLayout());
    FormData data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, -0);
    data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
    data.bottom = new FormAttachment(100, -ITabbedPropertyConstants.VSPACE);
    composite.setLayoutData(data);
    viewer = new GraphViewer(composite, SWT.NONE);
    viewer.setLayoutAlgorithm(treeAlgorithm, true);
    viewer.setContentProvider(new FunctionGraphContentProvider());
    viewer.setLabelProvider(new FunctionGraphLabelProvider(HaleUI.getServiceProvider(), true));
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) GraphViewer(org.eclipse.zest.core.viewers.GraphViewer) Composite(org.eclipse.swt.widgets.Composite) FunctionGraphContentProvider(eu.esdihumboldt.hale.ui.common.graph.content.FunctionGraphContentProvider) FillLayout(org.eclipse.swt.layout.FillLayout) FormAttachment(org.eclipse.swt.layout.FormAttachment) FunctionGraphLabelProvider(eu.esdihumboldt.hale.ui.common.graph.labels.FunctionGraphLabelProvider)

Aggregations

GraphViewer (org.eclipse.zest.core.viewers.GraphViewer)6 Display (org.eclipse.swt.widgets.Display)3 FunctionGraphContentProvider (eu.esdihumboldt.hale.ui.common.graph.content.FunctionGraphContentProvider)2 FunctionGraphLabelProvider (eu.esdihumboldt.hale.ui.common.graph.labels.FunctionGraphLabelProvider)2 OffscreenGraph (eu.esdihumboldt.hale.ui.util.graph.OffscreenGraph)2 Dimension (java.awt.Dimension)2 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 Composite (org.eclipse.swt.widgets.Composite)2 Graph (org.eclipse.zest.core.widgets.Graph)2 LayoutAlgorithm (org.eclipse.zest.layouts.LayoutAlgorithm)2 TreeLayoutAlgorithm (org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm)2 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)1 GraphMLReader (com.tinkerpop.blueprints.util.io.graphml.GraphMLReader)1 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