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);

 44:       Edge[] edgeTypes = graph.getEdgePrototypes();
 45:       for (Edge e : edgeTypes)
 46:          add(e);
 47:    }

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

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

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

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:    }

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

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