changeset 379:be7d7a297c3d

6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs Summary: Removed unnecessary code like unused variables, castings, imports etc Reviewed-by: peterz
author rupashka
date Mon, 02 Jun 2008 19:08:13 +0400
parents 5e0172d58a1c
children af37dad9022d
files src/share/classes/javax/swing/JSlider.java src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java src/share/classes/javax/swing/plaf/synth/SynthSliderUI.java
diffstat 3 files changed, 39 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/swing/JSlider.java	Mon May 26 17:58:09 2008 +0400
+++ b/src/share/classes/javax/swing/JSlider.java	Mon Jun 02 19:08:13 2008 +0400
@@ -25,14 +25,12 @@
 
 package javax.swing;
 
-import javax.swing.border.*;
 import javax.swing.event.*;
 import javax.swing.plaf.*;
 import javax.accessibility.*;
 
 import java.io.Serializable;
 import java.io.ObjectOutputStream;
-import java.io.ObjectInputStream;
 import java.io.IOException;
 
 import java.awt.Color;
@@ -409,8 +407,7 @@
      * @since 1.4
      */
     public ChangeListener[] getChangeListeners() {
-        return (ChangeListener[])listenerList.getListeners(
-                ChangeListener.class);
+        return listenerList.getListeners(ChangeListener.class);
     }
 
 
@@ -826,17 +823,16 @@
      * @see JComponent#updateUI
      */
     protected void updateLabelUIs() {
-        if ( getLabelTable() == null ) {
+        Dictionary labelTable = getLabelTable();
+
+        if (labelTable == null) {
             return;
         }
-        Enumeration labels = getLabelTable().keys();
+        Enumeration labels = labelTable.keys();
         while ( labels.hasMoreElements() ) {
-            Object value = getLabelTable().get( labels.nextElement() );
-            if ( value instanceof JComponent ) {
-                JComponent component = (JComponent)value;
-                component.updateUI();
-                component.setSize( component.getPreferredSize()  );
-            }
+            JComponent component = (JComponent) labelTable.get(labels.nextElement());
+            component.updateUI();
+            component.setSize(component.getPreferredSize());
         }
     }
 
@@ -845,11 +841,8 @@
         if (labelTable != null) {
             Enumeration labels = labelTable.elements();
             while (labels.hasMoreElements()) {
-                Object value = labels.nextElement();
-                if (value instanceof JComponent) {
-                    JComponent component = (JComponent)value;
-                    component.setSize(component.getPreferredSize());
-                }
+                JComponent component = (JComponent) labels.nextElement();
+                component.setSize(component.getPreferredSize());
             }
         }
     }
@@ -960,14 +953,14 @@
                 if ( e.getPropertyName().equals( "minimum" ) ||
                      e.getPropertyName().equals( "maximum" ) ) {
 
-                    Enumeration keys = getLabelTable().keys();
-                    Object key = null;
+                    Dictionary labelTable = getLabelTable();
+                    Enumeration keys = labelTable.keys();
                     Hashtable hashtable = new Hashtable();
 
                     // Save the labels that were added by the developer
                     while ( keys.hasMoreElements() ) {
-                        key = keys.nextElement();
-                        Object value = getLabelTable().get( key );
+                        Object key = keys.nextElement();
+                        Object value = labelTable.get(key);
                         if ( !(value instanceof LabelUIResource) ) {
                             hashtable.put( key, value );
                         }
@@ -979,7 +972,7 @@
                     // Add the saved labels
                     keys = hashtable.keys();
                     while ( keys.hasMoreElements() ) {
-                        key = keys.nextElement();
+                        Object key = keys.nextElement();
                         put( key, hashtable.get( key ) );
                     }
 
@@ -996,8 +989,10 @@
 
         SmartHashtable table = new SmartHashtable( increment, start );
 
-        if ( getLabelTable() != null && (getLabelTable() instanceof PropertyChangeListener) ) {
-            removePropertyChangeListener( (PropertyChangeListener)getLabelTable() );
+        Dictionary labelTable = getLabelTable();
+
+        if (labelTable != null && (labelTable instanceof PropertyChangeListener)) {
+            removePropertyChangeListener((PropertyChangeListener) labelTable);
         }
 
         addPropertyChangeListener( table );
--- a/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java	Mon May 26 17:58:09 2008 +0400
+++ b/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java	Mon Jun 02 19:08:13 2008 +0400
@@ -26,14 +26,11 @@
 package javax.swing.plaf.basic;
 
 import java.awt.Component;
-import java.awt.Container;
-import java.awt.Adjustable;
 import java.awt.event.*;
 import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Dimension;
 import java.awt.Rectangle;
-import java.awt.Point;
 import java.awt.Insets;
 import java.awt.Color;
 import java.awt.IllegalComponentStateException;
@@ -42,8 +39,6 @@
 import java.util.Dictionary;
 import java.util.Enumeration;
 
-import javax.swing.border.AbstractBorder;
-
 import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.plaf.*;
@@ -409,7 +404,7 @@
                 Enumeration elements = dictionary.elements();
                 int baseline = -1;
                 while (elements.hasMoreElements()) {
-                    Component label = (Component)elements.nextElement();
+                    JComponent label = (JComponent) elements.nextElement();
                     Dimension pref = label.getPreferredSize();
                     int labelBaseline = label.getBaseline(pref.width,
                                                           pref.height);
@@ -634,7 +629,7 @@
 
 
     protected void calculateTrackRect() {
-        int centerSpacing = 0; // used to center sliders added using BorderLayout.CENTER (bug 4275631)
+        int centerSpacing; // used to center sliders added using BorderLayout.CENTER (bug 4275631)
         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
             centerSpacing = thumbRect.height;
             if ( slider.getPaintTicks() ) centerSpacing += getTickLength();
@@ -764,7 +759,7 @@
         if ( dictionary != null ) {
             Enumeration keys = dictionary.keys();
             while ( keys.hasMoreElements() ) {
-                Component label = (Component)dictionary.get( keys.nextElement() );
+                JComponent label = (JComponent) dictionary.get(keys.nextElement());
                 widest = Math.max( label.getPreferredSize().width, widest );
             }
         }
@@ -777,7 +772,7 @@
         if ( dictionary != null ) {
             Enumeration keys = dictionary.keys();
             while ( keys.hasMoreElements() ) {
-                Component label = (Component)dictionary.get( keys.nextElement() );
+                JComponent label = (JComponent) dictionary.get(keys.nextElement());
                 tallest = Math.max( label.getPreferredSize().height, tallest );
             }
         }
@@ -1001,22 +996,14 @@
 
     public void paintTicks(Graphics g)  {
         Rectangle tickBounds = tickRect;
-        int i;
-        int maj, min, max;
-        int w = tickBounds.width;
-        int h = tickBounds.height;
-        int centerEffect, tickHeight;
 
         g.setColor(DefaultLookup.getColor(slider, this, "Slider.tickColor", Color.black));
 
-        maj = slider.getMajorTickSpacing();
-        min = slider.getMinorTickSpacing();
-
         if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
            g.translate( 0, tickBounds.y);
 
             int value = slider.getMinimum();
-            int xPos = 0;
+            int xPos;
 
             if ( slider.getMinorTickSpacing() > 0 ) {
                 while ( value <= slider.getMaximum() ) {
@@ -1042,7 +1029,7 @@
            g.translate(tickBounds.x, 0);
 
             int value = slider.getMinimum();
-            int yPos = 0;
+            int yPos;
 
             if ( slider.getMinorTickSpacing() > 0 ) {
                 int offset = 0;
@@ -1111,10 +1098,9 @@
                 Integer key = (Integer)keys.nextElement();
                 int value = key.intValue();
                 if (value >= minValue && value <= maxValue) {
-                    Component label = (Component)dictionary.get( key );
-                    if (label instanceof JComponent) {
-                        ((JComponent)label).setEnabled(enabled);
-                    }
+                    JComponent label = (JComponent) dictionary.get(key);
+                    label.setEnabled(enabled);
+
                     if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
                         g.translate( 0, labelBounds.y );
                         paintHorizontalLabel( g, value, label );
@@ -1364,7 +1350,7 @@
         int min = slider.getMinimum();
         int max = slider.getMaximum();
         double valueRange = (double)max - (double)min;
-        double pixelsPerValue = (double)trackHeight / (double)valueRange;
+        double pixelsPerValue = (double)trackHeight / valueRange;
         int trackBottom = trackY + (trackHeight - 1);
         int yPosition;
 
@@ -1715,7 +1701,7 @@
         * of the thumb relative to the origin of the track.
         */
         public void mouseDragged(MouseEvent e) {
-            int thumbMiddle = 0;
+            int thumbMiddle;
 
             if (!slider.isEnabled()) {
                 return;
@@ -1841,7 +1827,7 @@
         public void componentResized(ComponentEvent e)  {
             getHandler().componentResized(e);
         }
-    };
+    }
 
     /**
      * Focus-change listener.
@@ -1903,7 +1889,7 @@
             return b;
         }
 
-    };
+    }
 
 
     /**
--- a/src/share/classes/javax/swing/plaf/synth/SynthSliderUI.java	Mon May 26 17:58:09 2008 +0400
+++ b/src/share/classes/javax/swing/plaf/synth/SynthSliderUI.java	Mon Jun 02 19:08:13 2008 +0400
@@ -25,26 +25,17 @@
 
 package javax.swing.plaf.synth;
 
-import java.awt.Component;
-import java.awt.Container;
-import java.awt.Adjustable;
 import java.awt.event.*;
 import java.awt.Graphics;
 import java.awt.Dimension;
-import java.awt.Font;
 import java.awt.FontMetrics;
 import java.awt.Rectangle;
 import java.awt.Point;
 import java.awt.Insets;
-import java.awt.Color;
-import java.awt.IllegalComponentStateException;
-import java.awt.Polygon;
 import java.beans.*;
 import java.util.Dictionary;
 import java.util.Enumeration;
-import javax.swing.border.AbstractBorder;
 import javax.swing.*;
-import javax.swing.event.*;
 import javax.swing.plaf.*;
 import javax.swing.plaf.basic.BasicSliderUI;
 import sun.swing.plaf.synth.SynthUI;
@@ -203,8 +194,7 @@
                 centerY += valueHeight + 2;
                 centerY += trackHeight + trackInsets.top + trackInsets.bottom;
                 centerY += tickHeight + 2;
-                Component label = (Component)slider.getLabelTable().
-                                   elements().nextElement();
+                JComponent label = (JComponent) slider.getLabelTable().elements().nextElement();
                 Dimension pref = label.getPreferredSize();
                 return centerY + label.getBaseline(pref.width, pref.height);
             }
@@ -226,8 +216,7 @@
                     int trackHeight = contentHeight - valueHeight;
                     int yPosition = yPositionForValue(value.intValue(), trackY,
                                                       trackHeight);
-                    Component label = (Component)slider.getLabelTable().
-                            get(value);
+                    JComponent label = (JComponent) slider.getLabelTable().get(value);
                     Dimension pref = label.getPreferredSize();
                     return yPosition - pref.height / 2 +
                             label.getBaseline(pref.width, pref.height);
@@ -434,16 +423,14 @@
     /**
      * Calculates the pad for the label at the specified index.
      *
-     * @param index index of the label to calculate pad for.
+     * @param i index of the label to calculate pad for.
      * @return padding required to keep label visible.
      */
     private int getPadForLabel(int i) {
-        Dictionary dictionary = slider.getLabelTable();
         int pad = 0;
 
-        Object o = dictionary.get(i);
-        if (o != null) {
-            Component c = (Component)o;
+        JComponent c = (JComponent) slider.getLabelTable().get(i);
+        if (c != null) {
             int centerX = xPositionForValue(i);
             int cHalfWidth = c.getPreferredSize().width / 2;
             if (centerX - cHalfWidth < insetCache.left) {
@@ -500,8 +487,6 @@
         }
     }
 
-    private static Rectangle unionRect = new Rectangle();
-
     public void setThumbLocation(int x, int y) {
         super.setThumbLocation(x, y);
         // Value rect is tied to the thumb location.  We need to repaint when
@@ -544,7 +529,7 @@
                 trackBorder;
         int trackLength = trackBottom - trackTop;
         double valueRange = (double)max - (double)min;
-        double pixelsPerValue = (double)trackLength / (double)valueRange;
+        double pixelsPerValue = (double)trackLength / valueRange;
         int yPosition;
 
         if (!drawInverted()) {
@@ -802,8 +787,7 @@
         }
 
         public void mouseDragged(MouseEvent e) {
-            SynthScrollBarUI ui;
-            int thumbMiddle = 0;
+            int thumbMiddle;
 
             if (!slider.isEnabled()) {
                 return;