Changeset 26 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/gui
- Timestamp:
- 07/29/2017 02:22:14 PM (9 years ago)
- Location:
- trunk/src/scriptbuilder/gui/drawers
- Files:
-
- 3 edited
-
RangeSlider.java (modified) (10 diffs)
-
RangeSliderDemo.java (modified) (6 diffs)
-
RangeSliderUI.java (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/scriptbuilder/gui/drawers/RangeSlider.java
r25 r26 1 package scriptbuilder.gui.drawers; 1 2 2 3 import javax.swing.JSlider; 4 3 5 /* 4 6 The MIT License … … 23 25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 26 THE SOFTWARE. 25 */27 */ 26 28 27 29 /** … … 29 31 * The thumb controls are used to select the lower and upper value of a range 30 32 * with predetermined minimum and maximum values. 31 * 32 * <p>Note that RangeSlider makes use of the default BoundedRangeModel, which 33 * supports an inner range defined by a value and an extent. The upper value 33 * 34 * <p> 35 * Note that RangeSlider makes use of the default BoundedRangeModel, which 36 * supports an inner range defined by a value and an extent. The upper value 34 37 * returned by RangeSlider is simply the lower value plus the extent.</p> 35 38 */ 36 public class RangeSlider extends JSlider { 39 public class RangeSlider extends JSlider 40 { 37 41 38 42 /** 39 * Constructs a RangeSlider with default minimum and maximum values of 0 40 * and100.43 * Constructs a RangeSlider with default minimum and maximum values of 0 and 44 * 100. 41 45 */ 42 public RangeSlider() { 46 public RangeSlider() 47 { 43 48 initSlider(); 44 49 } 45 50 46 51 /** 47 * Constructs a RangeSlider with the specified default minimum and maximum 52 * Constructs a RangeSlider with the specified default minimum and maximum 48 53 * values. 49 54 */ 50 public RangeSlider(int min, int max) { 55 public RangeSlider(int min, int max) 56 { 51 57 super(min, max); 52 58 initSlider(); … … 56 62 * Initializes the slider by setting default properties. 57 63 */ 58 private void initSlider() { 64 private void initSlider() 65 { 59 66 setOrientation(HORIZONTAL); 60 67 } … … 65 72 */ 66 73 @Override 67 public void updateUI() { 74 public void updateUI() 75 { 68 76 setUI(new RangeSliderUI(this)); 69 77 // Update UI for slider labels. This must be called after updating the … … 76 84 */ 77 85 @Override 78 public int getValue() { 86 public int getValue() 87 { 79 88 return super.getValue(); 80 89 } … … 84 93 */ 85 94 @Override 86 public void setValue(int value) { 95 public void setValue(int value) 96 { 87 97 int oldValue = getValue(); 88 if (oldValue == value) { 98 if (oldValue == value) 99 { 89 100 return; 90 101 } … … 96 107 97 108 // Set new value and extent, and fire a single change event. 98 getModel().setRangeProperties(newValue, newExtent, getMinimum(), 99 getMaximum(), getValueIsAdjusting());109 getModel().setRangeProperties(newValue, newExtent, getMinimum(), 110 getMaximum(), getValueIsAdjusting()); 100 111 } 101 112 … … 103 114 * Returns the upper value in the range. 104 115 */ 105 public int getUpperValue() { 116 public int getUpperValue() 117 { 106 118 return getValue() + getExtent(); 107 119 } … … 110 122 * Sets the upper value in the range. 111 123 */ 112 public void setUpperValue(int value) { 124 public void setUpperValue(int value) 125 { 113 126 // Compute new extent. 114 127 int lowerValue = getValue(); 115 128 int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum() - lowerValue); 116 129 117 130 // Set extent to set upper value. 118 131 setExtent(newExtent); -
trunk/src/scriptbuilder/gui/drawers/RangeSliderDemo.java
r25 r26 1 package scriptbuilder.gui.drawers; 1 2 2 3 import java.awt.BorderLayout; … … 14 15 import javax.swing.event.ChangeEvent; 15 16 import javax.swing.event.ChangeListener; 17 16 18 /* 17 19 The MIT License … … 36 38 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 37 39 THE SOFTWARE. 38 */40 */ 39 41 40 42 /** 41 43 * Demo application panel to display a range slider. 42 44 */ 43 public class RangeSliderDemo extends JPanel { 45 public class RangeSliderDemo extends JPanel 46 { 44 47 45 48 private JLabel rangeSliderLabel1 = new JLabel(); … … 49 52 private RangeSlider rangeSlider = new RangeSlider(); 50 53 51 public RangeSliderDemo() { 54 public RangeSliderDemo() 55 { 52 56 setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); 53 57 setLayout(new GridBagLayout()); 54 58 55 59 rangeSliderLabel1.setText("Lower value:"); 56 60 rangeSliderLabel2.setText("Upper value:"); 57 61 rangeSliderValue1.setHorizontalAlignment(JLabel.LEFT); 58 62 rangeSliderValue2.setHorizontalAlignment(JLabel.LEFT); 59 63 60 64 rangeSlider.setPreferredSize(new Dimension(240, rangeSlider.getPreferredSize().height)); 61 65 rangeSlider.setMinimum(0); 62 66 rangeSlider.setMaximum(10); 63 67 64 68 // Add listener to update display. 65 rangeSlider.addChangeListener(new ChangeListener() { 66 public void stateChanged(ChangeEvent e) { 69 rangeSlider.addChangeListener(new ChangeListener() 70 { 71 public void stateChanged(ChangeEvent e) 72 { 67 73 RangeSlider slider = (RangeSlider) e.getSource(); 68 74 rangeSliderValue1.setText(String.valueOf(slider.getValue())); … … 72 78 73 79 add(rangeSliderLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 74 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));80 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0)); 75 81 add(rangeSliderValue1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 76 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 0), 0, 0));82 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 0), 0, 0)); 77 83 add(rangeSliderLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 78 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0));84 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 3, 3), 0, 0)); 79 85 add(rangeSliderValue2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 80 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 6, 0), 0, 0));81 add(rangeSlider , new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0,82 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));86 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 6, 0), 0, 0)); 87 add(rangeSlider, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, 88 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 83 89 } 84 85 public void display() { 90 91 public void display() 92 { 86 93 // Initialize values. 87 94 rangeSlider.setValue(3); 88 95 rangeSlider.setUpperValue(7); 89 96 90 97 // Initialize value display. 91 98 rangeSliderValue1.setText(String.valueOf(rangeSlider.getValue())); 92 99 rangeSliderValue2.setText(String.valueOf(rangeSlider.getUpperValue())); 93 100 94 101 // Create window frame. 95 102 JFrame frame = new JFrame(); … … 97 104 frame.setResizable(false); 98 105 frame.setTitle("Range Slider Demo"); 99 106 100 107 // Set window content and validate. 101 108 frame.getContentPane().setLayout(new BorderLayout()); 102 109 frame.getContentPane().add(this, BorderLayout.CENTER); 103 110 frame.pack(); 104 111 105 112 // Set window location and display. 106 113 frame.setLocationRelativeTo(null); 107 114 frame.setVisible(true); 108 115 } 109 116 110 117 /** 111 118 * Main application method. 119 * 112 120 * @param args String[] 113 121 */ 114 public static void main(String[] args) { 115 try { 122 public static void main(String[] args) 123 { 124 try 125 { 116 126 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 117 } catch (Exception ex) { 127 } 128 catch (Exception ex) 129 { 118 130 ex.printStackTrace(); 119 131 } 120 132 121 SwingUtilities.invokeLater(new Runnable() { 122 public void run() { 133 SwingUtilities.invokeLater(new Runnable() 134 { 135 public void run() 136 { 123 137 new RangeSliderDemo().display(); 124 138 } -
trunk/src/scriptbuilder/gui/drawers/RangeSliderUI.java
r25 r26 1 package scriptbuilder.gui.drawers; 2 1 3 import java.awt.Color; 2 4 import java.awt.Dimension; … … 6 8 import java.awt.RenderingHints; 7 9 import java.awt.Shape; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 8 12 import java.awt.event.MouseEvent; 9 13 import java.awt.geom.Ellipse2D; 10 14 11 15 import javax.swing.JComponent; 16 import javax.swing.JMenuItem; 17 import javax.swing.JPopupMenu; 12 18 import javax.swing.JSlider; 13 19 import javax.swing.SwingUtilities; … … 15 21 import javax.swing.event.ChangeListener; 16 22 import javax.swing.plaf.basic.BasicSliderUI; 23 17 24 /* 18 25 The MIT License … … 37 44 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 38 45 THE SOFTWARE. 39 */ 40 46 */ 41 47 /** 42 * UI delegate for the RangeSlider component. RangeSliderUI paints two thumbs,48 * UI delegate for the RangeSlider component. RangeSliderUI paints two thumbs, 43 49 * one for the lower value and one for the upper value. 44 50 */ 45 class RangeSliderUI extends BasicSliderUI { 46 47 /** Color of selected range. */ 51 class RangeSliderUI extends BasicSliderUI 52 { 53 54 /** 55 * Color of selected range. 56 */ 48 57 private Color rangeColor = Color.GREEN; 49 50 /** Location and size of thumb for upper value. */ 58 59 /** 60 * Location and size of thumb for upper value. 61 */ 51 62 private Rectangle upperThumbRect; 52 /** Indicator that determines whether upper thumb is selected. */ 63 /** 64 * Indicator that determines whether upper thumb is selected. 65 */ 53 66 private boolean upperThumbSelected; 54 55 /** Indicator that determines whether lower thumb is being dragged. */ 67 68 /** 69 * Indicator that determines whether lower thumb is being dragged. 70 */ 56 71 private transient boolean lowerDragging; 57 /** Indicator that determines whether upper thumb is being dragged. */ 72 /** 73 * Indicator that determines whether upper thumb is being dragged. 74 */ 58 75 private transient boolean upperDragging; 59 76 60 77 /** 61 78 * Constructs a RangeSliderUI for the specified slider component. 79 * 62 80 * @param b RangeSlider 63 81 */ 64 public RangeSliderUI(RangeSlider b) { 82 public RangeSliderUI(RangeSlider b) 83 { 65 84 super(b); 66 85 } 67 68 /** 69 * Installs this UI delegate on the specified component. 70 */ 71 @Override 72 public void installUI(JComponent c) { 86 87 /** 88 * Installs this UI delegate on the specified component. 89 */ 90 @Override 91 public void installUI(JComponent c) 92 { 73 93 upperThumbRect = new Rectangle(); 74 94 super.installUI(c); … … 79 99 */ 80 100 @Override 81 protected TrackListener createTrackListener(JSlider slider) { 101 protected TrackListener createTrackListener(JSlider slider) 102 { 82 103 return new RangeTrackListener(); 83 104 } … … 87 108 */ 88 109 @Override 89 protected ChangeListener createChangeListener(JSlider slider) { 110 protected ChangeListener createChangeListener(JSlider slider) 111 { 90 112 return new ChangeHandler(); 91 113 } 92 93 /** 94 * Updates the dimensions for both thumbs. 95 */ 96 @Override 97 protected void calculateThumbSize() { 114 115 /** 116 * Updates the dimensions for both thumbs. 117 */ 118 @Override 119 protected void calculateThumbSize() 120 { 98 121 // Call superclass method for lower thumb size. 99 122 super.calculateThumbSize(); 100 123 101 124 // Set upper thumb size. 102 125 upperThumbRect.setSize(thumbRect.width, thumbRect.height); 103 126 } 104 127 105 128 /** 106 129 * Updates the locations for both thumbs. 107 130 */ 108 131 @Override 109 protected void calculateThumbLocation() { 132 protected void calculateThumbLocation() 133 { 110 134 // Call superclass method for lower thumb location. 111 135 super.calculateThumbLocation(); 112 136 113 137 // Adjust upper value to snap to ticks if necessary. 114 if (slider.getSnapToTicks()) { 138 if (slider.getSnapToTicks()) 139 { 115 140 int upperValue = slider.getValue() + slider.getExtent(); 116 int snappedValue = upperValue; 141 int snappedValue = upperValue; 117 142 int majorTickSpacing = slider.getMajorTickSpacing(); 118 143 int minorTickSpacing = slider.getMinorTickSpacing(); 119 144 int tickSpacing = 0; 120 121 if (minorTickSpacing > 0) { 145 146 if (minorTickSpacing > 0) 147 { 122 148 tickSpacing = minorTickSpacing; 123 } else if (majorTickSpacing > 0) { 149 } 150 else if (majorTickSpacing > 0) 151 { 124 152 tickSpacing = majorTickSpacing; 125 153 } 126 154 127 if (tickSpacing != 0) { 155 if (tickSpacing != 0) 156 { 128 157 // If it's not on a tick, change the value 129 if ((upperValue - slider.getMinimum()) % tickSpacing != 0) { 130 float temp = (float)(upperValue - slider.getMinimum()) / (float)tickSpacing; 158 if ((upperValue - slider.getMinimum()) % tickSpacing != 0) 159 { 160 float temp = (float) (upperValue - slider.getMinimum()) / (float) tickSpacing; 131 161 int whichTick = Math.round(temp); 132 162 snappedValue = slider.getMinimum() + (whichTick * tickSpacing); 133 163 } 134 164 135 if (snappedValue != upperValue) { 165 if (snappedValue != upperValue) 166 { 136 167 slider.setExtent(snappedValue - slider.getValue()); 137 168 } 138 169 } 139 170 } 140 141 // Calculate upper thumb location. The thumb is centered over its 171 172 // Calculate upper thumb location. The thumb is centered over its 142 173 // value on the track. 143 if (slider.getOrientation() == JSlider.HORIZONTAL) { 174 if (slider.getOrientation() == JSlider.HORIZONTAL) 175 { 144 176 int upperPosition = xPositionForValue(slider.getValue() + slider.getExtent()); 145 177 upperThumbRect.x = upperPosition - (upperThumbRect.width / 2); 146 178 upperThumbRect.y = trackRect.y; 147 148 } else { 179 180 } 181 else 182 { 149 183 int upperPosition = yPositionForValue(slider.getValue() + slider.getExtent()); 150 184 upperThumbRect.x = trackRect.x; … … 152 186 } 153 187 } 154 188 155 189 /** 156 190 * Returns the size of a thumb. 157 191 */ 158 192 @Override 159 protected Dimension getThumbSize() { 193 protected Dimension getThumbSize() 194 { 160 195 return new Dimension(12, 12); 161 196 } 162 197 163 198 /** 164 * Paints the slider. The selected thumb is always painted on top of the199 * Paints the slider. The selected thumb is always painted on top of the 165 200 * other thumb. 166 201 */ 167 202 @Override 168 public void paint(Graphics g, JComponent c) { 203 public void paint(Graphics g, JComponent c) 204 { 169 205 super.paint(g, c); 170 206 171 207 Rectangle clipRect = g.getClipBounds(); 172 if (upperThumbSelected) { 208 if (upperThumbSelected) 209 { 173 210 // Paint lower thumb first, then upper thumb. 174 if (clipRect.intersects(thumbRect)) { 211 if (clipRect.intersects(thumbRect)) 212 { 175 213 paintLowerThumb(g); 176 214 } 177 if (clipRect.intersects(upperThumbRect)) { 215 if (clipRect.intersects(upperThumbRect)) 216 { 178 217 paintUpperThumb(g); 179 218 } 180 181 } else { 219 220 } 221 else 222 { 182 223 // Paint upper thumb first, then lower thumb. 183 if (clipRect.intersects(upperThumbRect)) { 224 if (clipRect.intersects(upperThumbRect)) 225 { 184 226 paintUpperThumb(g); 185 227 } 186 if (clipRect.intersects(thumbRect)) { 228 if (clipRect.intersects(thumbRect)) 229 { 187 230 paintLowerThumb(g); 188 231 } 189 232 } 190 233 } 191 234 192 235 /** 193 236 * Paints the track. 194 237 */ 195 238 @Override 196 public void paintTrack(Graphics g) { 239 public void paintTrack(Graphics g) 240 { 197 241 // Draw track. 198 242 super.paintTrack(g); 199 243 200 244 Rectangle trackBounds = trackRect; 201 202 if (slider.getOrientation() == JSlider.HORIZONTAL) { 245 246 if (slider.getOrientation() == JSlider.HORIZONTAL) 247 { 203 248 // Determine position of selected range by moving from the middle 204 249 // of one thumb to the other. 205 250 int lowerX = thumbRect.x + (thumbRect.width / 2); 206 251 int upperX = upperThumbRect.x + (upperThumbRect.width / 2); 207 252 208 253 // Determine track position. 209 254 int cy = (trackBounds.height / 2) - 2; … … 212 257 Color oldColor = g.getColor(); 213 258 g.translate(trackBounds.x, trackBounds.y + cy); 214 259 215 260 // Draw selected range. 216 261 g.setColor(rangeColor); 217 for (int y = 0; y <= 3; y++) { 262 for (int y = 0; y <= 3; y++) 263 { 218 264 g.drawLine(lowerX - trackBounds.x, y, upperX - trackBounds.x, y); 219 265 } … … 222 268 g.translate(-trackBounds.x, -(trackBounds.y + cy)); 223 269 g.setColor(oldColor); 224 225 } else { 270 271 } 272 else 273 { 226 274 // Determine position of selected range by moving from the middle 227 275 // of one thumb to the other. 228 276 int lowerY = thumbRect.x + (thumbRect.width / 2); 229 277 int upperY = upperThumbRect.x + (upperThumbRect.width / 2); 230 278 231 279 // Determine track position. 232 280 int cx = (trackBounds.width / 2) - 2; … … 238 286 // Draw selected range. 239 287 g.setColor(rangeColor); 240 for (int x = 0; x <= 3; x++) { 288 for (int x = 0; x <= 3; x++) 289 { 241 290 g.drawLine(x, lowerY - trackBounds.y, x, upperY - trackBounds.y); 242 291 } 243 292 244 293 // Restore position and color. 245 294 g.translate(-(trackBounds.x + cx), -trackBounds.y); … … 247 296 } 248 297 } 249 250 /** 251 * Overrides superclass method to do nothing. Thumb painting is handled298 299 /** 300 * Overrides superclass method to do nothing. Thumb painting is handled 252 301 * within the <code>paint()</code> method. 253 302 */ 254 303 @Override 255 public void paintThumb(Graphics g) { 304 public void paintThumb(Graphics g) 305 { 256 306 // Do nothing. 257 307 } … … 260 310 * Paints the thumb for the lower value using the specified graphics object. 261 311 */ 262 private void paintLowerThumb(Graphics g) { 312 private void paintLowerThumb(Graphics g) 313 { 263 314 Rectangle knobBounds = thumbRect; 264 315 int w = knobBounds.width; 265 int h = knobBounds.height; 266 316 int h = knobBounds.height; 317 267 318 // Create graphics copy. 268 319 Graphics2D g2d = (Graphics2D) g.create(); … … 273 324 // Draw thumb. 274 325 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 275 RenderingHints.VALUE_ANTIALIAS_ON);326 RenderingHints.VALUE_ANTIALIAS_ON); 276 327 g2d.translate(knobBounds.x, knobBounds.y); 277 328 … … 281 332 g2d.setColor(Color.BLUE); 282 333 g2d.draw(thumbShape); 283 334 284 335 // Dispose graphics. 285 336 g2d.dispose(); 286 337 } 287 338 288 339 /** 289 340 * Paints the thumb for the upper value using the specified graphics object. 290 341 */ 291 private void paintUpperThumb(Graphics g) { 342 private void paintUpperThumb(Graphics g) 343 { 292 344 Rectangle knobBounds = upperThumbRect; 293 345 int w = knobBounds.width; 294 int h = knobBounds.height; 295 346 int h = knobBounds.height; 347 296 348 // Create graphics copy. 297 349 Graphics2D g2d = (Graphics2D) g.create(); … … 302 354 // Draw thumb. 303 355 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 304 RenderingHints.VALUE_ANTIALIAS_ON);356 RenderingHints.VALUE_ANTIALIAS_ON); 305 357 g2d.translate(knobBounds.x, knobBounds.y); 306 358 … … 318 370 * Returns a Shape representing a thumb. 319 371 */ 320 private Shape createThumbShape(int width, int height) { 372 private Shape createThumbShape(int width, int height) 373 { 321 374 // Use circular shape. 322 375 Ellipse2D shape = new Ellipse2D.Double(0, 0, width, height); 323 376 return shape; 324 377 } 325 326 /** 327 * Sets the location of the upper thumb, and repaints the slider. This is328 * called when the upper thumb is dragged to repaint the slider. The378 379 /** 380 * Sets the location of the upper thumb, and repaints the slider. This is 381 * called when the upper thumb is dragged to repaint the slider. The 329 382 * <code>setThumbLocation()</code> method performs the same task for the 330 383 * lower thumb. 331 384 */ 332 private void setUpperThumbLocation(int x, int y) { 385 private void setUpperThumbLocation(int x, int y) 386 { 333 387 Rectangle upperUnionRect = new Rectangle(); 334 388 upperUnionRect.setBounds(upperThumbRect); … … 339 393 slider.repaint(upperUnionRect.x, upperUnionRect.y, upperUnionRect.width, upperUnionRect.height); 340 394 } 341 395 342 396 /** 343 397 * Moves the selected thumb in the specified direction by a block increment. 344 398 * This method is called when the user presses the Page Up or Down keys. 345 399 */ 346 public void scrollByBlock(int direction) { 347 synchronized (slider) { 400 public void scrollByBlock(int direction) 401 { 402 synchronized (slider) 403 { 348 404 int blockIncrement = (slider.getMaximum() - slider.getMinimum()) / 10; 349 if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) { 405 if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) 406 { 350 407 blockIncrement = 1; 351 408 } 352 409 int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 353 354 if (upperThumbSelected) { 410 411 if (upperThumbSelected) 412 { 355 413 int oldValue = ((RangeSlider) slider).getUpperValue(); 356 414 ((RangeSlider) slider).setUpperValue(oldValue + delta); 357 } else { 415 } 416 else 417 { 358 418 int oldValue = slider.getValue(); 359 419 slider.setValue(oldValue + delta); … … 361 421 } 362 422 } 363 423 364 424 /** 365 425 * Moves the selected thumb in the specified direction by a unit increment. 366 426 * This method is called when the user presses one of the arrow keys. 367 427 */ 368 public void scrollByUnit(int direction) { 369 synchronized (slider) { 428 public void scrollByUnit(int direction) 429 { 430 synchronized (slider) 431 { 370 432 int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 371 372 if (upperThumbSelected) { 433 434 if (upperThumbSelected) 435 { 373 436 int oldValue = ((RangeSlider) slider).getUpperValue(); 374 437 ((RangeSlider) slider).setUpperValue(oldValue + delta); 375 } else { 438 } 439 else 440 { 376 441 int oldValue = slider.getValue(); 377 442 slider.setValue(oldValue + delta); 378 443 } 379 } 380 } 381 382 /** 383 * Listener to handle model change events. This calculates the thumb444 } 445 } 446 447 /** 448 * Listener to handle model change events. This calculates the thumb 384 449 * locations and repaints the slider if the value change is not caused by 385 450 * dragging a thumb. 386 451 */ 387 public class ChangeHandler implements ChangeListener { 388 public void stateChanged(ChangeEvent arg0) { 389 if (!lowerDragging && !upperDragging) { 452 public class ChangeHandler implements ChangeListener 453 { 454 455 public void stateChanged(ChangeEvent arg0) 456 { 457 if (!lowerDragging && !upperDragging) 458 { 390 459 calculateThumbLocation(); 391 460 slider.repaint(); … … 393 462 } 394 463 } 395 464 465 /* 466 * Popup menu for incident actions 467 */ 468 private JPopupMenu createPopup() 469 { 470 JPopupMenu menu = new JPopupMenu(); 471 JMenuItem eventsMenuItem = new JMenuItem("Events"); 472 JMenuItem propsMenuItem = new JMenuItem("Properties"); 473 JMenuItem deleteMenuItem = new JMenuItem("Delete"); 474 eventsMenuItem.setActionCommand("Edit Events"); 475 propsMenuItem.setActionCommand("Modify Incident Properties"); 476 deleteMenuItem.setActionCommand("Delete Incident"); 477 478 PopupMenuItemListener menuItemListener = new PopupMenuItemListener(); 479 480 eventsMenuItem.addActionListener(menuItemListener); 481 propsMenuItem.addActionListener(menuItemListener); 482 deleteMenuItem.addActionListener(menuItemListener); 483 484 menu.add(eventsMenuItem); 485 menu.add(propsMenuItem); 486 menu.add(deleteMenuItem); 487 return menu; 488 } 489 490 class PopupMenuItemListener implements ActionListener 491 { 492 493 public void actionPerformed(ActionEvent e) 494 { 495 System.out.println(e.getActionCommand() + " will be handled here."); 496 } 497 } 498 396 499 /** 397 500 * Listener to handle mouse movements in the slider track. 398 501 */ 399 public class RangeTrackListener extends TrackListener { 400 502 public class RangeTrackListener extends TrackListener 503 { 504 401 505 @Override 402 public void mousePressed(MouseEvent e) { 403 if (!slider.isEnabled()) { 506 public void mousePressed(MouseEvent e) 507 { 508 if (!slider.isEnabled()) 509 { 404 510 return; 405 511 } … … 408 514 currentMouseY = e.getY(); 409 515 410 if (slider.isRequestFocusEnabled()) { 516 if (slider.isRequestFocusEnabled()) 517 { 411 518 slider.requestFocus(); 412 519 } 413 414 // Determine which thumb is pressed. If the upper thumb is 520 // Does user want a popup menu? 521 if (e.isPopupTrigger()) 522 { 523 JPopupMenu popup = createPopup(); 524 popup.show(e.getComponent(), currentMouseX, currentMouseY); 525 } 526 // Determine which thumb is pressed. If the upper thumb is 415 527 // selected (last one dragged), then check its position first; 416 528 // otherwise check the position of the lower thumb first. 417 529 boolean lowerPressed = false; 418 530 boolean upperPressed = false; 419 if (upperThumbSelected || slider.getMinimum() == slider.getValue()) { 420 if (upperThumbRect.contains(currentMouseX, currentMouseY)) { 531 if (upperThumbSelected || slider.getMinimum() == slider.getValue()) 532 { 533 if (upperThumbRect.contains(currentMouseX, currentMouseY)) 534 { 421 535 upperPressed = true; 422 } else if (thumbRect.contains(currentMouseX, currentMouseY)) { 536 } 537 else if (thumbRect.contains(currentMouseX, currentMouseY)) 538 { 423 539 lowerPressed = true; 424 540 } 425 } else { 426 if (thumbRect.contains(currentMouseX, currentMouseY)) { 541 } 542 else 543 { 544 if (thumbRect.contains(currentMouseX, currentMouseY)) 545 { 427 546 lowerPressed = true; 428 } else if (upperThumbRect.contains(currentMouseX, currentMouseY)) { 547 } 548 else if (upperThumbRect.contains(currentMouseX, currentMouseY)) 549 { 429 550 upperPressed = true; 430 551 } … … 432 553 433 554 // Handle lower thumb pressed. 434 if (lowerPressed) { 435 switch (slider.getOrientation()) { 436 case JSlider.VERTICAL: 437 offset = currentMouseY - thumbRect.y; 438 break; 439 case JSlider.HORIZONTAL: 440 offset = currentMouseX - thumbRect.x; 441 break; 555 if (lowerPressed) 556 { 557 switch (slider.getOrientation()) 558 { 559 case JSlider.VERTICAL: 560 offset = currentMouseY - thumbRect.y; 561 break; 562 case JSlider.HORIZONTAL: 563 offset = currentMouseX - thumbRect.x; 564 break; 442 565 } 443 566 upperThumbSelected = false; … … 446 569 } 447 570 lowerDragging = false; 448 571 449 572 // Handle upper thumb pressed. 450 if (upperPressed) { 451 switch (slider.getOrientation()) { 452 case JSlider.VERTICAL: 453 offset = currentMouseY - upperThumbRect.y; 454 break; 455 case JSlider.HORIZONTAL: 456 offset = currentMouseX - upperThumbRect.x; 457 break; 573 if (upperPressed) 574 { 575 switch (slider.getOrientation()) 576 { 577 case JSlider.VERTICAL: 578 offset = currentMouseY - upperThumbRect.y; 579 break; 580 case JSlider.HORIZONTAL: 581 offset = currentMouseX - upperThumbRect.x; 582 break; 458 583 } 459 584 upperThumbSelected = true; … … 463 588 upperDragging = false; 464 589 } 465 590 466 591 @Override 467 public void mouseReleased(MouseEvent e) { 592 public void mouseReleased(MouseEvent e) 593 { 468 594 lowerDragging = false; 469 595 upperDragging = false; … … 471 597 super.mouseReleased(e); 472 598 } 473 599 474 600 @Override 475 public void mouseDragged(MouseEvent e) { 476 if (!slider.isEnabled()) { 601 public void mouseDragged(MouseEvent e) 602 { 603 if (!slider.isEnabled()) 604 { 477 605 return; 478 606 } … … 481 609 currentMouseY = e.getY(); 482 610 483 if (lowerDragging) { 611 if (lowerDragging) 612 { 484 613 slider.setValueIsAdjusting(true); 485 614 moveLowerThumb(); 486 487 } else if (upperDragging) { 615 616 } 617 else if (upperDragging) 618 { 488 619 slider.setValueIsAdjusting(true); 489 620 moveUpperThumb(); 490 621 } 491 622 } 492 623 493 624 @Override 494 public boolean shouldScroll(int direction) { 625 public boolean shouldScroll(int direction) 626 { 495 627 return false; 496 628 } 497 629 498 630 /** 499 * Moves the location of the lower thumb, and sets its corresponding 631 * Moves the location of the lower thumb, and sets its corresponding 500 632 * value in the slider. 501 633 */ 502 private void moveLowerThumb() { 634 private void moveLowerThumb() 635 { 503 636 int thumbMiddle = 0; 504 505 switch (slider.getOrientation()) { 506 case JSlider.VERTICAL: 507 int halfThumbHeight = thumbRect.height / 2; 508 int thumbTop = currentMouseY - offset; 509 int trackTop = trackRect.y; 510 int trackBottom = trackRect.y + (trackRect.height - 1); 511 int vMax = yPositionForValue(slider.getValue() + slider.getExtent()); 512 513 // Apply bounds to thumb position. 514 if (drawInverted()) { 515 trackBottom = vMax; 516 } else { 517 trackTop = vMax; 518 } 519 thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); 520 thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); 521 522 setThumbLocation(thumbRect.x, thumbTop); 523 524 // Update slider value. 525 thumbMiddle = thumbTop + halfThumbHeight; 526 slider.setValue(valueForYPosition(thumbMiddle)); 527 break; 528 529 case JSlider.HORIZONTAL: 530 int halfThumbWidth = thumbRect.width / 2; 531 int thumbLeft = currentMouseX - offset; 532 int trackLeft = trackRect.x; 533 int trackRight = trackRect.x + (trackRect.width - 1); 534 int hMax = xPositionForValue(slider.getValue() + slider.getExtent()); 535 536 // Apply bounds to thumb position. 537 if (drawInverted()) { 538 trackLeft = hMax; 539 } else { 540 trackRight = hMax; 541 } 542 thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 543 thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 544 545 setThumbLocation(thumbLeft, thumbRect.y); 546 547 // Update slider value. 548 thumbMiddle = thumbLeft + halfThumbWidth; 549 slider.setValue(valueForXPosition(thumbMiddle)); 550 break; 551 552 default: 553 return; 637 638 switch (slider.getOrientation()) 639 { 640 case JSlider.VERTICAL: 641 int halfThumbHeight = thumbRect.height / 2; 642 int thumbTop = currentMouseY - offset; 643 int trackTop = trackRect.y; 644 int trackBottom = trackRect.y + (trackRect.height - 1); 645 int vMax = yPositionForValue(slider.getValue() + slider.getExtent()); 646 647 // Apply bounds to thumb position. 648 if (drawInverted()) 649 { 650 trackBottom = vMax; 651 } 652 else 653 { 654 trackTop = vMax; 655 } 656 thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); 657 thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); 658 659 setThumbLocation(thumbRect.x, thumbTop); 660 661 // Update slider value. 662 thumbMiddle = thumbTop + halfThumbHeight; 663 slider.setValue(valueForYPosition(thumbMiddle)); 664 break; 665 666 case JSlider.HORIZONTAL: 667 int halfThumbWidth = thumbRect.width / 2; 668 int thumbLeft = currentMouseX - offset; 669 int trackLeft = trackRect.x; 670 int trackRight = trackRect.x + (trackRect.width - 1); 671 int hMax = xPositionForValue(slider.getValue() + slider.getExtent()); 672 673 // Apply bounds to thumb position. 674 if (drawInverted()) 675 { 676 trackLeft = hMax; 677 } 678 else 679 { 680 trackRight = hMax; 681 } 682 thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 683 thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 684 685 setThumbLocation(thumbLeft, thumbRect.y); 686 687 // Update slider value. 688 thumbMiddle = thumbLeft + halfThumbWidth; 689 slider.setValue(valueForXPosition(thumbMiddle)); 690 break; 691 692 default: 693 return; 554 694 } 555 695 } 556 696 557 697 /** 558 * Moves the location of the upper thumb, and sets its corresponding 698 * Moves the location of the upper thumb, and sets its corresponding 559 699 * value in the slider. 560 700 */ 561 private void moveUpperThumb() { 701 private void moveUpperThumb() 702 { 562 703 int thumbMiddle = 0; 563 564 switch (slider.getOrientation()) { 565 case JSlider.VERTICAL: 566 int halfThumbHeight = thumbRect.height / 2; 567 int thumbTop = currentMouseY - offset; 568 int trackTop = trackRect.y; 569 int trackBottom = trackRect.y + (trackRect.height - 1); 570 int vMin = yPositionForValue(slider.getValue()); 571 572 // Apply bounds to thumb position. 573 if (drawInverted()) { 574 trackTop = vMin; 575 } else { 576 trackBottom = vMin; 577 } 578 thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); 579 thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); 580 581 setUpperThumbLocation(thumbRect.x, thumbTop); 582 583 // Update slider extent. 584 thumbMiddle = thumbTop + halfThumbHeight; 585 slider.setExtent(valueForYPosition(thumbMiddle) - slider.getValue()); 586 break; 587 588 case JSlider.HORIZONTAL: 589 int halfThumbWidth = thumbRect.width / 2; 590 int thumbLeft = currentMouseX - offset; 591 int trackLeft = trackRect.x; 592 int trackRight = trackRect.x + (trackRect.width - 1); 593 int hMin = xPositionForValue(slider.getValue()); 594 595 // Apply bounds to thumb position. 596 if (drawInverted()) { 597 trackRight = hMin; 598 } else { 599 trackLeft = hMin; 600 } 601 thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 602 thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 603 604 setUpperThumbLocation(thumbLeft, thumbRect.y); 605 606 // Update slider extent. 607 thumbMiddle = thumbLeft + halfThumbWidth; 608 slider.setExtent(valueForXPosition(thumbMiddle) - slider.getValue()); 609 break; 610 611 default: 612 return; 704 705 switch (slider.getOrientation()) 706 { 707 case JSlider.VERTICAL: 708 int halfThumbHeight = thumbRect.height / 2; 709 int thumbTop = currentMouseY - offset; 710 int trackTop = trackRect.y; 711 int trackBottom = trackRect.y + (trackRect.height - 1); 712 int vMin = yPositionForValue(slider.getValue()); 713 714 // Apply bounds to thumb position. 715 if (drawInverted()) 716 { 717 trackTop = vMin; 718 } 719 else 720 { 721 trackBottom = vMin; 722 } 723 thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); 724 thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); 725 726 setUpperThumbLocation(thumbRect.x, thumbTop); 727 728 // Update slider extent. 729 thumbMiddle = thumbTop + halfThumbHeight; 730 slider.setExtent(valueForYPosition(thumbMiddle) - slider.getValue()); 731 break; 732 733 case JSlider.HORIZONTAL: 734 int halfThumbWidth = thumbRect.width / 2; 735 int thumbLeft = currentMouseX - offset; 736 int trackLeft = trackRect.x; 737 int trackRight = trackRect.x + (trackRect.width - 1); 738 int hMin = xPositionForValue(slider.getValue()); 739 740 // Apply bounds to thumb position. 741 if (drawInverted()) 742 { 743 trackRight = hMin; 744 } 745 else 746 { 747 trackLeft = hMin; 748 } 749 thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 750 thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 751 752 setUpperThumbLocation(thumbLeft, thumbRect.y); 753 754 // Update slider extent. 755 thumbMiddle = thumbLeft + halfThumbWidth; 756 slider.setExtent(valueForXPosition(thumbMiddle) - slider.getValue()); 757 break; 758 759 default: 760 return; 613 761 } 614 762 }
Note: See TracChangeset
for help on using the changeset viewer.
