KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JSlider


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JSlider.java,v $
11    Revision 1.7 2004/04/30 13:20:43 bobintetley
12    Fix to unrealised peer preferred sizes, forwarding window events to
13    content panes and fix for mouse drag events.
14
15    Revision 1.6 2004/04/28 08:38:12 bobintetley
16    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
17
18    Revision 1.5 2004/04/23 10:03:17 bobintetley
19    MouseEvent BUTTON_MASK constants, JSlider.createStandardLabels(), FontMetrics
20    thread safety, Component/Toolkit.getFontMetrics() and MouseMotionAdapter
21
22    Revision 1.4 2004/03/30 14:22:27 bobintetley
23    Fix to Component min/max sizing code, fix to JPanel insets with titled
24    borders (all of which indirectly fix problems with BoxLayout). Addition
25    of ComponentAdapter
26
27    Revision 1.3 2004/01/26 12:02:49 bobintetley
28    JPanel titled border support
29
30    Revision 1.2 2004/01/26 08:11:00 bobintetley
31    Many bugfixes and addition of SwingSet
32
33    Revision 1.1 2004/01/13 08:43:50 bobintetley
34    Implemented JSlider and added it to the demos
35
36
37 */

38
39 package swingwtx.swing;
40
41 import swingwtx.swing.event.*;
42
43 import org.eclipse.swt.widgets.*;
44 import org.eclipse.swt.SWT;
45
46 import java.util.*;
47
48 public class JSlider extends JComponent implements SwingConstants {
49     
50     protected int orientation = SwingConstants.HORIZONTAL;
51     protected int min = 0;
52     protected int max = 100;
53     protected int value = 50;
54     protected int thumb = 10;
55     protected Dictionary labelTable = new Hashtable();
56     
57     protected Vector changeListeners = new Vector();
58     protected Scale ppeer = null;
59     
60     private int threadSafeInt = 0;
61     
62     public JSlider() { this(SwingConstants.HORIZONTAL, 0, 100, 50); }
63     public JSlider(int orientation) { this(orientation, 0, 100, 50); }
64     public JSlider(int min, int max) { this(SwingConstants.HORIZONTAL, min, max, (min + max) / 2); }
65     public JSlider(int min, int max, int value) { this(SwingConstants.HORIZONTAL, min, max, value); }
66     public JSlider(int orientation, int min, int max, int value) {
67         this.orientation = orientation;
68         this.min = min;
69         this.max = max;
70         this.value = value;
71     }
72     
73     public Hashtable createStandardLabels(int increment) {
74         Hashtable h = new Hashtable(10);
75         for (int i = 0; i <= max; i += increment)
76             h.put( new Integer JavaDoc(i), new Integer JavaDoc(i) );
77         return h;
78     }
79     
80     public Hashtable createStandardLabels(int increment, int start) {
81         Hashtable h = new Hashtable(10);
82         for (int i = start; i <= max; i += increment)
83             h.put( new Integer JavaDoc(i), new Integer JavaDoc(i) );
84         return h;
85     }
86     
87     public void addChangeListener(ChangeListener l) {
88         changeListeners.add(l);
89     }
90     public void removeChangeListener(ChangeListener l) {
91         changeListeners.remove(l);
92     }
93     
94     protected void fireStateChanged() {
95         for (int i = 0; i < changeListeners.size(); i++) {
96             ((ChangeListener) changeListeners.get(i)).stateChanged(new ChangeEvent(this));
97         }
98     }
99     
100     public int getValue() {
101         if (!SwingWTUtils.isSWTControlAvailable(ppeer))
102             return value;
103         SwingUtilities.invokeSync(new Runnable JavaDoc() {
104             public void run() {
105                 threadSafeInt = ppeer.getSelection();
106             }
107         });
108         return threadSafeInt;
109     }
110     
111     public void setValue(final int value) {
112         this.value = value;
113         if (SwingWTUtils.isSWTControlAvailable(ppeer))
114             SwingUtilities.invokeSync(new Runnable JavaDoc() {
115                 public void run() {
116                     ppeer.setSelection(value);
117                 }
118             });
119     }
120     
121     public int getExtent() { return thumb; }
122     public void setExtent(int extent) {
123         thumb = extent;
124         if (SwingWTUtils.isSWTControlAvailable(ppeer))
125             SwingUtilities.invokeSync(new Runnable JavaDoc() {
126                 public void run() {
127                     ppeer.setPageIncrement(thumb);
128                 }
129             });
130     }
131     public boolean getInverted() { return false; }
132     public boolean isInverted() { return false; }
133     public void setInverted(boolean b) {}
134     public int getMajorTickSpacing() { return getExtent(); }
135     public void setMajorTickSpacing(int spacing) { setExtent(spacing); }
136     public int getMinorTickSpacing() { return getExtent() / 2; }
137     public void setMinorTickSpacing(int spacing) { }
138     public boolean getSnapToTicks() { return true; }
139     public boolean getSnapToValue() { return true; }
140     public boolean isSnapToTicks() { return true; }
141     public boolean isSnapToValue() { return true; }
142     public void setSnapToTicks(boolean b) {}
143     public void setSnapToValue(boolean b) {}
144     public boolean getPaintTicks() { return true; }
145     public boolean isPaintTicks() { return true; }
146     public void setPaintTicks(boolean b) {}
147     public boolean getPaintTrack() { return true; }
148     public boolean isPaintTrack() { return true; }
149     public void setPaintTrack(boolean b) {}
150     public boolean getPaintLabels() { return true; }
151     public boolean isPaintLabels() { return true; }
152     public void setPaintLabels(boolean b) {}
153     public Dictionary getLabelTable() { return labelTable; }
154     public void setLabelTable(Dictionary table) { labelTable = table; }
155     
156     /** Overriden to calculate non-realised
157      * preferred size.
158      */

159     protected swingwt.awt.Dimension calculatePreferredSize() {
160         swingwt.awt.Dimension size = new swingwt.awt.Dimension(200, 25);
161         setSize(size);
162         return size;
163     }
164     
165     /**
166      * Once a parent component receives an "add" call for a child, this being
167      * the child, this should be called to tell us to instantiate the peer
168      * and load in any cached properties.
169      */

170     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
171         descendantHasPeer = true;
172         ppeer = new Scale(parent.getComposite(),
173             (border instanceof swingwtx.swing.border.EmptyBorder ? SWT.NONE : SWT.BORDER ) |
174             (orientation == SwingConstants.HORIZONTAL ? SWT.HORIZONTAL : SWT.VERTICAL)
175             );
176         ppeer.setMaximum(max);
177         ppeer.setMinimum(min);
178         ppeer.setPageIncrement(thumb);
179         ppeer.setSelection(value);
180         
181         // It's a bit small by default
182
if (orientation == SwingConstants.HORIZONTAL && pPrefSize == null)
183             setPreferredSize(new swingwt.awt.Dimension(200, 20));
184         if (orientation == SwingConstants.VERTICAL && pPrefSize == null)
185             setPreferredSize(new swingwt.awt.Dimension(20, 200));
186         
187         // Events for slider being dragged
188
ppeer.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
189             public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
190                 fireStateChanged();
191             }
192         });
193         
194         peer = ppeer;
195         this.parent = parent;
196     }
197     
198 }
199
Popular Tags