]> source.dussan.org Git - vaadin-framework.git/commitdiff
Code cleanup and documentation. Ready for first code-review.
authorJouni Koivuviita <jouni.koivuviita@itmill.com>
Mon, 1 Oct 2007 07:49:27 +0000 (07:49 +0000)
committerJouni Koivuviita <jouni.koivuviita@itmill.com>
Mon, 1 Oct 2007 07:49:27 +0000 (07:49 +0000)
svn changeset:2398/svn branch:trunk

src/com/itmill/toolkit/ui/Slider.java

index 5210093104f188abfcc6e79135eb4feaece893b7..495eac6dc877995c8adef424ae2db3b4c7851b58 100644 (file)
@@ -6,71 +6,93 @@ import com.itmill.toolkit.terminal.PaintException;
 import com.itmill.toolkit.terminal.PaintTarget;\r
 \r
 public class Slider extends AbstractField {\r
-       \r
+\r
        public static final int ORIENTATION_HORIZONTAL = 0;\r
+\r
        public static final int ORIENTATION_VERTICAL = 1;\r
-       \r
+\r
        /** Minimum value of slider */\r
        private double min = 0;\r
+\r
        /** Maximum value of slider */\r
        private double max = 100;\r
-       \r
+\r
        /**\r
         * Resolution, how many digits are considered relevant after desimal point.\r
-        * Must be a non-negative value \r
+        * Must be a non-negative value\r
         */\r
        private int resolution = 0;\r
-       \r
+\r
        /**\r
-        * Slider orientation (horizontal==default/vertical).\r
+        * Slider orientation (horizontal/vertical), defaults .\r
         */\r
        private int orientation = ORIENTATION_HORIZONTAL;\r
-       \r
+\r
        /**\r
-        * Slider size in pixels.\r
-        * In horizontal mode if set to -1, allow 100% with container.\r
-        * In vertical mode if set to -1, default height 120 pixels.\r
+        * Slider size in pixels. In horizontal mode, if set to -1, allow 100% width\r
+        * of container. In vertical mode, if set to -1, default height is\r
+        * determined by the client-side implementation.\r
         */\r
        private int size = -1;\r
-       \r
+\r
        /**\r
-        * Handle size in percents related to base size.\r
-        * Must be a value between 1-99. Other values are converted to nearest bound.\r
-        * A negative value sets the width to auto (client calculates).\r
+        * Handle (draggable control element) size in percents relative to base\r
+        * size. Must be a value between 1-99. Other values are converted to nearest\r
+        * bound. A negative value sets the width to auto (client-side\r
+        * implementation calculates).\r
         */\r
        private int handleSize = -1;\r
-       \r
+\r
        /**\r
-        * Show arrows that can be pressed to slide the \r
-        * handle in some increments (client-side \r
-        * implementation decides the increment).\r
+        * Show arrows that can be pressed to slide the handle in some increments\r
+        * (client-side implementation decides the increment, usually somewhere\r
+        * between 5-10% of slide range).\r
         */\r
        private boolean arrows = true;\r
-       \r
+\r
+       /**\r
+        * Default Slider constructor. Sets all values to defaults and the slide\r
+        * handle at minimum value.\r
+        * \r
+        */\r
        public Slider() {\r
                super();\r
                super.setValue(new Double(min));\r
        }\r
-       \r
+\r
+       /**\r
+        * Create a new slider with the caption given as parameter. All slider\r
+        * values set to defaults.\r
+        * \r
+        * @param caption\r
+        *            The caption for this Slider (e.g. "Volume").\r
+        */\r
        public Slider(String caption) {\r
                this();\r
                setCaption(caption);\r
        }\r
-       \r
+\r
+       /**\r
+        * Create a new slider with given range and resolution\r
+        * \r
+        * @param min\r
+        * @param max\r
+        * @param resolution\r
+        */\r
        public Slider(double min, double max, int resolution) {\r
                this();\r
                setMin(min);\r
                setMax(max);\r
                setResolution(resolution);\r
        }\r
-       \r
+\r
        public Slider(int min, int max) {\r
                this();\r
                setMin(min);\r
                setMax(max);\r
                setResolution(0);\r
        }\r
-       \r
+\r
        public Slider(String caption, int min, int max) {\r
                this(min, max);\r
                setCaption(caption);\r
@@ -81,15 +103,18 @@ public class Slider extends AbstractField {
        }\r
 \r
        /**\r
-        * Set the maximum value of the Slider. As a side-effect nullifies the "values" Set.\r
+        * Set the maximum value of the Slider. If the current value of the Slider\r
+        * is out of new bounds, the value is set to new minimum.\r
+        * \r
         * @param max\r
+        *            New maximum value of the Slider.\r
         */\r
        public void setMax(double max) {\r
                this.max = max;\r
                try {\r
-                       if((new Double(getValue().toString())).doubleValue() > max)\r
-                               super.setValue(new Double(min));\r
-               } catch(ClassCastException e) {\r
+                       if ((new Double(getValue().toString())).doubleValue() > max)\r
+                               super.setValue(new Double(max));\r
+               } catch (ClassCastException e) {\r
                        super.setValue(new Double(max));\r
                }\r
                requestRepaint();\r
@@ -98,129 +123,218 @@ public class Slider extends AbstractField {
        public double getMin() {\r
                return min;\r
        }\r
-       \r
+\r
        /**\r
-        * Set the minimum value of the Slider. As a side-effect nullifies the "values" Set.\r
-        * @param max\r
+        * Set the minimum value of the Slider. If the current value of the Slider\r
+        * is out of new bounds, the value is set to new minimum.\r
+        * \r
+        * @param min\r
+        *            New minimum value of the Slider.\r
         */\r
        public void setMin(double min) {\r
                this.min = min;\r
                try {\r
-                       if((new Double(getValue().toString())).doubleValue() < min)\r
+                       if ((new Double(getValue().toString())).doubleValue() < min)\r
                                super.setValue(new Double(min));\r
-               } catch(ClassCastException e) {\r
+               } catch (ClassCastException e) {\r
                        super.setValue(new Double(min));\r
                }\r
                requestRepaint();\r
        }\r
 \r
+       /**\r
+        * Get the current orientation of the Slider (horizontal or vertical).\r
+        * \r
+        * @return orientation\r
+        */\r
        public int getOrientation() {\r
                return orientation;\r
        }\r
 \r
+       /**\r
+        * Set the orientation of the Slider.\r
+        * \r
+        * @param int\r
+        *            new orientation\r
+        */\r
        public void setOrientation(int orientation) {\r
                this.orientation = orientation;\r
                requestRepaint();\r
        }\r
 \r
+       /**\r
+        * Get the current resolution of the Slider.\r
+        * \r
+        * @return resolution\r
+        */\r
        public int getResolution() {\r
                return resolution;\r
        }\r
 \r
+       /**\r
+        * Set a new resolution for the Slider.\r
+        * \r
+        * @param resolution\r
+        */\r
        public void setResolution(int resolution) {\r
-               if(resolution < 0)\r
+               if (resolution < 0)\r
                        return;\r
                this.resolution = resolution;\r
                requestRepaint();\r
        }\r
 \r
-       public void setValue(Double value, boolean repaintIsNotNeeded) throws ValueOutOfBoundsException {\r
-               double v = new Double(value.toString()).doubleValue();\r
+       /**\r
+        * Set the value of this Slider.\r
+        * \r
+        * @param value\r
+        *            New value of Slider. Must be within Sliders range (min - max),\r
+        *            otherwise throws an exception.\r
+        * @param repaintIsNotNeeded\r
+        *            If true, client-side is not requested to repaint itself.\r
+        * @throws ValueOutOfBoundsException\r
+        */\r
+       public void setValue(Double value, boolean repaintIsNotNeeded)\r
+                       throws ValueOutOfBoundsException {\r
+               double v = value.doubleValue();\r
                double newValue;\r
-               if(resolution>0) {\r
+               if (resolution > 0) {\r
                        // Round up to resolution\r
                        newValue = (int) (v * (double) Math.pow(10, resolution));\r
                        newValue = newValue / (double) Math.pow(10, resolution);\r
-                       if(min > newValue || max < newValue)\r
+                       if (min > newValue || max < newValue)\r
                                throw new ValueOutOfBoundsException(value);\r
                } else {\r
                        newValue = (int) v;\r
-                       if(min > newValue || max < newValue)\r
+                       if (min > newValue || max < newValue)\r
                                throw new ValueOutOfBoundsException(value);\r
                }\r
                super.setValue(new Double(newValue), repaintIsNotNeeded);\r
        }\r
-       \r
+\r
+       /**\r
+        * Set the value of this Slider.\r
+        * \r
+        * @param value\r
+        *            New value of Slider. Must be within Sliders range (min - max),\r
+        *            otherwise throws an exception.\r
+        * @throws ValueOutOfBoundsException\r
+        */\r
        public void setValue(Double value) throws ValueOutOfBoundsException {\r
                setValue(value, false);\r
        }\r
 \r
+       /**\r
+        * Set the value of this Slider.\r
+        * \r
+        * @param value\r
+        *            New value of Slider. Must be within Sliders range (min - max),\r
+        *            otherwise throws an exception.\r
+        * @throws ValueOutOfBoundsException\r
+        */\r
+       public void setValue(double value) throws ValueOutOfBoundsException {\r
+               setValue(new Double(value), false);\r
+       }\r
+\r
+       /**\r
+        * Get the current Slider size.\r
+        * \r
+        * @return size in pixels or -1.\r
+        */\r
        public int getSize() {\r
                return size;\r
        }\r
 \r
+       /**\r
+        * Set the size for this Slider.\r
+        * \r
+        * @param size\r
+        *            in pixels, or -1 auto sizing.\r
+        */\r
        public void setSize(int size) {\r
                this.size = size;\r
                requestRepaint();\r
        }\r
 \r
+       /**\r
+        * Get the handle size of this Slider.\r
+        * \r
+        * @return handle size in percentages.\r
+        */\r
        public int getHandleSize() {\r
                return handleSize;\r
        }\r
 \r
+       /**\r
+        * Set the handle size of this Slider.\r
+        * \r
+        * @param handleSize\r
+        *            in percentages relative to slider base size.\r
+        */\r
        public void setHandleSize(int handleSize) {\r
-               if(handleSize < 0)\r
+               if (handleSize < 0)\r
                        this.handleSize = -1;\r
-               else if(handleSize > 99)\r
+               else if (handleSize > 99)\r
                        this.handleSize = 99;\r
-               else if(handleSize < 1)\r
+               else if (handleSize < 1)\r
                        this.handleSize = 1;\r
-               else this.handleSize = handleSize;\r
+               else\r
+                       this.handleSize = handleSize;\r
                requestRepaint();\r
        }\r
-       \r
+\r
+       /**\r
+        * Show or hide slider arrows.\r
+        * \r
+        * @param visible\r
+        */\r
        public void setArrows(boolean visible) {\r
                arrows = visible;\r
                requestRepaint();\r
        }\r
-       \r
-       public boolean arrowsVisible() {\r
+\r
+       /**\r
+        * Does the slider have arrows?\r
+        * \r
+        * @return arrows visible\r
+        */\r
+       public boolean isArrowsVisible() {\r
                return arrows;\r
        }\r
 \r
        public String getTag() {\r
                return "slider";\r
        }\r
-       \r
+\r
        public void paintContent(PaintTarget target) throws PaintException {\r
                super.paintContent(target);\r
-               \r
+\r
                target.addAttribute("min", min);\r
-               if(max > min)\r
+               if (max > min)\r
                        target.addAttribute("max", max);\r
                else\r
                        target.addAttribute("max", min);\r
                target.addAttribute("resolution", resolution);\r
-               \r
-               if(resolution > 0)\r
-                       target.addVariable(this, "value", ((Double)getValue()).doubleValue());\r
+\r
+               if (resolution > 0)\r
+                       target.addVariable(this, "value", ((Double) getValue())\r
+                                       .doubleValue());\r
                else\r
-                       target.addVariable(this, "value", ((Double)getValue()).intValue());\r
-       \r
-               if(orientation == ORIENTATION_VERTICAL)\r
+                       target.addVariable(this, "value", ((Double) getValue()).intValue());\r
+\r
+               if (orientation == ORIENTATION_VERTICAL)\r
                        target.addAttribute("vertical", true);\r
-               \r
-               if(arrows)\r
+\r
+               if (arrows)\r
                        target.addAttribute("arrows", true);\r
-               \r
-               if(size > -1)\r
+\r
+               if (size > -1)\r
                        target.addAttribute("size", size);\r
-               \r
-               if(min != max && min < max)\r
+\r
+               if (min != max && min < max)\r
                        target.addAttribute("hsize", handleSize);\r
                else\r
                        target.addAttribute("hsize", 100);\r
-               \r
+\r
        }\r
 \r
        /**\r
@@ -234,45 +348,52 @@ public class Slider extends AbstractField {
                if (variables.containsKey("value")) {\r
                        Object value = variables.get("value");\r
                        Double newValue = new Double(value.toString());\r
-                       if(newValue != null && newValue != getValue() && !newValue.equals(getValue())) {\r
+                       if (newValue != null && newValue != getValue()\r
+                                       && !newValue.equals(getValue())) {\r
                                try {\r
                                        setValue(newValue, true);\r
-                               } catch(ValueOutOfBoundsException e) {\r
+                               } catch (ValueOutOfBoundsException e) {\r
                                        // Convert to nearest bound\r
                                        double out = e.getValue().doubleValue();\r
-                                       if(out < min)\r
+                                       if (out < min)\r
                                                out = min;\r
-                                       if(out > max)\r
+                                       if (out > max)\r
                                                out = max;\r
                                        super.setValue(new Double(out), false);\r
                                }\r
                        }\r
                }\r
        }\r
-       \r
+\r
+       /**\r
+        * ValueOutOfBoundsException\r
+        * \r
+        * @author IT Mill Ltd.\r
+        *\r
+        */\r
        public class ValueOutOfBoundsException extends Exception {\r
 \r
                /**\r
                 * Serial generated by Eclipse.\r
                 */\r
                private static final long serialVersionUID = -6451298598644446340L;\r
-               \r
+\r
                private Double value;\r
-               \r
+\r
                /**\r
-                * Constructs an <code>ValueOutOfBoundsException</code> with the specified\r
-                * detail message.\r
+                * Constructs an <code>ValueOutOfBoundsException</code> with the\r
+                * specified detail message.\r
                 * \r
                 * @param valueOutOfBounds\r
                 */\r
                public ValueOutOfBoundsException(Double valueOutOfBounds) {\r
                        this.value = valueOutOfBounds;\r
                }\r
-               \r
+\r
                public Double getValue() {\r
                        return this.value;\r
                }\r
-               \r
+\r
        }\r
 \r
        public Class getType() {\r