home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 11.4 KB | 420 lines |
- package symantec.itools.awt;
-
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Color;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseMotionListener;
-
- // 02/15/97 RKM Added validate - Fixes problem where calls to setMinValue & setMaxValue
- // would not work if called after reshape
- // 02/27/97 RKM Integrated Scott's change to use the background color of the component
- // 05/30/97 MSH Updated to support Java 1.1
- // 06/11/97 MSH Removed postEvent() and added correct listener registration fuctions
- // 07/17/97 LAB Added add/removeNotify for event listener registration. Separated event handling
- // innerclass into two innerclasses. Made the bound and constrained listener
- // registration functions call it's super. Changed setTickStyle to call it's super.
- // Moved doMove() into Slider since it was the same as HorizontalSlider:doMove().
- // Moved Action Listener/Event stuff into the base class. In general, moved common
- // things to the base class.
- // 07/24/97 CAR marked field transient as needed
- // implemented readObject
- // 08/12/97 RKM Removed property changed and veto support - it was all moved into the
- // base class and was no longer accessed from this class
- // Remove unnecessary clipRect in paint call
- // 10/05/97 LAB Made calculations in do_reshape use floats instead of ints to avoid rounding
- // errors with large numbers of ticks (Addresses Mac Bug #7591).
-
- /**
- * This component is used to select one value
- * from a continuous range of values. It has a movable thumb in front of a
- * gauge with ticks marks on it.
- * <p>
- * @see symantec.itools.awt.HorizontalSlider
- * @version 1.1, July 17, 1997
- * @author Symantec
- */
- public class VerticalSlider extends Slider
- {
- /**
- * Constructs a default VerticalSlider. The ticks
- * are drawn on both sides of the gauge with a frequency of 1.
- * The minimum value is 1. The maximum value is 10. The
- * border is shown.
- */
- public VerticalSlider()
- {
- this.thumb = new VerticalSliderThumbBoth();
- width = 175;
- }
-
- /**
- * Sets the current slider tick mark style.
- *
- * @param style the new tick mark style, one of TICK_LEFT, TICK_RIGHT, or TICK_BOTH
- * @see symantec.itools.awt.Slider#getTickStyle
- * @see Slider#TICK_LEFT
- * @see Slider#TICK_RIGHT
- * @see Slider#TICK_BOTH
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setTickStyle(int style) throws PropertyVetoException
- {
- if (this.style != style)
- {
- super.setTickStyle(style);
-
- switch (style)
- {
- case TICK_LEFT :
- thumb = new VerticalSliderThumbLeft();
- break;
- case TICK_RIGHT :
- thumb = new VerticalSliderThumbRight();
- break;
- default :
- thumb = new VerticalSliderThumbBoth();
- break;
- }
- repaint();
- }
- }
-
- /**
- * Tells this component that it has been added to a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- *
- * It has been overridden here to handle registering event listeners.
- *
- * @see #removeNotify
- */
- public synchronized void addNotify()
- {
- super.addNotify();
-
- //Hook up listeners
- if (mouse == null)
- {
- mouse = new Mouse();
- addMouseListener(mouse);
- }
- if (mouseMotion == null)
- {
- mouseMotion = new MouseMtn();
- addMouseMotionListener(mouseMotion);
- }
- }
-
- /**
- * Tells this component that it is being removed from a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is removed from a container. Typically, it is used to
- * destroy the peers of this component and all its subcomponents.
- *
- * It has been overridden here to handle registering event listeners.
- *
- * @see #addNotify
- */
- public synchronized void removeNotify()
- {
- //Unhook listeners
- if (mouse != null)
- {
- removeMouseListener(mouse);
- mouse = null;
- }
- if (mouseMotion != null)
- {
- removeMouseMotionListener(mouseMotion);
- mouseMotion = null;
- }
-
- super.removeNotify();
- }
-
- /**
- * This is the Mouse Event handling innerclass.
- */
- class Mouse extends java.awt.event.MouseAdapter implements java.io.Serializable
- {
- /**
- * Handles Mouse Pressed events
- * @param e the MouseEvent
- */
- public void mousePressed(MouseEvent e)
- {
- moveThumb( e.getY(), true );
- }
- }
-
- /**
- * This is the MouseMotion Event handling innerclass.
- */
- class MouseMtn extends java.awt.event.MouseMotionAdapter implements java.io.Serializable
- {
- /**
- * Handles Mouse Dragged events
- * @param e the MouseEvent
- */
- public void mouseDragged(MouseEvent e)
- {
- moveThumb( e.getY(), false );
- }
- }
-
- protected void do_reshape(int w, int h)
- {
- float hb = BORDER_X;
- float vb = BORDER_Y;
-
- if (w < hb)
- hb = w / 4;
-
- if (h < vb)
- vb = h / 4;
-
- float x0 = hb;
- float x1 = w - hb;
- float y0 = vb;
- float y1 = h - vb;
-
- if (x0 == 0)
- x0 = 1;
-
- if (x1 == 0)
- x1 = 1;
-
- if (y0 == 0)
- y0 = 1;
-
- if (y1 == 0)
- y1 = 1;
-
- float n = (max - min) / freq + 1;
-
- tick = new SliderTick[(int)n];
-
- float vs = (y1 - y0) / (n - 1), ch;
-
- for (int i = 0; i < n; ++i)
- {
- ch = i * vs;
- tick[i] = new SliderTick((int)x0, (int)x1, (int)(y0 + ch), (int)ch);
- }
-
- thumb.resize((int)(x1 - x0 - TICK_WIDTH - 1), (int)(vs / 2));
- }
-
- /**
- * 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 java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- super.paint(g);
-
- if (tick.length == 0)
- return;
-
- thumb.draw(g, tick[curPos]);
-
- if (prevPos != curPos)
- thumb.clip(g, tick[prevPos]);
-
- g.setColor(getBackground());
- g.fillRect(0, 0, width, height);
-
- g.setColor(Color.black);
-
- int x, y0, y1, w = width - 1, h = height - 1;
- boolean end;
-
- if (showBorder)
- g.drawRect(0, 0, w, h);
-
- for (int i = 0; i < tick.length; ++i)
- {
- end = i == 0 || i == tick.length - 1;
-
- SliderTick t = tick[i];
-
- if (style == TICK_LEFT || style == TICK_BOTH)
- g.drawLine(t.a + (end ? 0 : 1), t.c, t.a + TICK_WIDTH, t.c);
-
- if (style == TICK_RIGHT || style == TICK_BOTH)
- g.drawLine(t.b - TICK_WIDTH, t.c, t.b - (end ? 0 : 1), t.c);
- }
-
- SliderTick t = tick[0];
-
- x = (t.b + t.a) / 2;
- y0 = t.c - 5;
- y1 = tick[tick.length - 1].c + 5;
-
- g.drawLine(x, y0, x, y1);
-
- g.setColor(Color.gray);
- g.drawLine(x - 1, y1 + 1, x - 1, y0 - 1);
- g.drawLine(x - 1, y0 - 1, x + 1, y0 - 1);
-
- g.setColor(Color.lightGray);
- g.drawLine(x + 1, y0, x + 1, y1 + 1);
- g.drawLine(x + 1, y1 + 1, x, y1 + 1);
-
- g.setColor(Color.white);
- g.drawLine(x + 2, y0 - 1, x + 2, y1 + 2);
- g.drawLine(x + 2, y1 + 2, x - 1, y1 + 2);
-
- //???RKM??? This cannot work
- g.clipRect(0, 0, width, height);
-
- thumb.draw(g, tick[curPos]);
-
- prevPos = curPos;
- }
-
- private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
- in.defaultReadObject();
-
- //???RKM??? I do not believe this is necessary anymore, but can't test so I'll leave it in
- do_reshape(width, height);
- }
-
- /**
- * Length of the gauge ticks in pixels.
- */
- protected static final int TICK_WIDTH = 4;
-
- /**
- * The command name of the action event fired by this component.
- */
- protected String actionCommand = "VSliderMoved";
-
- private VerticalSliderThumb thumb;
- private Mouse mouse = null;
- private MouseMtn mouseMotion = null;
- }
-
- abstract class VerticalSliderThumb implements java.io.Serializable
- {
- void resize(int width, int height)
- {
- x = (this.width = width) - VerticalSlider.TICK_WIDTH - 1 - 1;
- y = (this.height = height) / 2;
- }
-
- abstract void draw(Graphics g, SliderTick t);
-
- protected void draw(int x0, int y0, int x1, int y1)
- {
- g.drawLine(t.a + x0 + VerticalSlider.TICK_WIDTH + 1, t.c + y0,
- t.a + x1 + VerticalSlider.TICK_WIDTH + 1, t.c + y1);
- }
-
- protected void initDraw(Graphics g, SliderTick t)
- {
- this.g = g;
- this.t = t;
-
- g.setColor(Color.lightGray);
- g.fillRect(t.a + 2 + VerticalSlider.TICK_WIDTH + 1, t.c - y + 1, x - 2, height - 2);
-
- g.setColor(Color.white);
- }
-
- void clip(Graphics g, SliderTick t)
- {
- g.clipRect(t.a, t.c - height / 2, width + 1, height + 1);
- }
-
- protected int x;
- protected int y;
- protected int width;
- protected int height;
- transient protected Graphics g;
- transient protected SliderTick t;
- }
-
-
- class VerticalSliderThumbBoth extends VerticalSliderThumb
- {
- void draw(Graphics g, SliderTick t)
- {
- super.initDraw(g, t);
-
- draw(x, -y, 1, -y);
- draw(1, -y, 1, y);
-
- g.setColor(Color.black);
- draw(x, -y, x, y);
- draw(x, y, 1, y);
-
- g.setColor(Color.gray);
- draw(x - 1, -y + 1, x - 1, y - 1);
- draw(x - 1, y - 1, 2, y - 1);
- }
- }
-
-
- class VerticalSliderThumbLeft extends VerticalSliderThumb
- {
- void draw(Graphics g, SliderTick t)
- {
- super.initDraw(g, t);
-
- int a = x / 4;
-
- draw(x, -y, a, -y);
- draw(a, -y, 1, 0);
-
- g.setColor(Color.black);
- draw(1, 0, a, y);
- draw(a, y, x, y);
- draw(x, y, x, -y);
-
- g.setColor(Color.gray);
- draw(2, 0, a, y - 1);
- draw(a, y - 1, x - 1, y - 1);
- draw(x - 1, y - 1, x - 1, -y + 1);
- }
- }
-
-
- class VerticalSliderThumbRight extends VerticalSliderThumb
- {
- void draw(Graphics g, SliderTick t)
- {
- super.initDraw(g, t);
-
- int a = width - VerticalSlider.TICK_WIDTH - 1 - 1 - x / 4;
-
- draw(1, y, 1, -y);
- draw(1, -y, a, -y);
- draw(a, -y, x - 1, 0);
-
- g.setColor(Color.black);
- draw(x, 0, a, y);
- draw(a, y, 1, y);
-
- g.setColor(Color.gray);
- draw(x - 1, 0, a, y - 1);
- draw(a, y - 1, 2, y - 1);
- }
- }
-
-