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