Source of ToolBar.java


  1: import java.awt.*;
  2: import java.awt.geom.*;
  3: import java.util.*;
  4: import javax.swing.*;

  6: /**
  7:    A tool bar that contains node and edge prototype icons.
  8:    Exactly one icon is selected at any time.
  9: */
 10: public class ToolBar extends JPanel
 11: {
 12:    /**
 13:       Constructs a tool bar with no icons.
 14:    */
 15:    public ToolBar(Graph graph)
 16:    {
 17:       group = new ButtonGroup();
 18:       tools = new ArrayList<Object>();

 20:       JToggleButton grabberButton = new JToggleButton(new
 21:          Icon()
 22:          {
 23:             public int getIconHeight() { return BUTTON_SIZE; }
 24:             public int getIconWidth() { return BUTTON_SIZE; }
 25:             public void paintIcon(Component c, Graphics g,
 26:                int x, int y)
 27:             {
 28:                Graphics2D g2 = (Graphics2D) g;
 29:                GraphPanel.drawGrabber(g2, x + OFFSET, y + OFFSET);
 30:                GraphPanel.drawGrabber(g2, x + OFFSET, y + BUTTON_SIZE - OFFSET);
 31:                GraphPanel.drawGrabber(g2, x + BUTTON_SIZE - OFFSET, y + OFFSET);
 32:                GraphPanel.drawGrabber(g2, x + BUTTON_SIZE - OFFSET, y + BUTTON_SIZE - OFFSET);
 33:             }
 34:          });
 35:       group.add(grabberButton);
 36:       add(grabberButton);
 37:       grabberButton.setSelected(true);
 38:       tools.add(null);

 40:       Node[] nodeTypes = graph.getNodePrototypes();
 41:       for (Node n : nodeTypes)
 42:          add(n);
 43:       Edge[] edgeTypes = graph.getEdgePrototypes();
 44:       for (Edge e : edgeTypes)
 45:          add(e);
 46:    }

 48:    /**
 49:       Gets the node or edge prototype that is associated with
 50:       the currently selected button
 51:       @return a Node or Edge prototype
 52:    */
 53:    public Object getSelectedTool()
 54:    {
 55:           int i = 0;
 56:       for (Object o : tools)
 57:       {
 58:          JToggleButton button = (JToggleButton) getComponent(i++);
 59:          if (button.isSelected()) return o;
 60:       }
 61:       return null;
 62:    }

 64:    /**
 65:       Adds a node to the tool bar.
 66:       @param n the node to add
 67:    */
 68:    public void add(final Node n)
 69:    {
 70:       JToggleButton button = new JToggleButton(new
 71:          Icon()
 72:          {
 73:             public int getIconHeight() { return BUTTON_SIZE; }
 74:             public int getIconWidth() { return BUTTON_SIZE; }
 75:             public void paintIcon(Component c, Graphics g,
 76:                int x, int y)
 77:             {
 78:                double width = n.getBounds().getWidth();
 79:                double height = n.getBounds().getHeight();
 80:                Graphics2D g2 = (Graphics2D) g;
 81:                double scaleX = (BUTTON_SIZE - OFFSET)/ width;
 82:                double scaleY = (BUTTON_SIZE - OFFSET)/ height;
 83:                double scale = Math.min(scaleX, scaleY);

 85:                AffineTransform oldTransform = g2.getTransform();
 86:                g2.translate(x, y);
 87:                g2.scale(scale, scale);
 88:                g2.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0));
 89:                g2.setColor(Color.black);
 90:                n.draw(g2);
 91:                g2.setTransform(oldTransform);
 92:             }
 93:          });
 94:       group.add(button);
 95:       add(button);
 96:       tools.add(n);
 97:    }

 99:    /**
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:    }

130:    private ButtonGroup group;
131:    private ArrayList<Object> tools;

133:    private static final int BUTTON_SIZE = 25;
134:    private static final int OFFSET = 4;
135: }