package com.borlander.rac353542.bislider.cdata;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import com.borlander.rac353542.bislider.BiSliderUIModel;
import com.borlander.rac353542.bislider.DefaultBiSliderUIModel;

/**
 * Single pack of objects required to instantiate BiSlider working with custom
 * object data model of <code>java.util.Date</code> values, representing
 * midnights in the current time zone.
 */
public class CalendarDateSuite {
    private static final DateFormat ourDateFormat = DateFormat.getDateInstance();

    public CalendarDateModel createDataModel(Date totalMin, Date totalMax, Date userMin, Date userMax) {
        CalendarDateModel result = new CalendarDateModel();
        result.setTotalRange(totalMin, totalMax);
        result.setUserMinimum(userMin);
        result.setUserMaximum(userMax);
        return result;
    }

    public BiSliderUIModel createUIModel() {
        DefaultBiSliderUIModel result = new DefaultBiSliderUIModel();
        result.setLabelProvider(createLabelProvider());
        // date values requires a lot of space
        result.setVerticalLabels(true);
        result.setLabelInsets(100);
        return result;
    }

    public DataObjectLabelProvider createLabelProvider() {
        return new DataObjectLabelProvider(CALENDAR_DATE) {
            public String getLabel(Object dataObject) {
                Date date = (Date) dataObject;
                return ourDateFormat.format(date);
            }
        };
    }

    public static class CalendarDateModel extends DataObjectDataModel {
        public CalendarDateModel() {
            super(CALENDAR_DATE);
        }

        public Date getTotalMinimumDate() {
            return (Date) getTotalMinimumObject();
        }

        public Date getTotalMaximumDate() {
            return (Date) getTotalMaximumObject();
        }

        public Date getUserMinimumDate() {
            return (Date) getUserMinimumObject();
        }

        public Date getUserMaximumDate() {
            return (Date) getUserMaximumObject();
        }

        public void setUserMaximum(Date maximum) {
            setUserMaximumObject(maximum);
        }

        public void setUserMinimum(Date minimum) {
            setUserMinimumObject(minimum);
        }

        public void setTotalRange(Date minimum, Date maximum) {
            setTotalObjectRange(minimum, maximum);
        }
    }

    /**
     * Mapper for date's. Each data object is some <code>java.util.Date</code>
     * representing a midnight in current locale's time zone.
     */
    public static DataObjectMapper CALENDAR_DATE = new DataObjectMapper() {

        private final Calendar myCalendar = Calendar.getInstance();

        public Object double2object(double value) {
            myCalendar.setTimeInMillis(Math.round(value));
//            setToMidnight();
            return myCalendar.getTime();
        }

        public double object2double(Object object) {
            myCalendar.setTime((Date) object);
//            setToMidnight();
            return myCalendar.getTimeInMillis();
        }

//        private void setToMidnight() {
//            myCalendar.set(Calendar.HOUR, 0);
//            myCalendar.set(Calendar.AM_PM, Calendar.AM);
//            myCalendar.set(Calendar.MINUTE, 0);
//            myCalendar.set(Calendar.SECOND, 0);
//            myCalendar.set(Calendar.MILLISECOND, 0);
//        }
        
        public double getPrecision() {
            return 1000 * 30;
//            return 1000 * 60 * 60 * 24;
        }
    };
}

@@ -125,64 +125,8 @@
 te;%0A
-import com.borlander.rac353542.bislider.BiSliderUIModel;
 %0Aimp
@@ -162,38 +162,17 @@
 islider.
-DefaultBiSliderUIModel
+*
 ;%0A%0A/**%0A 
@@ -2585,16 +2585,883 @@
 er() %7B%0A%0A
+%0A      private final Calendar myCalendar = Calendar.getInstance();%0A%0A      public Object double2object(double value) %7B%0A          myCalendar.setTimeInMillis(Math.round(value));%0A//          setToMidnight();%0A          return myCalendar.getTime();%0A      %7D%0A%0A      public double object2double(Object object) %7B%0A          myCalendar.setTime((Date) object);%0A//          setToMidnight();%0A          return myCalendar.getTimeInMillis();%0A      %7D%0A%0A//      private void setToMidnight() %7B%0A//          myCalendar.set(Calendar.HOUR, 0);%0A//          myCalendar.set(Calendar.AM_PM, Calendar.AM);%0A//          myCalendar.set(Calendar.MINUTE, 0);%0A//          myCalendar.set(Calendar.SECOND, 0);%0A//          myCalendar.set(Calendar.MILLISECOND, 0);%0A//      %7D%0A      %0A      public double getPrecision() %7B%0A          return 1000 * 30;%0A//          return 1000 * 60 * 60 * 24;%0A      %7D    %09%0A    %09%0A//
         
@@ -3516,25 +3516,29 @@
 Instance();%0A
-%0A
+//%0A//
         publ
@@ -3569,32 +3569,34 @@
 double value) %7B%0A
+//
             myCa
@@ -3661,32 +3661,34 @@
 etToMidnight();%0A
+//
             retu
@@ -3708,24 +3708,26 @@
 .getTime();%0A
+//
         %7D%0A%0A 
@@ -3716,33 +3716,37 @@
 ();%0A//        %7D%0A
-%0A
+//%0A//
         public d
@@ -3774,32 +3774,34 @@
 bject object) %7B%0A
+//
             myCa
@@ -3854,32 +3854,34 @@
 etToMidnight();%0A
+//
             retu
@@ -3909,24 +3909,26 @@
 InMillis();%0A
+//
         %7D%0A%0A/
@@ -3917,32 +3917,34 @@
 ();%0A//        %7D%0A
+//
 %0A//        priva
@@ -4235,32 +4235,34 @@
 0);%0A//        %7D%0A
+//
         %0A       
@@ -4250,24 +4250,26 @@
 %0A//        %0A
+//
         publ
@@ -4299,38 +4299,8 @@
 ) %7B%0A
-            return 1000 * 30;%0A
 //  
@@ -4333,24 +4333,26 @@
  * 60 * 24;%0A
+//
         %7D%0A  

Use my RN label provider