KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > LogarithmicTimeJSlider


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing;
18
19 import java.text.DecimalFormat JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import javax.swing.*;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25
26 /**
27  * This extension to the LogarithmicJSlider alters the major tick
28  * labels to label logarithmic time scales from milleseconds to days.
29  *
30  * @author Greg Hinkle (ghinkle@users.sourceforge.net), Apr 12, 2004
31  * @version $Revision: 480 $($Author: ghinkl $ / $Date: 2004-10-05 01:17:41 -0400 (Tue, 05 Oct 2004) $)
32  */

33 public class LogarithmicTimeJSlider extends LogarithmicJSlider {
34
35
36     public LogarithmicTimeJSlider(int orientation) {
37         super(orientation);
38     }
39
40     public LogarithmicTimeJSlider(int min, int max) {
41         super(min, max);
42     }
43
44     public LogarithmicTimeJSlider(int min, int max, int value) {
45         super(min, max, value);
46     }
47
48     public LogarithmicTimeJSlider(int orientation, int min, int max, int value) {
49         super(orientation, min, max, value);
50     }
51
52     public LogarithmicTimeJSlider(BoundedRangeModel brm) {
53         super(brm);
54     }
55
56     public LogarithmicTimeJSlider() {
57         super();
58     }
59
60     protected static DecimalFormat JavaDoc format = new DecimalFormat JavaDoc("#.#");
61
62     protected void createLabels(Hashtable JavaDoc table, int increment, int start) {
63         for (int labelIndex = start; labelIndex <= getMaximum(); labelIndex *= increment) {
64             String JavaDoc label = formatMilleseconds(labelIndex);
65             table.put(new Integer JavaDoc(labelIndex), new LabelUIResource(label, JLabel.CENTER));
66         }
67     }
68
69     public String JavaDoc getTime() {
70         return formatMilleseconds(getValue());
71     }
72
73     public String JavaDoc formatMilleseconds(int labelIndex) {
74         String JavaDoc label;
75         if (labelIndex >= (1000 * 60 * 60 * 24)) {
76             label = format.format(labelIndex / (double)(1000 * 60 * 60 * 24)) + " days";
77         } else if (labelIndex >= (1000 * 60 * 60)) {
78             label = format.format(labelIndex / (double)(1000 * 60 * 60)) + " hours";
79         } else if (labelIndex >= (double)(1000 * 60)) {
80             label = format.format(labelIndex / (double)(1000 * 60)) + " mins";
81         } else if (labelIndex >= 1000) {
82             label = format.format(labelIndex / (double)1000) + " secs";
83         } else {
84             label = labelIndex + " ms";
85         }
86         return label;
87     }
88
89
90     /**
91      * Just for testing
92      * @param args
93      */

94     public static void main(String JavaDoc[] args) {
95         JFrame frame = new JFrame();
96         frame.setSize(408, 408);
97
98         final LogarithmicTimeJSlider slider = new LogarithmicTimeJSlider(1000, 1000 * 60 * 60 * 24 * 7, 1000);
99         final JLabel label = new JLabel();
100
101         slider.setPaintTicks(true);
102         slider.setPaintLabels(true);
103         slider.setMajorTickSpacing(10);
104         slider.setMinorTickSpacing(10);
105
106         slider.addChangeListener(new ChangeListener JavaDoc() {
107             public void stateChanged(ChangeEvent JavaDoc e) {
108                 System.out.println("Value is now: " + slider.getValue());
109
110
111                 label.setText("Current value is: " + slider.formatMilleseconds(slider.getValue()));
112
113             }
114         });
115
116         frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
117         frame.getContentPane().add(slider);
118         frame.getContentPane().add(label);
119         frame.setVisible(true);
120
121         LogSliderUI ui = (LogSliderUI) slider.getUI();
122         for (int i = 10; i <= 100000; i *= 10) {
123             System.out.println("I: " + i + " xPos: " + ui.xPositionForValue(i) + " valueFor: " + ui.valueForXPosition(ui.xPositionForValue(i)));
124         }
125
126     }
127 }
128
Popular Tags