Search in sources :

Example 11 with RenderingEngine

use of org.cytoscape.view.presentation.RenderingEngine in project cytoscape-impl by cytoscape.

the class ViewUtils method getVisualLexicon.

public static VisualLexicon getVisualLexicon(final CyGroupManagerImpl groupMgr, final CyNetworkView netView) {
    final RenderingEngineManager rendererMgr = groupMgr.getService(RenderingEngineManager.class);
    final Collection<RenderingEngine<?>> renderingEngines = rendererMgr.getRenderingEngines(netView);
    if (renderingEngines != null && !renderingEngines.isEmpty())
        return renderingEngines.iterator().next().getVisualLexicon();
    return rendererMgr.getDefaultVisualLexicon();
}
Also used : RenderingEngine(org.cytoscape.view.presentation.RenderingEngine) RenderingEngineManager(org.cytoscape.view.presentation.RenderingEngineManager)

Example 12 with RenderingEngine

use of org.cytoscape.view.presentation.RenderingEngine in project cytoscape-impl by cytoscape.

the class SessionWriterImpl method zipSessionThumbnail.

private void zipSessionThumbnail() {
    CyNetworkView view = serviceRegistrar.getService(CyApplicationManager.class).getCurrentNetworkView();
    if (view == null)
        view = session.getNetworkViews().isEmpty() ? null : session.getNetworkViews().iterator().next();
    if (view != null) {
        Collection<RenderingEngine<?>> engines = serviceRegistrar.getService(RenderingEngineManager.class).getRenderingEngines(view);
        Image img = null;
        for (RenderingEngine<?> re : engines) {
            try {
                img = re.createImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
                break;
            } catch (Throwable t) {
                // Better just log the error than prevent the session from being saved
                // only because a third party renderer throws an exception,
                // even if it never creates/saves a thumbnail.
                logger.error("Cannot create session thumbnail", t);
            }
        }
        if (img != null) {
            try {
                final RenderedImage ri;
                if (img instanceof RenderedImage) {
                    ri = (RenderedImage) img;
                } else {
                    ri = new BufferedImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, BufferedImage.TYPE_INT_ARGB);
                    Graphics g = ((BufferedImage) ri).getGraphics();
                    g.drawImage(img, 0, 0, null);
                    g.dispose();
                }
                zos.putNextEntry(new ZipEntry(sessionDir + THUMBNAIL_FILE));
                ImageIO.write(ri, "png", zos);
                zos.closeEntry();
            } catch (Exception e) {
                // Just log the error. Don't let it prevent the session from being saved!
                logger.error("Cannot save session thumbnail", e);
            }
        }
    }
}
Also used : RenderingEngineManager(org.cytoscape.view.presentation.RenderingEngineManager) ZipEntry(java.util.zip.ZipEntry) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) CyApplicationManager(org.cytoscape.application.CyApplicationManager) Graphics(java.awt.Graphics) RenderingEngine(org.cytoscape.view.presentation.RenderingEngine) RenderedImage(java.awt.image.RenderedImage) CyNetworkView(org.cytoscape.view.model.CyNetworkView)

Example 13 with RenderingEngine

use of org.cytoscape.view.presentation.RenderingEngine in project clusterMaker2 by RBVI.

the class RankingPanel method createClusterImage.

/**
 * Convert a network to an image.  This is used by the MCODEResultsPanel.
 *
 * @param cluster Input network to convert to an image
 * @param height  Height that the resulting image should be
 * @param width   Width that the resulting image should be
 * @param layouter Reference to the layout algorithm
 * @param layoutNecessary Determinant of cluster size growth or shrinkage, the former requires layout
 * @return The resulting image
 */
public Image createClusterImage(final NodeCluster cluster, final int height, final int width, SpringEmbeddedLayouter layouter, boolean layoutNecessary) {
    // System.out.println("CCI: inside method");
    final CyRootNetwork root = clusterManager.getService(CyRootNetworkManager.class).getRootNetwork(network);
    // need to create a method get the subnetwork for a cluster
    final CyNetwork net = cluster.getSubNetwork(network, root, SavePolicy.DO_NOT_SAVE);
    // System.out.println("CCI: after getting root and network ");
    // Progress reporters.
    // There are three basic tasks, the progress of each is calculated and then combined
    // using the respective weighting to get an overall progress global progress
    // setting up the nodes and edges is deemed as 25% of the whole task
    int weightSetupNodes = 20;
    int weightSetupEdges = 5;
    // layout it is 70%
    double weightLayout = 75.0;
    double goalTotal = weightSetupNodes + weightSetupEdges;
    if (layoutNecessary) {
        goalTotal += weightLayout;
    }
    // keeps track of progress as a percent of the totalGoal
    double progress = 0;
    final VisualStyle vs = getClusterStyle();
    // System.out.println("CCI: after getClusterStyle");
    final CyNetworkView clusterView = createNetworkView(net, vs);
    // System.out.println("CCI: after createNetworkView");
    clusterView.setVisualProperty(NETWORK_WIDTH, new Double(width));
    clusterView.setVisualProperty(NETWORK_HEIGHT, new Double(height));
    for (View<CyNode> nv : clusterView.getNodeViews()) {
        if (interrupted) {
            // problems the next time around
            if (layouter != null)
                layouter.resetDoLayout();
            resetLoading();
            return null;
        }
        // Node position
        final double x;
        final double y;
        // first prevents the program from throwing a null pointer exception in the second condition)
        if (cluster.getView() != null && cluster.getView().getNodeView(nv.getModel()) != null) {
            // If it does, then we take the layout position that was already generated for it
            x = cluster.getView().getNodeView(nv.getModel()).getVisualProperty(NODE_X_LOCATION);
            y = cluster.getView().getNodeView(nv.getModel()).getVisualProperty(NODE_Y_LOCATION);
        } else {
            // Otherwise, randomize node positions before layout so that they don't all layout in a line
            // (so they don't fall into a local minimum for the SpringEmbedder)
            // If the SpringEmbedder implementation changes, this code may need to be removed
            // size is small for many default drawn graphs, thus +100
            x = (clusterView.getVisualProperty(NETWORK_WIDTH) + 100) * Math.random();
            y = (clusterView.getVisualProperty(NETWORK_HEIGHT) + 100) * Math.random();
            if (!layoutNecessary) {
                goalTotal += weightLayout;
                progress /= (goalTotal / (goalTotal - weightLayout));
                layoutNecessary = true;
            }
        }
        nv.setVisualProperty(NODE_X_LOCATION, x);
        nv.setVisualProperty(NODE_Y_LOCATION, y);
    // Might be specific to MCODE
    /*
			// Node shape
			if (cluster.getSeedNode() == nv.getModel().getSUID()) {
				nv.setLockedValue(NODE_SHAPE, NodeShapeVisualProperty.RECTANGLE);
			} else {
				nv.setLockedValue(NODE_SHAPE, NodeShapeVisualProperty.ELLIPSE);
			}
			 */
    /*
			// Update loader
			if (loader != null) {
				progress += 100.0 * (1.0 / (double) clusterView.getNodeViews().size()) *
							((double) weightSetupNodes / (double) goalTotal);
				loader.setProgress((int) progress, "Setup: nodes");
			}

			*/
    }
    if (clusterView.getEdgeViews() != null) {
        for (int i = 0; i < clusterView.getEdgeViews().size(); i++) {
            if (interrupted) {
                // logger.error("Interrupted: Edge Setup");
                if (layouter != null)
                    layouter.resetDoLayout();
                resetLoading();
                return null;
            }
        /*
				if (loader != null) {
					progress += 100.0 * (1.0 / (double) clusterView.getEdgeViews().size()) *
								((double) weightSetupEdges / (double) goalTotal);
					loader.setProgress((int) progress, "Setup: edges");
				}
				*/
        }
    }
    if (layoutNecessary) {
        if (layouter == null) {
            layouter = new SpringEmbeddedLayouter();
        }
        layouter.setGraphView(clusterView);
        // The doLayout method should return true if the process completes without interruption
        if (!layouter.doLayout(weightLayout, goalTotal, progress)) {
            // Otherwise, if layout is not completed, set the interruption to false, and return null, not an image
            resetLoading();
            return null;
        }
    }
    final Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = (Graphics2D) image.getGraphics();
    SwingUtilities.invokeLater(new Runnable() {

        // @Override
        public void run() {
            try {
                final Dimension size = new Dimension(width, height);
                JPanel panel = new JPanel();
                panel.setPreferredSize(size);
                panel.setSize(size);
                panel.setMinimumSize(size);
                panel.setMaximumSize(size);
                panel.setBackground((Color) vs.getDefaultValue(NETWORK_BACKGROUND_PAINT));
                JWindow window = new JWindow();
                window.getContentPane().add(panel, BorderLayout.CENTER);
                RenderingEngine<CyNetwork> re = renderingEngineFactory.createRenderingEngine(panel, clusterView);
                vs.apply(clusterView);
                clusterView.fitContent();
                clusterView.updateView();
                window.pack();
                window.repaint();
                re.createImage(width, height);
                re.printCanvas(g);
                g.dispose();
                if (clusterView.getNodeViews().size() > 0) {
                    cluster.setView(clusterView);
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    layouter.resetDoLayout();
    resetLoading();
    return image;
}
Also used : JPanel(javax.swing.JPanel) CyRootNetworkManager(org.cytoscape.model.subnetwork.CyRootNetworkManager) Color(java.awt.Color) JWindow(javax.swing.JWindow) CyNetwork(org.cytoscape.model.CyNetwork) Dimension(java.awt.Dimension) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) RenderingEngine(org.cytoscape.view.presentation.RenderingEngine) CyNode(org.cytoscape.model.CyNode) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Example 14 with RenderingEngine

use of org.cytoscape.view.presentation.RenderingEngine in project clusterMaker2 by RBVI.

the class ResultsPanel method createClusterImage.

/**
 * Convert a network to an image.  This is used by the MCODEResultsPanel.
 *
 * @param cluster Input network to convert to an image
 * @param height  Height that the resulting image should be
 * @param width   Width that the resulting image should be
 * @param layouter Reference to the layout algorithm
 * @param layoutNecessary Determinant of cluster size growth or shrinkage, the former requires layout
 * @return The resulting image
 */
public Image createClusterImage(final NodeCluster cluster, final int height, final int width, SpringEmbeddedLayouter layouter, boolean layoutNecessary) {
    // System.out.println("CCI: inside method");
    final CyRootNetwork root = clusterManager.getService(CyRootNetworkManager.class).getRootNetwork(network);
    // need to create a method get the subnetwork for a cluster
    final CyNetwork net = cluster.getSubNetwork(network, root, SavePolicy.DO_NOT_SAVE);
    // System.out.println("CCI: after getting root and network ");
    // Progress reporters.
    // There are three basic tasks, the progress of each is calculated and then combined
    // using the respective weighting to get an overall progress global progress
    // setting up the nodes and edges is deemed as 25% of the whole task
    int weightSetupNodes = 20;
    int weightSetupEdges = 5;
    // layout it is 70%
    double weightLayout = 75.0;
    double goalTotal = weightSetupNodes + weightSetupEdges;
    if (layoutNecessary) {
        goalTotal += weightLayout;
    }
    // keeps track of progress as a percent of the totalGoal
    double progress = 0;
    final VisualStyle vs = getClusterStyle();
    // System.out.println("CCI: after getClusterStyle");
    final CyNetworkView clusterView = createNetworkView(net, vs);
    // System.out.println("CCI: after createNetworkView");
    clusterView.setVisualProperty(NETWORK_WIDTH, new Double(width));
    clusterView.setVisualProperty(NETWORK_HEIGHT, new Double(height));
    for (View<CyNode> nv : clusterView.getNodeViews()) {
        if (interrupted) {
            // problems the next time around
            if (layouter != null)
                layouter.resetDoLayout();
            resetLoading();
            return null;
        }
        // Node position
        final double x;
        final double y;
        // first prevents the program from throwing a null pointer exception in the second condition)
        if (cluster.getView() != null && cluster.getView().getNodeView(nv.getModel()) != null) {
            // If it does, then we take the layout position that was already generated for it
            x = cluster.getView().getNodeView(nv.getModel()).getVisualProperty(NODE_X_LOCATION);
            y = cluster.getView().getNodeView(nv.getModel()).getVisualProperty(NODE_Y_LOCATION);
        } else {
            // Otherwise, randomize node positions before layout so that they don't all layout in a line
            // (so they don't fall into a local minimum for the SpringEmbedder)
            // If the SpringEmbedder implementation changes, this code may need to be removed
            // size is small for many default drawn graphs, thus +100
            x = (clusterView.getVisualProperty(NETWORK_WIDTH) + 100) * Math.random();
            y = (clusterView.getVisualProperty(NETWORK_HEIGHT) + 100) * Math.random();
            if (!layoutNecessary) {
                goalTotal += weightLayout;
                progress /= (goalTotal / (goalTotal - weightLayout));
                layoutNecessary = true;
            }
        }
        nv.setVisualProperty(NODE_X_LOCATION, x);
        nv.setVisualProperty(NODE_Y_LOCATION, y);
    // Might be specific to MCODE
    /*
			// Node shape
			if (cluster.getSeedNode() == nv.getModel().getSUID()) {
				nv.setLockedValue(NODE_SHAPE, NodeShapeVisualProperty.RECTANGLE);
			} else {
				nv.setLockedValue(NODE_SHAPE, NodeShapeVisualProperty.ELLIPSE);
			}
			 */
    /*
			// Update loader
			if (loader != null) {
				progress += 100.0 * (1.0 / (double) clusterView.getNodeViews().size()) *
							((double) weightSetupNodes / (double) goalTotal);
				loader.setProgress((int) progress, "Setup: nodes");
			}
			
			*/
    }
    if (clusterView.getEdgeViews() != null) {
        for (int i = 0; i < clusterView.getEdgeViews().size(); i++) {
            if (interrupted) {
                // logger.error("Interrupted: Edge Setup");
                if (layouter != null)
                    layouter.resetDoLayout();
                resetLoading();
                return null;
            }
        /*
				if (loader != null) {
					progress += 100.0 * (1.0 / (double) clusterView.getEdgeViews().size()) *
								((double) weightSetupEdges / (double) goalTotal);
					loader.setProgress((int) progress, "Setup: edges");
				}
				*/
        }
    }
    if (layoutNecessary) {
        if (layouter == null) {
            layouter = new SpringEmbeddedLayouter();
        }
        layouter.setGraphView(clusterView);
        // The doLayout method should return true if the process completes without interruption
        if (!layouter.doLayout(weightLayout, goalTotal, progress)) {
            // Otherwise, if layout is not completed, set the interruption to false, and return null, not an image
            resetLoading();
            return null;
        }
    }
    final Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = (Graphics2D) image.getGraphics();
    SwingUtilities.invokeLater(new Runnable() {

        // @Override
        public void run() {
            try {
                final Dimension size = new Dimension(width, height);
                JPanel panel = new JPanel();
                panel.setPreferredSize(size);
                panel.setSize(size);
                panel.setMinimumSize(size);
                panel.setMaximumSize(size);
                panel.setBackground((Color) vs.getDefaultValue(NETWORK_BACKGROUND_PAINT));
                JWindow window = new JWindow();
                window.getContentPane().add(panel, BorderLayout.CENTER);
                RenderingEngine<CyNetwork> re = renderingEngineFactory.createRenderingEngine(panel, clusterView);
                vs.apply(clusterView);
                clusterView.fitContent();
                clusterView.updateView();
                window.pack();
                window.repaint();
                re.createImage(width, height);
                re.printCanvas(g);
                g.dispose();
                if (clusterView.getNodeViews().size() > 0) {
                    cluster.setView(clusterView);
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    layouter.resetDoLayout();
    resetLoading();
    return image;
}
Also used : JPanel(javax.swing.JPanel) CyRootNetworkManager(org.cytoscape.model.subnetwork.CyRootNetworkManager) Color(java.awt.Color) JWindow(javax.swing.JWindow) CyNetwork(org.cytoscape.model.CyNetwork) Dimension(java.awt.Dimension) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) RenderingEngine(org.cytoscape.view.presentation.RenderingEngine) CyNode(org.cytoscape.model.CyNode) VisualStyle(org.cytoscape.view.vizmap.VisualStyle) CyNetworkView(org.cytoscape.view.model.CyNetworkView) CyRootNetwork(org.cytoscape.model.subnetwork.CyRootNetwork)

Aggregations

RenderingEngine (org.cytoscape.view.presentation.RenderingEngine)14 CyNetworkView (org.cytoscape.view.model.CyNetworkView)7 RenderingEngineManager (org.cytoscape.view.presentation.RenderingEngineManager)6 CyNetwork (org.cytoscape.model.CyNetwork)4 CyNode (org.cytoscape.model.CyNode)4 Dimension (java.awt.Dimension)3 Image (java.awt.Image)3 BufferedImage (java.awt.image.BufferedImage)3 ArrayList (java.util.ArrayList)3 JPanel (javax.swing.JPanel)3 Color (java.awt.Color)2 Graphics2D (java.awt.Graphics2D)2 JWindow (javax.swing.JWindow)2 CyApplicationManager (org.cytoscape.application.CyApplicationManager)2 CyFileFilter (org.cytoscape.io.CyFileFilter)2 CyEdge (org.cytoscape.model.CyEdge)2 CyRootNetwork (org.cytoscape.model.subnetwork.CyRootNetwork)2 CyRootNetworkManager (org.cytoscape.model.subnetwork.CyRootNetworkManager)2 VisualLexicon (org.cytoscape.view.model.VisualLexicon)2 VisualProperty (org.cytoscape.view.model.VisualProperty)2