001
002 package nl.cwi.sen1.visplugin.graphplugin;
003
004 import java.awt.event.MouseEvent;
005
006 import nl.cwi.sen1.gui.plugin.prefusedot.DotEdgeRenderer;
007 import nl.cwi.sen1.gui.plugin.prefusedot.DotLabelLayout;
008 import nl.cwi.sen1.gui.plugin.prefusedot.DotNodeLayout;
009 import nl.cwi.sen1.gui.plugin.prefusedot.DotNodeRenderer;
010 import prefuse.Constants;
011 import prefuse.Display;
012 import prefuse.Visualization;
013 import prefuse.action.ActionList;
014 import prefuse.action.RepaintAction;
015 import prefuse.action.assignment.ColorAction;
016 import prefuse.action.assignment.FontAction;
017 import prefuse.action.layout.Layout;
018 import prefuse.controls.DragControl;
019 import prefuse.controls.FocusControl;
020 import prefuse.controls.PanControl;
021 import prefuse.controls.WheelZoomControl;
022 import prefuse.controls.ZoomControl;
023 import prefuse.data.Graph;
024 import prefuse.data.Schema;
025 import prefuse.render.AbstractShapeRenderer;
026 import prefuse.render.DefaultRendererFactory;
027 import prefuse.render.EdgeRenderer;
028 import prefuse.render.LabelRenderer;
029 import prefuse.util.ColorLib;
030 import prefuse.util.FontLib;
031 import prefuse.util.PrefuseLib;
032 import prefuse.visual.VisualItem;
033 import prefuse.visual.expression.InGroupPredicate;
034
035 /**
036 * Builder to create a display (formatting, colors and layout) for a graph.
037 *
038 * @author A. Belgraver
039 * @author Anton Gerdessen (reviewer)
040 * @author Jurgen Vinju
041 * @date 07-3-2007
042 */
043 public class GraphDisplayBuilder {
044 private DotNodeLayout dotLayout;
045
046 private static final Schema LABEL_SCHEMA = PrefuseLib.getVisualItemSchema();
047 {
048 LABEL_SCHEMA.setDefault(VisualItem.INTERACTIVE, false);
049 LABEL_SCHEMA.setDefault(VisualItem.TEXTCOLOR, ColorLib.gray(100));
050 LABEL_SCHEMA.setDefault(VisualItem.FONT, FontLib.getFont("Tahoma", 16));
051 }
052
053 /**
054 * Main method for the graph creation.
055 *
056 * @param graph the graph to be displayed.
057 * @return
058 * @author A. Belgraver
059 * @author Anton Gerdessen (reviewer)
060 * @date 07-3-2007
061 */
062 public Display createDisplayFromGraph(Graph graph){
063
064 // Setup the visualization type and renderer
065 Visualization vis = new Visualization();
066 vis.addGraph(GraphConstants.GRAPH, graph);
067 vis.addDecorators(GraphConstants.LABELS, GraphConstants.NODES);
068
069 DefaultRendererFactory drf = new DefaultRendererFactory();
070
071 LabelRenderer lr = new LabelRenderer(GraphConstants.LABEL);
072 lr.setHorizontalTextAlignment(Constants.CENTER);
073 lr.setVerticalTextAlignment(Constants.CENTER);
074 lr.setRenderType(LabelRenderer.RENDER_TYPE_NONE);
075 drf.add(new InGroupPredicate(GraphConstants.LABELS), lr);
076
077 DotNodeRenderer nr = new DotNodeRenderer();
078 nr.setRenderType(AbstractShapeRenderer.RENDER_TYPE_DRAW_AND_FILL);
079 drf.add(new InGroupPredicate(GraphConstants.NODES), nr);
080
081 EdgeRenderer er = new DotEdgeRenderer(Constants.EDGE_TYPE_LINE, Constants.EDGE_ARROW_FORWARD);
082 drf.add(new InGroupPredicate(GraphConstants.EDGES), er);
083
084 vis.setRendererFactory(drf);
085
086 // Create an actions avaible for the graph.
087 ActionList layout = new ActionList();
088 dotLayout = new DotNodeLayout(GraphConstants.GRAPH);
089 layout.add(dotLayout);
090 Layout labelLayout = new DotLabelLayout(GraphConstants.LABELS);
091 layout.add(labelLayout);
092 layout.add(new RepaintAction());
093 vis.putAction(GraphConstants.LAYOUT, layout);
094 vis.putAction(GraphConstants.LABELLAYOUT, labelLayout);
095
096 vis.setInteractive(GraphConstants.LABELS, null, false);
097
098 // Add the actions available for this graph.
099 ActionList colors = createColorActions();
100 vis.putAction(GraphConstants.COLOR, colors);
101
102 // Setup the display in which to display the graph
103 Display display = createDisplay(vis);
104
105 // Start the graph.
106 vis.run(GraphConstants.COLOR);
107 vis.run(GraphConstants.LAYOUT);
108
109 return display;
110 }
111
112 /**
113 * Set the color for the nodes, if no color is set they will be black and white.
114 *
115 * @return ActionList holding needed color actions.
116 * @author A. Belgraver
117 * @author Anton Gerdessen (reviewer)
118 * @date 07-3-2007
119 */
120 private ActionList createColorActions() {
121 // Text options.
122 ColorAction text = new ColorAction(GraphConstants.LABELS, VisualItem.TEXTCOLOR,
123 ColorLib.gray(0));
124 text.setDefaultColor(GraphConstants.TEXTCOLOR);
125 FontAction nFont = new FontAction(GraphConstants.LABELS, GraphConstants.NODE_FONT);
126
127 // Outer line color.
128 ColorAction nStroke = new ColorAction(GraphConstants.NODES,
129 VisualItem.STROKECOLOR);
130 nStroke.setDefaultColor(GraphConstants.NODE_LINECOLOR);
131
132 // Fill color.
133 ColorAction nFill = new ColorAction(GraphConstants.NODES, VisualItem.FILLCOLOR);
134 nFill.setDefaultColor(GraphConstants.NODE_FILLCOLOR);
135
136 // Edge lines and heads.
137 ColorAction nEdges = new ColorAction(GraphConstants.EDGES, VisualItem.STROKECOLOR);
138 nEdges.setDefaultColor(GraphConstants.ARROWCOLOR);
139 ColorAction nHeads = new ColorAction(GraphConstants.EDGES, VisualItem.FILLCOLOR);
140 nHeads.setDefaultColor(GraphConstants.ARROWCOLOR);
141
142 // Bundle the color actions in an actionlist.
143 ActionList colors = new ActionList();
144 colors.add(nFont);
145 colors.add(nStroke);
146 colors.add(nFill);
147 colors.add(nEdges);
148 colors.add(nHeads);
149 colors.add(text);
150 colors.add(new RepaintAction());
151
152 return colors;
153 }
154
155 /**
156 * Create and configure the visualization display.
157 *
158 * @param vis The visualisation to display
159 * @return A configured display object
160 * @author A. Belgraver
161 * @author Anton Gerdessen (reviewer)
162 * @date 07-3-2007
163 */
164 private Display createDisplay(Visualization vis) {
165 Display d = new Display(vis);
166
167 // Anti aliasing on
168 d.setHighQuality(true);
169
170 // Allow manipulation of those nodes while visible in the panel
171 d.addControlListener(new FocusControl());
172 d.addControlListener(new DragControl() {
173 public void itemDragged(VisualItem item, MouseEvent e) {
174 super.itemDragged(item, e);
175 // This is a bit sluggish, because it relayouts all labels instead of just
176 // the one that has moved
177 item.getVisualization().run(GraphConstants.LABELLAYOUT);
178 }
179 });
180 d.addControlListener(new PanControl());
181
182 // Allowing zooming in the panel.
183 d.addControlListener(new ZoomControl());
184 d.addControlListener(new WheelZoomControl());
185
186 return d;
187 }
188 }