001package horstmann.ch08_umleditor; 002import java.awt.Color; 003import java.awt.Component; 004import java.awt.Graphics; 005import java.awt.Graphics2D; 006import java.awt.geom.AffineTransform; 007import java.util.ArrayList; 008 009import javax.swing.ButtonGroup; 010import javax.swing.Icon; 011import javax.swing.JPanel; 012import javax.swing.JToggleButton; 013 014/** 015 A tool bar that contains node and edge prototype icons. 016 Exactly one icon is selected at any time. 017 */ 018@SuppressWarnings("serial") 019public class ToolBar extends JPanel 020{ 021 /** 022 Constructs a tool bar with no icons. 023 */ 024 public ToolBar(Graph graph) 025 { 026 group = new ButtonGroup(); 027 tools = new ArrayList<Object>(); 028 029 JToggleButton grabberButton = new JToggleButton(new 030 Icon() 031 { 032 public int getIconHeight() { return BUTTON_SIZE; } 033 public int getIconWidth() { return BUTTON_SIZE; } 034 public void paintIcon(Component c, Graphics g, 035 int x, int y) 036 { 037 Graphics2D g2 = (Graphics2D) g; 038 GraphPanel.drawGrabber(g2, x + OFFSET, y + OFFSET); 039 GraphPanel.drawGrabber(g2, x + OFFSET, y + BUTTON_SIZE - OFFSET); 040 GraphPanel.drawGrabber(g2, x + BUTTON_SIZE - OFFSET, y + OFFSET); 041 GraphPanel.drawGrabber(g2, x + BUTTON_SIZE - OFFSET, y + BUTTON_SIZE - OFFSET); 042 } 043 }); 044 group.add(grabberButton); 045 add(grabberButton); 046 grabberButton.setSelected(true); 047 tools.add(null); 048 049 Node[] nodeTypes = graph.getNodePrototypes(); 050 for (Node n : nodeTypes) 051 add(n); 052 Edge[] edgeTypes = graph.getEdgePrototypes(); 053 for (Edge e : edgeTypes) 054 add(e); 055 } 056 057 /** 058 Gets the node or edge prototype that is associated with 059 the currently selected button 060 @return a Node or Edge prototype 061 */ 062 public Object getSelectedTool() 063 { 064 int i = 0; 065 for (Object o : tools) 066 { 067 JToggleButton button = (JToggleButton) getComponent(i++); 068 if (button.isSelected()) return o; 069 } 070 return null; 071 } 072 073 /** 074 Adds a node to the tool bar. 075 @param n the node to add 076 */ 077 public void add(final Node n) 078 { 079 JToggleButton button = new JToggleButton(new 080 Icon() 081 { 082 public int getIconHeight() { return BUTTON_SIZE; } 083 public int getIconWidth() { return BUTTON_SIZE; } 084 public void paintIcon(Component c, Graphics g, 085 int x, int y) 086 { 087 double width = n.getBounds().getWidth(); 088 double height = n.getBounds().getHeight(); 089 Graphics2D g2 = (Graphics2D) g; 090 double scaleX = (BUTTON_SIZE - OFFSET)/ width; 091 double scaleY = (BUTTON_SIZE - OFFSET)/ height; 092 double scale = Math.min(scaleX, scaleY); 093 094 AffineTransform oldTransform = g2.getTransform(); 095 g2.translate(x, y); 096 g2.scale(scale, scale); 097 g2.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0)); 098 g2.setColor(Color.black); 099 n.draw(g2); 100 g2.setTransform(oldTransform); 101 } 102 }); 103 group.add(button); 104 add(button); 105 tools.add(n); 106 } 107 108 /** 109 Adds an edge to the tool bar. 110 @param e the edge to add 111 */ 112 public void add(final Edge e) 113 { 114 JToggleButton button = new JToggleButton(new 115 Icon() 116 { 117 public int getIconHeight() { return BUTTON_SIZE; } 118 public int getIconWidth() { return BUTTON_SIZE; } 119 public void paintIcon(Component c, Graphics g, 120 int x, int y) 121 { 122 Graphics2D g2 = (Graphics2D) g; 123 PointNode p = new PointNode(); 124 p.translate(OFFSET, OFFSET); 125 PointNode q = new PointNode(); 126 q.translate(BUTTON_SIZE - OFFSET, BUTTON_SIZE - OFFSET); 127 e.connect(p, q); 128 g2.translate(x, y); 129 g2.setColor(Color.black); 130 e.draw(g2); 131 g2.translate(-x, -y); 132 } 133 }); 134 group.add(button); 135 add(button); 136 tools.add(e); 137 } 138 139 private ButtonGroup group; 140 private ArrayList<Object> tools; 141 142 private static final int BUTTON_SIZE = 25; 143 private static final int OFFSET = 4; 144}