countdown timers in multilist

86 views
Skip to first unread message

rvac...@gmail.com

unread,
Jul 22, 2013, 5:26:38 AM7/22/13
to codenameone...@googlegroups.com
I have uploaded a project that shows multiple countdown timers in a Multilist. except that I am not able to render the list as I wish to. In the multilist I wish to utilise the multiple lines to display different items. (e.g. The countdown clock should be visible at Line1 (where currently "multibutton" is appearing" and the item1, item2 which is actually the title to appear in another line (line2 or line3 etc).
 
I am new to java and got this code fragment online and modified it a bit. Pl suggest if I am being on the right track and how to render the clock in the Multilist using hashtables.
 
The to-do video tutorial says I can add the elements in a hashtable and the table in a vector and set the list model to the vector. the sitation here is bit different because I may be adding timers continuously. how should I approach this problem.
 
Any changes in the code improving its efficiency/elegance are welcome and so are snippets or suggestions.
 
thanks./

Rajesh Acharya

unread,
Jul 22, 2013, 5:44:16 AM7/22/13
to codenameone...@googlegroups.com
I am unable to upload the project so heres the code.
 
/**
 * Your application code goes here
 */
package userclasses;
import generated.StateMachineBase;
import com.codename1.ui.*;
import com.codename1.ui.events.*;
import com.codename1.ui.list.DefaultListModel;
import com.codename1.ui.util.Resources;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import java.util.Calendar;

/**
 *
 * @author Your name here
 */
public class StateMachine extends StateMachineBase {
   
   
    //declaring variable
    String source_id;
    public Hashtable expTimerTable = new Hashtable();
    int threadCnt; double test_time;
   
    public StateMachine(String resFile) {
        super(resFile);
        // do not modify, write code in initVars and initialize class members there,
        // the constructor might be invoked too late due to race conditions that might occur
    }
   
    /**
     * this method should be used to initialize variables instead of
     * the constructor/class scope to avoid race conditions
     */
    protected void initVars(Resources res) {
    }
 

    @Override
    protected void onMain_ButtonAction(Component c, ActionEvent event) {
        final TimeValueForSource tValueSrc = new TimeValueForSource();
        threadCnt = threadCnt+1;
        System.out.println("SOURCE DETAILS =  "+ source_id );
        tValueSrc.setSource_id(source_id);
        Double t;
        t= Double.valueOf(Math.floor(test_time*60));
        final long exp_time_in_sec = t.longValue();
        final Timer cntDwnTimer = new Timer();
        cntDwnTimer.scheduleAtFixedRate(new TimerTask() {
        double time_end = exp_time_in_sec * 1000 + System.currentTimeMillis(); ;
        double time_remaining = 0;
        double time_TenPercent= 0;
        TimerUtility timer_Util = new TimerUtility();
        public void run() {
             // expTimerTable = new Hashtable();
              TimerUtility timer = new TimerUtility();
              time_remaining = time_end-System.currentTimeMillis();
              time_TenPercent = Math.floor(exp_time_in_sec * 1000 * 0.1);
              System.out.println("time_TenPercent  " + time_TenPercent + "time_remaining  " + time_remaining);
              if(time_remaining  > (time_TenPercent-2000) && time_remaining < time_TenPercent ){
                     System.out.println(" 10% of the time is remaining!!!!!!!   ");
                   }
              if(time_remaining <= 0){
                     System.out.println(" TIMER UP!!!!!  ");
                     cntDwnTimer.cancel();
                   }
              tValueSrc.setTimeInMs(time_remaining);
              tValueSrc.setTimer_Time(timer_Util.onTick(time_remaining));
              String st = Thread.currentThread().getName();
              System.out.println("st  " + st);
             // expTimerTable.put("Line1", tValueSrc);
              expTimerTable.put(Thread.currentThread().getName(), tValueSrc);
             
              }
          }, 10, 1000);
    }
    @Override
    protected void onMain_TextFieldAction(Component c, ActionEvent event) {
    TextField tf = findTextField();
    test_time = Double.parseDouble(tf.getText());
    }
    @Override
    protected void onMain_ComboBoxAction(Component c, ActionEvent event) {
        ComboBox cb = findComboBox();
        source_id = cb.getSelectedItem().toString();
    }
   
    @Override
    protected boolean initListModelMultiList(final com.codename1.ui.List cmp) {
       
        Timer timer = new Timer();
        timer.schedule( new TimerTask() {
          public void run() {
              Hashtable expTimer = expTimerTable;
              TimeValueForSource tSrc = new TimeValueForSource();
              Vector tList = new Vector();
               String keyValForTimer =null;
               Enumeration keyEnum =  expTimer.keys();
               String key1 = (String) keyEnum.toString();
               System.out.println("expTimer Key1 =  " + key1);
               while(keyEnum.hasMoreElements()){
                   keyValForTimer = (String)keyEnum.nextElement();
                   System.out.println("keyValForTimer Keys =  " + keyValForTimer);
                   tSrc = (TimeValueForSource)expTimer.get(keyValForTimer);
                   if(tSrc.getTimeInMs() != 0.0)
                      tList.add(tSrc.getSource_id() + " " + tSrc.getTimer_Time());
                    }      
                       cmp.setModel(new com.codename1.ui.list.DefaultListModel(tList));
             }
             }, 0,1000);
        return true;
    }
}
TimeValueForSrc
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package userclasses;
/**
 *
 * @author Shree
 */
public class TimeValueForSource {
 private String source, sp_material, source_id ;
 private String timer_Time;
 private double timeInMs;
 private String threadId;
 
 public String getSource(){
     return this.source;
}
 
 public String getSp_material(){
     return this.sp_material;
 }
 
 public String getSource_id(){
     return this.source_id;
 }
 
 public String getThreadId(){
     return this.threadId;
 }
 
 public void setThreadId(String threadId){
     this.threadId = threadId;
 }
 
 public void setSource(String source){
     this.source = source;
 }
 
 public void setSp_material(String sp_material){
     this.sp_material = sp_material;
 }
 
 public void setSource_id(String source_id){
     this.source_id = source_id;
 }
 
 public void setTimer_Time(String timer_Time){
     this.timer_Time = timer_Time;
 }
 
 public String getTimer_Time(){
     return this.timer_Time;
 }
 
 public  void setTimeInMs(double timeInMs){
      this.timeInMs = timeInMs;
 }
 
 public double getTimeInMs(){
     return this.timeInMs;
 }
}
 
TimerUtility
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package userclasses;
/**
 *
 * @author Shree
 */
public class TimerUtility {
   
  
    public String onTick(double millisUntilFinished) {
             double hrs=0;
             long hrs_rounded = 0;
             double mnts=0;
             long mnts_rounded =0;
             double scnds=0;
             long scnds_rounded=0;
             scnds=(millisUntilFinished/1000);
            
              if(scnds > 59){
                    scnds_rounded = Double.valueOf(Math.floor(scnds%60)).longValue();           
              }  
              else{
                  scnds_rounded = Double.valueOf(Math.floor(scnds)).longValue();
              }
            
              if (scnds>59) {
                 mnts=(scnds/60);
                 mnts_rounded = Double.valueOf(Math.floor(mnts)).longValue();
                 if (mnts_rounded <= 0) {
                     mnts=0;
                 }
             }
             if (mnts>59) {
                 hrs=(mnts/60);
                 hrs_rounded = Double.valueOf(Math.floor(hrs)).longValue();
                 if (hrs <=0) {
                     hrs=0;
                 }
             }
            String disHour = (hrs_rounded < 10 ? "0" : "") + hrs_rounded,
             disMinu = (mnts_rounded < 10 ? "0" : "") + mnts_rounded ,
             disSec = (scnds_rounded < 10 ? "0" : "") + scnds_rounded ;
           return disHour + ":" + disMinu + ":" + disSec;
         }
   
}
 
 


--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/v4fR8sQMYn0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/9459cf49-6e01-419e-b7c8-d0bee5765cf9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

test_mainscreen.jpg
Test_ list screen.jpg

Rajesh Acharya

unread,
Jul 22, 2013, 5:50:21 AM7/22/13
to codenameone...@googlegroups.com
My bad ..... in the OP correct the following....
" (e.g. The countdown clock should be visible at Line1 (where currently "multibutton" is appearing" >> this should be>> where "Entry1" is appearing >> and the item1, item2 which is actually the title to appear in another line (line2 or line3 etc). "

Shai Almog

unread,
Jul 22, 2013, 10:39:58 AM7/22/13
to codenameone...@googlegroups.com, rvac...@gmail.com
So is this resolved?
Reply all
Reply to author
Forward
0 new messages