| 1 | package scriptbuilder.gui.drawers; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Color; |
|---|
| 4 | import java.awt.Dimension; |
|---|
| 5 | import java.awt.Graphics; |
|---|
| 6 | import java.awt.Graphics2D; |
|---|
| 7 | import java.awt.Rectangle; |
|---|
| 8 | import java.awt.RenderingHints; |
|---|
| 9 | import java.awt.Shape; |
|---|
| 10 | import java.awt.event.ActionEvent; |
|---|
| 11 | import java.awt.event.ActionListener; |
|---|
| 12 | import java.awt.event.MouseEvent; |
|---|
| 13 | import java.awt.geom.Ellipse2D; |
|---|
| 14 | |
|---|
| 15 | import javax.swing.JComponent; |
|---|
| 16 | import javax.swing.JMenuItem; |
|---|
| 17 | import javax.swing.JOptionPane; |
|---|
| 18 | import javax.swing.JPopupMenu; |
|---|
| 19 | import javax.swing.JSlider; |
|---|
| 20 | import javax.swing.SwingUtilities; |
|---|
| 21 | import javax.swing.event.ChangeEvent; |
|---|
| 22 | import javax.swing.event.ChangeListener; |
|---|
| 23 | import javax.swing.plaf.basic.BasicSliderUI; |
|---|
| 24 | |
|---|
| 25 | /* |
|---|
| 26 | The MIT License |
|---|
| 27 | |
|---|
| 28 | Copyright (c) 2010 Ernest Yu. All rights reserved. |
|---|
| 29 | |
|---|
| 30 | Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 31 | of this software and associated documentation files (the "Software"), to deal |
|---|
| 32 | in the Software without restriction, including without limitation the rights |
|---|
| 33 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 34 | copies of the Software, and to permit persons to whom the Software is |
|---|
| 35 | furnished to do so, subject to the following conditions: |
|---|
| 36 | |
|---|
| 37 | The above copyright notice and this permission notice shall be included in |
|---|
| 38 | all copies or substantial portions of the Software. |
|---|
| 39 | |
|---|
| 40 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 41 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 42 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 43 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 44 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 45 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 46 | THE SOFTWARE. |
|---|
| 47 | */ |
|---|
| 48 | /** |
|---|
| 49 | * UI delegate for the RangeSlider component. RangeSliderUI paints two thumbs, |
|---|
| 50 | * one for the lower value and one for the upper value. |
|---|
| 51 | * |
|---|
| 52 | * Hacked to display and drag only the lower thumb by jdalbey |
|---|
| 53 | */ |
|---|
| 54 | class RangeSliderUI extends BasicSliderUI |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Color of selected range. |
|---|
| 59 | */ |
|---|
| 60 | private Color rangeColor = Color.GREEN; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Location and size of thumb for upper value. |
|---|
| 64 | */ |
|---|
| 65 | private Rectangle upperThumbRect; |
|---|
| 66 | /** |
|---|
| 67 | * Indicator that determines whether upper thumb is selected. |
|---|
| 68 | */ |
|---|
| 69 | private boolean upperThumbSelected; |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Indicator that determines whether lower thumb is being dragged. |
|---|
| 73 | */ |
|---|
| 74 | private transient boolean lowerDragging; |
|---|
| 75 | /** |
|---|
| 76 | * Indicator that determines whether upper thumb is being dragged. |
|---|
| 77 | */ |
|---|
| 78 | private transient boolean upperDragging; |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Constructs a RangeSliderUI for the specified slider component. |
|---|
| 82 | * |
|---|
| 83 | * @param b RangeSlider |
|---|
| 84 | */ |
|---|
| 85 | public RangeSliderUI(RangeSlider b) |
|---|
| 86 | { |
|---|
| 87 | super(b); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Installs this UI delegate on the specified component. |
|---|
| 92 | */ |
|---|
| 93 | @Override |
|---|
| 94 | public void installUI(JComponent c) |
|---|
| 95 | { |
|---|
| 96 | upperThumbRect = new Rectangle(); |
|---|
| 97 | super.installUI(c); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Creates a listener to handle track events in the specified slider. |
|---|
| 102 | */ |
|---|
| 103 | @Override |
|---|
| 104 | protected TrackListener createTrackListener(JSlider slider) |
|---|
| 105 | { |
|---|
| 106 | return new RangeTrackListener(); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Creates a listener to handle change events in the specified slider. |
|---|
| 111 | */ |
|---|
| 112 | @Override |
|---|
| 113 | protected ChangeListener createChangeListener(JSlider slider) |
|---|
| 114 | { |
|---|
| 115 | return new ChangeHandler(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Updates the dimensions for both thumbs. |
|---|
| 120 | */ |
|---|
| 121 | @Override |
|---|
| 122 | protected void calculateThumbSize() |
|---|
| 123 | { |
|---|
| 124 | // Call superclass method for lower thumb size. |
|---|
| 125 | super.calculateThumbSize(); |
|---|
| 126 | |
|---|
| 127 | // Set upper thumb size. |
|---|
| 128 | upperThumbRect.setSize(thumbRect.width, thumbRect.height); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Updates the locations for both thumbs if the value change is not caused |
|---|
| 133 | * by dragging a thumb. |
|---|
| 134 | */ |
|---|
| 135 | @Override |
|---|
| 136 | protected void calculateThumbLocation() |
|---|
| 137 | { |
|---|
| 138 | // Call superclass method for lower thumb location. |
|---|
| 139 | super.calculateThumbLocation(); |
|---|
| 140 | |
|---|
| 141 | // Adjust upper value to snap to ticks if necessary. |
|---|
| 142 | if (slider.getSnapToTicks()) |
|---|
| 143 | { |
|---|
| 144 | int upperValue = slider.getValue() + slider.getExtent(); |
|---|
| 145 | int snappedValue = upperValue; |
|---|
| 146 | int majorTickSpacing = slider.getMajorTickSpacing(); |
|---|
| 147 | int minorTickSpacing = slider.getMinorTickSpacing(); |
|---|
| 148 | int tickSpacing = 0; |
|---|
| 149 | |
|---|
| 150 | if (minorTickSpacing > 0) |
|---|
| 151 | { |
|---|
| 152 | tickSpacing = minorTickSpacing; |
|---|
| 153 | } |
|---|
| 154 | else if (majorTickSpacing > 0) |
|---|
| 155 | { |
|---|
| 156 | tickSpacing = majorTickSpacing; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | if (tickSpacing != 0) |
|---|
| 160 | { |
|---|
| 161 | // If it's not on a tick, change the value |
|---|
| 162 | if ((upperValue - slider.getMinimum()) % tickSpacing != 0) |
|---|
| 163 | { |
|---|
| 164 | float temp = (float) (upperValue - slider.getMinimum()) / (float) tickSpacing; |
|---|
| 165 | int whichTick = Math.round(temp); |
|---|
| 166 | snappedValue = slider.getMinimum() + (whichTick * tickSpacing); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | if (snappedValue != upperValue) |
|---|
| 170 | { |
|---|
| 171 | slider.setExtent(snappedValue - slider.getValue()); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | // Calculate upper thumb location. The thumb is centered over its |
|---|
| 177 | // value on the track. |
|---|
| 178 | if (slider.getOrientation() == JSlider.HORIZONTAL) |
|---|
| 179 | { |
|---|
| 180 | int upperPosition = xPositionForValue(slider.getValue() + slider.getExtent()); |
|---|
| 181 | upperThumbRect.x = upperPosition - (upperThumbRect.width / 2); |
|---|
| 182 | upperThumbRect.y = trackRect.y; |
|---|
| 183 | |
|---|
| 184 | } |
|---|
| 185 | else |
|---|
| 186 | { |
|---|
| 187 | int upperPosition = yPositionForValue(slider.getValue() + slider.getExtent()); |
|---|
| 188 | upperThumbRect.x = trackRect.x; |
|---|
| 189 | upperThumbRect.y = upperPosition - (upperThumbRect.height / 2); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** |
|---|
| 194 | * Returns the size of a thumb. |
|---|
| 195 | */ |
|---|
| 196 | @Override |
|---|
| 197 | protected Dimension getThumbSize() |
|---|
| 198 | { |
|---|
| 199 | return new Dimension(12, 12); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * Paints the slider. The selected thumb is always painted on top of the |
|---|
| 204 | * other thumb. |
|---|
| 205 | */ |
|---|
| 206 | @Override |
|---|
| 207 | public void paint(Graphics g, JComponent c) |
|---|
| 208 | { |
|---|
| 209 | super.paint(g, c); |
|---|
| 210 | |
|---|
| 211 | Rectangle clipRect = g.getClipBounds(); |
|---|
| 212 | if (upperThumbSelected) |
|---|
| 213 | { |
|---|
| 214 | // Paint lower thumb first, then upper thumb. |
|---|
| 215 | if (clipRect.intersects(thumbRect)) |
|---|
| 216 | { |
|---|
| 217 | paintLowerThumb(g); |
|---|
| 218 | } |
|---|
| 219 | if (clipRect.intersects(upperThumbRect)) |
|---|
| 220 | { |
|---|
| 221 | paintUpperThumb(g); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | else |
|---|
| 226 | { |
|---|
| 227 | // Paint upper thumb first, then lower thumb. |
|---|
| 228 | if (clipRect.intersects(upperThumbRect)) |
|---|
| 229 | { |
|---|
| 230 | paintUpperThumb(g); |
|---|
| 231 | } |
|---|
| 232 | if (clipRect.intersects(thumbRect)) |
|---|
| 233 | { |
|---|
| 234 | paintLowerThumb(g); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * Paints the track. |
|---|
| 241 | */ |
|---|
| 242 | @Override |
|---|
| 243 | public void paintTrack(Graphics g) |
|---|
| 244 | { |
|---|
| 245 | // Draw track. |
|---|
| 246 | super.paintTrack(g); |
|---|
| 247 | |
|---|
| 248 | Rectangle trackBounds = trackRect; |
|---|
| 249 | |
|---|
| 250 | if (slider.getOrientation() == JSlider.HORIZONTAL) |
|---|
| 251 | { |
|---|
| 252 | // Determine position of selected range by moving from the middle |
|---|
| 253 | // of one thumb to the other. |
|---|
| 254 | int lowerX = thumbRect.x + (thumbRect.width / 2); |
|---|
| 255 | int upperX = upperThumbRect.x + (upperThumbRect.width / 2); |
|---|
| 256 | |
|---|
| 257 | // Determine track position. |
|---|
| 258 | int cy = (trackBounds.height / 2) - 2; |
|---|
| 259 | |
|---|
| 260 | // Save color and shift position. |
|---|
| 261 | Color oldColor = g.getColor(); |
|---|
| 262 | g.translate(trackBounds.x, trackBounds.y + cy); |
|---|
| 263 | |
|---|
| 264 | // Draw selected range. |
|---|
| 265 | g.setColor(rangeColor); |
|---|
| 266 | for (int y = 0; y <= 3; y++) |
|---|
| 267 | { |
|---|
| 268 | g.drawLine(lowerX - trackBounds.x, y, upperX - trackBounds.x, y); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | // Restore position and color. |
|---|
| 272 | g.translate(-trackBounds.x, -(trackBounds.y + cy)); |
|---|
| 273 | g.setColor(oldColor); |
|---|
| 274 | |
|---|
| 275 | } |
|---|
| 276 | else |
|---|
| 277 | { |
|---|
| 278 | // Determine position of selected range by moving from the middle |
|---|
| 279 | // of one thumb to the other. |
|---|
| 280 | int lowerY = thumbRect.x + (thumbRect.width / 2); |
|---|
| 281 | int upperY = upperThumbRect.x + (upperThumbRect.width / 2); |
|---|
| 282 | |
|---|
| 283 | // Determine track position. |
|---|
| 284 | int cx = (trackBounds.width / 2) - 2; |
|---|
| 285 | |
|---|
| 286 | // Save color and shift position. |
|---|
| 287 | Color oldColor = g.getColor(); |
|---|
| 288 | g.translate(trackBounds.x + cx, trackBounds.y); |
|---|
| 289 | |
|---|
| 290 | // Draw selected range. |
|---|
| 291 | g.setColor(rangeColor); |
|---|
| 292 | for (int x = 0; x <= 3; x++) |
|---|
| 293 | { |
|---|
| 294 | g.drawLine(x, lowerY - trackBounds.y, x, upperY - trackBounds.y); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | // Restore position and color. |
|---|
| 298 | g.translate(-(trackBounds.x + cx), -trackBounds.y); |
|---|
| 299 | g.setColor(oldColor); |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | /** |
|---|
| 304 | * Overrides superclass method to do nothing. Thumb painting is handled |
|---|
| 305 | * within the <code>paint()</code> method. |
|---|
| 306 | */ |
|---|
| 307 | @Override |
|---|
| 308 | public void paintThumb(Graphics g) |
|---|
| 309 | { |
|---|
| 310 | // Do nothing. |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | * Paints the thumb for the lower value using the specified graphics object. |
|---|
| 315 | */ |
|---|
| 316 | private void paintLowerThumb(Graphics g) |
|---|
| 317 | { |
|---|
| 318 | Rectangle knobBounds = thumbRect; |
|---|
| 319 | int w = knobBounds.width; |
|---|
| 320 | int h = knobBounds.height; |
|---|
| 321 | |
|---|
| 322 | // Create graphics copy. |
|---|
| 323 | Graphics2D g2d = (Graphics2D) g.create(); |
|---|
| 324 | |
|---|
| 325 | // Create default thumb shape. |
|---|
| 326 | Shape thumbShape = createThumbShape(w - 1, h - 1); |
|---|
| 327 | |
|---|
| 328 | // Draw thumb. |
|---|
| 329 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
|---|
| 330 | RenderingHints.VALUE_ANTIALIAS_ON); |
|---|
| 331 | g2d.translate(knobBounds.x, knobBounds.y); |
|---|
| 332 | |
|---|
| 333 | g2d.setColor(Color.CYAN); |
|---|
| 334 | g2d.fill(thumbShape); |
|---|
| 335 | |
|---|
| 336 | g2d.setColor(Color.BLUE); |
|---|
| 337 | g2d.draw(thumbShape); |
|---|
| 338 | |
|---|
| 339 | // Dispose graphics. |
|---|
| 340 | g2d.dispose(); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | /** |
|---|
| 344 | * Paints the thumb for the upper value using the specified graphics object. |
|---|
| 345 | */ |
|---|
| 346 | private void paintUpperThumb(Graphics g) |
|---|
| 347 | { |
|---|
| 348 | // Remove drawing of upper thumb - jdalbey |
|---|
| 349 | // Rectangle knobBounds = upperThumbRect; |
|---|
| 350 | // int w = knobBounds.width; |
|---|
| 351 | // int h = knobBounds.height; |
|---|
| 352 | // |
|---|
| 353 | // // Create graphics copy. |
|---|
| 354 | // Graphics2D g2d = (Graphics2D) g.create(); |
|---|
| 355 | // |
|---|
| 356 | // // Create default thumb shape. |
|---|
| 357 | // Shape thumbShape = createThumbShape(w - 1, h - 1); |
|---|
| 358 | // |
|---|
| 359 | // // Draw thumb. |
|---|
| 360 | // g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
|---|
| 361 | // RenderingHints.VALUE_ANTIALIAS_ON); |
|---|
| 362 | // g2d.translate(knobBounds.x, knobBounds.y); |
|---|
| 363 | // |
|---|
| 364 | // g2d.setColor(Color.PINK); |
|---|
| 365 | // g2d.fill(thumbShape); |
|---|
| 366 | // |
|---|
| 367 | // g2d.setColor(Color.RED); |
|---|
| 368 | // g2d.draw(thumbShape); |
|---|
| 369 | // |
|---|
| 370 | // // Dispose graphics. |
|---|
| 371 | // g2d.dispose(); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * Returns a Shape representing a thumb. |
|---|
| 376 | */ |
|---|
| 377 | private Shape createThumbShape(int width, int height) |
|---|
| 378 | { |
|---|
| 379 | // Use circular shape. |
|---|
| 380 | Ellipse2D shape = new Ellipse2D.Double(0, 0, width, height); |
|---|
| 381 | return shape; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | /** |
|---|
| 385 | * Sets the location of the upper thumb, and repaints the slider. This is |
|---|
| 386 | * called when the upper thumb is dragged to repaint the slider. The |
|---|
| 387 | * <code>setThumbLocation()</code> method performs the same task for the |
|---|
| 388 | * lower thumb. |
|---|
| 389 | */ |
|---|
| 390 | private void setUpperThumbLocation(int x, int y) |
|---|
| 391 | { |
|---|
| 392 | Rectangle upperUnionRect = new Rectangle(); |
|---|
| 393 | upperUnionRect.setBounds(upperThumbRect); |
|---|
| 394 | |
|---|
| 395 | upperThumbRect.setLocation(x, y); |
|---|
| 396 | |
|---|
| 397 | SwingUtilities.computeUnion(upperThumbRect.x, upperThumbRect.y, upperThumbRect.width, upperThumbRect.height, upperUnionRect); |
|---|
| 398 | slider.repaint(upperUnionRect.x, upperUnionRect.y, upperUnionRect.width, upperUnionRect.height); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | /** |
|---|
| 402 | * Moves the selected thumb in the specified direction by a block increment. |
|---|
| 403 | * This method is called when the user presses the Page Up or Down keys. |
|---|
| 404 | */ |
|---|
| 405 | public void scrollByBlock(int direction) |
|---|
| 406 | { |
|---|
| 407 | synchronized (slider) |
|---|
| 408 | { |
|---|
| 409 | int blockIncrement = (slider.getMaximum() - slider.getMinimum()) / 10; |
|---|
| 410 | if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) |
|---|
| 411 | { |
|---|
| 412 | blockIncrement = 1; |
|---|
| 413 | } |
|---|
| 414 | int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); |
|---|
| 415 | |
|---|
| 416 | if (upperThumbSelected) |
|---|
| 417 | { |
|---|
| 418 | int oldValue = ((RangeSlider) slider).getUpperValue(); |
|---|
| 419 | ((RangeSlider) slider).setUpperValue(oldValue + delta); |
|---|
| 420 | } |
|---|
| 421 | else |
|---|
| 422 | { |
|---|
| 423 | int oldValue = slider.getValue(); |
|---|
| 424 | slider.setValue(oldValue + delta); |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | /** |
|---|
| 430 | * Moves the selected thumb in the specified direction by a unit increment. |
|---|
| 431 | * This method is called when the user presses one of the arrow keys. |
|---|
| 432 | */ |
|---|
| 433 | public void scrollByUnit(int direction) |
|---|
| 434 | { |
|---|
| 435 | synchronized (slider) |
|---|
| 436 | { |
|---|
| 437 | int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); |
|---|
| 438 | |
|---|
| 439 | if (upperThumbSelected) |
|---|
| 440 | { |
|---|
| 441 | int oldValue = ((RangeSlider) slider).getUpperValue(); |
|---|
| 442 | ((RangeSlider) slider).setUpperValue(oldValue + delta); |
|---|
| 443 | } |
|---|
| 444 | else |
|---|
| 445 | { |
|---|
| 446 | int oldValue = slider.getValue(); |
|---|
| 447 | slider.setValue(oldValue + delta); |
|---|
| 448 | } |
|---|
| 449 | } |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | /** |
|---|
| 453 | * Listener to handle model change events. This calculates the thumb |
|---|
| 454 | * locations and repaints the slider if the value change is not caused by |
|---|
| 455 | * dragging a thumb. |
|---|
| 456 | */ |
|---|
| 457 | public class ChangeHandler implements ChangeListener |
|---|
| 458 | { |
|---|
| 459 | |
|---|
| 460 | public void stateChanged(ChangeEvent arg0) |
|---|
| 461 | { |
|---|
| 462 | if (!lowerDragging && !upperDragging) |
|---|
| 463 | { |
|---|
| 464 | calculateThumbLocation(); |
|---|
| 465 | slider.repaint(); |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | /* |
|---|
| 471 | * Popup menu for incident actions |
|---|
| 472 | */ |
|---|
| 473 | private JPopupMenu createPopup() |
|---|
| 474 | { |
|---|
| 475 | JPopupMenu menu = new JPopupMenu(); |
|---|
| 476 | JMenuItem eventsMenuItem = new JMenuItem("Events"); |
|---|
| 477 | JMenuItem propsMenuItem = new JMenuItem("Properties"); |
|---|
| 478 | JMenuItem deleteMenuItem = new JMenuItem("Delete"); |
|---|
| 479 | eventsMenuItem.setActionCommand("Edit Events"); |
|---|
| 480 | propsMenuItem.setActionCommand("Modify Incident Properties"); |
|---|
| 481 | deleteMenuItem.setActionCommand("Delete Incident"); |
|---|
| 482 | |
|---|
| 483 | PopupMenuItemListener menuItemListener = new PopupMenuItemListener(); |
|---|
| 484 | |
|---|
| 485 | eventsMenuItem.addActionListener(menuItemListener); |
|---|
| 486 | propsMenuItem.addActionListener(menuItemListener); |
|---|
| 487 | deleteMenuItem.addActionListener(menuItemListener); |
|---|
| 488 | |
|---|
| 489 | menu.add(eventsMenuItem); |
|---|
| 490 | menu.add(propsMenuItem); |
|---|
| 491 | menu.add(deleteMenuItem); |
|---|
| 492 | return menu; |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | class PopupMenuItemListener implements ActionListener |
|---|
| 496 | { |
|---|
| 497 | |
|---|
| 498 | public void actionPerformed(ActionEvent e) |
|---|
| 499 | { |
|---|
| 500 | JOptionPane.showMessageDialog(null, e.getActionCommand() + " will be handled here."); |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | /** |
|---|
| 505 | * Listener to handle mouse movements in the slider track. |
|---|
| 506 | */ |
|---|
| 507 | public class RangeTrackListener extends TrackListener |
|---|
| 508 | { |
|---|
| 509 | |
|---|
| 510 | @Override |
|---|
| 511 | public void mousePressed(MouseEvent e) |
|---|
| 512 | { |
|---|
| 513 | if (!slider.isEnabled()) |
|---|
| 514 | { |
|---|
| 515 | return; |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | currentMouseX = e.getX(); |
|---|
| 519 | currentMouseY = e.getY(); |
|---|
| 520 | |
|---|
| 521 | if (slider.isRequestFocusEnabled()) |
|---|
| 522 | { |
|---|
| 523 | slider.requestFocus(); |
|---|
| 524 | } |
|---|
| 525 | // Does user want a popup menu? |
|---|
| 526 | if (e.isPopupTrigger()) |
|---|
| 527 | { |
|---|
| 528 | JPopupMenu popup = createPopup(); |
|---|
| 529 | popup.show(e.getComponent(), currentMouseX, currentMouseY); |
|---|
| 530 | } |
|---|
| 531 | // Determine which thumb is pressed. If the upper thumb is |
|---|
| 532 | // selected (last one dragged), then check its position first; |
|---|
| 533 | // otherwise check the position of the lower thumb first. |
|---|
| 534 | boolean lowerPressed = false; |
|---|
| 535 | boolean upperPressed = false; |
|---|
| 536 | if (upperThumbSelected || slider.getMinimum() == slider.getValue()) |
|---|
| 537 | { |
|---|
| 538 | if (upperThumbRect.contains(currentMouseX, currentMouseY)) |
|---|
| 539 | { |
|---|
| 540 | //upperPressed = true; |
|---|
| 541 | } |
|---|
| 542 | else if (thumbRect.contains(currentMouseX, currentMouseY)) |
|---|
| 543 | { |
|---|
| 544 | lowerPressed = true; |
|---|
| 545 | } |
|---|
| 546 | } |
|---|
| 547 | else |
|---|
| 548 | { |
|---|
| 549 | if (thumbRect.contains(currentMouseX, currentMouseY)) |
|---|
| 550 | { |
|---|
| 551 | lowerPressed = true; |
|---|
| 552 | } |
|---|
| 553 | else if (upperThumbRect.contains(currentMouseX, currentMouseY)) |
|---|
| 554 | { |
|---|
| 555 | //upperPressed = true; |
|---|
| 556 | } |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | // Handle lower thumb pressed. |
|---|
| 560 | if (lowerPressed) |
|---|
| 561 | { |
|---|
| 562 | switch (slider.getOrientation()) |
|---|
| 563 | { |
|---|
| 564 | case JSlider.VERTICAL: |
|---|
| 565 | offset = currentMouseY - thumbRect.y; |
|---|
| 566 | break; |
|---|
| 567 | case JSlider.HORIZONTAL: |
|---|
| 568 | offset = currentMouseX - thumbRect.x; |
|---|
| 569 | break; |
|---|
| 570 | } |
|---|
| 571 | upperThumbSelected = false; |
|---|
| 572 | lowerDragging = true; |
|---|
| 573 | // ALERT: Return from middle of method |
|---|
| 574 | return; |
|---|
| 575 | } |
|---|
| 576 | lowerDragging = false; |
|---|
| 577 | |
|---|
| 578 | // Handle upper thumb pressed. |
|---|
| 579 | if (upperPressed) |
|---|
| 580 | { |
|---|
| 581 | switch (slider.getOrientation()) |
|---|
| 582 | { |
|---|
| 583 | case JSlider.VERTICAL: |
|---|
| 584 | offset = currentMouseY - upperThumbRect.y; |
|---|
| 585 | break; |
|---|
| 586 | case JSlider.HORIZONTAL: |
|---|
| 587 | offset = currentMouseX - upperThumbRect.x; |
|---|
| 588 | break; |
|---|
| 589 | } |
|---|
| 590 | upperThumbSelected = true; |
|---|
| 591 | upperDragging = true; |
|---|
| 592 | // ALERT: Return from middle of method |
|---|
| 593 | return; |
|---|
| 594 | } |
|---|
| 595 | upperDragging = false; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | @Override |
|---|
| 599 | public void mouseReleased(MouseEvent e) |
|---|
| 600 | { |
|---|
| 601 | lowerDragging = false; |
|---|
| 602 | upperDragging = false; |
|---|
| 603 | slider.setValueIsAdjusting(false); |
|---|
| 604 | super.mouseReleased(e); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | @Override |
|---|
| 608 | public void mouseDragged(MouseEvent e) |
|---|
| 609 | { |
|---|
| 610 | if (!slider.isEnabled()) |
|---|
| 611 | { |
|---|
| 612 | return; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | currentMouseX = e.getX(); |
|---|
| 616 | currentMouseY = e.getY(); |
|---|
| 617 | |
|---|
| 618 | if (lowerDragging) |
|---|
| 619 | { |
|---|
| 620 | slider.setValueIsAdjusting(true); |
|---|
| 621 | moveLowerThumb(); |
|---|
| 622 | syncUpperThumb(); |
|---|
| 623 | } |
|---|
| 624 | else if (upperDragging) |
|---|
| 625 | { |
|---|
| 626 | slider.setValueIsAdjusting(true); |
|---|
| 627 | moveUpperThumb(); |
|---|
| 628 | } |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | @Override |
|---|
| 632 | public boolean shouldScroll(int direction) |
|---|
| 633 | { |
|---|
| 634 | return false; |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | /** |
|---|
| 638 | * Moves the location of the lower thumb, and sets its corresponding |
|---|
| 639 | * value in the slider. |
|---|
| 640 | */ |
|---|
| 641 | private void moveLowerThumb() |
|---|
| 642 | { |
|---|
| 643 | int thumbMiddle = 0; |
|---|
| 644 | |
|---|
| 645 | switch (slider.getOrientation()) |
|---|
| 646 | { |
|---|
| 647 | case JSlider.VERTICAL: |
|---|
| 648 | int halfThumbHeight = thumbRect.height / 2; |
|---|
| 649 | int thumbTop = currentMouseY - offset; |
|---|
| 650 | int trackTop = trackRect.y; |
|---|
| 651 | int trackBottom = trackRect.y + (trackRect.height - 1); |
|---|
| 652 | int vMax = yPositionForValue(slider.getValue() + slider.getExtent()); |
|---|
| 653 | |
|---|
| 654 | // Apply bounds to thumb position. |
|---|
| 655 | if (drawInverted()) |
|---|
| 656 | { |
|---|
| 657 | trackBottom = vMax; |
|---|
| 658 | } |
|---|
| 659 | else |
|---|
| 660 | { |
|---|
| 661 | trackTop = vMax; |
|---|
| 662 | } |
|---|
| 663 | thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); |
|---|
| 664 | thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); |
|---|
| 665 | |
|---|
| 666 | setThumbLocation(thumbRect.x, thumbTop); // repaint |
|---|
| 667 | |
|---|
| 668 | // Update slider value. |
|---|
| 669 | thumbMiddle = thumbTop + halfThumbHeight; |
|---|
| 670 | slider.setValue(valueForYPosition(thumbMiddle)); |
|---|
| 671 | break; |
|---|
| 672 | |
|---|
| 673 | case JSlider.HORIZONTAL: |
|---|
| 674 | int halfThumbWidth = thumbRect.width / 2; |
|---|
| 675 | int thumbLeft = currentMouseX - offset; |
|---|
| 676 | int trackLeft = trackRect.x; |
|---|
| 677 | int trackRight = trackRect.x + (trackRect.width - 1); |
|---|
| 678 | int hMax = xPositionForValue(slider.getValue() + slider.getExtent()); |
|---|
| 679 | |
|---|
| 680 | // Apply bounds to thumb position. |
|---|
| 681 | if (drawInverted()) |
|---|
| 682 | { |
|---|
| 683 | trackLeft = hMax; |
|---|
| 684 | } |
|---|
| 685 | else |
|---|
| 686 | { |
|---|
| 687 | trackRight = hMax; |
|---|
| 688 | } |
|---|
| 689 | thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); |
|---|
| 690 | thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); |
|---|
| 691 | |
|---|
| 692 | setThumbLocation(thumbLeft, thumbRect.y); //repaint |
|---|
| 693 | |
|---|
| 694 | // Update slider value. |
|---|
| 695 | thumbMiddle = thumbLeft + halfThumbWidth; |
|---|
| 696 | slider.setValue(valueForXPosition(thumbMiddle)); |
|---|
| 697 | break; |
|---|
| 698 | |
|---|
| 699 | default: |
|---|
| 700 | return; |
|---|
| 701 | } |
|---|
| 702 | } |
|---|
| 703 | |
|---|
| 704 | /** |
|---|
| 705 | * Moves the location of the upper thumb to be in sync with the lower |
|---|
| 706 | * thumb so that the extent is unchanged. |
|---|
| 707 | * |
|---|
| 708 | * @author jdalbey |
|---|
| 709 | */ |
|---|
| 710 | private void syncUpperThumb() |
|---|
| 711 | { |
|---|
| 712 | int thumbMiddle = 0; |
|---|
| 713 | |
|---|
| 714 | switch (slider.getOrientation()) |
|---|
| 715 | { |
|---|
| 716 | case JSlider.HORIZONTAL: |
|---|
| 717 | int halfThumbWidth = thumbRect.width / 2; |
|---|
| 718 | int sliderLength = xPositionForValue(slider.getExtent()); |
|---|
| 719 | int thumbLeft = currentMouseX - offset + sliderLength; |
|---|
| 720 | int trackLeft = trackRect.x; |
|---|
| 721 | int trackRight = trackRect.x + (trackRect.width - 1); |
|---|
| 722 | int hMin = xPositionForValue(slider.getValue()); |
|---|
| 723 | |
|---|
| 724 | // Handle backwards (inverted) slider |
|---|
| 725 | if (drawInverted()) |
|---|
| 726 | { |
|---|
| 727 | trackRight = hMin; |
|---|
| 728 | } |
|---|
| 729 | else |
|---|
| 730 | { |
|---|
| 731 | trackLeft = hMin; |
|---|
| 732 | } |
|---|
| 733 | // Apply bounds to thumb position. |
|---|
| 734 | thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); |
|---|
| 735 | thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); |
|---|
| 736 | |
|---|
| 737 | setUpperThumbLocation(thumbLeft, thumbRect.y); // repaint slider |
|---|
| 738 | |
|---|
| 739 | // Don't Update slider extent. |
|---|
| 740 | break; |
|---|
| 741 | |
|---|
| 742 | default: |
|---|
| 743 | return; |
|---|
| 744 | } |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | /** |
|---|
| 748 | * Moves the location of the upper thumb, and sets its corresponding |
|---|
| 749 | * value in the slider. |
|---|
| 750 | */ |
|---|
| 751 | private void moveUpperThumb() |
|---|
| 752 | { |
|---|
| 753 | int thumbMiddle = 0; |
|---|
| 754 | |
|---|
| 755 | switch (slider.getOrientation()) |
|---|
| 756 | { |
|---|
| 757 | case JSlider.VERTICAL: |
|---|
| 758 | int halfThumbHeight = thumbRect.height / 2; |
|---|
| 759 | int thumbTop = currentMouseY - offset; |
|---|
| 760 | int trackTop = trackRect.y; |
|---|
| 761 | int trackBottom = trackRect.y + (trackRect.height - 1); |
|---|
| 762 | int vMin = yPositionForValue(slider.getValue()); |
|---|
| 763 | |
|---|
| 764 | // Apply bounds to thumb position. |
|---|
| 765 | if (drawInverted()) |
|---|
| 766 | { |
|---|
| 767 | trackTop = vMin; |
|---|
| 768 | } |
|---|
| 769 | else |
|---|
| 770 | { |
|---|
| 771 | trackBottom = vMin; |
|---|
| 772 | } |
|---|
| 773 | thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); |
|---|
| 774 | thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); |
|---|
| 775 | |
|---|
| 776 | setUpperThumbLocation(thumbRect.x, thumbTop); |
|---|
| 777 | |
|---|
| 778 | // Update slider extent. |
|---|
| 779 | thumbMiddle = thumbTop + halfThumbHeight; |
|---|
| 780 | slider.setExtent(valueForYPosition(thumbMiddle) - slider.getValue()); |
|---|
| 781 | break; |
|---|
| 782 | |
|---|
| 783 | case JSlider.HORIZONTAL: |
|---|
| 784 | int halfThumbWidth = thumbRect.width / 2; |
|---|
| 785 | int thumbLeft = currentMouseX - offset; |
|---|
| 786 | int trackLeft = trackRect.x; |
|---|
| 787 | int trackRight = trackRect.x + (trackRect.width - 1); |
|---|
| 788 | int hMin = xPositionForValue(slider.getValue()); |
|---|
| 789 | |
|---|
| 790 | // Apply bounds to thumb position. |
|---|
| 791 | if (drawInverted()) |
|---|
| 792 | { |
|---|
| 793 | trackRight = hMin; |
|---|
| 794 | } |
|---|
| 795 | else |
|---|
| 796 | { |
|---|
| 797 | trackLeft = hMin; |
|---|
| 798 | } |
|---|
| 799 | thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); |
|---|
| 800 | thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); |
|---|
| 801 | |
|---|
| 802 | setUpperThumbLocation(thumbLeft, thumbRect.y); |
|---|
| 803 | |
|---|
| 804 | // Update slider extent. |
|---|
| 805 | thumbMiddle = thumbLeft + halfThumbWidth; |
|---|
| 806 | slider.setExtent(valueForXPosition(thumbMiddle) - slider.getValue()); |
|---|
| 807 | break; |
|---|
| 808 | |
|---|
| 809 | default: |
|---|
| 810 | return; |
|---|
| 811 | } |
|---|
| 812 | } |
|---|
| 813 | } |
|---|
| 814 | } |
|---|