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