Changeset 26 in tmcsimulator-scriptbuilder


Ignore:
Timestamp:
07/29/2017 02:22:14 PM (9 years ago)
Author:
jdalbey
Message:

RangeSliderUI.java: Added popup menu (with stubs for actions)

Location:
trunk/src/scriptbuilder/gui/drawers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/scriptbuilder/gui/drawers/RangeSlider.java

    r25 r26  
     1package scriptbuilder.gui.drawers; 
    12 
    23import javax.swing.JSlider; 
     4 
    35/* 
    46The MIT License 
     
    2325OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
    2426THE SOFTWARE. 
    25 */ 
     27 */ 
    2628 
    2729/** 
     
    2931 * The thumb controls are used to select the lower and upper value of a range 
    3032 * 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 
    3437 * returned by RangeSlider is simply the lower value plus the extent.</p> 
    3538 */ 
    36 public class RangeSlider extends JSlider { 
     39public class RangeSlider extends JSlider 
     40{ 
    3741 
    3842    /** 
    39      * Constructs a RangeSlider with default minimum and maximum values of 0 
    40      * and 100. 
     43     * Constructs a RangeSlider with default minimum and maximum values of 0 and 
     44     * 100. 
    4145     */ 
    42     public RangeSlider() { 
     46    public RangeSlider() 
     47    { 
    4348        initSlider(); 
    4449    } 
    4550 
    4651    /** 
    47      * Constructs a RangeSlider with the specified default minimum and maximum  
     52     * Constructs a RangeSlider with the specified default minimum and maximum 
    4853     * values. 
    4954     */ 
    50     public RangeSlider(int min, int max) { 
     55    public RangeSlider(int min, int max) 
     56    { 
    5157        super(min, max); 
    5258        initSlider(); 
     
    5662     * Initializes the slider by setting default properties. 
    5763     */ 
    58     private void initSlider() { 
     64    private void initSlider() 
     65    { 
    5966        setOrientation(HORIZONTAL); 
    6067    } 
     
    6572     */ 
    6673    @Override 
    67     public void updateUI() { 
     74    public void updateUI() 
     75    { 
    6876        setUI(new RangeSliderUI(this)); 
    6977        // Update UI for slider labels.  This must be called after updating the 
     
    7684     */ 
    7785    @Override 
    78     public int getValue() { 
     86    public int getValue() 
     87    { 
    7988        return super.getValue(); 
    8089    } 
     
    8493     */ 
    8594    @Override 
    86     public void setValue(int value) { 
     95    public void setValue(int value) 
     96    { 
    8797        int oldValue = getValue(); 
    88         if (oldValue == value) { 
     98        if (oldValue == value) 
     99        { 
    89100            return; 
    90101        } 
     
    96107 
    97108        // 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()); 
    100111    } 
    101112 
     
    103114     * Returns the upper value in the range. 
    104115     */ 
    105     public int getUpperValue() { 
     116    public int getUpperValue() 
     117    { 
    106118        return getValue() + getExtent(); 
    107119    } 
     
    110122     * Sets the upper value in the range. 
    111123     */ 
    112     public void setUpperValue(int value) { 
     124    public void setUpperValue(int value) 
     125    { 
    113126        // Compute new extent. 
    114127        int lowerValue = getValue(); 
    115128        int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum() - lowerValue); 
    116          
     129 
    117130        // Set extent to set upper value. 
    118131        setExtent(newExtent); 
  • trunk/src/scriptbuilder/gui/drawers/RangeSliderDemo.java

    r25 r26  
     1package scriptbuilder.gui.drawers; 
    12 
    23import java.awt.BorderLayout; 
     
    1415import javax.swing.event.ChangeEvent; 
    1516import javax.swing.event.ChangeListener; 
     17 
    1618/* 
    1719The MIT License 
     
    3638OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
    3739THE SOFTWARE. 
    38 */ 
     40 */ 
    3941 
    4042/** 
    4143 * Demo application panel to display a range slider. 
    4244 */ 
    43 public class RangeSliderDemo extends JPanel { 
     45public class RangeSliderDemo extends JPanel 
     46{ 
    4447 
    4548    private JLabel rangeSliderLabel1 = new JLabel(); 
     
    4952    private RangeSlider rangeSlider = new RangeSlider(); 
    5053 
    51     public RangeSliderDemo() { 
     54    public RangeSliderDemo() 
     55    { 
    5256        setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); 
    5357        setLayout(new GridBagLayout()); 
    54          
     58 
    5559        rangeSliderLabel1.setText("Lower value:"); 
    5660        rangeSliderLabel2.setText("Upper value:"); 
    5761        rangeSliderValue1.setHorizontalAlignment(JLabel.LEFT); 
    5862        rangeSliderValue2.setHorizontalAlignment(JLabel.LEFT); 
    59          
     63 
    6064        rangeSlider.setPreferredSize(new Dimension(240, rangeSlider.getPreferredSize().height)); 
    6165        rangeSlider.setMinimum(0); 
    6266        rangeSlider.setMaximum(10); 
    63          
     67 
    6468        // 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            { 
    6773                RangeSlider slider = (RangeSlider) e.getSource(); 
    6874                rangeSliderValue1.setText(String.valueOf(slider.getValue())); 
     
    7278 
    7379        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)); 
    7581        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)); 
    7783        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)); 
    7985        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)); 
    8389    } 
    84      
    85     public void display() { 
     90 
     91    public void display() 
     92    { 
    8693        // Initialize values. 
    8794        rangeSlider.setValue(3); 
    8895        rangeSlider.setUpperValue(7); 
    89          
     96 
    9097        // Initialize value display. 
    9198        rangeSliderValue1.setText(String.valueOf(rangeSlider.getValue())); 
    9299        rangeSliderValue2.setText(String.valueOf(rangeSlider.getUpperValue())); 
    93          
     100 
    94101        // Create window frame. 
    95102        JFrame frame = new JFrame(); 
     
    97104        frame.setResizable(false); 
    98105        frame.setTitle("Range Slider Demo"); 
    99          
     106 
    100107        // Set window content and validate. 
    101108        frame.getContentPane().setLayout(new BorderLayout()); 
    102109        frame.getContentPane().add(this, BorderLayout.CENTER); 
    103110        frame.pack(); 
    104          
     111 
    105112        // Set window location and display. 
    106113        frame.setLocationRelativeTo(null); 
    107114        frame.setVisible(true); 
    108115    } 
    109      
     116 
    110117    /** 
    111118     * Main application method. 
     119     * 
    112120     * @param args String[] 
    113121     */ 
    114     public static void main(String[] args) { 
    115         try { 
     122    public static void main(String[] args) 
     123    { 
     124        try 
     125        { 
    116126            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    117         } catch (Exception ex) { 
     127        } 
     128        catch (Exception ex) 
     129        { 
    118130            ex.printStackTrace(); 
    119131        } 
    120132 
    121         SwingUtilities.invokeLater(new Runnable() { 
    122             public void run() { 
     133        SwingUtilities.invokeLater(new Runnable() 
     134        { 
     135            public void run() 
     136            { 
    123137                new RangeSliderDemo().display(); 
    124138            } 
  • trunk/src/scriptbuilder/gui/drawers/RangeSliderUI.java

    r25 r26  
     1package scriptbuilder.gui.drawers; 
     2 
    13import java.awt.Color; 
    24import java.awt.Dimension; 
     
    68import java.awt.RenderingHints; 
    79import java.awt.Shape; 
     10import java.awt.event.ActionEvent; 
     11import java.awt.event.ActionListener; 
    812import java.awt.event.MouseEvent; 
    913import java.awt.geom.Ellipse2D; 
    1014 
    1115import javax.swing.JComponent; 
     16import javax.swing.JMenuItem; 
     17import javax.swing.JPopupMenu; 
    1218import javax.swing.JSlider; 
    1319import javax.swing.SwingUtilities; 
     
    1521import javax.swing.event.ChangeListener; 
    1622import javax.swing.plaf.basic.BasicSliderUI; 
     23 
    1724/* 
    1825The MIT License 
     
    3744OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
    3845THE SOFTWARE. 
    39 */ 
    40  
     46 */ 
    4147/** 
    42  * UI delegate for the RangeSlider component.  RangeSliderUI paints two thumbs, 
     48 * UI delegate for the RangeSlider component. RangeSliderUI paints two thumbs, 
    4349 * one for the lower value and one for the upper value. 
    4450 */ 
    45 class RangeSliderUI extends BasicSliderUI { 
    46  
    47     /** Color of selected range. */ 
     51class RangeSliderUI extends BasicSliderUI 
     52{ 
     53 
     54    /** 
     55     * Color of selected range. 
     56     */ 
    4857    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     */ 
    5162    private Rectangle upperThumbRect; 
    52     /** Indicator that determines whether upper thumb is selected. */ 
     63    /** 
     64     * Indicator that determines whether upper thumb is selected. 
     65     */ 
    5366    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     */ 
    5671    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     */ 
    5875    private transient boolean upperDragging; 
    59      
     76 
    6077    /** 
    6178     * Constructs a RangeSliderUI for the specified slider component. 
     79     * 
    6280     * @param b RangeSlider 
    6381     */ 
    64     public RangeSliderUI(RangeSlider b) { 
     82    public RangeSliderUI(RangeSlider b) 
     83    { 
    6584        super(b); 
    6685    } 
    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    { 
    7393        upperThumbRect = new Rectangle(); 
    7494        super.installUI(c); 
     
    7999     */ 
    80100    @Override 
    81     protected TrackListener createTrackListener(JSlider slider) { 
     101    protected TrackListener createTrackListener(JSlider slider) 
     102    { 
    82103        return new RangeTrackListener(); 
    83104    } 
     
    87108     */ 
    88109    @Override 
    89     protected ChangeListener createChangeListener(JSlider slider) { 
     110    protected ChangeListener createChangeListener(JSlider slider) 
     111    { 
    90112        return new ChangeHandler(); 
    91113    } 
    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    { 
    98121        // Call superclass method for lower thumb size. 
    99122        super.calculateThumbSize(); 
    100          
     123 
    101124        // Set upper thumb size. 
    102125        upperThumbRect.setSize(thumbRect.width, thumbRect.height); 
    103126    } 
    104      
     127 
    105128    /** 
    106129     * Updates the locations for both thumbs. 
    107130     */ 
    108131    @Override 
    109     protected void calculateThumbLocation() { 
     132    protected void calculateThumbLocation() 
     133    { 
    110134        // Call superclass method for lower thumb location. 
    111135        super.calculateThumbLocation(); 
    112          
     136 
    113137        // Adjust upper value to snap to ticks if necessary. 
    114         if (slider.getSnapToTicks()) { 
     138        if (slider.getSnapToTicks()) 
     139        { 
    115140            int upperValue = slider.getValue() + slider.getExtent(); 
    116             int snappedValue = upperValue;  
     141            int snappedValue = upperValue; 
    117142            int majorTickSpacing = slider.getMajorTickSpacing(); 
    118143            int minorTickSpacing = slider.getMinorTickSpacing(); 
    119144            int tickSpacing = 0; 
    120              
    121             if (minorTickSpacing > 0) { 
     145 
     146            if (minorTickSpacing > 0) 
     147            { 
    122148                tickSpacing = minorTickSpacing; 
    123             } else if (majorTickSpacing > 0) { 
     149            } 
     150            else if (majorTickSpacing > 0) 
     151            { 
    124152                tickSpacing = majorTickSpacing; 
    125153            } 
    126154 
    127             if (tickSpacing != 0) { 
     155            if (tickSpacing != 0) 
     156            { 
    128157                // 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; 
    131161                    int whichTick = Math.round(temp); 
    132162                    snappedValue = slider.getMinimum() + (whichTick * tickSpacing); 
    133163                } 
    134164 
    135                 if (snappedValue != upperValue) {  
     165                if (snappedValue != upperValue) 
     166                { 
    136167                    slider.setExtent(snappedValue - slider.getValue()); 
    137168                } 
    138169            } 
    139170        } 
    140          
    141         // Calculate upper thumb location.  The thumb is centered over its  
     171 
     172        // Calculate upper thumb location.  The thumb is centered over its 
    142173        // value on the track. 
    143         if (slider.getOrientation() == JSlider.HORIZONTAL) { 
     174        if (slider.getOrientation() == JSlider.HORIZONTAL) 
     175        { 
    144176            int upperPosition = xPositionForValue(slider.getValue() + slider.getExtent()); 
    145177            upperThumbRect.x = upperPosition - (upperThumbRect.width / 2); 
    146178            upperThumbRect.y = trackRect.y; 
    147              
    148         } else { 
     179 
     180        } 
     181        else 
     182        { 
    149183            int upperPosition = yPositionForValue(slider.getValue() + slider.getExtent()); 
    150184            upperThumbRect.x = trackRect.x; 
     
    152186        } 
    153187    } 
    154      
     188 
    155189    /** 
    156190     * Returns the size of a thumb. 
    157191     */ 
    158192    @Override 
    159     protected Dimension getThumbSize() { 
     193    protected Dimension getThumbSize() 
     194    { 
    160195        return new Dimension(12, 12); 
    161196    } 
    162197 
    163198    /** 
    164      * Paints the slider.  The selected thumb is always painted on top of the 
     199     * Paints the slider. The selected thumb is always painted on top of the 
    165200     * other thumb. 
    166201     */ 
    167202    @Override 
    168     public void paint(Graphics g, JComponent c) { 
     203    public void paint(Graphics g, JComponent c) 
     204    { 
    169205        super.paint(g, c); 
    170          
     206 
    171207        Rectangle clipRect = g.getClipBounds(); 
    172         if (upperThumbSelected) { 
     208        if (upperThumbSelected) 
     209        { 
    173210            // Paint lower thumb first, then upper thumb. 
    174             if (clipRect.intersects(thumbRect)) { 
     211            if (clipRect.intersects(thumbRect)) 
     212            { 
    175213                paintLowerThumb(g); 
    176214            } 
    177             if (clipRect.intersects(upperThumbRect)) { 
     215            if (clipRect.intersects(upperThumbRect)) 
     216            { 
    178217                paintUpperThumb(g); 
    179218            } 
    180              
    181         } else { 
     219 
     220        } 
     221        else 
     222        { 
    182223            // Paint upper thumb first, then lower thumb. 
    183             if (clipRect.intersects(upperThumbRect)) { 
     224            if (clipRect.intersects(upperThumbRect)) 
     225            { 
    184226                paintUpperThumb(g); 
    185227            } 
    186             if (clipRect.intersects(thumbRect)) { 
     228            if (clipRect.intersects(thumbRect)) 
     229            { 
    187230                paintLowerThumb(g); 
    188231            } 
    189232        } 
    190233    } 
    191      
     234 
    192235    /** 
    193236     * Paints the track. 
    194237     */ 
    195238    @Override 
    196     public void paintTrack(Graphics g) { 
     239    public void paintTrack(Graphics g) 
     240    { 
    197241        // Draw track. 
    198242        super.paintTrack(g); 
    199          
     243 
    200244        Rectangle trackBounds = trackRect; 
    201          
    202         if (slider.getOrientation() == JSlider.HORIZONTAL) { 
     245 
     246        if (slider.getOrientation() == JSlider.HORIZONTAL) 
     247        { 
    203248            // Determine position of selected range by moving from the middle 
    204249            // of one thumb to the other. 
    205250            int lowerX = thumbRect.x + (thumbRect.width / 2); 
    206251            int upperX = upperThumbRect.x + (upperThumbRect.width / 2); 
    207              
     252 
    208253            // Determine track position. 
    209254            int cy = (trackBounds.height / 2) - 2; 
     
    212257            Color oldColor = g.getColor(); 
    213258            g.translate(trackBounds.x, trackBounds.y + cy); 
    214              
     259 
    215260            // Draw selected range. 
    216261            g.setColor(rangeColor); 
    217             for (int y = 0; y <= 3; y++) { 
     262            for (int y = 0; y <= 3; y++) 
     263            { 
    218264                g.drawLine(lowerX - trackBounds.x, y, upperX - trackBounds.x, y); 
    219265            } 
     
    222268            g.translate(-trackBounds.x, -(trackBounds.y + cy)); 
    223269            g.setColor(oldColor); 
    224              
    225         } else { 
     270 
     271        } 
     272        else 
     273        { 
    226274            // Determine position of selected range by moving from the middle 
    227275            // of one thumb to the other. 
    228276            int lowerY = thumbRect.x + (thumbRect.width / 2); 
    229277            int upperY = upperThumbRect.x + (upperThumbRect.width / 2); 
    230              
     278 
    231279            // Determine track position. 
    232280            int cx = (trackBounds.width / 2) - 2; 
     
    238286            // Draw selected range. 
    239287            g.setColor(rangeColor); 
    240             for (int x = 0; x <= 3; x++) { 
     288            for (int x = 0; x <= 3; x++) 
     289            { 
    241290                g.drawLine(x, lowerY - trackBounds.y, x, upperY - trackBounds.y); 
    242291            } 
    243              
     292 
    244293            // Restore position and color. 
    245294            g.translate(-(trackBounds.x + cx), -trackBounds.y); 
     
    247296        } 
    248297    } 
    249      
    250     /** 
    251      * Overrides superclass method to do nothing.  Thumb painting is handled 
     298 
     299    /** 
     300     * Overrides superclass method to do nothing. Thumb painting is handled 
    252301     * within the <code>paint()</code> method. 
    253302     */ 
    254303    @Override 
    255     public void paintThumb(Graphics g) { 
     304    public void paintThumb(Graphics g) 
     305    { 
    256306        // Do nothing. 
    257307    } 
     
    260310     * Paints the thumb for the lower value using the specified graphics object. 
    261311     */ 
    262     private void paintLowerThumb(Graphics g) { 
     312    private void paintLowerThumb(Graphics g) 
     313    { 
    263314        Rectangle knobBounds = thumbRect; 
    264315        int w = knobBounds.width; 
    265         int h = knobBounds.height;       
    266          
     316        int h = knobBounds.height; 
     317 
    267318        // Create graphics copy. 
    268319        Graphics2D g2d = (Graphics2D) g.create(); 
     
    273324        // Draw thumb. 
    274325        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    275             RenderingHints.VALUE_ANTIALIAS_ON); 
     326                RenderingHints.VALUE_ANTIALIAS_ON); 
    276327        g2d.translate(knobBounds.x, knobBounds.y); 
    277328 
     
    281332        g2d.setColor(Color.BLUE); 
    282333        g2d.draw(thumbShape); 
    283          
     334 
    284335        // Dispose graphics. 
    285336        g2d.dispose(); 
    286337    } 
    287      
     338 
    288339    /** 
    289340     * Paints the thumb for the upper value using the specified graphics object. 
    290341     */ 
    291     private void paintUpperThumb(Graphics g) { 
     342    private void paintUpperThumb(Graphics g) 
     343    { 
    292344        Rectangle knobBounds = upperThumbRect; 
    293345        int w = knobBounds.width; 
    294         int h = knobBounds.height;       
    295          
     346        int h = knobBounds.height; 
     347 
    296348        // Create graphics copy. 
    297349        Graphics2D g2d = (Graphics2D) g.create(); 
     
    302354        // Draw thumb. 
    303355        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    304             RenderingHints.VALUE_ANTIALIAS_ON); 
     356                RenderingHints.VALUE_ANTIALIAS_ON); 
    305357        g2d.translate(knobBounds.x, knobBounds.y); 
    306358 
     
    318370     * Returns a Shape representing a thumb. 
    319371     */ 
    320     private Shape createThumbShape(int width, int height) { 
     372    private Shape createThumbShape(int width, int height) 
     373    { 
    321374        // Use circular shape. 
    322375        Ellipse2D shape = new Ellipse2D.Double(0, 0, width, height); 
    323376        return shape; 
    324377    } 
    325      
    326     /**  
    327      * Sets the location of the upper thumb, and repaints the slider.  This is 
    328      * called when the upper thumb is dragged to repaint the slider.  The 
     378 
     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 
    329382     * <code>setThumbLocation()</code> method performs the same task for the 
    330383     * lower thumb. 
    331384     */ 
    332     private void setUpperThumbLocation(int x, int y) { 
     385    private void setUpperThumbLocation(int x, int y) 
     386    { 
    333387        Rectangle upperUnionRect = new Rectangle(); 
    334388        upperUnionRect.setBounds(upperThumbRect); 
     
    339393        slider.repaint(upperUnionRect.x, upperUnionRect.y, upperUnionRect.width, upperUnionRect.height); 
    340394    } 
    341      
     395 
    342396    /** 
    343397     * Moves the selected thumb in the specified direction by a block increment. 
    344398     * This method is called when the user presses the Page Up or Down keys. 
    345399     */ 
    346     public void scrollByBlock(int direction) { 
    347         synchronized (slider) { 
     400    public void scrollByBlock(int direction) 
     401    { 
     402        synchronized (slider) 
     403        { 
    348404            int blockIncrement = (slider.getMaximum() - slider.getMinimum()) / 10; 
    349             if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) { 
     405            if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) 
     406            { 
    350407                blockIncrement = 1; 
    351408            } 
    352409            int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 
    353              
    354             if (upperThumbSelected) { 
     410 
     411            if (upperThumbSelected) 
     412            { 
    355413                int oldValue = ((RangeSlider) slider).getUpperValue(); 
    356414                ((RangeSlider) slider).setUpperValue(oldValue + delta); 
    357             } else { 
     415            } 
     416            else 
     417            { 
    358418                int oldValue = slider.getValue(); 
    359419                slider.setValue(oldValue + delta); 
     
    361421        } 
    362422    } 
    363      
     423 
    364424    /** 
    365425     * Moves the selected thumb in the specified direction by a unit increment. 
    366426     * This method is called when the user presses one of the arrow keys. 
    367427     */ 
    368     public void scrollByUnit(int direction) { 
    369         synchronized (slider) { 
     428    public void scrollByUnit(int direction) 
     429    { 
     430        synchronized (slider) 
     431        { 
    370432            int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 
    371              
    372             if (upperThumbSelected) { 
     433 
     434            if (upperThumbSelected) 
     435            { 
    373436                int oldValue = ((RangeSlider) slider).getUpperValue(); 
    374437                ((RangeSlider) slider).setUpperValue(oldValue + delta); 
    375             } else { 
     438            } 
     439            else 
     440            { 
    376441                int oldValue = slider.getValue(); 
    377442                slider.setValue(oldValue + delta); 
    378443            } 
    379         }        
    380     } 
    381      
    382     /** 
    383      * Listener to handle model change events.  This calculates the thumb  
     444        } 
     445    } 
     446 
     447    /** 
     448     * Listener to handle model change events. This calculates the thumb 
    384449     * locations and repaints the slider if the value change is not caused by 
    385450     * dragging a thumb. 
    386451     */ 
    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            { 
    390459                calculateThumbLocation(); 
    391460                slider.repaint(); 
     
    393462        } 
    394463    } 
    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 
    396499    /** 
    397500     * Listener to handle mouse movements in the slider track. 
    398501     */ 
    399     public class RangeTrackListener extends TrackListener { 
    400          
     502    public class RangeTrackListener extends TrackListener 
     503    { 
     504 
    401505        @Override 
    402         public void mousePressed(MouseEvent e) { 
    403             if (!slider.isEnabled()) { 
     506        public void mousePressed(MouseEvent e) 
     507        { 
     508            if (!slider.isEnabled()) 
     509            { 
    404510                return; 
    405511            } 
     
    408514            currentMouseY = e.getY(); 
    409515 
    410             if (slider.isRequestFocusEnabled()) { 
     516            if (slider.isRequestFocusEnabled()) 
     517            { 
    411518                slider.requestFocus(); 
    412519            } 
    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 
    415527            // selected (last one dragged), then check its position first; 
    416528            // otherwise check the position of the lower thumb first. 
    417529            boolean lowerPressed = false; 
    418530            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                { 
    421535                    upperPressed = true; 
    422                 } else if (thumbRect.contains(currentMouseX, currentMouseY)) { 
     536                } 
     537                else if (thumbRect.contains(currentMouseX, currentMouseY)) 
     538                { 
    423539                    lowerPressed = true; 
    424540                } 
    425             } else { 
    426                 if (thumbRect.contains(currentMouseX, currentMouseY)) { 
     541            } 
     542            else 
     543            { 
     544                if (thumbRect.contains(currentMouseX, currentMouseY)) 
     545                { 
    427546                    lowerPressed = true; 
    428                 } else if (upperThumbRect.contains(currentMouseX, currentMouseY)) { 
     547                } 
     548                else if (upperThumbRect.contains(currentMouseX, currentMouseY)) 
     549                { 
    429550                    upperPressed = true; 
    430551                } 
     
    432553 
    433554            // 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; 
    442565                } 
    443566                upperThumbSelected = false; 
     
    446569            } 
    447570            lowerDragging = false; 
    448              
     571 
    449572            // 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; 
    458583                } 
    459584                upperThumbSelected = true; 
     
    463588            upperDragging = false; 
    464589        } 
    465          
     590 
    466591        @Override 
    467         public void mouseReleased(MouseEvent e) { 
     592        public void mouseReleased(MouseEvent e) 
     593        { 
    468594            lowerDragging = false; 
    469595            upperDragging = false; 
     
    471597            super.mouseReleased(e); 
    472598        } 
    473          
     599 
    474600        @Override 
    475         public void mouseDragged(MouseEvent e) { 
    476             if (!slider.isEnabled()) { 
     601        public void mouseDragged(MouseEvent e) 
     602        { 
     603            if (!slider.isEnabled()) 
     604            { 
    477605                return; 
    478606            } 
     
    481609            currentMouseY = e.getY(); 
    482610 
    483             if (lowerDragging) { 
     611            if (lowerDragging) 
     612            { 
    484613                slider.setValueIsAdjusting(true); 
    485614                moveLowerThumb(); 
    486                  
    487             } else if (upperDragging) { 
     615 
     616            } 
     617            else if (upperDragging) 
     618            { 
    488619                slider.setValueIsAdjusting(true); 
    489620                moveUpperThumb(); 
    490621            } 
    491622        } 
    492          
     623 
    493624        @Override 
    494         public boolean shouldScroll(int direction) { 
     625        public boolean shouldScroll(int direction) 
     626        { 
    495627            return false; 
    496628        } 
    497629 
    498630        /** 
    499          * Moves the location of the lower thumb, and sets its corresponding  
     631         * Moves the location of the lower thumb, and sets its corresponding 
    500632         * value in the slider. 
    501633         */ 
    502         private void moveLowerThumb() { 
     634        private void moveLowerThumb() 
     635        { 
    503636            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; 
    554694            } 
    555695        } 
    556696 
    557697        /** 
    558          * Moves the location of the upper thumb, and sets its corresponding  
     698         * Moves the location of the upper thumb, and sets its corresponding 
    559699         * value in the slider. 
    560700         */ 
    561         private void moveUpperThumb() { 
     701        private void moveUpperThumb() 
     702        { 
    562703            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; 
    613761            } 
    614762        } 
Note: See TracChangeset for help on using the changeset viewer.