1 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 (i), new Integer (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 (i), new Integer (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 () { 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 () { 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 () { 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 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 170 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 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 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 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 |