View Javadoc

1   package org.wcb.common.component;
2   
3   import javax.swing.*;
4   import java.util.Hashtable;
5   import java.util.Calendar;
6   
7   /***
8    * Copyright (C) 1999  Walter Bogaardt
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
23   *
24   * Project: Alice X10 Home Automation
25   * Filename:  $Id: JTimeSlider.java,v 1.2 2004/02/06 20:06:36 wbogaardt Exp $<BR>
26   * Abstract: Used to display a slide bar with times.
27   *
28   * $Log: JTimeSlider.java,v $
29   * Revision 1.2  2004/02/06 20:06:36  wbogaardt
30   * replaced ampm drop boxes with time buttons which launch a time panel move menu items around on main panel
31   *
32   * Revision 1.1  2004/02/05 14:31:12  wbogaardt
33   * adding time slider
34   *
35   */
36  public class JTimeSlider extends JSlider{
37  
38     private static JLabel[] labels = {new JLabel("12a"), new JLabel("2"),
39                             new JLabel("4"), new JLabel("6"),
40                             new JLabel("8"), new JLabel("10"),
41                             new JLabel("12p"), new JLabel("2"),
42                             new JLabel("4"), new JLabel("6"),
43                             new JLabel("8"), new JLabel("10"), new JLabel("12a")
44                             };
45  
46      /***
47       * This class takes a calendar and set the slide
48       * bar to the specified calendar time.
49       * @param cHourTime The time to set the pointer on the slide bar.
50       */
51      public JTimeSlider(Calendar cHourTime){
52          Hashtable table = new Hashtable();
53          for(int i = 0, loc  = 0; i < labels.length; i++, loc += 2){
54               table.put(new Integer(valuePercentOfSlider(loc)), labels[i]);
55          }
56          setLabelTable(table);
57          setValue(this.valuePercentOfSlider(cHourTime.get(Calendar.HOUR_OF_DAY)));
58          setPaintLabels(true);
59          setMajorTickSpacing(8);
60          setMinorTickSpacing(2);
61          setPaintTicks(true);
62      }
63  
64      /***
65       * Calculate from the hour as it translate to position on the slide bar.
66       * @param iHour The hour time
67       * @return the position of the slidebar
68       */
69      private int valuePercentOfSlider(int iHour){
70          Double dVal = new Double(iHour);
71          double dvalue = dVal.doubleValue();
72          double valueDecimal = dvalue / 24;
73          Double result = new Double(valueDecimal * 100);
74          return result.intValue();
75      }
76  
77      /***
78       * Calculate based on the position of the slide bar as what it means in hours and
79       * minutes.
80       * @return a string of HH:mm so that it can display as text.
81       */
82      public String getCalenderOfSlider(){
83          Double dSlider = new Double(getValue());
84          double dslide = dSlider.doubleValue();
85          double slidepercent = (dslide/100);
86          Double result = new Double(slidepercent * 24);
87  
88          int iHour = result.intValue();
89          double dMin = (result.doubleValue() - iHour)  * 60;
90          Double doubleminutes = new Double(dMin);
91          int iMinute = doubleminutes.intValue();
92          return iHour + ":" + iMinute;
93      }
94  }