package brf.j2me.dynaworks.ui;

import brf.j2me.dynaworks.*;

public class DySlider implements UserWidget {

   /**
     range values.
    */
   private int min;
   private int max;
   private int r;
   /**
      position of the slider.
    */
   private int pos;
   /**
      bounding box of the control.
    */
   private int xl;
   private int yt;
   private int xr;
   private int yb;
   private int w;
   private int h;
    
   /**
      constructor.

@param x int - x coordinate of upper-left corner. @param y int - y coordinate of upper-left corner. @param w int - width of the control. @param h int - height of the control. */ public DySlider(int x, int y, int w, int h) { // ensure minimum dimension. if (w < 20) w = 20; if (h < 10) h = 10; // store the bounds and dimensions. xl = x; xr = x+w-1; yt = y; yb = y+h-1; this.w = w; this.h = h; } /** set the range for the slider.

@param min int - minimum value for slider. @param max int - maximum value for slider. @param pos int - start position for marker. */ public void setRange(int min, int max, int pos) { // store the range values. this.min = min; this.max = max; r = max - min + 1; // check the position against bounds. if (pos < min || pos > max) pos = min; // set marker position. this.pos = pos; } /** set the position of the slider. @param pos int - new position of slider. @return int - old position of slider. */ public int setPosition(int pos) { // anything changed at all? if (this.pos == pos) // no: return current position. return this.pos; // remember the last position. int lastPos = this.pos; // is the value inside range? if (pos >= min && pos <= max) { // store new position. this.pos = pos; //redraw update (lastPos); } // return old position. return lastPos; } /** get the current position. @return int - current position of slider. */ public int getPosition() { return pos; } /** check if point is inside the widget @param x int - x coordinate of check point. @param y int - y coordinate of check point. @return boolean - point inside control? */ public boolean contains(int x, int y) { // find out if the point lays within our bounds... return ( (x >= xl) && (x <= xr) && (y >= yt) && (y <= yb) ); } /** Does the control have the focus?

@return boolean - focus on this control? */ public boolean hasFocus() { return false; } /** control is losing the focus.

*/ public void loseFocus() { } /** Handle a "pen down" event. Return an ActionEvent if neccessary.

@param x int - x coordinate of hit point. @param y int - y coordinate of hit point. @return ActionEvent - event triggered by PenDown (or null). */ public ActionEvent handlePenDown(int x, int y) { // is this point inside the bounds? if (!contains (x, y)) return null; // get the position corresponding to the PenDown point. int newPos = ((x - xl) * r / w) + min; // changed position? if (newPos == pos) return null; // set new position and trigger event. setPosition (newPos); return new ActionEvent (this, ActionEvent.VALUE_CHANGED); } /** Handle a "pen move" event. Return an ActionEvent if neccessary.

@param x int - x coordinate of pen point. @param y int - y coordinate of pen point. @return ActionEvent - event triggered by PenMove (or null). */ public ActionEvent handlePenMove(int x, int y) { // this method has the same logic as PenDown. return handlePenDown (x, y); } /** Handle a "pen up" event. Return an ActionEvent if neccessary.

@param x int - x coordinate of pen point. @param y int - y coordinate of pen point. @return ActionEvent - event triggered by PenUp (or null). */ public ActionEvent handlePenUp(int x, int y) { // no event processing. return null; } /** Handle a "keyDown" event. Return an ActionEvent if neccessary.

@param key int - key send to the control. @return ActionEvent - event triggered by keyDown (or null). */ public ActionEvent handleKeyDown(int keyCode) { // no event processing. return null; } /** Paint the user control. */ public void paint() { // get a graphics object. Graphics g = Environment.getGraphics (); // set the draw region. g.setDrawRegion (xl, yt, w, h); // erase background. g.drawRectangle (xl, yt, w, h, Graphics.ERASE, 0); // draw tick line. g.drawLine (xl+6, yt+1, xr-6, yt+1, Graphics.PLAIN); g.drawLine (xl+6, yt+2, xr-6, yt+2, Graphics.PLAIN); // draw position marker. int xs = ((pos-min) * (w - 13)) / r + (xl + 6); g.drawLine (xs-5, yb-1, xs, yt+4, Graphics.PLAIN); g.drawLine (xs, yt+4, xs+5, yb-1, Graphics.PLAIN); g.drawLine (xs+5, yb-1, xs-5, yb-1, Graphics.PLAIN); // unset draw region. g.resetDrawRegion (); } /** Update the visual representation. */ public void update(int lastPos) { // get a graphics object. Graphics g = Environment.getGraphics (); // set the draw region. g.setDrawRegion (xl, yt, w, h); // erase background. g.drawRectangle (xl, yt, w, h, Graphics.ERASE, 0); // draw new position int xs = ((pos-min) * (w - 13)) / r + (xl + 6); g.drawLine (xs-5, yb-1, xs, yt+4, Graphics.PLAIN); g.drawLine (xs, yt+4, xs+5, yb-1, Graphics.PLAIN); g.drawLine (xs+5, yb-1, xs-5, yb-1, Graphics.PLAIN); // unset draw region. g.resetDrawRegion (); } }