View Javadoc

1   /*
2    * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
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.ScalingControl;
42  import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
43  import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
44  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
45  import edu.uci.ics.jung.visualization.renderers.Renderer;
46  
47  /**
48   * Demonstrates annotation of graph elements.
49   * 
50   * @author Tom Nelson
51   * 
52   */
53  @SuppressWarnings("serial")
54  public class AnnotationsDemo<V, E> extends JApplet {
55  
56      static final String instructions = 
57          "<html>"+
58          "<b><h2><center>Instructions for Annotations</center></h2></b>"+
59          "<p>The Annotation Controls allow you to select:"+
60          "<ul>"+
61          "<li>Shape"+
62          "<li>Color"+
63          "<li>Fill (or outline)"+
64          "<li>Above or below (UPPER/LOWER) the graph display"+
65          "</ul>"+
66          "<p>Mouse Button one press starts a Shape,"+
67          "<p>drag and release to complete."+
68          "<p>Mouse Button three pops up an input dialog"+
69          "<p>for text. This will create a text annotation."+
70          "<p>You may use html for multi-line, etc."+
71          "<p>You may even use an image tag and image url"+
72          "<p>to put an image in the annotation."+
73          "<p><p>"+
74          "<p>To remove an annotation, shift-click on it"+
75          "<p>in the Annotations mode."+
76          "<p>If there is overlap, the Annotation with center"+
77          "<p>closest to the mouse point will be removed.";
78      
79      JDialog helpDialog;
80      
81      Paintable viewGrid;
82      
83      /**
84       * create an instance of a simple graph in two views with controls to
85       * demo the features.
86       * 
87       */
88      public AnnotationsDemo() {
89          
90          // create a simple graph for the demo
91          Graph<String, Number> graph = TestGraphs.getOneComponentGraph();
92          
93          // the preferred sizes for the two views
94          Dimension preferredSize1 = new Dimension(600,600);
95          
96          // create one layout for the graph
97          FRLayout<String,Number> layout = new FRLayout<String,Number>(graph);
98          layout.setMaxIterations(500);
99          
100         VisualizationModel<String,Number> vm =
101             new DefaultVisualizationModel<String,Number>(layout, preferredSize1);
102         
103         // create 2 views that share the same model
104         final VisualizationViewer<String,Number> vv = 
105             new VisualizationViewer<String,Number>(vm, preferredSize1);
106         vv.setBackground(Color.white);
107         vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.cyan));
108         vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<String>(vv.getPickedVertexState(), Color.red, Color.yellow));
109         vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
110         vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
111         
112         // add default listener for ToolTips
113         vv.setVertexToolTipTransformer(new ToStringLabeller<String>());
114         
115 //        ToolTipManager.sharedInstance().setDismissDelay(10000);
116         
117         Container content = getContentPane();
118         Container panel = new JPanel(new BorderLayout());
119         
120         GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);
121         panel.add(gzsp);
122         
123         helpDialog = new JDialog();
124         helpDialog.getContentPane().add(new JLabel(instructions));
125         
126         RenderContext<String,Number> rc = vv.getRenderContext();
127         AnnotatingGraphMousePlugin<String,Number> annotatingPlugin =
128         	new AnnotatingGraphMousePlugin<String,Number>(rc);
129         // create a GraphMouse for the main view
130         // 
131         final AnnotatingModalGraphMouse<String,Number> graphMouse = 
132         	new AnnotatingModalGraphMouse<String,Number>(rc, annotatingPlugin);
133         vv.setGraphMouse(graphMouse);
134         vv.addKeyListener(graphMouse.getModeKeyListener());
135         
136         final ScalingControl scaler = new CrossoverScalingControl();
137 
138         JButton plus = new JButton("+");
139         plus.addActionListener(new ActionListener() {
140             public void actionPerformed(ActionEvent e) {
141                 scaler.scale(vv, 1.1f, vv.getCenter());
142             }
143         });
144         JButton minus = new JButton("-");
145         minus.addActionListener(new ActionListener() {
146             public void actionPerformed(ActionEvent e) {
147                 scaler.scale(vv, 1/1.1f, vv.getCenter());
148             }
149         });
150         
151         JComboBox modeBox = graphMouse.getModeComboBox();
152         modeBox.setSelectedItem(ModalGraphMouse.Mode.ANNOTATING);
153         
154         JButton help = new JButton("Help");
155         help.addActionListener(new ActionListener() {
156             public void actionPerformed(ActionEvent e) {
157                 helpDialog.pack();
158                 helpDialog.setVisible(true);
159             }
160         });
161 
162         JPanel controls = new JPanel();
163         JPanel zoomControls = new JPanel();
164         zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom"));
165         zoomControls.add(plus);
166         zoomControls.add(minus);
167         controls.add(zoomControls);
168         
169         JPanel modeControls = new JPanel();
170         modeControls.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
171         modeControls.add(graphMouse.getModeComboBox());
172         controls.add(modeControls);
173         
174         JPanel annotationControlPanel = new JPanel();
175         annotationControlPanel.setBorder(BorderFactory.createTitledBorder("Annotation Controls"));
176         
177         AnnotationControls<String,Number> annotationControls = 
178             new AnnotationControls<String,Number>(annotatingPlugin);
179         
180         annotationControlPanel.add(annotationControls.getAnnotationsToolBar());
181         controls.add(annotationControlPanel);
182         
183         JPanel helpControls = new JPanel();
184         helpControls.setBorder(BorderFactory.createTitledBorder("Help"));
185         helpControls.add(help);
186         controls.add(helpControls);
187         content.add(panel);
188         content.add(controls, BorderLayout.SOUTH);
189     }
190     
191     /**
192      * a driver for this demo
193      */
194     public static void main(String[] args) {
195         JFrame f = new JFrame();
196         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
197         f.getContentPane().add(new AnnotationsDemo<String,Number>());
198         f.pack();
199         f.setVisible(true);
200     }
201 }