import prefuse.action.ActionList; import prefuse.action.RepaintAction; import prefuse.action.assignment.ColorAction; import prefuse.action.layout.graph.*; import prefuse.controls.*; import prefuse.controls.PanControl; import prefuse.controls.FocusControl; import prefuse.controls.WheelZoomControl; import prefuse.controls.ZoomToFitControl; import prefuse.Constants; import prefuse.Display; import prefuse.data.Graph; import prefuse.data.*; import prefuse.data.io.TreeMLReader; import prefuse.render.DefaultRendererFactory; import prefuse.render.LabelRenderer; import prefuse.util.ColorLib; import prefuse.Visualization; import prefuse.visual.VisualItem; import prefuse.action.layout.graph.BalloonTreeLayout; import javax.swing.JFrame; import java.awt.event.ActionListener; import prefuse.action.filter.FisheyeTreeFilter; import prefuse.action.layout.CollapsedSubtreeLayout; import javax.swing.JSplitPane; import prefuse.activity.Activity; import java.awt.geom.Rectangle2D; import java.awt.Rectangle; public class TreeDemo4 { //Declare our variables public static final String ITEMS = "items"; public static final String NAME = "name"; public static final String ACTION = "action"; //Create a table schmea which contains the name (from the //TreeML file) and an actionlistener associated with each //entry protected static final Schema ITEM_SCHEMA = new Schema(); static{ ITEM_SCHEMA.addColumn(NAME, String.class); ITEM_SCHEMA.addColumn(ACTION, ActionListener.class); } private static Table items= ITEM_SCHEMA.instantiate(); public static final Rectangle2D bounds = new Rectangle(300, 300); public static Display treeBuilder(String visType){ //Create the graph object outside of the try loop //otherwise we can't compile Graph myTree = null; try { //Read the TreeML formatted document samplebackup.xml myTree = new TreeMLReader().readGraph("/samplebackup.xml"); } catch(Exception e){ //If that doesn't work, die and print out a stack trace e.printStackTrace(); System.out.println("Died trying to open TreeML File"); System.exit(1); } //Build the Visualization Object Visualization MyViz = new Visualization(); MyViz.add("graph", myTree); MyViz.setInteractive("graph.edges", null, false); MyViz.addTable(ITEMS, items); //Build the Label Renderer Object LabelRenderer MyLabel = new LabelRenderer("name"); MyLabel.setRoundedCorner(4,4); MyViz.setRendererFactory(new DefaultRendererFactory(MyLabel)); //Define the Color Scheme //Nodes are white ColorAction nodeFill = new ColorAction("graph.nodes", VisualItem.FILLCOLOR, ColorLib.rgb(255,255,255)); //Text is Black ColorAction textColor = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.rgb(0,0,0)); //Edges (connections) are black ColorAction edgeColor = new ColorAction("graph.edges", VisualItem.STROKECOLOR, ColorLib.rgb(0,0,0)); //Build the Coloring Actionlist ActionList ColorIt = new ActionList(Activity.INFINITY); ColorIt.add(nodeFill); ColorIt.add(textColor); ColorIt.add(edgeColor); //Create a null ForceActionList ActionList ForceAction = new ActionList(Activity.INFINITY); //Build the Layout Actionlist. Use the //Fisheye Tree Filter with a boundary of 1 ActionList DoLayout = new ActionList(); DoLayout.add(new FisheyeTreeFilter("graph", 1)); //What kind of tree are we using? if (visType == "Tree"){ NodeLinkTreeLayout treeLayout = new NodeLinkTreeLayout("graph", Constants.ORIENT_BOTTOM_TOP, 50, 0, 8); MyViz.putAction("treeLayout", treeLayout); DoLayout.add(treeLayout); DoLayout.add(new RepaintAction()); } else if (visType == "Radial"){ RadialTreeLayout treeLayout = new RadialTreeLayout("graph"); MyViz.putAction("treeLayout", treeLayout); DoLayout.add(treeLayout); DoLayout.add(new RepaintAction()); } //Haven't been able to get either radial or balloon visualizers //working.. More coming on that soon else if (visType == "Balloon"){ BalloonTreeLayout treeLayout = new BalloonTreeLayout("graph", 20); //ForceAction.add(new CircleLayout("graph")); //ForceAction.add(new GridLayout("graph")); DoLayout.add(treeLayout); ForceAction.add(new ForceDirectedLayout("graph")); ForceAction.add(new RepaintAction()); } else if(visType == "Square"){ SquarifiedTreeMapLayout treeLayout = new SquarifiedTreeMapLayout("graph"); treeLayout.setFrameWidth(100); treeLayout.setLayoutBounds(bounds); DoLayout.add(treeLayout); DoLayout.add(new RepaintAction()); //ForceAction.add(new ForceDirectedLayout("graph", true)); //ForceAction.add(new ForceDirectedLayout("graph")); ForceAction.add(new RepaintAction()); } //Configure Collapsed Subtree Layout with left to right orientation CollapsedSubtreeLayout subLayout = new CollapsedSubtreeLayout("graph", Constants.ORIENT_LEFT_RIGHT); //Add collapsed subtree layout to the layout actionlist DoLayout.add(subLayout); //Add a repaint to the end of the layout action list DoLayout.add(new RepaintAction()); //Register Layout and Coloring Actionlists //With the Visualization Object MyViz.putAction("DoLayout", DoLayout); MyViz.putAction("ColorIt", ColorIt); MyViz.putAction("ForceAction", ForceAction); //Create a display object Display MyDisplay = new Display(MyViz); MyDisplay.setSize(300, 300); //Add Controller Objects to the Visualization MyDisplay.addControlListener(new PanControl()); MyDisplay.addControlListener(new ZoomControl()); MyDisplay.addControlListener(new DragControl()); MyDisplay.addControlListener(new WheelZoomControl()); MyDisplay.addControlListener(new ZoomToFitControl("graph", Control.RIGHT_MOUSE_BUTTON)); MyDisplay.addControlListener(new FocusControl(1,"DoLayout")); //Run the actionlists and return the Display object MyViz.run("ColorIt"); MyViz.run("DoLayout"); MyViz.run("ForceAction"); return MyDisplay; } public static void main(String args[]){ //Build the JSplitpane objects to hold different visualization displays JSplitPane aPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treeBuilder("Balloon"), treeBuilder("Square")); JSplitPane bPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treeBuilder("Tree"), treeBuilder("Radial")); //JSplitPane to put it all together JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, aPane, bPane); //Build the JFrame to hold the JSplitpane JFrame MyFrame = new JFrame("Tree Display Options"); MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyFrame.add(splitPane); MyFrame.pack(); MyFrame.setVisible(true); } }