annotate java/src/uk/ac/qmul/eecs/ccmi/gui/GraphToolbar.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
f@0 5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 6
f@0 7 This program is free software: you can redistribute it and/or modify
f@0 8 it under the terms of the GNU General Public License as published by
f@0 9 the Free Software Foundation, either version 3 of the License, or
f@0 10 (at your option) any later version.
f@0 11
f@0 12 This program is distributed in the hope that it will be useful,
f@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 15 GNU General Public License for more details.
f@0 16
f@0 17 You should have received a copy of the GNU General Public License
f@0 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 19 */
f@0 20
f@0 21 package uk.ac.qmul.eecs.ccmi.gui;
f@0 22
f@0 23 import java.awt.Color;
f@0 24 import java.awt.Component;
f@0 25 import java.awt.Graphics;
f@0 26 import java.awt.Graphics2D;
f@0 27 import java.awt.event.ActionEvent;
f@0 28 import java.awt.event.ActionListener;
f@0 29 import java.awt.geom.AffineTransform;
f@0 30 import java.awt.geom.Line2D;
f@0 31 import java.awt.geom.Point2D;
f@0 32 import java.awt.geom.Rectangle2D;
f@0 33 import java.util.Enumeration;
f@0 34 import java.util.ResourceBundle;
f@0 35
f@0 36 import javax.swing.ButtonGroup;
f@0 37 import javax.swing.Icon;
f@0 38 import javax.swing.JButton;
f@0 39 import javax.swing.JToggleButton;
f@0 40 import javax.swing.JToolBar;
f@0 41
f@0 42
f@0 43 /**
f@0 44 A Toolbar that contains node and edge prototype icons. By using the toolbar
f@0 45 the user can create nodes and edges in the diagram out of clonation from the prototype.
f@0 46 */
f@0 47 @SuppressWarnings("serial")
f@0 48 public class GraphToolbar extends JToolBar {
f@0 49 /**
f@0 50 * Constructs a tool bar with no icons.
f@0 51 *
f@0 52 * @param diagram the diagram this toolbar is related to
f@0 53 */
f@0 54 public GraphToolbar(Diagram diagram){
f@0 55 /* creates icon for select button */
f@0 56 Icon icon = new Icon(){
f@0 57 public int getIconHeight() { return BUTTON_SIZE; }
f@0 58 public int getIconWidth() { return BUTTON_SIZE; }
f@0 59 public void paintIcon(Component c, Graphics g,
f@0 60 int x, int y){
f@0 61 Graphics2D g2 = (Graphics2D)g;
f@0 62 GraphPanel.drawGrabber(g2, x + OFFSET, y + OFFSET);
f@0 63 GraphPanel.drawGrabber(g2, x + OFFSET, y + BUTTON_SIZE - OFFSET);
f@0 64 GraphPanel.drawGrabber(g2, x + BUTTON_SIZE - OFFSET, y + OFFSET);
f@0 65 GraphPanel.drawGrabber(g2, x + BUTTON_SIZE - OFFSET, y + BUTTON_SIZE - OFFSET);
f@0 66 }
f@0 67 };
f@0 68 /* add selection button */
f@0 69 ResourceBundle resources =
f@0 70 ResourceBundle.getBundle(EditorFrame.class.getName());
f@0 71 String text = resources.getString("grabber.text");
f@0 72 selectButton = new NodeButton(null,icon);
f@0 73 selectButton.setToolTipText(text);
f@0 74 nodeButtonsGroup = new ButtonGroup();
f@0 75 nodeButtonsGroup.add(selectButton);
f@0 76 add(selectButton);
f@0 77
f@0 78 /* add diagram buttons to the toolbar */
f@0 79 Node[] nodeTypes = diagram.getNodePrototypes();
f@0 80 for (int i = 0; i < nodeTypes.length; i++){
f@0 81 text = nodeTypes[i].getType();
f@0 82 add(nodeTypes[i], text );
f@0 83 }
f@0 84
f@0 85 /* select the select-button as default */
f@0 86 nodeButtonsGroup.setSelected(selectButton.getModel(), true);
f@0 87
f@0 88 /* separate node buttons from edge buttons */
f@0 89 addSeparator();
f@0 90
f@0 91 /* add diagram edges to the toolbar */
f@0 92 Edge[] edgeTypes = diagram.getEdgePrototypes();
f@0 93 for (int i = 0; i < edgeTypes.length; i++){
f@0 94 text = edgeTypes[i].getType();
f@0 95 add(edgeTypes[i], text );
f@0 96 }
f@0 97 }
f@0 98
f@0 99 /**
f@0 100 Gets the node prototype that is associated with
f@0 101 the currently selected button
f@0 102 @return a {@code Node} prototype
f@0 103 */
f@0 104 public Node getSelectedTool() {
f@0 105 @SuppressWarnings("rawtypes")
f@0 106 Enumeration elements = nodeButtonsGroup.getElements();
f@0 107 while (elements.hasMoreElements()) {
f@0 108 NodeButton b = (NodeButton)elements.nextElement();
f@0 109 if (b.isSelected()) {
f@0 110 /* switch back to the select-button */
f@0 111 nodeButtonsGroup.setSelected(selectButton.getModel(), true);
f@0 112 return b.getNode();
f@0 113 }
f@0 114 }
f@0 115 /* getting here means the selection button is selected */
f@0 116 return null;
f@0 117 }
f@0 118
f@0 119 /**
f@0 120 Adds a node to the tool bar.
f@0 121 @param n the node to add
f@0 122 @param tip the tool tip appearing when hovering on this edge button
f@0 123 */
f@0 124 public void add(final Node n, String tip){
f@0 125 Icon icon = new Icon(){
f@0 126 public int getIconHeight() { return BUTTON_SIZE; }
f@0 127 public int getIconWidth() { return BUTTON_SIZE; }
f@0 128 public void paintIcon(Component c, Graphics g,
f@0 129 int x, int y){
f@0 130 double width = n.getBounds().getWidth();
f@0 131 double height = n.getBounds().getHeight();
f@0 132 Graphics2D g2 = (Graphics2D)g;
f@0 133 double scaleX = (BUTTON_SIZE - OFFSET)/ width;
f@0 134 double scaleY = (BUTTON_SIZE - OFFSET)/ height;
f@0 135 double scale = Math.min(scaleX, scaleY);
f@0 136
f@0 137 AffineTransform oldTransform = g2.getTransform();
f@0 138 g2.translate(x, y);
f@0 139 g2.translate(OFFSET/2*scaleX,OFFSET/2*scaleY);
f@0 140 g2.scale(scale, scale);
f@0 141 g2.setColor(Color.black);
f@0 142 n.draw(g2);
f@0 143 g2.setTransform(oldTransform);
f@0 144 }
f@0 145 };
f@0 146
f@0 147 NodeButton button = new NodeButton(n, icon);
f@0 148 button.setToolTipText(tip);
f@0 149
f@0 150 add(button);
f@0 151 nodeButtonsGroup.add(button);
f@0 152 }
f@0 153
f@0 154 /**
f@0 155 Adds an edge to the tool bar.
f@0 156 @param e the edge to add
f@0 157 @param tip the tool tip appearing when hovering on this edge button
f@0 158 */
f@0 159 public void add(final Edge e, String tip){
f@0 160 Icon icon = new Icon(){
f@0 161 public int getIconHeight() { return BUTTON_SIZE; }
f@0 162 public int getIconWidth() { return BUTTON_SIZE; }
f@0 163 public void paintIcon(Component c, Graphics g,
f@0 164 int x, int y){
f@0 165 Graphics2D g2 = (Graphics2D)g;
f@0 166 /* create two points */
f@0 167 Point2D p = new Point2D.Double();
f@0 168 Point2D q = new Point2D.Double();
f@0 169 p.setLocation(OFFSET, OFFSET);
f@0 170 q.setLocation(BUTTON_SIZE - OFFSET, BUTTON_SIZE - OFFSET);
f@0 171
f@0 172 Line2D line = new Line2D.Double(p,q);
f@0 173 Rectangle2D bounds = new Rectangle2D.Double();
f@0 174 bounds.add(line.getBounds2D());
f@0 175
f@0 176 double width = bounds.getWidth();
f@0 177 double height = bounds.getHeight();
f@0 178 double scaleX = (BUTTON_SIZE - OFFSET)/ width;
f@0 179 double scaleY = (BUTTON_SIZE - OFFSET)/ height;
f@0 180 double scale = Math.min(scaleX, scaleY);
f@0 181
f@0 182 AffineTransform oldTransform = g2.getTransform();
f@0 183 g2.translate(x, y);
f@0 184 g2.scale(scale, scale);
f@0 185 g2.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0));
f@0 186
f@0 187 g2.setColor(Color.black);
f@0 188 g2.setStroke(e.getStyle().getStroke());
f@0 189 g2.draw(line);
f@0 190 g2.setTransform(oldTransform);
f@0 191 }
f@0 192 };
f@0 193 final JButton button = new JButton(icon);
f@0 194 button.setToolTipText(tip);
f@0 195 button.setFocusable(false);
f@0 196
f@0 197 button.addActionListener(new ActionListener(){
f@0 198 @Override
f@0 199 public void actionPerformed(ActionEvent evt) {
f@0 200 edgeCreatedListener.edgeCreated((Edge)e.clone());
f@0 201 }});
f@0 202 add(button);
f@0 203 }
f@0 204
f@0 205 /**
f@0 206 * Sets the {@code EdgeCreatedListener} for this toolbar. Any previous set listener
f@0 207 * will be overwritten.
f@0 208 *
f@0 209 * @param edgeCreatedListener the new {@code EdgeCreatedListener} for this toolbar
f@0 210 */
f@0 211 public void setEdgeCreatedListener(EdgeCreatedListener edgeCreatedListener){
f@0 212 this.edgeCreatedListener = edgeCreatedListener;
f@0 213 }
f@0 214
f@0 215 private class NodeButton extends JToggleButton{
f@0 216 public NodeButton(Node node, Icon icon){
f@0 217 super(icon);
f@0 218 setFocusable(false);
f@0 219 this.node = node;
f@0 220 }
f@0 221
f@0 222 public Node getNode(){
f@0 223 return node;
f@0 224 }
f@0 225 Node node;
f@0 226 }
f@0 227
f@0 228 /**
f@0 229 * The listener interface receiving events when the user clicks on
f@0 230 * an {@code Edge} button. Unlike {@code Node} buttons which are {@code JTobbleButton}
f@0 231 * objects to select the {@code Node} returned by {@code getSelectedTool()},
f@0 232 * the {@code Edge} buttons just trigger the registered listener with an immediate effect.
f@0 233 */
f@0 234 public interface EdgeCreatedListener {
f@0 235 /**
f@0 236 * Invoked when an {@code Edge} button is pressed.
f@0 237 * @param e the {@code Edge} related to the pressed button
f@0 238 */
f@0 239 void edgeCreated(Edge e);
f@0 240 }
f@0 241
f@0 242 private ButtonGroup nodeButtonsGroup;
f@0 243 private EdgeCreatedListener edgeCreatedListener;
f@0 244 private NodeButton selectButton;
f@0 245
f@0 246 private static final int BUTTON_SIZE = 30;
f@0 247 private static final int OFFSET = 5;
f@0 248 }
f@0 249