home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 20.5 KB | 762 lines |
- package symantec.itools.awt;
-
- import java.awt.AWTException;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.LayoutManager;
- import java.awt.Panel;
- import java.awt.Point;
- import symantec.itools.util.Timer;
-
- /**
- * ButtonBase abstract class used to implement special Symantec buttons.
- * It supports InfoTips, 3-D beveled borders, and continuous notification
- * messages while the button is pressed.
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- public abstract class ButtonBase
- extends Canvas
- {
- /**
- * True if the button is currently pressed.
- */
- protected boolean pressed;
- /**
- * True if the button has been released.
- */
- protected boolean released;
- /**
- * True if mouse is over button.
- */
- protected boolean inButton;
- /**
- * If true button will continuously send messages when pressed.
- */
- protected boolean notifyWhilePressed;
- /**
- * If true an InfoTip will appear after mouse has been over button a few moments.
- */
- protected boolean showInfoTip;
- /**
- * The notify timer is running.
- */
- protected boolean running;
- /**
- * Notification has been sent in response to a mouse down.
- */
- protected boolean notified;
- /**
- * Show the focus when the mouse enters the button.
- */
- protected boolean showFocus;
- /**
- * Draw InfoTip during paint().
- */
- protected boolean doInfoTip;
- /**
- * The 3-D bevel height of the button in pixels.
- */
- protected int bevel;
- /**
- * The delay in milliseconds between notifications when the button pressed.
- */
- protected int notifyDelay;
- /**
- * The delay in milliseconds till InfoTips first appear.
- */
- protected int infoTipDelay;
- /**
- * Adjustment for 3-D bevel while button is pressed.
- */
- protected int pressedAdjustment;
- /**
- * The text to display in the InfoTip.
- */
- protected String infoTipText;
- /**
- * The color of the text in the InfoTip.
- */
- protected Color infoTipTextColor;
- /**
- * Timer used to time notification messages while button pressed.
- */
- protected Timer notifyTimer;
- /**
- * Timer used to time delay before InfoTips appear.
- */
- protected Timer infoTipTimer;
- /**
- * Location to display the InfoTip, x-coordinate.
- */
- protected int infoTipX;
- /**
- * Location to display the InfoTip, y-coordinate.
- */
- protected int infoTipY;
- /**
- * Layout manager for InfoTips.
- */
- protected LayoutManager infoTipLayoutManager;
-
- /**
- * Constructs a default ButtonBase. The defaults are no notifyWhilePressed,
- * no InfoTips, and a bevel height of 1.
- */
- protected ButtonBase()
- {
- pressed = false;
- released = true;
- notifyWhilePressed = false;
- showInfoTip = false;
- running = false;
- notified = false;
- notifyTimer = null;
- infoTipTimer = null;
- infoTipTextColor = Color.black;
- notifyDelay = 1000;
- infoTipDelay = 1000;
- bevel = 1;
- pressedAdjustment = 0;
- resize(10, 10);
- }
-
- /**
- * Sets the 3-D bevel height of the button.
- * @param size size of bevel, in pixels
- * @see #getBevelHeight
- */
- public void setBevelHeight(int size)
- // throws AWTException
- {
- try
- {
- checkBevelSize(size);
- }
- catch(AWTException e)
- {
- System.err.println("Invalid Bevel Size " + size);
- }
-
- bevel = size;
-
- invalidate();
- }
-
- /**
- * Returns the current 3-D bevel height of the button, in pixels.
- * @see #setBevelHeight
- */
- public int getBevelHeight()
- {
- return bevel;
- }
-
- /**
- * Sets whether the button will continually send notify messages while pressed.
- * @param f true = send messages; false = do not send messages
- * @see #getNotifyWhilePressed
- * @see #setNotifyDelay
- * @see #getNotifyDelay
- */
- public void setNotifyWhilePressed(boolean f)
- {
- notifyWhilePressed = f;
-
- if (notifyWhilePressed)
- {
- notifyTimer = new Timer(this, notifyDelay, true, Event.ACTION_EVENT);
- }
- else if (notifyTimer != null)
- {
- notifyTimer = null;
- }
- }
-
- /**
- * Returns the current notifyWhilePressed status.
- * @see #setNotifyWhilePressed
- * @see #setNotifyDelay
- * @see #getNotifyDelay
- */
- public boolean getNotifyWhilePressed()
- {
- return notifyWhilePressed;
- }
-
- /**
- * Sets the notification message delay in milliseconds.
- * @param d the delay between notification messages in milliseconds
- * @see #setNotifyWhilePressed
- * @see #getNotifyDelay
- */
- public void setNotifyDelay(int d)
- {
- notifyDelay = d;
- }
-
- /**
- * Returns the current delay (in milliseconds) between notification messages.
- * @see #setNotifyWhilePressed
- * @see #setNotifyDelay
- */
- public int getNotifyDelay()
- {
- return notifyDelay;
- }
-
- /**
- * Sets whether an InfoTip is displayed for this button.
- * @param f If true an InfoTip will be displayed for this button
- * @see #getShowInfoTip
- * @see #setInfoTipDelay
- * @see #getInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipText
- * @see #getInfoTipTextColor
- * @see #setInfoTipTextColor
- */
- public void setShowInfoTip(boolean f)
- {
- showInfoTip = f;
-
- if (showInfoTip)
- {
- infoTipTimer = new Timer(this, notifyDelay, true, Event.ACTION_EVENT);
- }
- else if (infoTipTimer != null)
- {
- infoTipTimer = null;
- }
- }
-
- /**
- * Returns whether an InfoTip is displayed for this button.
- * @see #setShowInfoTip
- * @see #setInfoTipDelay
- * @see #getInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipText
- * @see #getInfoTipTextColor
- * @see #setInfoTipTextColor
- */
- public boolean getShowInfoTip()
- {
- return showInfoTip;
- }
-
- /**
- * Sets the delay till an InfoTip is displayed for this button.
- * @param d the delay, in milliseconds, from the time the mouse enters the
- * button till the InfoTip is displayed
- * @see #setShowInfoTip
- * @see #getShowInfoTip
- * @see #getInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipText
- * @see #getInfoTipTextColor
- * @see #setInfoTipTextColor
- */
- public void setInfoTipDelay(int d)
- {
- infoTipDelay = d;
- }
-
- /**
- * Returns the delay, in milliseconds, from the time the mouse enters the
- * button till the InfoTip is displayed
- * @see #setShowInfoTip
- * @see #getShowInfoTip
- * @see #setInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipText
- * @see #getInfoTipTextColor
- * @see #setInfoTipTextColor
- */
- public int getInfoTipDelay()
- {
- return infoTipDelay;
- }
-
- /**
- * Sets whether this button will be shown as having the focus when the mouse enters.
- * @param boolean f true = show at mouse enter; false = do not show at mouse enter
- * @see #getShowFocus
- */
- public void setShowFocus(boolean f)
- {
- showFocus = f;
- }
-
- /**
- * Returns whether this button will be shown as having the focus when the mouse enters.
- * @see #setShowFocus
- */
- public boolean getShowFocus()
- {
- return showFocus;
- }
-
- /**
- * Sets the text displayed in this button's InfoTip.
- * @param t the text to display in the InfoTip
- * @see #setShowInfoTip
- * @see #getShowInfoTip
- * @see #setInfoTipDelay
- * @see #getInfoTipDelay
- * @see #getInfoTipText
- * @see #getInfoTipTextColor
- * @see #setInfoTipTextColor
- */
- public void setInfoTipText(String t)
- {
- infoTipText = t;
- }
-
- /**
- * Returns the text displayed in this button's InfoTip.
- * @see #setShowInfoTip
- * @see #getShowInfoTip
- * @see #setInfoTipDelay
- * @see #getInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipTextColor
- * @see #setInfoTipTextColor
- */
- public String getInfoTipText()
- {
- return infoTipText;
- }
-
- /**
- * Sets the color of text displayed in this button's InfoTip.
- * @param c the color of the text displayed in the InfoTip
- * @see #setShowInfoTip
- * @see #getShowInfoTip
- * @see #setInfoTipDelay
- * @see #getInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipText
- * @see #getInfoTipTextColor
- */
- public void setInfoTipTextColor(Color c)
- {
- infoTipTextColor = c;
- }
-
- /**
- * Returns the color of text displayed in this button's InfoTip.
- * @see #setShowInfoTip
- * @see #getShowInfoTip
- * @see #setInfoTipDelay
- * @see #getInfoTipDelay
- * @see #setInfoTipText
- * @see #getInfoTipText
- * @see #setInfoTipTextColor
- */
- public Color getInfoTipTextColor()
- {
- return infoTipTextColor;
- }
-
- /**
- * Processes MOUSE_UP events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_UP
- * event. These events occur when the mouse button is released while
- * inside this component.
- * If the notification timer is running it is stopped.
- * If the mouse was pressed inside the button then an event is posted.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseDown
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseUp(Event e, int x, int y)
- {
- if (running)
- {
- running = false;
- notifyTimer.stop();
- }
-
- if (pressed)
- {
- pressed = false;
- pressedAdjustment = 0;
-
- if (!notifyWhilePressed || !notified)
- {
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- }
- }
-
- released = true;
- repaint();
-
- return true;
- }
-
- /**
- * Processes MOUSE_DOWN events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DOWN
- * event. These events occur when the mouse button is pressed while
- * inside this component.
- * If the notifyWhilePressed flag is true the notification Timer is started
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseUp
- * @see java.awt.Component#handleEvent
- * @see #setNotifyWhilePressed
- * @see #setNotifyDelay
- */
- public boolean mouseDown(Event e, int x, int y)
- {
- if (notifyWhilePressed && !running)
- {
- running = true;
- notifyTimer.start();
- }
-
- pressed = true;
- released = false;
- pressedAdjustment = bevel;
- repaint();
-
- return true;
- }
-
- /**
- * Processes MOUSE_ENTER events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_ENTER
- * event. These events occur when the mouse first moves over this
- * component.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseExit
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseEnter(Event e, int x, int y)
- {
- inButton = true;
-
- if (showInfoTip)
- {
- infoTipX = x;
- infoTipY = y;
- infoTipTimer.start();
- }
-
- if (!released)
- {
- mouseDown(e, x, y);
- }
-
- //repaint();
-
- return true;
- }
-
- /**
- * Processes MOUSE_EXIT events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_EXIT
- * event. These events occur when the mouse first leaves this
- * component.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseEnter
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseExit(Event e, int x, int y)
- {
- inButton = false;
-
- if (showInfoTip)
- {
- Panel infoTipPanel;
-
- infoTipTimer.stop();
- infoTipPanel = InfoTipManager.getInfoTipPanel();
- infoTipPanel.getParent().setLayout(infoTipLayoutManager);
- infoTipPanel.hide();
- }
-
- if (pressed)
- {
- pressed = false;
- pressedAdjustment = 0;
- }
-
- //repaint();
-
- return true;
- }
-
- /**
- * Handles internal actions for this component.
- * This is a standard Java AWT method which usually gets called by the AWT
- * method handleEvent() in response to receiving an ACTION_EVENT event. In those
- * cases the o parameter contains the value in the event's arg field.
- *
- * @param e the event that caused this action
- * @param o the action
- * @return true if the action was handled
- * @see java.awt.Component#handleEvent
- */
- public boolean action(Event e, Object o)
- {
- if (notifyWhilePressed)
- {
- if (e.target == notifyTimer && !symantec.beans.Beans.isDesignTime())
- {
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- return true;
- }
- }
-
- if (showInfoTip)
- {
- if (e.target == infoTipTimer)
- {
- doInfoTip = true;
- repaint();
- infoTipTimer.stop();
-
- return true;
- }
- }
-
- return super.action(e, o);
- }
-
- /**
- * Enables this component so that it will respond to user input.
- * This is a standard Java AWT method which gets called to enable
- * this component. Once enabled this component will respond to user input.
- *
- * @see #disable
- */
- public void enable()
- {
- if (!isEnabled())
- {
- super.enable();
- pressed = false;
- pressedAdjustment = 0;
- }
-
- repaint();
- }
-
- /**
- * Disables this component so that it doesn't respond to user input.
- * This is a standard Java AWT method which gets called to disable
- * this component. Once disabled this component will not respond to user
- * input.
- *
- * @see #enable
- */
- public void disable()
- {
- if (isEnabled())
- {
- super.disable();
-
- if (notifyTimer != null)
- {
- notifyTimer.stop();
- }
-
- if (infoTipTimer != null)
- {
- infoTipTimer.stop();
- }
-
- pressed = false;
- pressedAdjustment = 0;
- }
-
- repaint();
- }
-
- /**
- * Handles redrawing of this component on the screen.
- * This is a standard Java AWT method which gets called by the Java
- * AWT (repaint()) to handle repainting this component on the screen.
- * The graphics context clipping region is set to the bounding rectangle
- * of this component and its <0,0> coordinate is this component's
- * top-left corner.
- * Typically this method paints the background color to clear the
- * component's drawing space, sets graphics context to be the foreground
- * color, and then calls paint() to draw the component.
- *
- * It is overridden here to prevent the flicker associated with the standard
- * update() method's repainting of the background before painting the component
- * itself.
- *
- * @param g the graphics context
- * @see java.awt.Component#repaint
- * @see #paint
- */
- public void update(Graphics g)
- {
- Dimension s;
-
- s = size();
-
- g.clipRect(0, 0, s.width, s.height);
- paint(g);
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see #update
- */
- public void paint(Graphics g)
- {
- Dimension s;
- int width;
- int height;
- int x;
- int y;
- int w;
- int h;
- int i;
-
- s = size();
- width = s.width;
- height = s.height;
- x = bevel + 1;
- y = bevel + 1;
- w = width - 1;
- h = height - 1;
-
- g.setColor(Color.lightGray);
- g.fillRect(0, 0, width, height);
-
- if (pressed)
- {
- y = x += bevel > 0 ? 2 : 1;
- g.setColor(Color.lightGray);
-
- for(i = 1; i < bevel + 1; i++)
- {
- g.drawLine(i, h - i, w - i, h - i);
- g.drawLine(w - i, h - i, w - i, i);
- }
-
- g.setColor(Color.gray);
-
- for(i = 1; i < bevel + 1; ++i)
- {
- g.drawLine(i, h, i, i);
- g.drawLine(i, i, w, i);
- }
- }
- else
- {
- g.setColor(Color.white);
-
- for(i = 1; i < bevel + 1; i++)
- {
- g.drawLine(i, h - i, i, i);
- g.drawLine(i, i, w - i, i);
- }
-
- g.setColor(Color.gray);
-
- for(i = 1; i < bevel + 2; ++i)
- {
- g.drawLine(i, h - i, w - i, h - i);
- g.drawLine(w - i, h - i, w - i, i);
- }
- }
-
- g.setColor(Color.black);
- g.drawLine(1, 0, width - 2, 0);
- g.drawLine(0, 1, 0, height - 2);
- g.drawLine(1, height - 1, width - 2, height - 1);
- g.drawLine(width - 1, height - 2, width - 1, 1);
-
- if(showInfoTip)
- {
- if (doInfoTip)
- {
- drawInfoTip();
- }
- }
- }
-
- /**
- * Determines where to draw this button's InfoTip then draws it.
- */
- protected void drawInfoTip()
- {
- Panel infoTipPanel;
- Point p;
-
- doInfoTip = false;
- p = location();
-
- infoTipPanel = InfoTipManager.getInfoTipPanel();
-
- infoTipX += p.x;
- infoTipY += p.y;
-
- infoTipLayoutManager = infoTipPanel.getParent().getLayout();
- InfoTipManager.draw(infoTipX, infoTipY, infoTipText, getFontMetrics(getFont()), Color.yellow, Color.black);
- }
-
- /**
- * Ensures the given bevel size is valid for this button.
- */
- private void checkBevelSize(int i)
- throws AWTException
- {
- Dimension s;
-
- s = size();
-
- if (i < 0 || i >= (s.width / 2) || i >= (s.height / 2))
- {
- throw new AWTException("invalid bevel size");
- }
- }
- }
-
-