javauis/lcdui_qt/src/javax/microedition/lcdui/Gauge.java
changeset 23 98ccebc37403
parent 21 2a9601315dfc
child 56 abc41079b313
child 61 bf7ee68962da
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description: 
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 package javax.microedition.lcdui;
    17 package javax.microedition.lcdui;
    18 
    18 
    19 import org.eclipse.swt.graphics.Point;
    19 import org.eclipse.swt.graphics.Point;
    20 
    20 
    21 /**
    21 /**
    22  * Class representing the Gauge item.
    22  * Class representing the Gauge item.
    23  */
    23  */
    24 public class Gauge extends Item {
    24 public class Gauge extends Item
       
    25 {
    25 
    26 
    26     /**
    27     /**
    27      * Indefinite constant.
    28      * Indefinite constant.
    28      */
    29      */
    29     public static final int INDEFINITE = -1;
    30     public static final int INDEFINITE = -1;
    58      * @param name the label.
    59      * @param name the label.
    59      * @param interactive if its interactive.
    60      * @param interactive if its interactive.
    60      * @param maxVal the maximum value.
    61      * @param maxVal the maximum value.
    61      * @param initVal the initial value.
    62      * @param initVal the initial value.
    62      */
    63      */
    63     public Gauge(String name, boolean interactive, int maxVal, int initVal) {
    64     public Gauge(String name, boolean interactive, int maxVal, int initVal)
       
    65     {
    64         setLabel(name);
    66         setLabel(name);
    65         this.interactive = interactive;
    67         this.interactive = interactive;
    66         setMaxValue(maxVal);
    68         setMaxValue(maxVal);
    67         setValue(initVal);
    69         setValue(initVal);
    68     }
    70     }
    72      *
    74      *
    73      * @param value the value.
    75      * @param value the value.
    74      * @param maxVal the maximum value.
    76      * @param maxVal the maximum value.
    75      * @return validated value.
    77      * @return validated value.
    76      */
    78      */
    77     private static int checkValue(int value, int maxVal) {
    79     private static int checkValue(int value, int maxVal)
    78         if (maxVal == INDEFINITE) {
    80     {
    79             switch (value) {
    81         if(maxVal == INDEFINITE)
    80                 case CONTINUOUS_IDLE:
    82         {
    81                 case INCREMENTAL_IDLE:
    83             switch(value)
    82                 case CONTINUOUS_RUNNING:
    84             {
    83                 case INCREMENTAL_UPDATING:
    85             case CONTINUOUS_IDLE:
    84                     break;
    86             case INCREMENTAL_IDLE:
    85                 default:
    87             case CONTINUOUS_RUNNING:
    86                     throw new IllegalArgumentException(
    88             case INCREMENTAL_UPDATING:
    87                             MsgRepository.GAUGE_EXCEPTION_INVALID_VALUE);
    89                 break;
       
    90             default:
       
    91                 throw new IllegalArgumentException(
       
    92                     MsgRepository.GAUGE_EXCEPTION_INVALID_VALUE);
    88             }
    93             }
    89             return value;
    94             return value;
    90         }
    95         }
    91         else {
    96         else
       
    97         {
    92             // make sure the value is in [0, maxValue] range
    98             // make sure the value is in [0, maxValue] range
    93             value = (value < 0 ? 0 : value);
    99             value = (value < 0 ? 0 : value);
    94             value = (value > maxVal ? maxVal : value);
   100             value = (value > maxVal ? maxVal : value);
    95             return value;
   101             return value;
    96         }
   102         }
   101      *
   107      *
   102      * @param maxVal the maximum value.
   108      * @param maxVal the maximum value.
   103      * @param interactive is interactive.
   109      * @param interactive is interactive.
   104      * @return validated maximum value.
   110      * @return validated maximum value.
   105      */
   111      */
   106     private static int checkMaxValue(int maxVal, boolean interactive) {
   112     private static int checkMaxValue(int maxVal, boolean interactive)
   107         if (!interactive) {
   113     {
   108             if (maxVal == INDEFINITE) {
   114         if(!interactive)
       
   115         {
       
   116             if(maxVal == INDEFINITE)
       
   117             {
   109                 return maxVal;
   118                 return maxVal;
   110             }
   119             }
   111         }
   120         }
   112         if (maxVal <= 0) {
   121         if(maxVal <= 0)
       
   122         {
   113             throw new IllegalArgumentException(
   123             throw new IllegalArgumentException(
   114                     MsgRepository.GAUGE_EXCEPTION_INVALID_MAXVALUE);
   124                 MsgRepository.GAUGE_EXCEPTION_INVALID_MAXVALUE);
   115         }
   125         }
   116         return maxVal;
   126         return maxVal;
   117     }
   127     }
   118 
   128 
   119     /**
   129     /**
   120      * Sets value of this gauge.
   130      * Sets value of this gauge.
   121      *
   131      *
   122      * @param value New value. Must be between zero and maxvalue inclusive.
   132      * @param value New value. Must be between zero and maxvalue inclusive.
   123      */
   133      */
   124     public void setValue(int value) {
   134     public void setValue(int value)
       
   135     {
   125         // TODO: eSWT support required
   136         // TODO: eSWT support required
   126         /*
   137         /*
   127          * if Gauge state is incremental-updating and it is non-interactive and
   138          * if Gauge state is incremental-updating and it is non-interactive and
   128          * indefinite, we should update Gauge when this method is called, but
   139          * indefinite, we should update Gauge when this method is called, but
   129          * that requires eSWT extension.
   140          * that requires eSWT extension.
   135     /**
   146     /**
   136      * Get value.
   147      * Get value.
   137      *
   148      *
   138      * @return the value.
   149      * @return the value.
   139      */
   150      */
   140     public int getValue() {
   151     public int getValue()
       
   152     {
   141         return value;
   153         return value;
   142     }
   154     }
   143 
   155 
   144     /**
   156     /**
   145      * Set the maximum value.
   157      * Set the maximum value.
   146      *
   158      *
   147      * @param maxValue the maximum value.
   159      * @param maxValue the maximum value.
   148      */
   160      */
   149     public void setMaxValue(int maxValue) {
   161     public void setMaxValue(int maxValue)
       
   162     {
   150         this.maxValue = checkMaxValue(maxValue, interactive);
   163         this.maxValue = checkMaxValue(maxValue, interactive);
   151         this.value = checkValue(getValue(), this.maxValue);
   164         this.value = checkValue(getValue(), this.maxValue);
   152         updateParent(UPDATE_CONTENT);
   165         updateParent(UPDATE_CONTENT);
   153     }
   166     }
   154 
   167 
   155     /**
   168     /**
   156      * Get maximum value.
   169      * Get maximum value.
   157      *
   170      *
   158      * @return the maximum value.
   171      * @return the maximum value.
   159      */
   172      */
   160     public int getMaxValue() {
   173     public int getMaxValue()
       
   174     {
   161         return maxValue;
   175         return maxValue;
   162     }
   176     }
   163 
   177 
   164     /**
   178     /**
   165      * Checks if gauge is interactive.
   179      * Checks if gauge is interactive.
   166      *
   180      *
   167      * @return true if the Gauge is interactive.
   181      * @return true if the Gauge is interactive.
   168      */
   182      */
   169     public boolean isInteractive() {
   183     public boolean isInteractive()
       
   184     {
   170         return interactive;
   185         return interactive;
   171     }
   186     }
   172 
   187 
   173     /**
   188     /**
   174      * Updates Form or Alert.
   189      * Updates Form or Alert.
   175      *
   190      *
   176      * @see javax.microedition.lcdui.Item#updateParent(int)
   191      * @see javax.microedition.lcdui.Item#updateParent(int)
   177      */
   192      */
   178     void updateParent(int updateReason) {
   193     void updateParent(int updateReason)
   179         if (isContainedInAlert()) {
   194     {
       
   195         if(isContainedInAlert())
       
   196         {
   180             ((Alert) getParent()).updateIndicator();
   197             ((Alert) getParent()).updateIndicator();
   181         }
   198         }
   182         else {
   199         else
       
   200         {
   183             super.updateParent(updateReason);
   201             super.updateParent(updateReason);
   184         }
   202         }
   185     }
   203     }
   186 
   204 
   187     /**
   205     /**
   188      * Calculates minimum size of this item.
   206      * Calculates minimum size of this item.
   189      *
   207      *
   190      * @return Minimum size.
   208      * @return Minimum size.
   191      */
   209      */
   192     Point calculateMinimumSize() {
   210     Point calculateMinimumSize()
       
   211     {
   193         return GaugeLayouter.calculateMinimumBounds(this);
   212         return GaugeLayouter.calculateMinimumBounds(this);
   194     }
   213     }
   195 
   214 
   196     /**
   215     /**
   197      * Calculates preferred size of this item.
   216      * Calculates preferred size of this item.
   198      *
   217      *
   199      * @return Preferred size.
   218      * @return Preferred size.
   200      */
   219      */
   201     Point calculatePreferredSize() {
   220     Point calculatePreferredSize()
       
   221     {
   202         return GaugeLayouter.calculatePreferredBounds(this);
   222         return GaugeLayouter.calculatePreferredBounds(this);
   203     }
   223     }
   204 
   224 
   205     /**
   225     /**
   206      * Called by widget listeners to update Item value.
   226      * Called by widget listeners to update Item value.
   207      */
   227      */
   208     void internalSetValue(int newValue) {
   228     void internalSetValue(int newValue)
       
   229     {
   209         this.value = checkValue(newValue, this.maxValue);
   230         this.value = checkValue(newValue, this.maxValue);
   210         // notify item state listener
   231         // notify item state listener
   211         notifyStateChanged();
   232         notifyStateChanged();
   212     }
   233     }
   213 
   234 
   214     /**
   235     /**
   215      * Return layout with optional custom flags.
   236      * Return layout with optional custom flags.
   216      *
   237      *
   217      * @return layout directive
   238      * @return layout directive
   218      */
   239      */
   219     int internalGetLayout() {
   240     int internalGetLayout()
       
   241     {
   220         return super.internalGetLayout() | Item.LAYOUT_NEWLINE_BEFORE;
   242         return super.internalGetLayout() | Item.LAYOUT_NEWLINE_BEFORE;
   221     }
   243     }
   222 
   244 
   223     /**
   245     /**
   224      * @return if the Gauge is indefinite.
   246      * @return if the Gauge is indefinite.
   225      */
   247      */
   226     boolean isIndefinite() {
   248     boolean isIndefinite()
       
   249     {
   227         return (maxValue == INDEFINITE);
   250         return (maxValue == INDEFINITE);
   228     }
   251     }
   229 
   252 
   230     /**
   253     /**
   231      * Returns if this indicator meets the restrictions for its use in an Alert.
   254      * Returns if this indicator meets the restrictions for its use in an Alert.
   232      */
   255      */
   233     boolean isSuitableForAlert() {
   256     boolean isSuitableForAlert()
       
   257     {
   234         return (!isInteractive()
   258         return (!isInteractive()
   235                 && getParent() == null
   259                 && getParent() == null
   236                 && !hasLabel()
   260                 && !hasLabel()
   237                 && getLayout() == Item.LAYOUT_DEFAULT
   261                 && getLayout() == Item.LAYOUT_DEFAULT
   238                 && !isSizeLocked()
   262                 && !isSizeLocked()
   241     }
   265     }
   242 
   266 
   243     /* (non-Javadoc)
   267     /* (non-Javadoc)
   244      * @see javax.microedition.lcdui.Item#isFocusable()
   268      * @see javax.microedition.lcdui.Item#isFocusable()
   245      */
   269      */
   246     boolean isFocusable() {
   270     boolean isFocusable()
       
   271     {
   247         return (isInteractive() || (getNumCommands() > 0));
   272         return (isInteractive() || (getNumCommands() > 0));
   248     }
   273     }
   249 
   274 
   250 }
   275 }