1
2
3
4
5
6
7
8
9 package edu.uci.ics.jung.samples;
10
11 import java.awt.BorderLayout;
12 import java.awt.Color;
13 import java.awt.Container;
14 import java.awt.Dimension;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17
18 import javax.swing.BorderFactory;
19 import javax.swing.JApplet;
20 import javax.swing.JButton;
21 import javax.swing.JComboBox;
22 import javax.swing.JDialog;
23 import javax.swing.JFrame;
24 import javax.swing.JLabel;
25 import javax.swing.JPanel;
26
27 import edu.uci.ics.jung.algorithms.layout.FRLayout;
28 import edu.uci.ics.jung.graph.Graph;
29 import edu.uci.ics.jung.graph.util.TestGraphs;
30 import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
31 import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
32 import edu.uci.ics.jung.visualization.RenderContext;
33 import edu.uci.ics.jung.visualization.VisualizationModel;
34 import edu.uci.ics.jung.visualization.VisualizationViewer;
35 import edu.uci.ics.jung.visualization.VisualizationServer.Paintable;
36 import edu.uci.ics.jung.visualization.annotations.AnnotatingGraphMousePlugin;
37 import edu.uci.ics.jung.visualization.annotations.AnnotatingModalGraphMouse;
38 import edu.uci.ics.jung.visualization.annotations.AnnotationControls;
39 import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
40 import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
41 import edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode;
42 import edu.uci.ics.jung.visualization.control.ScalingControl;
43 import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
44 import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
45 import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
46 import edu.uci.ics.jung.visualization.renderers.Renderer;
47
48
49
50
51
52
53
54 @SuppressWarnings("serial")
55 public class AnnotationsDemo<V, E> extends JApplet {
56
57 static final String instructions =
58 "<html>"+
59 "<b><h2><center>Instructions for Annotations</center></h2></b>"+
60 "<p>The Annotation Controls allow you to select:"+
61 "<ul>"+
62 "<li>Shape"+
63 "<li>Color"+
64 "<li>Fill (or outline)"+
65 "<li>Above or below (UPPER/LOWER) the graph display"+
66 "</ul>"+
67 "<p>Mouse Button one press starts a Shape,"+
68 "<p>drag and release to complete."+
69 "<p>Mouse Button three pops up an input dialog"+
70 "<p>for text. This will create a text annotation."+
71 "<p>You may use html for multi-line, etc."+
72 "<p>You may even use an image tag and image url"+
73 "<p>to put an image in the annotation."+
74 "<p><p>"+
75 "<p>To remove an annotation, shift-click on it"+
76 "<p>in the Annotations mode."+
77 "<p>If there is overlap, the Annotation with center"+
78 "<p>closest to the mouse point will be removed.";
79
80 JDialog helpDialog;
81
82 Paintable viewGrid;
83
84
85
86
87
88
89 public AnnotationsDemo() {
90
91
92 Graph<String, Number> graph = TestGraphs.getOneComponentGraph();
93
94
95 Dimension preferredSize1 = new Dimension(600,600);
96
97
98 FRLayout<String,Number> layout = new FRLayout<String,Number>(graph);
99 layout.setMaxIterations(500);
100
101 VisualizationModel<String,Number> vm =
102 new DefaultVisualizationModel<String,Number>(layout, preferredSize1);
103
104
105 final VisualizationViewer<String,Number> vv =
106 new VisualizationViewer<String,Number>(vm, preferredSize1);
107 vv.setBackground(Color.white);
108 vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.cyan));
109 vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(vv.getPickedVertexState(), Color.red, Color.yellow));
110 vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
111 vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
112
113
114 vv.setVertexToolTipTransformer(new ToStringLabeller());
115
116 Container content = getContentPane();
117 Container panel = new JPanel(new BorderLayout());
118
119 GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);
120 panel.add(gzsp);
121
122 helpDialog = new JDialog();
123 helpDialog.getContentPane().add(new JLabel(instructions));
124
125 RenderContext<String,Number> rc = vv.getRenderContext();
126 AnnotatingGraphMousePlugin<String,Number> annotatingPlugin =
127 new AnnotatingGraphMousePlugin<String,Number>(rc);
128
129
130 final AnnotatingModalGraphMouse<String,Number> graphMouse =
131 new AnnotatingModalGraphMouse<String,Number>(rc, annotatingPlugin);
132 vv.setGraphMouse(graphMouse);
133 vv.addKeyListener(graphMouse.getModeKeyListener());
134
135 final ScalingControl scaler = new CrossoverScalingControl();
136
137 JButton plus = new JButton("+");
138 plus.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
140 scaler.scale(vv, 1.1f, vv.getCenter());
141 }
142 });
143 JButton minus = new JButton("-");
144 minus.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent e) {
146 scaler.scale(vv, 1/1.1f, vv.getCenter());
147 }
148 });
149
150 JComboBox<Mode> modeBox = graphMouse.getModeComboBox();
151 modeBox.setSelectedItem(ModalGraphMouse.Mode.ANNOTATING);
152
153 JButton help = new JButton("Help");
154 help.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent e) {
156 helpDialog.pack();
157 helpDialog.setVisible(true);
158 }
159 });
160
161 JPanel controls = new JPanel();
162 JPanel zoomControls = new JPanel();
163 zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom"));
164 zoomControls.add(plus);
165 zoomControls.add(minus);
166 controls.add(zoomControls);
167
168 JPanel modeControls = new JPanel();
169 modeControls.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
170 modeControls.add(graphMouse.getModeComboBox());
171 controls.add(modeControls);
172
173 JPanel annotationControlPanel = new JPanel();
174 annotationControlPanel.setBorder(BorderFactory.createTitledBorder("Annotation Controls"));
175
176 AnnotationControls<String,Number> annotationControls =
177 new AnnotationControls<String,Number>(annotatingPlugin);
178
179 annotationControlPanel.add(annotationControls.getAnnotationsToolBar());
180 controls.add(annotationControlPanel);
181
182 JPanel helpControls = new JPanel();
183 helpControls.setBorder(BorderFactory.createTitledBorder("Help"));
184 helpControls.add(help);
185 controls.add(helpControls);
186 content.add(panel);
187 content.add(controls, BorderLayout.SOUTH);
188 }
189
190 public static void main(String[] args) {
191 JFrame f = new JFrame();
192 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
193 f.getContentPane().add(new AnnotationsDemo<String,Number>());
194 f.pack();
195 f.setVisible(true);
196 }
197 }