com.ibm.bluej.commonutil.visualization.PrintableVisualizationViewer.java Source code

Java tutorial

Introduction

Here is the source code for com.ibm.bluej.commonutil.visualization.PrintableVisualizationViewer.java

Source

/*
Copyright (c) 2012 IBM Corp.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
 * Created on Oct 26, 2006
 * 
 */
package com.ibm.bluej.commonutil.visualization;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import org.apache.commons.collections.*;

import com.ibm.bluej.commonutil.*;

import edu.uci.ics.jung.graph.*;
import edu.uci.ics.jung.graph.decorators.*;
import edu.uci.ics.jung.visualization.*;
import edu.uci.ics.jung.visualization.control.*;

public class PrintableVisualizationViewer<T extends ITree> extends VisualizationViewer {
    public TreeGraph<T> tg;
    public PluggableRenderer renderer;
    private Font font;
    private final GraphCallbacks<T> callbacks;
    Layout l;

    private class Resizer implements ComponentListener {
        public void componentHidden(ComponentEvent e) {
            // TODO Auto-generated method stub      
        }

        public void componentMoved(ComponentEvent e) {
            // TODO Auto-generated method stub         
        }

        public void componentResized(ComponentEvent e) {
            l.resize(getSize());
        }

        public void componentShown(ComponentEvent e) {
            // TODO Auto-generated method stub      
        }
    }

    private class FixedVertexFont implements VertexFontFunction {
        public Font getFont(Vertex arg0) {
            return font;
        }
    }

    public void setGraphFont(Font f) {
        font = f;
        renderer.setVertexFontFunction(new FixedVertexFont());
        this.invalidate(); //TODO: make sure this works
    }

    //replace all references to ITree with type parameter
    public static <T extends ITree> PrintableVisualizationViewer<T> create(GraphCallbacks<T> callbacks) {
        TreeGraph tg = new TreeGraph(callbacks);
        PluggableRenderer r = new PluggableRenderer();
        TreeLayout l = new TreeLayout(tg);
        final PrintableVisualizationViewer<T> vv = new PrintableVisualizationViewer<T>(l, r, tg, callbacks);

        vv.setStandardOptions();

        return vv;
    }

    protected void setStandardOptions() {
        PluggableRenderer r = (PluggableRenderer) this.getRenderer();
        r.setVertexStringer(new StringVertexStringer());
        r.setEdgeStringer(new StringEdgeStringer());
        r.setEdgeShapeFunction(new SpacedCurve());
        r.setVertexPaintFunction(new SmartVertexPaintFunction(r, callbacks));
        r.setEdgeLabelClosenessFunction(new ConstantEdgeValue(.5));
        r.setEdgePaintFunction(new SmartEdgePaintFunction(r, callbacks));
        r.setEdgeFontFunction(new FontHandler());

        this.setDoubleBuffered(true);
        DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
        this.setGraphMouse(graphMouse);
        graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
        this.getPickedState().addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getItem() instanceof LabeledDirectedSparseVertex) {
                    LabeledDirectedSparseVertex v = (LabeledDirectedSparseVertex) e.getItem();
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        callbacks.picked((T) v.marked);
                        return;
                    }
                    if (e.getStateChange() == ItemEvent.DESELECTED) {
                        callbacks.unpicked((T) v.marked);
                        return;
                    }
                    System.err.println(e.toString());
                    System.err.println(e.getItem().toString());
                } else {
                    LabeledDirectedSparseEdge edge = (LabeledDirectedSparseEdge) e.getItem();
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        callbacks.picked((T) edge.getFrom(), edge.getLabel(), (T) edge.getTo());
                        return;
                    }
                    if (e.getStateChange() == ItemEvent.DESELECTED) {
                        callbacks.unpicked((T) edge.getFrom(), edge.getLabel(), (T) edge.getTo());
                        return;
                    }
                    System.out.println("Edge pick");
                    System.out.println(e.getItem().getClass().getName());
                    System.out.println(e.getStateChange());
                }
            }
        });
        setToolTipFunction(new DefaultToolTipFunction() {
            public String getToolTipText(Vertex v) {
                return callbacks.getToolTip((T) ((LabeledDirectedSparseVertex) v).marked);
            }
            /*
            public String getToolTipText(Edge edge) {
               edu.uci.ics.jung.utils.Pair accts = edge.getEndpoints();
               Vertex v1 = (Vertex) accts.getFirst();
               Vertex v2 = (Vertex) accts.getSecond();
               return v1 + " -- " + v2;
            }
            */
        });
        Predicate p = new Predicate() {
            public boolean evaluate(Object arg0) {
                if (!(arg0 instanceof LabeledDirectedSparseEdge)) {
                    System.err.println("Unexpected: " + arg0);
                    return false;
                }
                LabeledDirectedSparseEdge edge = (LabeledDirectedSparseEdge) arg0;
                return callbacks.shouldShowEdge((T) edge.getFrom(), edge.getLabel(), (T) edge.getTo());
            }
        };
        r.setEdgeIncludePredicate(p);
        this.setPickSupport(new ShapePickSupport(4));

        //System.out.println(getPickSupport().getClass().getName());
    }

    public void update(T t) {
        this.stop();
        tg.update(t);
        this.restart();
        this.repaint();
    }

    public void update(T t, Collection<Tuple3<T, String, T>> extras) {
        this.stop();
        tg.update(t);
        tg.addExtraEdges(extras);
        this.restart();
        this.repaint();
    }

    public void update(Collection<T> ts, Collection<Tuple3<T, String, T>> extras) {
        this.stop();
        tg.update(ts);
        tg.addExtraEdges(extras);
        this.restart();
        this.repaint();
    }

    public void print(Graphics g) {
        super.paintComponent(g);
    }

    private static final long serialVersionUID = 1L;

    public PrintableVisualizationViewer(Layout arg0, Renderer arg1, TreeGraph tg, GraphCallbacks<T> callbacks) {
        super(arg0, arg1);
        this.tg = tg;
        this.l = arg0;
        this.renderer = (PluggableRenderer) arg1;
        this.callbacks = callbacks;
        addComponentListener(new Resizer());
    }

    private final static class FontHandler implements VertexFontFunction, EdgeFontFunction {
        protected boolean bold = false;
        Font f = new Font("Helvetica", Font.PLAIN, 12);
        Font b = new Font("Helvetica", Font.BOLD, 12);

        public void setBold(boolean bold) {
            this.bold = bold;
        }

        public Font getFont(Vertex v) {
            if (bold)
                return b;
            else
                return f;
        }

        public Font getFont(Edge e) {
            if (bold)
                return b;
            else
                return f;
        }
    }
}