Search in sources :

Example 1 with VisualizationViewer

use of edu.uci.ics.jung.visualization.VisualizationViewer in project tdq-studio-se by Talend.

the class JungGraphGenerator method configureVViewer.

/**
 * DOC bZhou Comment method "configureVViewer".
 *
 * @param vv
 */
private void configureVViewer(final VisualizationViewer vv) {
    // vv = new VisualizationViewer(new CircleLayout(graph), pr);
    // vv = new VisualizationViewer(new SpringLayout(graph), pr);
    // vv = new VisualizationViewer(new KKLayout(graph), pr);
    vv.setBackground(Color.white);
    vv.setPickSupport(new ShapePickSupport());
    vv.addPostRenderPaintable(new VisualizationViewer.Paintable() {

        int x;

        int y;

        Font font;

        FontMetrics metrics;

        int swidth;

        int sheight;

        // $NON-NLS-1$
        String str = PluginConstant.EMPTY_STRING;

        public void paint(Graphics g) {
            Dimension d = vv.getSize();
            if (font == null) {
                font = new Font(g.getFont().getName(), Font.BOLD, 30);
                metrics = g.getFontMetrics(font);
                swidth = metrics.stringWidth(str);
                sheight = metrics.getMaxAscent() + metrics.getMaxDescent();
                x = (d.width - swidth) / 2;
                y = (int) (d.height - sheight * 1.5);
            }
            g.setFont(font);
            Color oldColor = g.getColor();
            g.setColor(Color.DARK_GRAY);
            g.drawString(str, x, y);
            g.setColor(oldColor);
        }

        public boolean useTransform() {
            return false;
        }
    });
    // vv.addGraphMouseListener(new TestGraphMouseListener());
    // add my listener for ToolTips
    vv.setToolTipFunction(new DefaultToolTipFunction());
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) Color(java.awt.Color) SatelliteVisualizationViewer(edu.uci.ics.jung.visualization.control.SatelliteVisualizationViewer) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) ShapePickSupport(edu.uci.ics.jung.visualization.ShapePickSupport) Dimension(java.awt.Dimension) DefaultToolTipFunction(edu.uci.ics.jung.graph.decorators.DefaultToolTipFunction) Font(java.awt.Font)

Example 2 with VisualizationViewer

use of edu.uci.ics.jung.visualization.VisualizationViewer in project tdq-studio-se by Talend.

the class JungGraphGenerator method generate.

public Composite generate(Composite parent, boolean isWithHelp) {
    Composite frameComp = createAWTSWTComposite(parent);
    frame = SWT_AWT.new_Frame(frameComp);
    // $NON-NLS-1$
    frame.setTitle(DefaultMessagesImpl.getString("JungGraphGenerator.NominalAnalysis"));
    pr = new LineRender(graphbuilder);
    sr = new LineRender(graphbuilder);
    PersistentLayout layout = new PersistentLayoutImpl(new ISOMLayout(graph));
    VisualizationModel vm = new DefaultVisualizationModel(layout);
    vv = new VisualizationViewer(vm, pr);
    sv = new SatelliteVisualizationViewer(vv, vm, sr, new Dimension(200, 200));
    configureVViewer(vv);
    // create contollers
    JPanel controllers = createToolControllers(vv);
    // MOD yyi 2009-09-09 feature 8834
    if (this.isPreview)
        controllers.setVisible(false);
    JPanel panel = new GraphZoomScrollPane(vv);
    if (isWithHelp) {
        helpDialog = createSatelliteDialog(vv, sv);
        controllers.add(createShowSatelliteCheck(helpDialog));
    }
    frame.add(panel);
    frame.add(controllers, BorderLayout.SOUTH);
    frame.validate();
    addListeners();
    return frameComp;
}
Also used : SatelliteVisualizationViewer(edu.uci.ics.jung.visualization.control.SatelliteVisualizationViewer) JPanel(javax.swing.JPanel) DefaultVisualizationModel(edu.uci.ics.jung.visualization.DefaultVisualizationModel) Composite(org.eclipse.swt.widgets.Composite) VisualizationModel(edu.uci.ics.jung.visualization.VisualizationModel) DefaultVisualizationModel(edu.uci.ics.jung.visualization.DefaultVisualizationModel) PersistentLayoutImpl(edu.uci.ics.jung.visualization.PersistentLayoutImpl) SatelliteVisualizationViewer(edu.uci.ics.jung.visualization.control.SatelliteVisualizationViewer) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) PersistentLayout(edu.uci.ics.jung.visualization.PersistentLayout) Dimension(java.awt.Dimension) ISOMLayout(edu.uci.ics.jung.visualization.ISOMLayout)

Example 3 with VisualizationViewer

use of edu.uci.ics.jung.visualization.VisualizationViewer in project tdq-studio-se by Talend.

the class MyFirstMain method run.

public void run(final GraphBuilder graphbuilder) {
    Graph graph = graphbuilder.createMultiGraph(allData);
    PluggableRenderer pr = new PluggableRenderer();
    vv = new VisualizationViewer(new FRLayout(graph), pr);
    // vv = new VisualizationViewer(new CircleLayout(graph), pr);
    // vv = new VisualizationViewer(new SpringLayout(graph), pr);
    // vv = new VisualizationViewer(new KKLayout(graph), pr);
    vv.setBackground(Color.white);
    vv.setPickSupport(new ShapePickSupport());
    pr.setEdgeShapeFunction(new EdgeShape.Line());
    vv.addPostRenderPaintable(new VisualizationViewer.Paintable() {

        int x;

        int y;

        Font font;

        FontMetrics metrics;

        int swidth;

        int sheight;

        String str = "My first Demo";

        public void paint(Graphics g) {
            Dimension d = vv.getSize();
            if (font == null) {
                font = new Font(g.getFont().getName(), Font.BOLD, 30);
                metrics = g.getFontMetrics(font);
                swidth = metrics.stringWidth(str);
                sheight = metrics.getMaxAscent() + metrics.getMaxDescent();
                x = (d.width - swidth) / 2;
                y = (int) (d.height - sheight * 1.5);
            }
            g.setFont(font);
            Color oldColor = g.getColor();
            g.setColor(Color.DARK_GRAY);
            g.drawString(str, x, y);
            g.setColor(oldColor);
        }

        public boolean useTransform() {
            return false;
        }
    });
    pr.setVertexPaintFunction(new VertexPaintFunction() {

        public Paint getFillPaint(Vertex v) {
            final Object userDatum = v.getUserDatum(GraphBuilder.COLUMN_IDX_KEY);
            if (userDatum != null) {
                Integer colIndex = (Integer) userDatum;
                return AWTColorUtils.getColor(colIndex);
            }
            return null;
        }

        public Paint getDrawPaint(Vertex v) {
            return Color.BLACK;
        }
    });
    pr.setVertexStringer(new VertexStringer() {

        public String getLabel(ArchetypeVertex v) {
            final Object userDatum = v.getUserDatum(GraphBuilder.V_LABEL_KEY);
            if (userDatum != null) {
                return (String) userDatum;
            }
            return null;
        }
    });
    pr.setEdgeStrokeFunction(new EdgeStrokeFunction() {

        public Stroke getStroke(Edge e) {
            Integer weight = graphbuilder.getEdgeWeight().getNumber(e).intValue();
            // return new BasicStroke(10 * weight / graphbuilder.getTotalWeight());
            return new BasicStroke(10.0f / weight);
        }
    });
    pr.setEdgePaintFunction(new EdgePaintFunction() {

        public Paint getFillPaint(Edge e) {
            return null;
        }

        public Paint getDrawPaint(Edge e) {
            return getInternalPaint(e);
        }

        private Paint getInternalPaint(Edge e) {
            final Object userDatum = e.getUserDatum(GraphBuilder.E_ROWNUM_KEY);
            if (userDatum != null) {
                Integer color = (Integer) userDatum;
                return AWTColorUtils.getColor(color);
            }
            return Color.GRAY;
        }
    });
    pr.setEdgeStringer(new EdgeStringer() {

        public String getLabel(ArchetypeEdge arg0) {
            return String.valueOf(graphbuilder.getEdgeWeight().getNumber(arg0));
        // final Object userDatum = arg0.getUserDatum(GraphBuilder.E_LABEL_KEY);
        // if (userDatum != null) {
        // return (String) userDatum;
        // }
        // return null;
        }
    });
    // vv.addGraphMouseListener(new TestGraphMouseListener());
    // add my listener for ToolTips
    vv.setToolTipFunction(new DefaultToolTipFunction());
    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphMouse graphMouse = new DefaultModalGraphMouse();
    vv.setGraphMouse(graphMouse);
    JMenuBar menu = new JMenuBar();
    menu.add(((DefaultModalGraphMouse) graphMouse).getModeMenu());
    panel.setCorner(menu);
    final ScalingControl scaler = new CrossoverScalingControl();
    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });
    JButton reset = new JButton("reset");
    reset.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            vv.getLayoutTransformer().setToIdentity();
            vv.getViewTransformer().setToIdentity();
        }
    });
    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(reset);
    content.add(controls, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}
Also used : BasicStroke(java.awt.BasicStroke) Vertex(edu.uci.ics.jung.graph.Vertex) ArchetypeVertex(edu.uci.ics.jung.graph.ArchetypeVertex) JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) FRLayout(edu.uci.ics.jung.visualization.FRLayout) JButton(javax.swing.JButton) Font(java.awt.Font) Container(java.awt.Container) JFrame(javax.swing.JFrame) FontMetrics(java.awt.FontMetrics) ArchetypeEdge(edu.uci.ics.jung.graph.ArchetypeEdge) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) ScalingControl(edu.uci.ics.jung.visualization.control.ScalingControl) CrossoverScalingControl(edu.uci.ics.jung.visualization.control.CrossoverScalingControl) ShapePickSupport(edu.uci.ics.jung.visualization.ShapePickSupport) GraphMouse(edu.uci.ics.jung.visualization.VisualizationViewer.GraphMouse) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) EdgeStringer(edu.uci.ics.jung.graph.decorators.EdgeStringer) EdgeShape(edu.uci.ics.jung.graph.decorators.EdgeShape) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) ArchetypeVertex(edu.uci.ics.jung.graph.ArchetypeVertex) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) Color(java.awt.Color) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) Dimension(java.awt.Dimension) VertexPaintFunction(edu.uci.ics.jung.graph.decorators.VertexPaintFunction) Paint(java.awt.Paint) EdgePaintFunction(edu.uci.ics.jung.graph.decorators.EdgePaintFunction) Paint(java.awt.Paint) Graphics(java.awt.Graphics) Graph(edu.uci.ics.jung.graph.Graph) ActionListener(java.awt.event.ActionListener) VertexStringer(edu.uci.ics.jung.graph.decorators.VertexStringer) PluggableRenderer(edu.uci.ics.jung.visualization.PluggableRenderer) EdgeStrokeFunction(edu.uci.ics.jung.graph.decorators.EdgeStrokeFunction) DefaultToolTipFunction(edu.uci.ics.jung.graph.decorators.DefaultToolTipFunction) CrossoverScalingControl(edu.uci.ics.jung.visualization.control.CrossoverScalingControl) Edge(edu.uci.ics.jung.graph.Edge) ArchetypeEdge(edu.uci.ics.jung.graph.ArchetypeEdge) JMenuBar(javax.swing.JMenuBar)

Example 4 with VisualizationViewer

use of edu.uci.ics.jung.visualization.VisualizationViewer in project tdq-studio-se by Talend.

the class MyFullDemoMain method startFunction.

public JPanel startFunction() {
    final GraphBuilder graphBuilder = new GraphBuilder();
    // getGraph();
    Graph g = graphBuilder.createMultiGraph(MyFirstMain.createListObjects());
    // init variables
    edge_weight = graphBuilder.getEdgeWeight();
    vertexWeights = graphBuilder.getVertexWeight();
    final GraphRenderer graphRenderer = new GraphRenderer();
    // pr = graphRenderer.createPluggableRenderer();
    pr = new PluggableRenderer();
    Layout layout = new FRLayout(g);
    vv = new VisualizationViewer(layout, pr);
    // add Shape based pick support
    vv.setPickSupport(new ShapePickSupport());
    PickedState picked_state = vv.getPickedState();
    affineTransformer = vv.getLayoutTransformer();
    // create decorators
    vcf = new SeedColor(picked_state);
    ewcs = new EdgeWeightStrokeFunction(edge_weight);
    vsh = new VertexStrokeHighlight(picked_state);
    ff = new FontHandler();
    vs_none = new ConstantVertexStringer(null);
    es_none = new ConstantEdgeStringer(null);
    vssa = new VertexShapeSizeAspect(vertexWeights);
    show_edge = new DirectionDisplayPredicate(true, true);
    show_arrow = new DirectionDisplayPredicate(true, false);
    show_vertex = new VertexDisplayPredicate(false);
    // uses a gradient edge if unpicked, otherwise uses picked selection
    edgePaint = new GradientPickedEdgePaintFunction(new PickableEdgePaintFunction(picked_state, Color.black, Color.cyan), vv, vv, picked_state);
    pr.setVertexPaintFunction(vcf);
    pr.setVertexStrokeFunction(vsh);
    pr.setVertexStringer(vs_none);
    pr.setVertexFontFunction(ff);
    pr.setVertexShapeFunction(vssa);
    pr.setVertexIncludePredicate(show_vertex);
    pr.setEdgePaintFunction(edgePaint);
    pr.setEdgeStringer(es_none);
    pr.setEdgeFontFunction(ff);
    pr.setEdgeStrokeFunction(ewcs);
    pr.setEdgeIncludePredicate(show_edge);
    pr.setEdgeShapeFunction(new EdgeShape.Line());
    pr.setEdgeArrowPredicate(show_arrow);
    JPanel jp = new JPanel();
    jp.setLayout(new BorderLayout());
    vv.setBackground(Color.white);
    GraphZoomScrollPane scrollPane = new GraphZoomScrollPane(vv);
    jp.add(scrollPane);
    gm = new DefaultModalGraphMouse();
    vv.setGraphMouse(gm);
    gm.add(new PopupGraphMousePlugin());
    addBottomControls(jp);
    vssa.setScaling(true);
    vv.setToolTipFunction(new VoltageTips());
    vv.setToolTipText("<html><center>Use the mouse wheel to zoom<p>Click and Drag the mouse to pan<p>Shift-click and Drag to Rotate</center></html>");
    return jp;
}
Also used : JPanel(javax.swing.JPanel) ConstantEdgeStringer(edu.uci.ics.jung.graph.decorators.ConstantEdgeStringer) PickableEdgePaintFunction(edu.uci.ics.jung.graph.decorators.PickableEdgePaintFunction) FRLayout(edu.uci.ics.jung.visualization.FRLayout) GradientPickedEdgePaintFunction(org.talend.dq.indicators.graph.GradientPickedEdgePaintFunction) FontHandler(org.talend.dq.indicators.graph.FontHandler) BorderLayout(java.awt.BorderLayout) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) GraphBuilder(org.talend.dq.indicators.graph.GraphBuilder) ShapePickSupport(edu.uci.ics.jung.visualization.ShapePickSupport) DirectionDisplayPredicate(org.talend.dq.indicators.graph.DirectionDisplayPredicate) EdgeShape(edu.uci.ics.jung.graph.decorators.EdgeShape) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) EdgeWeightStrokeFunction(org.talend.dq.indicators.graph.EdgeWeightStrokeFunction) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) ConstantVertexStringer(edu.uci.ics.jung.graph.decorators.ConstantVertexStringer) VertexDisplayPredicate(org.talend.dq.indicators.graph.VertexDisplayPredicate) GraphRenderer(org.talend.dq.indicators.graph.GraphRenderer) Layout(edu.uci.ics.jung.visualization.Layout) FRLayout(edu.uci.ics.jung.visualization.FRLayout) BorderLayout(java.awt.BorderLayout) GridLayout(java.awt.GridLayout) AbstractPopupGraphMousePlugin(edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin) PluggableRenderer(edu.uci.ics.jung.visualization.PluggableRenderer) PickedState(edu.uci.ics.jung.visualization.PickedState) VertexShapeSizeAspect(org.talend.dq.indicators.graph.VertexShapeSizeAspect) VertexStrokeHighlight(org.talend.dq.indicators.graph.VertexStrokeHighlight)

Example 5 with VisualizationViewer

use of edu.uci.ics.jung.visualization.VisualizationViewer 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)

Aggregations

VisualizationViewer (edu.uci.ics.jung.visualization.VisualizationViewer)35 Dimension (java.awt.Dimension)33 DefaultModalGraphMouse (edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse)32 GraphZoomScrollPane (edu.uci.ics.jung.visualization.GraphZoomScrollPane)31 JPanel (javax.swing.JPanel)31 GridLayout (java.awt.GridLayout)29 Layout (edu.uci.ics.jung.algorithms.layout.Layout)25 Paint (java.awt.Paint)25 JSplitPane (javax.swing.JSplitPane)21 Pair (catdata.Pair)20 FRLayout (edu.uci.ics.jung.algorithms.layout.FRLayout)20 Color (java.awt.Color)18 Function (com.google.common.base.Function)17 Graph (edu.uci.ics.jung.graph.Graph)17 BasicStroke (java.awt.BasicStroke)17 CardLayout (java.awt.CardLayout)17 Stroke (java.awt.Stroke)17 List (java.util.List)17 Triple (catdata.Triple)16 DirectedSparseMultigraph (edu.uci.ics.jung.graph.DirectedSparseMultigraph)16