001package ca.bc.webarts.applets.menu.gui;
002import java.awt.*;
003import java.awt.event.*;
004
005
006/**
007 *  Description of the Class
008 *
009 * @created    October 18, 2002
010 */
011public class MyMenuButton extends Canvas implements ActionCommand
012{
013  /**  Description of the Field */
014  private static int instanceNumber;
015  /**  Description of the Field */
016  protected String label;
017  /**  Description of the Field */
018  protected String actionCommand;
019  /**  Description of the Field */
020  protected boolean pressed;
021  /**  Description of the Field */
022  private int instanceCurrentNumber;
023  /**  Description of the Field */
024  private String className = "MyMenuButton";
025  /**  Description of the Field */
026  private ActionListener actionListener;
027
028
029  /**  Constructor for the MyMenuButton object */
030  public MyMenuButton()
031  {
032    this("Button");
033  }
034
035
036  /**
037   *  Constructor for the MyMenuButton object
038   *
039   * @param  string  Description of the Parameter
040   */
041  public MyMenuButton(String string)
042  {
043    label = string;
044    instanceCurrentNumber = instanceNumber;
045    instanceNumber++;
046    this.setCursor(new Cursor(12));
047    this.enableEvents(16L);
048  }
049
050
051  /**
052   *  Sets the actionCommand attribute of the MyMenuButton object
053   *
054   * @param  string  The new actionCommand value
055   */
056  public void setActionCommand(String string)
057  {
058    actionCommand = string;
059  }
060
061
062  /**
063   *  Gets the actionCommand attribute of the MyMenuButton object
064   *
065   * @return    The actionCommand value
066   */
067  public String getActionCommand()
068  {
069    if (actionCommand == null)
070    {
071      return label;
072    }
073    return actionCommand;
074  }
075
076
077  /**
078   *  Description of the Method
079   *
080   * @param  graphics  Description of the Parameter
081   */
082  public void paint(Graphics graphics)
083  {
084    Color color = this.getBackground();
085    int i = this.getSize().width - 1;
086    int i_0_ = this.getSize().height - 1;
087    Color color_1_;
088    Color color_2_;
089    if (!pressed)
090    {
091      color_1_ = color.brighter();
092      color_2_ = color.darker();
093    }
094    else
095    {
096      color_1_ = color.darker();
097      color_2_ = color.brighter();
098    }
099    graphics.setColor(color);
100    graphics.fillRect(1, 1, i - 1, i_0_ - 1);
101    graphics.setColor(color_1_);
102    graphics.drawLine(0, 0, i, 0);
103    graphics.drawLine(0, 1, i, 1);
104    graphics.drawLine(0, 1, 0, i_0_ - 1);
105    graphics.setColor(color_2_);
106    graphics.drawLine(1, i_0_ - 1, i, i_0_ - 1);
107    graphics.drawLine(0, i_0_, i, i_0_);
108    graphics.drawLine(i, 1, i, i_0_ - 1);
109    FontMetrics fontmetrics = this.getFontMetrics(this.getFont());
110    graphics.setColor(this.getForeground());
111    if (!pressed)
112    {
113      graphics.drawString(label,
114          i / 2 - fontmetrics.stringWidth(label) / 2,
115          (i_0_ / 2 + fontmetrics.getHeight() / 2
116           - fontmetrics.getMaxDescent()));
117    }
118    else
119    {
120      graphics.drawString(label,
121          i / 2 - fontmetrics.stringWidth(label) / 2 + 1,
122          (i_0_ / 2 + fontmetrics.getHeight() / 2
123           - fontmetrics.getMaxDescent() + 1));
124    }
125  }
126
127
128  /**
129   *  Description of the Method
130   *
131   * @param  mouseevent  Description of the Parameter
132   */
133  public void processMouseEvent(MouseEvent mouseevent)
134  {
135    switch (mouseevent.getID())
136    {
137        case 501:
138          pressed = true;
139          this.repaint();
140          break;
141        case 502:
142          if (actionListener != null && pressed)
143          {
144            actionListener.actionPerformed
145                (new ActionEvent(this, 1001, getActionCommand()));
146          }
147          if (pressed)
148          {
149            pressed = false;
150          }
151          this.repaint();
152          break;
153        case 505:
154          if (pressed)
155          {
156            pressed = false;
157          }
158          this.repaint();
159          break;
160    }
161    super.processMouseEvent(mouseevent);
162  }
163
164
165  /**
166   *  Description of the Method
167   *
168   * @param  actionlistener  Description of the Parameter
169   */
170  public synchronized void removeActionListener
171      (ActionListener actionlistener)
172  {
173    actionListener
174         = AWTEventMulticaster.remove(actionListener, actionlistener);
175  }
176
177
178  /**
179   *  Adds a feature to the ActionListener attribute of the MyMenuButton object
180   *
181   * @param  actionlistener  The feature to be added to the ActionListener
182   *      attribute
183   */
184  public synchronized void addActionListener(ActionListener actionlistener)
185  {
186    actionListener
187         = AWTEventMulticaster.add(actionListener, actionlistener);
188  }
189
190
191  /**
192   *  Gets the preferredSize attribute of the MyMenuButton object
193   *
194   * @return    The preferredSize value
195   */
196  public Dimension getPreferredSize()
197  {
198    if (label != null)
199    {
200      FontMetrics fontmetrics = this.getFontMetrics(this.getFont());
201      return new Dimension((fontmetrics.stringWidth(label)
202           + fontmetrics.getMaxDescent() * 5),
203          (fontmetrics.getHeight()
204           + fontmetrics.getMaxDescent() * 2));
205    }
206    return getMinimumSize();
207  }
208
209
210  /**
211   *  Gets the minimumSize attribute of the MyMenuButton object
212   *
213   * @return    The minimumSize value
214   */
215  public Dimension getMinimumSize()
216  {
217    return new Dimension(100, 50);
218  }
219
220
221  /**
222   *  Gets the name attribute of the MyMenuButton object
223   *
224   * @return    The name value
225   */
226  public String getName()
227  {
228    if (label != null)
229    {
230      return className + ", label=" + label;
231    }
232    return className + instanceCurrentNumber;
233  }
234
235
236  /**
237   *  Sets the name attribute of the MyMenuButton object
238   *
239   * @param  string  The new name value
240   */
241  public void setName(String string)
242  {
243    className = className;
244  }
245
246
247  /**
248   *  Sets the label attribute of the MyMenuButton object
249   *
250   * @param  string  The new label value
251   */
252  public void setLabel(String string)
253  {
254    label = string;
255    this.invalidate();
256    this.repaint();
257  }
258
259
260  /**
261   *  Gets the label attribute of the MyMenuButton object
262   *
263   * @return    The label value
264   */
265  public String getLabel()
266  {
267    return label;
268  }
269}
270