1 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 () { 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 () { 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 134 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 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 |