Back to index

moin  1.9.0~rc2
PaletteButton.java
Go to the documentation of this file.
00001 /*
00002  * @(#)PaletteButton.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.util;
00007 
00008 import java.awt.*;
00009 import java.awt.event.*;
00010 
00020 public abstract class PaletteButton
00021                 extends Canvas
00022                 implements MouseListener, MouseMotionListener {
00023 
00024     static final int NORMAL = 1;
00025     static final int PRESSED = 2;
00026     static final int SELECTED = 3;
00027 
00028     private PaletteListener  fListener;
00029     private int             fState;
00030     private int             fOldState;
00031 
00032 
00037     public PaletteButton(PaletteListener listener) {
00038         fListener = listener;
00039         fState = fOldState = NORMAL;
00040         addMouseListener(this);
00041         addMouseMotionListener(this);
00042     }
00043 
00044     public abstract void paintBackground(Graphics g);
00045     public abstract void paintNormal(Graphics g);
00046     public abstract void paintPressed(Graphics g);
00047     public abstract void paintSelected(Graphics g);
00048 
00049     public Object value() {
00050         return null;
00051     }
00052 
00053     public String name() {
00054         return "";
00055     }
00056 
00057     public void reset() {
00058         fState = NORMAL;
00059         repaint();
00060     }
00061 
00062     public void select() {
00063         fState = SELECTED;
00064         repaint();
00065     }
00066 
00067     public void mousePressed(MouseEvent e) {
00068         fOldState = fState;
00069         fState = PRESSED;
00070         repaint();
00071     }
00072 
00073     public void mouseDragged(MouseEvent e) {
00074         if (contains(e.getX(),e.getY()))
00075             fState = PRESSED;
00076         else
00077             fState = fOldState;
00078         repaint();
00079     }
00080 
00081     public void mouseReleased(MouseEvent e) {
00082         fState = fOldState;
00083         repaint();
00084         if (contains(e.getX(),e.getY()))
00085             fListener.paletteUserSelected(this);
00086     }
00087 
00088     public void mouseMoved(MouseEvent e) {
00089         fListener.paletteUserOver(this, true);
00090     }
00091 
00092     public void mouseExited(MouseEvent e) {
00093         if (fState == PRESSED) // JDK1.1 on Windows sometimes looses mouse released
00094             mouseDragged(e);
00095         fListener.paletteUserOver(this, false);
00096     }
00097 
00098     public void mouseClicked(MouseEvent e) {}
00099     public void mouseEntered(MouseEvent e) {}
00100 
00101     public void update(Graphics g) {
00102         paint(g);
00103     }
00104 
00105     public void paint(Graphics g) {
00106         paintBackground(g);
00107 
00108         switch (fState) {
00109         case PRESSED:
00110             paintPressed(g);
00111             break;
00112         case SELECTED:
00113             paintSelected(g);
00114             break;
00115         case NORMAL:
00116         default:
00117             paintNormal(g);
00118             break;
00119         }
00120     }
00121 }