home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / control / controlbutton.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.5 KB  |  130 lines

  1. // ControlButton.java
  2. // 19.03.96
  3. //
  4. // a button on the control panel
  5.  
  6. package cybcerone.control;
  7.  
  8. import java.awt.Image;
  9. import java.awt.Graphics;
  10. import java.awt.Event;
  11.  
  12. import cybcerone.utils.Appletlike;
  13. import cybcerone.utils.IdPanel;
  14.  
  15. /**
  16.  * The common ancestor of all of the buttons on the control bar.
  17.  */
  18. class ControlButton extends IdPanel {
  19.   protected static final String imagePath = ControlPanel.imagePath;
  20.  
  21.   private Image pressedBg;
  22.   private Image disabledBg;
  23.   protected boolean pressed;
  24.   protected boolean disabled;
  25.   
  26.   ControlButton (String id, String statusText, Appletlike app) {
  27.     super (id, statusText, app);
  28.     pressed = false;
  29.     disabled = true;
  30.   }
  31.  
  32.   /**
  33.    * Assign the pressed image.
  34.    */
  35.   protected void setPressedBg (String PressedBgFile, int priority) {
  36.     this.pressedBg = app.getImage (PressedBgFile, priority);
  37.   }
  38.  
  39.   /**
  40.    * Assign the diabled image.
  41.    */
  42.   protected void setDisabledBg (String DisabledBgFile, int priority) {
  43.     this.disabledBg = app.getImage (DisabledBgFile, priority);
  44.   }
  45.  
  46.   /**
  47.    * Press the button.
  48.    */
  49.   public boolean mouseDown (Event evt, int x, int y) {
  50.     if (!disabled) {
  51.       pressed = true;
  52.       repaint ();
  53.     }
  54.     return true;
  55.   }
  56.  
  57.   /**
  58.    * Unpress the button.
  59.    */
  60.   public boolean mouseUp (Event evt, int x, int y) {
  61.     if (!disabled) {
  62.       if (pressed)
  63.     doAction ();
  64.       pressed = false;
  65.       repaint ();
  66.     }
  67.     return true;
  68.   }
  69.  
  70.   /**
  71.    * Unpress the button.
  72.    */
  73.   public boolean mouseExit (Event evt, int x, int y) {
  74.     if (!disabled) {
  75.       pressed = false;
  76.       repaint ();
  77.     }
  78.     return true;
  79.   }
  80.  
  81.   /**
  82.    * Draw the image for this button's current state.
  83.    */
  84.   public void paint (Graphics g) {
  85.     if (disabled) {
  86.       if (disabledBg != null) 
  87.     g.drawImage (disabledBg, 0, 0, this);
  88.       else
  89.     hide ();
  90.     } else if (pressed) {
  91.       if (pressedBg != null) g.drawImage (pressedBg, 0, 0, this);
  92.     } else {
  93.       super.paint (g);
  94.     }
  95.   }
  96.  
  97.   /**
  98.    * Disable this button.
  99.    */
  100.   public void disable () {
  101.     disabled = true;
  102.     repaint ();
  103.   }
  104.  
  105.   /**
  106.    * Enable this button.
  107.    */
  108.   public void enable () {
  109.     disabled = false;
  110.     show ();
  111.     repaint ();
  112.   }
  113.  
  114.   /**
  115.    * When the button is pushed, this is the screen it leads to.
  116.    */
  117.   public void setNext (String next) {
  118.     super.setNext (next);
  119.   }
  120.  
  121.   /**
  122.    * The time has come to take action.  Override this to take a
  123.    * specific action.
  124.    */
  125.   protected void doAction () {
  126.     if (next != null)
  127.       app.update (next);
  128.   }
  129. }
  130.