View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors
3    * All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    *
8    * 
9    */
10  package edu.uci.ics.jung.visualization.annotations;
11  
12  import java.awt.Color;
13  import java.awt.Component;
14  import java.awt.Shape;
15  import java.awt.event.ActionEvent;
16  import java.awt.event.ActionListener;
17  import java.awt.event.ItemEvent;
18  import java.awt.event.ItemListener;
19  import java.awt.geom.Ellipse2D;
20  import java.awt.geom.Rectangle2D;
21  import java.awt.geom.RectangularShape;
22  import java.awt.geom.RoundRectangle2D;
23  
24  import javax.swing.DefaultListCellRenderer;
25  import javax.swing.JButton;
26  import javax.swing.JColorChooser;
27  import javax.swing.JComboBox;
28  import javax.swing.JList;
29  import javax.swing.JToggleButton;
30  import javax.swing.JToolBar;
31  
32  /**
33   * a collection of controls for annotations.
34   * allows selection of colors, shapes, etc
35   * @author Tom Nelson - tomnelson@dev.java.net
36   *
37   */
38  public class AnnotationControls<V,E> {
39  	
40  	protected AnnotatingGraphMousePlugin<V,E> annotatingPlugin;
41  
42  	public AnnotationControls(AnnotatingGraphMousePlugin<V,E> annotatingPlugin) {
43  		this.annotatingPlugin = annotatingPlugin;
44  	}
45  	
46      @SuppressWarnings("serial")
47      public JComboBox<Shape> getShapeBox() {
48      	JComboBox<Shape> shapeBox = new JComboBox<Shape>(
49      			new Shape[] {
50      					new Rectangle2D.Double(),
51      					new RoundRectangle2D.Double(0,0,0,0,50,50),
52      					new Ellipse2D.Double()
53      			});
54      	shapeBox.setRenderer(new DefaultListCellRenderer() {
55      		@Override
56              public Component getListCellRendererComponent(JList<?> list, Object value,
57      			int index, boolean isSelected, boolean hasFocus) {
58      			String valueString = value.toString();
59      			valueString = valueString.substring(0,valueString.indexOf("2D"));
60      			valueString = valueString.substring(valueString.lastIndexOf('.')+1);
61      			return super.getListCellRendererComponent(list, valueString, index,
62      					isSelected, hasFocus);
63      		}
64      	});
65      	shapeBox.addItemListener(new ItemListener() {
66  
67  			public void itemStateChanged(ItemEvent e) {
68  				if(e.getStateChange() == ItemEvent.SELECTED) {
69  					annotatingPlugin.setRectangularShape((RectangularShape)e.getItem());
70  				}
71  				
72  			}});
73      	return shapeBox;
74      }
75      
76      public JButton getColorChooserButton() {
77      	final JButton colorChooser = new JButton("Color");
78      	colorChooser.setForeground(annotatingPlugin.getAnnotationColor());
79      	colorChooser.addActionListener(new ActionListener() {
80  
81  			public void actionPerformed(ActionEvent e) {
82  				Color color = JColorChooser.showDialog(colorChooser, "Annotation Color", 
83  						colorChooser.getForeground());
84  				annotatingPlugin.setAnnotationColor(color);
85  				colorChooser.setForeground(color);
86  			}});
87      	return colorChooser;
88      }
89      
90      public JComboBox<Annotation.Layer> getLayerBox() {
91      	final JComboBox<Annotation.Layer> layerBox = new JComboBox<Annotation.Layer>(
92      			new Annotation.Layer[] {
93      			Annotation.Layer.LOWER, Annotation.Layer.UPPER
94      			});
95      	layerBox.addItemListener(new ItemListener() {
96  
97  			public void itemStateChanged(ItemEvent e) {
98  				if(e.getStateChange() == ItemEvent.SELECTED) {
99  					annotatingPlugin.setLayer((Annotation.Layer)e.getItem());
100 				}
101 				
102 			}});
103 
104     	return layerBox;
105     }
106 
107     public JToggleButton getFillButton() {
108     	JToggleButton fillButton = new JToggleButton("Fill");
109     	fillButton.addItemListener(new ItemListener() {
110 
111 			public void itemStateChanged(ItemEvent e) {
112 				annotatingPlugin.setFill(e.getStateChange() == ItemEvent.SELECTED);
113 				
114 			}});
115     	return fillButton;
116     }
117     
118     public JToolBar getAnnotationsToolBar() {
119     	JToolBar toolBar = new JToolBar();
120     	toolBar.add(this.getShapeBox());
121     	toolBar.add(this.getColorChooserButton());
122     	toolBar.add(this.getFillButton());
123     	toolBar.add(this.getLayerBox());
124     	return toolBar;
125     	
126     }
127 
128 	
129 
130 }