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<Object>();
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 (Node n : nodeTypes)
042:          add(n);
043: 
044:       Edge[] edgeTypes = graph.getEdgePrototypes();
045:       for (Edge e : edgeTypes)
046:          add(e);
047:    }
048: 
049:    /**
050:       Gets the node or edge prototype that is associated with
051:       the currently selected button
052:       @return a Node or Edge prototype
053:    */
054:    public Object getSelectedTool()
055:    {
056:       int i=0;
057:       for (Object o : tools)
058:       {
059:          JToggleButton button = (JToggleButton) getComponent(i++);
060:          if (button.isSelected()) return o;
061:       }
062:       return null;
063:    }
064: 
065:    /**
066:       Adds a node to the tool bar.
067:       @param n the node to add
068:    */
069:    public void add(final Node n)
070:    {
071:       JToggleButton button = new JToggleButton(new
072:          Icon()
073:          {
074:             public int getIconHeight() { return BUTTON_SIZE; }
075:             public int getIconWidth() { return BUTTON_SIZE; }
076:             public void paintIcon(Component c, Graphics g,
077:                int x, int y)
078:             {
079:                double width = n.getBounds().getWidth();
080:                double height = n.getBounds().getHeight();
081:                Graphics2D g2 = (Graphics2D) g;
082:                double scaleX = (BUTTON_SIZE - OFFSET)/ width;
083:                double scaleY = (BUTTON_SIZE - OFFSET)/ height;
084:                double scale = Math.min(scaleX, scaleY);
085: 
086:                AffineTransform oldTransform = g2.getTransform();
087:                g2.translate(x, y);
088:                g2.scale(scale, scale);
089:                g2.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0));
090:                g2.setColor(Color.black);
091:                n.draw(g2);
092:                g2.setTransform(oldTransform);
093:             }
094:          });
095:       group.add(button);
096:       add(button);
097:       tools.add(n);
098:    }
099: 
100:    /**
101:       Adds an edge to the tool bar.
102:       @param n the edge to add
103:    */
104:    public void add(final Edge e)
105:    {
106:       JToggleButton button = new JToggleButton(new
107:          Icon()
108:          {
109:             public int getIconHeight() { return BUTTON_SIZE; }
110:             public int getIconWidth() { return BUTTON_SIZE; }
111:             public void paintIcon(Component c, Graphics g,
112:                int x, int y)
113:             {
114:                Graphics2D g2 = (Graphics2D) g;
115:                PointNode p = new PointNode();
116:                p.translate(OFFSET, OFFSET);
117:                PointNode q = new PointNode();
118:                q.translate(BUTTON_SIZE - OFFSET, BUTTON_SIZE - OFFSET);
119:                e.connect(p, q);
120:                g2.translate(x, y);
121:                g2.setColor(Color.black);
122:                e.draw(g2);
123:                g2.translate(-x, -y);
124:             }
125:          });
126:       group.add(button);
127:       add(button);
128:       tools.add(e);
129:    }
130: 
131:    private ButtonGroup group;
132:    private ArrayList<Object> tools;
133: 
134:    private static final int BUTTON_SIZE = 25;
135:    private static final int OFFSET = 4;
136: }