KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JScrollBar


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: JScrollBar.java,v $
11    Revision 1.6 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.5 2004/04/28 08:38:12 bobintetley
15    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
16
17    Revision 1.4 2004/02/02 12:36:37 bobintetley
18    Proper JScrollPane/ScrollBar implementation
19
20    Revision 1.3 2004/01/27 10:02:01 bobintetley
21    Proper JScrollBar implementation
22
23    Revision 1.2 2003/12/15 17:37:18 bobintetley
24    ComboBox model interfaces and support
25
26    Revision 1.1 2003/12/15 16:40:04 bobintetley
27    Core methods + skeleton JTableHeader/JScrollBar support
28
29 */

30
31 package swingwtx.swing;
32
33 import org.eclipse.swt.SWT;
34
35 import swingwt.awt.*;
36 import swingwt.awt.event.*;
37
38 import java.util.*;
39
40 public class JScrollBar extends JComponent implements Adjustable {
41     
42     protected org.eclipse.swt.widgets.Slider ppeer = null;
43     
44     public final static int VERTICAL = 0;
45     public final static int HORIZONTAL = 1;
46     
47     protected int orientation = VERTICAL;
48     protected int value = 0;
49     protected int extent = 100;
50     protected int blockIncrement = 10;
51     protected int increment = 5;
52     protected int min = 0;
53     protected int max = 100;
54     protected Vector adjustmentListeners = new Vector();
55
56     public JScrollBar() { this(VERTICAL); }
57     public JScrollBar(int orientation) { this(orientation, 0, 5, 0, 100); }
58     public JScrollBar(int orientation, int value, int extent, int min, int max) {
59         this.orientation = orientation;
60         this.value = value;
61         this.extent = extent;
62         this.min = min;
63         this.max = max;
64     }
65     
66         
67     public void addAdjustmentListener(AdjustmentListener l) {
68         adjustmentListeners.add(l);
69     }
70     public void removeAdjustmentListener(AdjustmentListener l) {
71         adjustmentListeners.remove(l);
72     }
73     
74     public int getOrientation() { return orientation; }
75     public void setOrientation(int orientation) {
76         this.orientation = orientation;
77     }
78     public int getUnitIncrement(int direction) { return increment; }
79     public void setUnitIncrement(int unitIncrement) { increment = unitIncrement; updateBar(); }
80     public int getBlockIncrement(int direction) { return blockIncrement; }
81     public void setBlockIncrement(int blockIncrement) { this.blockIncrement = blockIncrement; updateBar();}
82     public int getUnitIncrement() { return increment; }
83     public int getBlockIncrement() { return blockIncrement; }
84     
85     private int retval = 0;
86     public int getValue() {
87         if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return value;
88         SwingUtilities.invokeSync(new Runnable JavaDoc() {
89             public void run() {
90                 retval = ppeer.getSelection();
91             }
92         });
93         return retval;
94     }
95     
96     public void setValue(int value) { this.value = value; updateBar(); }
97     public int getVisibleAmount() { return extent; }
98     public void setVisibleAmount(int extent) { this.extent = extent; updateBar(); }
99     public int getMinimum() { return min; }
100     public void setMinimum(int minimum) {}
101     public int getMaximum() { return max; }
102     public void setMaximum(int maximum) { max = maximum; updateBar(); }
103     public boolean getValueIsAdjusting() { return false;}
104     public void setValueIsAdjusting(boolean b) { }
105     
106     public void setValues(int newValue, int newExtent, int newMin, int newMax) {
107         extent = newExtent;
108         value = newValue;
109         min = newMin;
110         max = newMax;
111         updateBar();
112     }
113     
114     protected void updateBar() {
115         SwingUtilities.invokeSync( new Runnable JavaDoc() {
116             public void run() {
117                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
118                     ppeer.setIncrement(increment);
119                     ppeer.setPageIncrement(blockIncrement);
120                     ppeer.setThumb(increment);
121                     ppeer.setMaximum(max);
122                     ppeer.setMinimum(min);
123                     ppeer.setSelection(value);
124                 }
125             }
126         });
127     }
128
129     /**
130      * Once a parent component receives an "add" call for a child, this being
131      * the child, this should be called to tell us to instantiate the peer
132      * and load in any cached properties.
133      */

134     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
135         descendantHasPeer = true;
136         ppeer = new org.eclipse.swt.widgets.Slider(parent.getComposite(), orientation == HORIZONTAL ? SWT.HORIZONTAL : SWT.VERTICAL);
137         peer = ppeer;
138         this.parent = parent;
139         updateBar();
140         registerBarEvents();
141     }
142     
143     protected void registerBarEvents() {
144         final JScrollBar me = this;
145         ppeer.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
146             public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
147                 AdjustmentEvent ev = new AdjustmentEvent(me, 0, AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, ppeer.getSelection());
148                 processAdjustmentEvent(ev);
149             }
150         });
151     }
152     
153     protected void processAdjustmentEvent(AdjustmentEvent e) {
154         for (int i = 0; i < adjustmentListeners.size(); i++) {
155             ((AdjustmentListener) adjustmentListeners.get(i)).adjustmentValueChanged(e);
156         }
157     }
158     
159 }
160
Popular Tags