javauis/lcdui_akn/javalcdui/javasrc/javax/microedition/lcdui/Gauge.java
branchRCL_3
changeset 26 2455ef1f5bbc
parent 14 04becd199f91
equal deleted inserted replaced
25:ae942d28ec0e 26:2455ef1f5bbc
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package javax.microedition.lcdui;
       
    20 
       
    21 import com.nokia.mj.impl.rt.legacy.NativeError;
       
    22 
       
    23 public class Gauge extends Item
       
    24 {
       
    25     public static final int INDEFINITE = -1;
       
    26     //
       
    27     public static final int CONTINUOUS_IDLE = 0;        // == ~INDEFINITE
       
    28     public static final int INCREMENTAL_IDLE = 1;
       
    29     public static final int CONTINUOUS_RUNNING = 2;
       
    30     public static final int INCREMENTAL_UPDATING = 3;
       
    31 
       
    32     //
       
    33     // A strictly positive integer or the special value INDEFINITE.
       
    34     //
       
    35     private int iMaxValue;
       
    36 
       
    37     //
       
    38     // If iMaxValue is a positive integer, then iValue ranges over the
       
    39     // non-negative integers (including zero).
       
    40     //
       
    41     // If iMaxValue is the special value INDEFINITE, then iValue takes
       
    42     // one value from the set (CONINTUOUS_IDLE, INCREMENTAL_IDLE,
       
    43     // CONTINUOUS_RUNNING, INCREMENTAL_UPDATING).
       
    44     //
       
    45     private int iValue;
       
    46 
       
    47     //
       
    48     // Indicates that the user can change iValue.
       
    49     //
       
    50     private boolean iInteractive;
       
    51 
       
    52     public Gauge(String aLabel, boolean aInteractive, int aMaxValue, int aValue)
       
    53     {
       
    54         super();
       
    55         synchronized (iToolkit)
       
    56         {
       
    57             aMaxValue = ValidateMaxValue(aMaxValue, aInteractive);
       
    58             aValue    = ValidateValue(aValue, aMaxValue);
       
    59 
       
    60             final int    toolkit = iToolkit.getHandle();
       
    61             final String label   = aLabel == null?"":aLabel;
       
    62 
       
    63             iHandle = Toolkit.checkHandle(_create(toolkit, this, label, aInteractive, aMaxValue, aValue));
       
    64             iMaxValue = aMaxValue;
       
    65             iValue = aValue;
       
    66             iLabel = aLabel;
       
    67             iInteractive = aInteractive;
       
    68         }
       
    69     }
       
    70 
       
    71     public void setValue(int aValue)
       
    72     {
       
    73         synchronized (iToolkit)
       
    74         {
       
    75             aValue = ValidateValue(aValue, iMaxValue);
       
    76             NativeError.check(_setValue(iToolkit.getHandle(), getHandle(), aValue));
       
    77             iValue = aValue;
       
    78         }
       
    79     }
       
    80 
       
    81     public int getValue()
       
    82     {
       
    83         synchronized (iToolkit)
       
    84         {
       
    85             if (iInteractive)
       
    86             {
       
    87                 //
       
    88                 // User may have modified value, must check peer and
       
    89                 // update iValue to match.
       
    90                 //
       
    91                 iValue = _getValue(iToolkit.getHandle(), getHandle());
       
    92             }
       
    93             else
       
    94             {
       
    95                 // No point in going native since the user cannot have
       
    96                 // modified the value.
       
    97                 // Thus, nothing to do.
       
    98             }
       
    99 
       
   100             return iValue;
       
   101         }
       
   102     }
       
   103 
       
   104     public void setMaxValue(int aMaxValue)
       
   105     {
       
   106         synchronized (iToolkit)
       
   107         {
       
   108             if (iMaxValue == aMaxValue)
       
   109             {
       
   110                 return;
       
   111             }
       
   112             int maxValue = ValidateMaxValue(aMaxValue, iInteractive);
       
   113             int value    = ValidateValue(getValue(), maxValue, iMaxValue);
       
   114             NativeError.check(_setMaxValue(iToolkit.getHandle(), getHandle(), maxValue, value));
       
   115             iMaxValue = maxValue;
       
   116             iValue = value;
       
   117         }
       
   118     }
       
   119 
       
   120     public int getMaxValue()
       
   121     {
       
   122         return iMaxValue;
       
   123     }
       
   124 
       
   125     public boolean isInteractive()
       
   126     {
       
   127         return iInteractive;
       
   128     }
       
   129 
       
   130     private boolean IsIndefinite()
       
   131     {
       
   132         final boolean indefinite = (iMaxValue == INDEFINITE);
       
   133         return indefinite;
       
   134     }
       
   135 
       
   136     private static int ValidateMaxValue(int aMaxValue, boolean aInteractive)
       
   137     {
       
   138         //
       
   139         // Non-interactive gauges may have the special max value of
       
   140         // INDEFINITE
       
   141         //
       
   142         if (!aInteractive && (aMaxValue == INDEFINITE))
       
   143         {
       
   144             return aMaxValue;
       
   145         }
       
   146 
       
   147         //
       
   148         // All remaining cases max value must be a positive integer.
       
   149         //
       
   150         if (aMaxValue <= 0)
       
   151         {
       
   152             throw new IllegalArgumentException("invalid max value");
       
   153         }
       
   154 
       
   155         return aMaxValue;
       
   156     }
       
   157 
       
   158     private static int ValidateRange(int aValue, int aMaxValue)
       
   159     {
       
   160         aValue = Math.min(aValue, aMaxValue);   // clamp to max
       
   161         aValue = Math.max(aValue, 0);           // clamp to min (also maps INDEFINITE to CONTINUOUS_IDLE)
       
   162         return aValue;
       
   163     }
       
   164 
       
   165     private static int ValidateValue(int aValue, int aNewRange)
       
   166     {
       
   167         if (aNewRange == INDEFINITE)
       
   168         {
       
   169             switch (aValue)
       
   170             {
       
   171             case CONTINUOUS_IDLE:
       
   172             case INCREMENTAL_IDLE:
       
   173             case CONTINUOUS_RUNNING:
       
   174             case INCREMENTAL_UPDATING:
       
   175                 break;
       
   176             default:
       
   177                 throw new IllegalArgumentException();
       
   178             }
       
   179             return aValue;
       
   180         }
       
   181         else
       
   182         {
       
   183             return ValidateRange(aValue, aNewRange);
       
   184         }
       
   185     }
       
   186 
       
   187     private static int ValidateValue(int aValue, int aNewRange, int aOldRange)
       
   188     {
       
   189         if (aNewRange == INDEFINITE)
       
   190         {
       
   191             return CONTINUOUS_IDLE;
       
   192         }
       
   193         else if (aOldRange == INDEFINITE)
       
   194         {
       
   195             return 0;
       
   196         }
       
   197         else
       
   198         {
       
   199             return ValidateRange(aValue, aNewRange);
       
   200         }
       
   201     }
       
   202 
       
   203     boolean isAlertGauge()
       
   204     {
       
   205         boolean alertGauge=false;
       
   206         synchronized (iToolkit)
       
   207         {
       
   208             alertGauge = (!iInteractive) && (null==iListener) && (null==iLabel) && (null == iScreen)
       
   209                          && (Item.LAYOUT_DEFAULT==iLayout)&&(!iSizeLocked)&&(iCommands.count()==0);
       
   210         }
       
   211         return alertGauge;
       
   212     }
       
   213 
       
   214     private native int _create(int aToolkit,Gauge aGauge,String aLabel,boolean aInteractive,int aMaxValue,int aInitialValue);
       
   215     private native int _setValue(int aToolkit,int aItem,int aValue);
       
   216     private native int _getValue(int aToolkit,int aItem);
       
   217     private native int _setMaxValue(int aToolkit,int aItem,int aMaxValue, int aValue);
       
   218 }