package org.mwc.debrief.lite.gui.custom;

import java.util.Calendar;

import javax.swing.JSlider;

/**
 * An extension of JSlider to select a range of values using two thumb controls. The thumb controls
 * are used to select the lower and upper value of a range with predetermined minimum and maximum
 * values.
 * 
 * <p>
 * Note that RangeSlider makes use of the default BoundedRangeModel, which supports an inner range
 * defined by a value and an extent. The upper value returned by RangeSlider is simply the lower
 * value plus the extent.
 * </p>
 */
public class RangeSlider extends JSlider
{

  /**
   * 
   */
  private static final long serialVersionUID = 1L;

  /**
   * Constructs a RangeSlider with default minimum and maximum values of 0 and 100.
   */
  public RangeSlider()
  {
    initSlider();
  }

  /**
   * Constructs a RangeSlider with the specified default minimum and maximum values.
   */
  public RangeSlider(int min, int max)
  {
    super(min, max);
    initSlider();
  }

  /**
   * Constructs a RangeSlider with using a Calendar, storing the values divided by 1000
   * 
   * @param min
   * @param max
   */
  public RangeSlider(Calendar min, Calendar max)
  {
    super((int) (min.getTimeInMillis() / 1000L), (int) (max.getTimeInMillis()
        / 1000L), (int) ((min.getTimeInMillis() / 1000L) / 2 + (min
            .getTimeInMillis() / 1000L) / 2));
    initSlider();
  }

  /**
   * Initializes the slider by setting default properties.
   */
  private void initSlider()
  {
    setOrientation(HORIZONTAL);
    setValue(getMinimum());
    setUpperValue(getMaximum());
  }

  /**
   * Overrides the superclass method to install the UI delegate to draw two thumbs.
   */
  @Override
  public void updateUI()
  {
    setUI(new RangeSliderUI(this));
    // Update UI for slider labels. This must be called after updating the
    // UI of the slider. Refer to JSlider.updateUI().
    updateLabelUIs();
  }

  /**
   * Returns the lower value in the range.
   */
  @Override
  public int getValue()
  {
    return super.getValue();
  }

  /**
   * Sets the lower value in the range.
   */
  @Override
  public void setValue(int value)
  {
    int oldValue = getValue();
    if (oldValue == value)
    {
      return;
    }

    // Compute new value and extent to maintain upper value.
    int oldExtent = getExtent();
    int newValue = Math.min(Math.max(getMinimum(), value), oldValue
        + oldExtent);
    int newExtent = oldExtent + oldValue - newValue;

    // Set new value and extent, and fire a single change event.
    getModel().setRangeProperties(newValue, newExtent, getMinimum(),
        getMaximum(), getValueIsAdjusting());
  }

  /**
   * Returns the upper value in the range.
   */
  public int getUpperValue()
  {
    return getValue() + getExtent();
  }

  /**
   * Sets the upper value in the range.
   */
  public void setUpperValue(int value)
  {
    // Compute new extent.
    int lowerValue = getValue();
    int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum()
        - lowerValue);

    // Set extent to set upper value.
    setExtent(newExtent);
  }
}
@@ -66,35 +66,105 @@
 ar;%0A
-%0Aimport javax.swing.JSlider
+import java.util.GregorianCalendar;%0A%0Aimport javax.swing.JSlider;%0A%0Aimport junit.framework.TestCase
 ;%0A%0A/
@@ -667,407 +667,517 @@
 r%0A%7B%0A
-%0A  /**%0A   * %0A   */%0A  private static final long serialVersionUID = 1L;%0A%0A  /**%0A   * Constructs a RangeSlider with default minimum and maximum values of 0 and 100.%0A   */%0A  public RangeSlider()
+  %0A  public static class TestConversion extends TestCase%0A  %7B%0A    public void testConvert()%0A    %7B%0A      Calendar date = new GregorianCalendar();%0A      int millis = (int) (date.getTimeInMillis() / 1000L);%0A      Calendar newD = toDate(millis);
 %0A  
-%7B%0A
     in
-itS
+t s
 lider
-();%0A  %7D%0A%0A  /**%0A   * Constructs a RangeSlider with the specified default minimum and maximum values.%0A   */%0A  public RangeSlider(int min, int max)%0A  %7B%0A    super(min, max);%0A    initSlider();%0A  %7D
+Val = toInt(date);%0A      Calendar newD2 = toDate(sliderVal);%0A      assertEquals(%22conversion works%22, millis, sliderVal);%0A      assertEquals(%22conversion works%22, newD, newD2);%0A    %7D%0A  %7D%0A%0A  /**%0A   * %0A   */%0A  private static final long serialVersionUID = 1L;
 %0A%0A  
@@ -1379,101 +1379,165 @@
 per(
-(int) (min.getTimeInMillis() / 1000L), (int) (max.getTimeInMillis
+toInt(min), toInt(max),  toInt(min) + (toInt(max) - toInt(min))/2);%0A    initSlider
 ()
+;
 %0A  
+%7D%0A
   
+%0A
   
-  / 1000L),
+private static int toInt(Calendar date)%0A  %7B%0A    return
  (int) (
 (min
@@ -1532,20 +1532,20 @@
  (int) (
-(min
+date
 .getTime
@@ -1567,34 +1567,125 @@
 00L)
- / 2 + (min
+;%0A  %7D%0A  %0A  private static Calendar toDate(int val)
 %0A  
+%7B%0A
     
-      .g
+GregorianCalendar cal = new GregorianCalendar();%0A    cal.s
 etTi
@@ -1699,42 +1699,36 @@
 lis(
-) /
+val *
  1000L)
- / 2));%0A    initSlider()
+;%0A    return cal
 ;%0A  
@@ -1923,32 +1923,352 @@
 aximum());%0A  %7D%0A%0A
+  public Calendar getLowerDate()%0A  %7B%0A    return toDate(getValue());%0A  %7D%0A  %0A  public Calendar getUpperDate()%0A  %7B%0A    return toDate(getUpperValue());%0A  %7D%0A  %0A  public void setLowerDate(Calendar date)%0A  %7B%0A    setValue(toInt(date));%0A  %7D%0A  %0A  public void setUpperDate(Calendar date)%0A  %7B%0A    setUpperValue(toInt(date));%0A  %7D%0A  %0A
   /**%0A   * Overr
@@ -3500,29 +3500,30 @@
 e.%0A   */%0A  p
-ublic
+rivate
  void setUpp

Tidy time support