javauis/lcdui_qt/src/javax/microedition/lcdui/Ticker.java
changeset 23 98ccebc37403
parent 21 2a9601315dfc
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
    22 import org.eclipse.swt.widgets.Label;
    22 import org.eclipse.swt.widgets.Label;
    23 
    23 
    24 /**
    24 /**
    25  * Implementation of LCDUI <code>Ticker</code> class.
    25  * Implementation of LCDUI <code>Ticker</code> class.
    26  */
    26  */
    27 public class Ticker {
    27 public class Ticker
       
    28 {
    28 
    29 
    29     /**
    30     /**
    30      * How many pixels the ticker will move after every delay.
    31      * How many pixels the ticker will move after every delay.
    31      */
    32      */
    32     private float stepSize = 1;
    33     private float stepSize = 1;
    58     /**
    59     /**
    59      * Constructor.
    60      * Constructor.
    60      *
    61      *
    61      * @param txt Displayed string. If null, throws NullPointerException.
    62      * @param txt Displayed string. If null, throws NullPointerException.
    62      */
    63      */
    63     public Ticker(String txt) {
    64     public Ticker(String txt)
       
    65     {
    64         setString(txt);
    66         setString(txt);
    65     }
    67     }
    66 
    68 
    67     /**
    69     /**
    68      * Set the displayed string.
    70      * Set the displayed string.
    69      *
    71      *
    70      * @param txt Displayed string. If null, throws NullPointerException.
    72      * @param txt Displayed string. If null, throws NullPointerException.
    71      */
    73      */
    72     public void setString(String txt) {
    74     public void setString(String txt)
    73         if (txt == null) {
    75     {
       
    76         if(txt == null)
       
    77         {
    74             throw new NullPointerException(
    78             throw new NullPointerException(
    75                     MsgRepository.TICKER_EXCEPTION_NULL_STRING);
    79                 MsgRepository.TICKER_EXCEPTION_NULL_STRING);
    76         }
    80         }
    77         text = txt;
    81         text = txt;
    78         updateDirection();
    82         updateDirection();
    79         ESWTUIThreadRunner.syncExec(new Runnable() {
    83         ESWTUIThreadRunner.syncExec(new Runnable()
    80             public void run() {
    84         {
    81                 for (int i = 0; i < labels.size(); i++) {
    85             public void run()
       
    86             {
       
    87                 for(int i = 0; i < labels.size(); i++)
       
    88                 {
    82                     Label label = ((Label) labels.elementAt(i));
    89                     Label label = ((Label) labels.elementAt(i));
    83                     if (!label.isDisposed()) {
    90                     if(!label.isDisposed())
    84                        label.setText(getFormattedString());
    91                     {
    85                        label.pack();
    92                         label.setText(getFormattedString());
    86                      }
    93                         label.pack();
       
    94                     }
    87                 }
    95                 }
    88             }
    96             }
    89         });
    97         });
    90     }
    98     }
    91 
    99 
    92     /**
   100     /**
    93      * Get the displayed string.
   101      * Get the displayed string.
    94      *
   102      *
    95      * @return the displayed string.
   103      * @return the displayed string.
    96      */
   104      */
    97     public String getString() {
   105     public String getString()
       
   106     {
    98         return text;
   107         return text;
    99     }
   108     }
   100 
   109 
   101     /**
   110     /**
   102      * Gets the displayed string where new line characters are replaced
   111      * Gets the displayed string where new line characters are replaced
   103      * with spaces. When setting text to eSWT Label one should use this
   112      * with spaces. When setting text to eSWT Label one should use this
   104      * method instead of getString().
   113      * method instead of getString().
   105      *
   114      *
   106      * @return String where newline characters are replaced with spaces.
   115      * @return String where newline characters are replaced with spaces.
   107      */
   116      */
   108     String getFormattedString() {
   117     String getFormattedString()
       
   118     {
   109         StringBuffer formattedText = new StringBuffer(text.length());
   119         StringBuffer formattedText = new StringBuffer(text.length());
   110         for (int i = 0; i < text.length(); i++) {
   120         for(int i = 0; i < text.length(); i++)
   111             if (text.charAt(i) == '\r') {
   121         {
       
   122             if(text.charAt(i) == '\r')
       
   123             {
   112                 continue;
   124                 continue;
   113             }
   125             }
   114             else if ((text.charAt(i) == '\n')
   126             else if((text.charAt(i) == '\n')
   115                 || (text.charAt(i) == '\u2028')) {
   127                     || (text.charAt(i) == '\u2028'))
       
   128             {
   116                 formattedText.append(" ");
   129                 formattedText.append(" ");
   117             }
   130             }
   118             else {
   131             else
       
   132             {
   119                 formattedText.append(text.charAt(i));
   133                 formattedText.append(text.charAt(i));
   120             }
   134             }
   121         }
   135         }
   122         return formattedText.toString();
   136         return formattedText.toString();
   123     }
   137     }
   128      * one label in one Ticker, because same Ticker may exist in many
   142      * one label in one Ticker, because same Ticker may exist in many
   129      * Displayables.
   143      * Displayables.
   130      *
   144      *
   131      * @param label Label to add.
   145      * @param label Label to add.
   132      */
   146      */
   133     void addLabel(final Label label) {
   147     void addLabel(final Label label)
   134         if (label != null) {
   148     {
   135             ESWTUIThreadRunner.syncExec(new Runnable() {
   149         if(label != null)
   136                 public void run() {
   150         {
       
   151             ESWTUIThreadRunner.syncExec(new Runnable()
       
   152             {
       
   153                 public void run()
       
   154                 {
   137                     labels.addElement(label);
   155                     labels.addElement(label);
   138                 }
   156                 }
   139             });
   157             });
   140         }
   158         }
   141     }
   159     }
   144      * Removes eSWT Label from this ticker. If ticker is removed from
   162      * Removes eSWT Label from this ticker. If ticker is removed from
   145      * displayable, displayable must call this.
   163      * displayable, displayable must call this.
   146      *
   164      *
   147      * @param label Label to be removed. If null, nothing happens.
   165      * @param label Label to be removed. If null, nothing happens.
   148      */
   166      */
   149     void removeLabel(final Label label) {
   167     void removeLabel(final Label label)
   150         if (label != null) {
   168     {
   151             ESWTUIThreadRunner.syncExec(new Runnable() {
   169         if(label != null)
   152                 public void run() {
   170         {
       
   171             ESWTUIThreadRunner.syncExec(new Runnable()
       
   172             {
       
   173                 public void run()
       
   174                 {
   153                     labels.removeElement(label);
   175                     labels.removeElement(label);
   154                 }
   176                 }
   155             });
   177             });
   156         }
   178         }
   157     }
   179     }
   161      * This should be called when the width of the displayable changes.
   183      * This should be called when the width of the displayable changes.
   162      *
   184      *
   163      * Ticker speed is adjusted so that it takes about 7 seconds
   185      * Ticker speed is adjusted so that it takes about 7 seconds
   164      * for every character to run across the screen.
   186      * for every character to run across the screen.
   165      */
   187      */
   166     void updateSpeed() {
   188     void updateSpeed()
   167         if (labels.size() > 0) {
   189     {
   168             ESWTUIThreadRunner.syncExec(new Runnable() {
   190         if(labels.size() > 0)
   169                 public void run() {
   191         {
       
   192             ESWTUIThreadRunner.syncExec(new Runnable()
       
   193             {
       
   194                 public void run()
       
   195                 {
   170                     screenWidth = ((Label) labels.elementAt(0)).getParent()
   196                     screenWidth = ((Label) labels.elementAt(0)).getParent()
   171                             .getBounds().width;
   197                                   .getBounds().width;
   172                 }
   198                 }
   173             });
   199             });
   174             stepSize = (screenWidth * Config.TICKER_MOVEMENT_DELAY) / Config.TICKER_DISPLAY_TIME;
   200             stepSize = (screenWidth * Config.TICKER_MOVEMENT_DELAY) / Config.TICKER_DISPLAY_TIME;
   175         }
   201         }
   176     }
   202     }
   185      * Note that there must be at least one label in this ticker when started.
   211      * Note that there must be at least one label in this ticker when started.
   186      *
   212      *
   187      * It is ok to call this method even if Ticker is already running.
   213      * It is ok to call this method even if Ticker is already running.
   188      * In that case the method will just return.
   214      * In that case the method will just return.
   189      */
   215      */
   190     void start() {
   216     void start()
   191         if (isRunning) {
   217     {
       
   218         if(isRunning)
       
   219         {
   192             // Ticker already running.
   220             // Ticker already running.
   193             return;
   221             return;
   194         }
   222         }
   195         isRunning = true;
   223         isRunning = true;
   196         updateDirection();
   224         updateDirection();
   197         updateSpeed();
   225         updateSpeed();
   198 
   226 
   199 
   227 
   200 
   228 
   201         if (timer == null) {
   229         if(timer == null)
       
   230         {
   202             timer = new Timer();
   231             timer = new Timer();
   203         }
   232         }
   204 
   233 
   205         timerTask = new TimerTask() {
   234         timerTask = new TimerTask()
   206             public void run() {
   235         {
   207                 if (isRunning) {
   236             public void run()
       
   237             {
       
   238                 if(isRunning)
       
   239                 {
   208                     updateLocation();
   240                     updateLocation();
   209                 }
   241                 }
   210                 else {
   242                 else
       
   243                 {
   211                     timerTask.cancel();
   244                     timerTask.cancel();
   212                 }
   245                 }
   213             }
   246             }
   214         };
   247         };
   215 
   248 
   216         timer.schedule(timerTask, 0, Config.TICKER_MOVEMENT_DELAY);
   249         timer.schedule(timerTask, 0, Config.TICKER_MOVEMENT_DELAY);
   217     }
   250     }
   218 
   251 
   219 	private void updateDirection() {
   252     private void updateDirection()
   220 		leftToRight = StringUtil.isRightToLeftText(text);
   253     {
   221         if (leftToRight) {
   254         leftToRight = StringUtil.isRightToLeftText(text);
       
   255         if(leftToRight)
       
   256         {
   222             tickerX = Integer.MAX_VALUE;
   257             tickerX = Integer.MAX_VALUE;
   223         }
   258         }
   224         else {
   259         else
       
   260         {
   225             tickerX = Integer.MIN_VALUE;
   261             tickerX = Integer.MIN_VALUE;
   226         }
   262         }
   227 	}
   263     }
   228 
   264 
   229     /**
   265     /**
   230      * Updates ticker location.
   266      * Updates ticker location.
   231      */
   267      */
   232     private void updateLocation() {
   268     private void updateLocation()
   233         ESWTUIThreadRunner.syncExec(new Runnable() {
   269     {
   234             public void run() {
   270         ESWTUIThreadRunner.syncExec(new Runnable()
   235                 if (labels.size() <= 0) {
   271         {
       
   272             public void run()
       
   273             {
       
   274                 if(labels.size() <= 0)
       
   275                 {
   236                     // Ticker is removed from all displayables
   276                     // Ticker is removed from all displayables
   237                     // so it is ok to stop it.
   277                     // so it is ok to stop it.
   238                     isRunning = false;
   278                     isRunning = false;
   239                     return;
   279                     return;
   240                 }
   280                 }
   241 
   281 
   242                 for (int i = 0; i < labels.size(); i++) {
   282                 for(int i = 0; i < labels.size(); i++)
       
   283                 {
   243                     Label label = ((Label) labels.elementAt(i));
   284                     Label label = ((Label) labels.elementAt(i));
   244                     if (!label.isDisposed()) {
   285                     if(!label.isDisposed())
       
   286                     {
   245                         label.setLocation((int) tickerX, 0);
   287                         label.setLocation((int) tickerX, 0);
   246                     }
   288                     }
   247                 }
   289                 }
   248 
   290 
   249                 if (((Label) labels.elementAt(0)).isDisposed()) {
   291                 if(((Label) labels.elementAt(0)).isDisposed())
       
   292                 {
   250                     // Label is disposed. This may happen only
   293                     // Label is disposed. This may happen only
   251                     // when MIDlet is closing and in that case
   294                     // when MIDlet is closing and in that case
   252                     // this method would throw exception without
   295                     // this method would throw exception without
   253                     // this check.
   296                     // this check.
   254                     isRunning = false;
   297                     isRunning = false;
   255                     return;
   298                     return;
   256                 }
   299                 }
   257 
   300 
   258                 int labelWidth = ((Label) labels.elementAt(0)).
   301                 int labelWidth = ((Label) labels.elementAt(0)).
   259                     getBounds().width;
   302                                  getBounds().width;
   260 
   303 
   261                 if (leftToRight) {
   304                 if(leftToRight)
       
   305                 {
   262                     // Scrolling from left to right:
   306                     // Scrolling from left to right:
   263                     if (tickerX > screenWidth) {
   307                     if(tickerX > screenWidth)
       
   308                     {
   264                         tickerX = -labelWidth;
   309                         tickerX = -labelWidth;
   265                     }
   310                     }
   266                     else {
   311                     else
       
   312                     {
   267                         tickerX += stepSize;
   313                         tickerX += stepSize;
   268                     }
   314                     }
   269                 }
   315                 }
   270                 else {
   316                 else
       
   317                 {
   271                     // Scrolling from right to left:
   318                     // Scrolling from right to left:
   272                     if (tickerX < -labelWidth) {
   319                     if(tickerX < -labelWidth)
       
   320                     {
   273                         tickerX = screenWidth;
   321                         tickerX = screenWidth;
   274                     }
   322                     }
   275                     else {
   323                     else
       
   324                     {
   276                         tickerX -= stepSize;
   325                         tickerX -= stepSize;
   277                     }
   326                     }
   278                 }
   327                 }
   279             }
   328             }
   280         });
   329         });