001    /* 
002     * Copyright (c) Holger Pfaff - http://pfaff.ws
003     *
004     * This software maybe used for any purpose provided the
005     * above copyright notice is retained. It is supplied as is.
006     * No warranty expressed or implied - Use at your own risk.
007     */
008    
009    import java.awt.*;
010    import java.awt.event.*;
011    import java.util.*;
012    
013    /**
014      * (#)Knob.java
015      * @author   Holger Pfaff
016      * @version  3.2 19-Mar-2004<br><br>
017      *
018      * Implement a Knob (Button) widget
019      */
020    
021    public class Knob extends Caption {
022    
023      /**
024       * action command for this knob
025       */  
026      protected String acommand    = null;
027      
028      /**
029       * create an empty knob
030       */  
031      public Knob() {
032        this("");
033      }
034      
035      /**
036       * create a knob with speified label
037       *
038       * @param v value to add/remove
039       */  
040      public Knob(String l) {
041        super(l);
042        insets = new Insets(1,1,1,1);       // top left bottom right
043        innerInsets = new Insets(2,3,2,3);
044        borderType = RAISED;
045        borderDepth = 2;
046        mode = CENTER;
047        addMouseListener(this);
048        addMouseMotionListener(this);
049      }
050        
051      /**
052       * get current action command
053       *
054       * @return current action command
055       */  
056      public String getActionCommand() {
057        return acommand;
058      }
059      
060      /**
061       * set current action command
062       *
063       * @param a action command
064       */  
065      public void setActionCommand(String a) {
066        acommand = a;
067      }
068      
069      /**
070       * what to do on mouse press
071       */  
072      public void onPress() {
073        setActive(true);
074      }
075      
076      /**
077       * what to do on mouse release
078       */  
079      public void onRelease() {
080        active = false;
081        if(isEntered()) {
082          entered = false;
083          notifyActionListeners(new ActionEvent(this, 0, acommand));
084        }
085        repaint();
086      }
087    
088      /**
089       * implement mouse listener
090       *
091       * @param e MouseEvent
092       */  
093      public void mouseExited(MouseEvent e) {
094        setEntered(false);
095      }
096      
097      /**
098       * implement mouse listener
099       */  
100      public void mousePressed(MouseEvent e) {
101        setEntered(true);
102        onPress();
103      }
104      
105      /**
106       * implement mouse listener
107       *
108       * @param e MouseEvent
109       */  
110      public void mouseReleased(MouseEvent e) {
111        repaint();
112        onRelease();
113      }
114    
115      /**
116       * implement mouse listener
117       *
118       * @param e MouseEvent
119       */  
120      public void mouseMoved(MouseEvent e) {
121        if(isEntered() != contains(e.getPoint())) {
122          setEntered(!isEntered());
123        }
124      }
125      
126      /**
127       * implement mouse listener
128       *
129       * @param e MouseEvent
130       */  
131      public void mouseDragged(MouseEvent e) {
132        mouseMoved(e);
133      }
134    }