home *** CD-ROM | disk | FTP | other *** search
/ Datatid 2000 #1 / Datatid-2000-01.iso / Internet / JAVA_NAVIGATOR / JAVANAVIGATOR.EXE / %MAINDIR% / JavaNavigatorV50.exe / ButtonBase.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-06-09  |  4.9 KB  |  218 lines

  1. import java.awt.AWTException;
  2. import java.awt.Canvas;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Event;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9.  
  10. public abstract class ButtonBase extends Canvas {
  11.    protected boolean pressed = false;
  12.    protected boolean released = true;
  13.    protected boolean inButton;
  14.    protected boolean notifyWhilePressed = false;
  15.    protected boolean running = false;
  16.    protected boolean notified = false;
  17.    protected boolean showFocus;
  18.    protected int bevel = 1;
  19.    protected int notifyDelay = 1000;
  20.    protected int pressedAdjustment = 0;
  21.    protected Timer notifyTimer = null;
  22.    protected Image imgBG;
  23.  
  24.    protected ButtonBase() {
  25.       ((Component)this).resize(10, 10);
  26.    }
  27.  
  28.    public void setBevelHeight(int size) {
  29.       try {
  30.          this.checkBevelSize(size);
  31.       } catch (AWTException var2) {
  32.          System.err.println("Invalid Bevel Size " + size);
  33.       }
  34.  
  35.       this.bevel = size;
  36.       ((Component)this).invalidate();
  37.    }
  38.  
  39.    public int getBevelHeight() {
  40.       return this.bevel;
  41.    }
  42.  
  43.    public void setNotifyWhilePressed(boolean f) {
  44.       this.notifyWhilePressed = f;
  45.       if (this.notifyWhilePressed) {
  46.          this.notifyTimer = new Timer(this, this.notifyDelay, true, 1001);
  47.       } else {
  48.          if (this.notifyTimer != null) {
  49.             this.notifyTimer = null;
  50.          }
  51.  
  52.       }
  53.    }
  54.  
  55.    public boolean getNotifyWhilePressed() {
  56.       return this.notifyWhilePressed;
  57.    }
  58.  
  59.    public void setNotifyDelay(int d) {
  60.       this.notifyDelay = d;
  61.    }
  62.  
  63.    public int getNotifyDelay() {
  64.       return this.notifyDelay;
  65.    }
  66.  
  67.    public void setShowFocus(boolean f) {
  68.       this.showFocus = f;
  69.    }
  70.  
  71.    public boolean getShowFocus() {
  72.       return this.showFocus;
  73.    }
  74.  
  75.    public void setBGImage(Image img) {
  76.       this.imgBG = img;
  77.       ((Component)this).invalidate();
  78.    }
  79.  
  80.    public boolean mouseUp(Event e, int x, int y) {
  81.       if (this.running) {
  82.          this.running = false;
  83.          this.notifyTimer.stop();
  84.       }
  85.  
  86.       if (this.pressed) {
  87.          this.pressed = false;
  88.          this.pressedAdjustment = 0;
  89.          if (!this.notifyWhilePressed || !this.notified) {
  90.             ((Component)this).postEvent(new Event(this, 1001, (Object)null));
  91.          }
  92.       }
  93.  
  94.       this.released = true;
  95.       ((Component)this).repaint();
  96.       return true;
  97.    }
  98.  
  99.    public boolean mouseDown(Event e, int x, int y) {
  100.       if (this.notifyWhilePressed && !this.running) {
  101.          this.running = true;
  102.          this.notifyTimer.start();
  103.       }
  104.  
  105.       this.pressed = true;
  106.       this.released = false;
  107.       this.pressedAdjustment = this.bevel;
  108.       ((Component)this).repaint();
  109.       return true;
  110.    }
  111.  
  112.    public boolean mouseEnter(Event e, int x, int y) {
  113.       this.inButton = true;
  114.       if (!this.released) {
  115.          this.mouseDown(e, x, y);
  116.       }
  117.  
  118.       return true;
  119.    }
  120.  
  121.    public boolean mouseExit(Event e, int x, int y) {
  122.       this.inButton = false;
  123.       if (this.pressed) {
  124.          this.pressed = false;
  125.          this.pressedAdjustment = 0;
  126.       }
  127.  
  128.       return true;
  129.    }
  130.  
  131.    public boolean action(Event e, Object o) {
  132.       if (this.notifyWhilePressed && e.target == this.notifyTimer && !Beans.isDesignTime()) {
  133.          ((Component)this).postEvent(new Event(this, 1001, (Object)null));
  134.          return true;
  135.       } else {
  136.          return super.action(e, o);
  137.       }
  138.    }
  139.  
  140.    public void enable() {
  141.       if (!((Component)this).isEnabled()) {
  142.          super.enable();
  143.          this.pressed = false;
  144.          this.pressedAdjustment = 0;
  145.       }
  146.  
  147.       ((Component)this).repaint();
  148.    }
  149.  
  150.    public void disable() {
  151.       if (((Component)this).isEnabled()) {
  152.          super.disable();
  153.          if (this.notifyTimer != null) {
  154.             this.notifyTimer.stop();
  155.          }
  156.  
  157.          this.pressed = false;
  158.          this.pressedAdjustment = 0;
  159.       }
  160.  
  161.       ((Component)this).repaint();
  162.    }
  163.  
  164.    public void update(Graphics g) {
  165.       Dimension s = ((Component)this).size();
  166.       this.paint(g);
  167.    }
  168.  
  169.    public void paint(Graphics g) {
  170.       Dimension s = ((Component)this).size();
  171.       int width = s.width;
  172.       int height = s.height;
  173.       int x = this.bevel + 1;
  174.       int y = this.bevel + 1;
  175.       int w = width - 1;
  176.       int h = height - 1;
  177.       if (this.pressed) {
  178.          int var10000 = x + (this.bevel > 0 ? 2 : 1);
  179.          g.setColor(Color.lightGray);
  180.  
  181.          for(int i = 1; i < this.bevel + 1; ++i) {
  182.             g.drawLine(i, h - i, w - i, h - i);
  183.             g.drawLine(w - i, h - i, w - i, i);
  184.          }
  185.  
  186.          g.setColor(Color.gray);
  187.  
  188.          for(int var12 = 1; var12 < this.bevel + 1; ++var12) {
  189.             g.drawLine(var12, h, var12, var12);
  190.             g.drawLine(var12, var12, w, var12);
  191.          }
  192.  
  193.       } else {
  194.          g.setColor(Color.white);
  195.  
  196.          for(int i = 1; i < this.bevel + 1; ++i) {
  197.             g.drawLine(i, h - i, i, i);
  198.             g.drawLine(i, i, w - i, i);
  199.          }
  200.  
  201.          g.setColor(Color.gray);
  202.  
  203.          for(int var10 = 1; var10 < this.bevel + 2; ++var10) {
  204.             g.drawLine(var10, h - var10, w - var10, h - var10);
  205.             g.drawLine(w - var10, h - var10, w - var10, var10);
  206.          }
  207.  
  208.       }
  209.    }
  210.  
  211.    private void checkBevelSize(int i) throws AWTException {
  212.       Dimension s = ((Component)this).size();
  213.       if (i < 0 || i >= s.width / 2 || i >= s.height / 2) {
  214.          throw new AWTException("invalid bevel size");
  215.       }
  216.    }
  217. }
  218.