home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 12.4 KB | 492 lines |
- package symantec.itools.awt;
-
-
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Color;
-
-
- /**
- * A VerticalSlider component. 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.Slider
- * @see symantec.itools.awt.HorizontalSlider
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- // 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
-
- public class VerticalSlider
- extends Slider
- {
- private static final int BORDER_X = 10;
- private static final int BORDER_Y = 15;
- /**
- * Length of the gauge ticks in pixels.
- */
- protected static final int TICK_WIDTH = 4;
-
- private VerticalSliderTick tick[];
- private VerticalSliderThumb thumb;
-
- /**
- * Constructs a default VerticalSlider.
- */
- /**
- * 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();
- this.style = TICK_BOTH;
- this.min = 1;
- this.max = 10;
- this.freq = 1;
-
- prevPos =
- curPos = 0;
-
- width = 175;
- height = 50;
-
- showBorder = true;
-
- tick = null;
- }
-
- /**
- * Sets the current slider tick mark style.
- * @see #getTickStyle
- * @see Slider#TICK_LEFT
- * @see Slider#TICK_RIGHT
- * @see Slider#TICK_BOTH
- */
- public void setTickStyle(int style)
- {
- if (this.style != style)
- {
- this.style = style;
-
- switch (style)
- {
- case TICK_LEFT :
- thumb = new VerticalSliderThumbLeft();
- break;
-
- case TICK_RIGHT :
- thumb = new VerticalSliderThumbRight();
- break;
-
- default :
- thumb = new VerticalSliderThumbBoth();
- break;
- }
-
- invalidate();
- }
- }
-
- /**
- * Returns the current slider tick mark style.
- * @see #setTickStyle
- * @see Slider#TICK_LEFT
- * @see Slider#TICK_RIGHT
- * @see Slider#TICK_BOTH
- */
- public int getTickStyle()
- {
- return style;
- }
-
- /**
- * 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.
- *
- * @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 java.awt.Component#mouseUp
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseDown(Event e, int x, int y)
- {
- moveThumb(y, true);
-
- return true;
- }
-
- /**
- * Processes MOUSE_DRAG events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DRAG
- * event. These events occur when the mouse is moved around inside this
- * component while the button is pressed.
- *
- * @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 java.awt.Component#mouseMove
- * @see java.awt.Component#handleEvent
- */
- public boolean mouseDrag(Event e, int x, int y)
- {
- moveThumb(y, false);
-
- return true;
- }
-
- private void do_reshape(int w, int h)
- {
- int hb = BORDER_X;
- int vb = BORDER_Y;
-
- if (w < hb)
- hb = w / 4;
-
- if (h < vb)
- vb = h / 4;
-
- int x0 = hb;
- int x1 = w - hb;
- int y0 = vb;
- int y1 = h - vb;
-
- if (x0 == 0)
- x0 = 1;
-
- if (x1 == 0)
- x1 = 1;
-
- if (y0 == 0)
- y0 = 1;
-
- if (y1 == 0)
- y1 = 1;
-
- int n = (max - min) / freq + 1;
-
- tick = new VerticalSliderTick[n];
-
- int vs = (y1 - y0) / (n - 1), ch;
-
- for (int i = 0; i < n; ++i) {
- ch = i * vs;
- tick[i] = new VerticalSliderTick(x0, x1, y0 + ch, ch);
- }
-
- thumb.resize(x1 - x0 - TICK_WIDTH - 1, vs / 2);
- }
-
- /**
- * Moves and/or resizes this component.
- * This is a standard Java AWT method which gets called to move and/or
- * resize this component. Components that are in containers with layout
- * managers should not call this method, but rely on the layout manager
- * instead.
- *
- * @param x horizontal position in the parent's coordinate space
- * @param y vertical position in the parent's coordinate space
- * @param width the new width
- * @param height the new height
- */
- public void reshape(int x, int y, int w, int h)
- {
- width = w;
- height = h;
-
- do_reshape(w, h);
-
- super.reshape(x, y, w, h);
- }
-
- /**
- * This routine updates the thumb position, paints the VerticalSlider, and
- * posts a new action event, as needed. If the thumb position has
- * changed or the forcePost parameter is true the component will be painted
- * and an action event posted.
- * @param pos the new thumb position
- * @param forcePost true forces a repaint and posting of an action message
- * even if the thumb position hasn't changed
- */
- protected void doMove(int pos, boolean forcePost)
- {
- if (tick == null)
- {
- prevPos = curPos = pos;
- return;
- }
-
- if (pos >= tick.length)
- pos = tick.length - 1;
-
- if (pos != curPos || forcePost)
- {
- prevPos = curPos;
- curPos = pos;
- paint(getGraphics());
-
- postEvent(new Event(this, Event.ACTION_EVENT, new Integer(curPos * freq + min)));
- }
- }
-
- private void moveThumb(int y, boolean forcePost)
- {
- if (tick.length > 1)
- {
- int dist = tick[1].y - tick[0].y;
-
- if (dist == 0)
- return;
-
- int newPos = (y - tick[0].y) / dist;
-
- if (newPos < 0)
- newPos = 0;
-
- if (((y - tick[0].y) % dist) > (dist / 2))
- ++newPos;
-
- doMove(newPos, forcePost);
- }
- }
-
- /**
- * 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)
- {
- if (tick.length == 0)
- return;
-
- VerticalSliderTick t;
-
- g.clipRect(0, 0, width, height);
-
- 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;
-
- t = tick[i];
-
- if (style == TICK_LEFT || style == TICK_BOTH)
- g.drawLine(t.x0 + (end ? 0 : 1), t.y, t.x0 + TICK_WIDTH, t.y);
-
- if (style == TICK_RIGHT || style == TICK_BOTH)
- g.drawLine(t.x1 - TICK_WIDTH, t.y, t.x1 - (end ? 0 : 1), t.y);
- }
-
- t = tick[0];
-
- x = (t.x1 + t.x0) / 2;
- y0 = t.y - 5;
- y1 = tick[tick.length - 1].y + 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);
-
- g.clipRect(0, 0, width, height);
-
- thumb.draw(g, tick[curPos]);
-
- prevPos = curPos;
- }
-
- /**
- * Ensures that this component is laid out properly, as needed.
- * This is a standard Java AWT method which gets called by the AWT to
- * make sure this component and its subcomponents have a valid layout.
- * If this component was made invalid with a call to invalidate(), then
- * it is laid out again.
- *
- * @see java.awt.Component#invalidate
- */
- public void validate() {
- super.validate();
-
- do_reshape(width, height);
- }
- }
-
-
- class VerticalSliderTick
- {
- int x0, x1, y, v;
-
- VerticalSliderTick(int ix0, int ix1, int iy, int iv)
- {
- x0 = ix0;
- x1 = ix1;
- y = iy;
-
- v = iv;
- }
- }
-
-
- abstract class VerticalSliderThumb
- {
- protected int x, y, width, height;
- protected Graphics g;
- protected VerticalSliderTick t;
-
- VerticalSliderThumb()
- {
- }
-
- 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, VerticalSliderTick t);
-
-
- protected void draw(int x0, int y0, int x1, int y1)
- {
- g.drawLine(t.x0 + x0 + VerticalSlider.TICK_WIDTH + 1, t.y + y0,
- t.x0 + x1 + VerticalSlider.TICK_WIDTH + 1, t.y + y1);
- }
-
-
- protected void initDraw(Graphics g, VerticalSliderTick t)
- {
- this.g = g;
- this.t = t;
-
- g.setColor(Color.lightGray);
- g.fillRect(t.x0 + 2 + VerticalSlider.TICK_WIDTH + 1, t.y - y + 1, x - 2, height - 2);
-
- g.setColor(Color.white);
- }
-
-
- void clip(Graphics g, VerticalSliderTick t)
- {
- g.clipRect(t.x0, t.y - height / 2, width + 1, height + 1);
- }
- }
-
-
- class VerticalSliderThumbBoth extends VerticalSliderThumb
- {
- void draw(Graphics g, VerticalSliderTick 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, VerticalSliderTick 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, VerticalSliderTick 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);
- }
- }
-
-