litesoft
@ HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
package org.litesoft.GWT.client.widgets.range; import com.google.gwt.event.dom.client.*; import com.google.gwt.resources.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import java.util.*; public class SliderEmulated extends FocusPanel implements Slider { private int mMin, mStep, mMax; /** * The change listeners. */ private ArrayList<ChangeHandler> changeHandlers = null; /** * The current value. */ private double curValue; /** * The knob that slides across the line. */ private Image knobImage; /** * The know image resources */ private ImageResource knobNormal; private ImageResource knobDisabled; private ImageResource knobSliding; /** * The line that the knob moves over. */ private Element lineElement; /** * The offset between the edge of the shell and the line. */ private int lineLeftOffset = 0; /** * The maximum slider value. */ private double maxValue; /** * The minimum slider value. */ private double minValue; /** * A bit indicating whether or not we are currently sliding the * slider bar due * to mouse events. */ private boolean slidingMouse = false; /** * A bit indicating whether or not the slider is enabled */ private boolean enabled = true; /** * The size of the increments between knob positions. */ private double stepSize; /** * Constructor */ public SliderEmulated( int pMin, int pStep, int pMax ) { mMin = pMin; mStep = pStep; mMax = pMax; // Create the outer shell DOM.setStyleAttribute( getElement(), "position", "relative" ); // Create the line lineElement = DOM.createDiv(); DOM.appendChild( getElement(), lineElement ); DOM.setStyleAttribute( lineElement, "position", "absolute" ); DOM.setStyleAttribute( lineElement, "border", "1px solid black" ); DOM.setStyleAttribute( lineElement, "height", "4px" ); DOM.setStyleAttribute( lineElement, "width", "90%" ); DOM.setStyleAttribute( lineElement, "top", "8px" ); DOM.setStyleAttribute( lineElement, "overflow", "hidden" ); sinkEvents( Event.MOUSEEVENTS | Event.FOCUSEVENTS ); } @Override public Widget getWidget() { return this; } public int getMin() { return mMin; } public int getStep() { return mStep; } public int getMax() { return mMax; } public void setKnobNormal( ImageResource knobNormal ) { this.knobNormal = knobNormal; // Create the knob if ( knobImage == null ) { knobImage = new Image( knobNormal ); Element knobElement = knobImage.getElement(); DOM.appendChild( getElement(), knobElement ); DOM.setStyleAttribute( knobElement, "position", "absolute" ); DOM.setStyleAttribute( knobElement, "cursor", "pointer" ); // Initialise our size setSize( "100%", knobImage.getHeight() + "px" ); } } public void setKnobDisabled( ImageResource knobDisabled ) { this.knobDisabled = knobDisabled; } public void setKnobSliding( ImageResource knobSliding ) { this.knobSliding = knobSliding; } /** * Add a change listener to this SliderBar. * * @param listener the listener to add */ public void addChangeHandler( ChangeHandler listener ) { if ( changeHandlers == null ) { changeHandlers = new ArrayList<ChangeHandler>(); } changeHandlers.add( listener ); } /** * Return the current value. * * @return the current value */ public double getCurrentValue() { return curValue; } /** * Return the max value. * * @return the max value */ public double getMaxValue() { return maxValue; } /** * Return the minimum value. * * @return the minimum value */ public double getMinValue() { return minValue; } /** * Return the step size. * * @return the step size */ public double getStepSize() { return stepSize; } /** * Return the total range between the minimum and maximum values. * * @return the total range */ public double getTotalRange() { if ( minValue > maxValue ) { return 0; } else { return maxValue - minValue; } } /** * @return Gets whether this widget is enabled */ public boolean isEnabled() { return enabled; } /** * Listen for events that will move the knob. * * @param event the event that occurred */ @Override public void onBrowserEvent( Event event ) { super.onBrowserEvent( event ); if ( enabled ) { switch ( DOM.eventGetType( event ) ) { // Unhighlight and cancel keyboard events case Event.ONBLUR: if ( slidingMouse ) { DOM.releaseCapture( getElement() ); slidingMouse = false; slideKnob( event ); stopSliding( true, true ); } unhighlight(); break; // Highlight on focus case Event.ONFOCUS: highlight(); break; // Mousewheel events case Event.ONMOUSEWHEEL: int velocityY = DOM.eventGetMouseWheelVelocityY( event ); DOM.eventPreventDefault( event ); if ( velocityY > 0 ) { shiftRight( 1 ); } else { shiftLeft( 1 ); } break; // Mouse Events case Event.ONMOUSEDOWN: setFocus( true ); slidingMouse = true; DOM.setCapture( getElement() ); startSliding( true, true ); DOM.eventPreventDefault( event ); slideKnob( event ); break; case Event.ONMOUSEUP: if ( slidingMouse ) { DOM.releaseCapture( getElement() ); slidingMouse = false; slideKnob( event ); stopSliding( true, true ); } break; case Event.ONMOUSEMOVE: if ( slidingMouse ) { slideKnob( event ); } break; } } } /** * This method is called when the dimensions of the parent element * change. * Subclasses should override this method as needed. * * @param width the new client width of the element * @param height the new client height of the element */ public void onResize( int width, int height ) { // Center the line in the shell int lineWidth = DOM.getElementPropertyInt( lineElement, "offsetWidth" ); lineLeftOffset = (width / 2) - (lineWidth / 2); DOM.setStyleAttribute( lineElement, "left", lineLeftOffset + "px" ); // Draw the other components drawKnob(); } /** * Redraw the progress bar when something changes the layout. */ public void redraw() { if ( isAttached() ) { int width = DOM.getElementPropertyInt( getElement(), "clientWidth" ); int height = DOM.getElementPropertyInt( getElement(), "clientHeight" ); onResize( width, height ); } } /** * Remove a change listener from this SliderBar. * * @param listener the listener to remove */ public void removeChangeHandler( ChangeHandler listener ) { if ( changeHandlers != null ) { changeHandlers.remove( listener ); } } /** * Set the current value and fire the onValueChange event. * * @param curValue the current value */ public void setCurrentValue( double curValue ) { setCurrentValue( curValue, true ); } /** * Set the current value and optionally fire the onValueChange event. * * @param curValue the current value * @param fireEvent fire the onValue change event if true */ public void setCurrentValue( double curValue, boolean fireEvent ) { // Confine the value to the range this.curValue = Math.max( minValue, Math.min( maxValue, curValue ) ); double remainder = (this.curValue - minValue) % stepSize; this.curValue -= remainder; // Go to next step if more than halfway there if ( (remainder > (stepSize / 2)) && ((this.curValue + stepSize) <= maxValue) ) { this.curValue += stepSize; } // Redraw the knob drawKnob(); // Fire the onValueChange event if ( fireEvent && (changeHandlers != null) ) { for ( ChangeHandler changeHandler : changeHandlers ) { changeHandler.onChange( null ); } } } /** * Sets whether this widget is enabled. * * @param enabled true to enable the widget, false to disable it */ public void setEnabled( boolean enabled ) { this.enabled = enabled; if ( enabled ) { knobImage.setResource( knobNormal ); } else { knobImage.setResource( knobDisabled ); } redraw(); } /** * Set the max value. * * @param maxValue the current value */ public void setMaxValue( double maxValue ) { this.maxValue = maxValue; resetCurrentValue(); } /** * Set the minimum value. * * @param minValue the current value */ public void setMinValue( double minValue ) { this.minValue = minValue; resetCurrentValue(); } /** * Set the step size. * * @param stepSize the current value */ public void setStepSize( double stepSize ) { this.stepSize = stepSize; resetCurrentValue(); } /** * Shift to the left (smaller value). * * @param numSteps the number of steps to shift */ public void shiftLeft( int numSteps ) { setCurrentValue( getCurrentValue() - numSteps * stepSize ); } /** * Shift to the right (greater value). * * @param numSteps the number of steps to shift */ public void shiftRight( int numSteps ) { setCurrentValue( getCurrentValue() + numSteps * stepSize ); } /** * Get the percentage of the knob's position relative to the size of * the line. * The return value will be between 0.0 and 1.0. * * @return the current percent complete */ protected double getKnobPercent() { // If we have no range if ( maxValue <= minValue ) { return 0; } // Calculate the relative progress double percent = (curValue - minValue) / (maxValue - minValue); return Math.max( 0.0, Math.min( 1.0, percent ) ); } /** * This method is called immediately after a widget becomes attached * to the * browser's document. */ @Override protected void onLoad() { // Reset the position attribute of the parent element DOM.setStyleAttribute( getElement(), "position", "relative" ); redraw(); } @Override protected void onUnload() { } /** * Draw the knob where it is supposed to be relative to the line. */ private void drawKnob() { // Abort if not attached if ( !isAttached() ) { return; } // Move the knob to the correct position Element knobElement = knobImage.getElement(); int lineWidth = DOM.getElementPropertyInt( lineElement, "offsetWidth" ); int knobWidth = DOM.getElementPropertyInt( knobElement, "offsetWidth" ); int knobLeftOffset = (int) (lineLeftOffset + (getKnobPercent() * lineWidth) - (knobWidth / 2)); knobLeftOffset = Math.min( knobLeftOffset, lineLeftOffset + lineWidth - (knobWidth / 2) - 1 ); DOM.setStyleAttribute( knobElement, "left", knobLeftOffset + "px" ); } /** * Highlight this widget. */ private void highlight() { String styleName = getStylePrimaryName(); DOM.setElementProperty( getElement(), "className", styleName + " " + styleName + "-focused" ); } /** * Reset the progress to constrain the progress to the current range * and * redraw the knob as needed. */ private void resetCurrentValue() { setCurrentValue( getCurrentValue() ); } /** * Slide the knob to a new location. * * @param event the mouse event */ private void slideKnob( Event event ) { int x = DOM.eventGetClientX( event ); if ( x > 0 ) { int lineWidth = DOM.getElementPropertyInt( lineElement, "offsetWidth" ); int lineLeft = DOM.getAbsoluteLeft( lineElement ); double percent = (double) (x - lineLeft) / lineWidth * 1.0; setCurrentValue( getTotalRange() * percent + minValue, true ); } } /** * Start sliding the knob. * * @param highlight true to change the style * @param fireEvent true to fire the event */ private void startSliding( boolean highlight, boolean fireEvent ) { if ( highlight ) { DOM.setStyleAttribute( lineElement, "backgroundColor", "#DDDDDD" ); knobImage.setResource( knobSliding ); } } /** * Stop sliding the knob. * * @param unhighlight true to change the style * @param fireEvent true to fire the event */ private void stopSliding( boolean unhighlight, boolean fireEvent ) { if ( unhighlight ) { DOM.setStyleAttribute( lineElement, "backgroundColor", "white" ); knobImage.setResource( knobNormal ); } } /** * Unhighlight this widget. */ private void unhighlight() { DOM.setElementProperty( getElement(), "className", getStylePrimaryName() ); } } |