Go to the documentation of this file.00001 package org.ros.android.robot_monitor;
00002 
00003 import java.util.List;
00004 
00005 import android.content.Context;
00006 import android.graphics.drawable.Drawable;
00007 import android.widget.Button;
00008 import android.widget.TableLayout;
00009 import android.widget.TextView;
00010 
00011 import diagnostic_msgs.DiagnosticArray;
00012 import diagnostic_msgs.DiagnosticStatus;
00013 
00014 public class DiagnosticsArrayDisplay {
00015         
00016         Context context;
00017         
00018         TableLayout tl;
00019         TextView tv;
00020         
00021         Drawable error;
00022         Drawable warn;
00023         Drawable ok;
00024         Drawable stale;
00025         int error_color;
00026         int warn_color;
00027         int ok_color;
00028         int stale_color;
00029         
00030         byte level;
00031         
00032         public DiagnosticsArrayDisplay(Context context){
00033                 this.context = context;
00034         }
00035         
00036         void setTableLayout(TableLayout tl){
00037                 this.tl = tl;
00038         }
00039         
00040         void setTextView(TextView tv){
00041                 this.tv = tv;
00042         }
00043         
00044         void setDrawables(Drawable error, Drawable warn, Drawable ok, Drawable stale){
00045                 this.error = error;
00046                 this.warn = warn;
00047                 this.ok = ok;
00048                 this.stale = stale;
00049         }
00050         
00051         void setColors(int error_color, int warn_color, int ok_color, int stale_color){
00052                 this.error_color = error_color;
00053                 this.warn_color = warn_color;
00054                 this.ok_color = ok_color;
00055                 this.stale_color = stale_color;
00056         }
00057         
00058         void displayArray(DiagnosticArray msg){
00059                 final List<DiagnosticStatus> dsa = msg.getStatus();
00060                 this.level = DiagnosticStatus.OK;
00061                 tl.post(new Runnable(){
00062                         @Override
00063                         public void run(){
00064                                 tl.removeAllViews();
00065                                 
00066                                 
00067                                 for(final DiagnosticStatus ds : dsa){
00068                                         Button b = new Button(context);
00069                                         b.setText(ds.getName());
00070                                         level = updateLevel(level, ds.getLevel());
00071                                         if(ds.getLevel() == 3){ 
00072                                                 b.setTextColor(stale_color);
00073                                                 b.setCompoundDrawablesWithIntrinsicBounds(stale, null, null, null);
00074                                         } else if(ds.getLevel() == DiagnosticStatus.ERROR){
00075                                                 b.setTextColor(error_color);
00076                                                 b.setCompoundDrawablesWithIntrinsicBounds(error, null, null, null);
00077                                         } else if(ds.getLevel() == DiagnosticStatus.WARN){
00078                                                 b.setTextColor(warn_color);
00079                                                 b.setCompoundDrawablesWithIntrinsicBounds(warn, null, null, null);
00080                                         } else { 
00081                                                 b.setTextColor(ok_color);
00082                                                 b.setCompoundDrawablesWithIntrinsicBounds(ok, null, null, null);
00083                                         }
00084                                         
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110                                         tl.addView(b);
00111                                 }
00112                         }
00113                 });
00114                 tl.postInvalidate();
00115                 
00116                 tv.post(new Runnable(){
00117                         @Override
00118                         public void run(){
00119                                 
00120                                 if(level == 3){ 
00121                                         tv.setBackgroundColor(stale_color);
00122                                 } else if(level == DiagnosticStatus.ERROR){
00123                                         tv.setBackgroundColor(error_color);
00124                                 } else if(level == DiagnosticStatus.WARN){
00125                                         tv.setBackgroundColor(warn_color);
00126                                 } else { 
00127                                         tv.setBackgroundColor(ok_color);
00128                                 }
00129                         }
00130                 });
00131                 tv.postInvalidate();
00132 
00133         }
00134         
00135         byte updateLevel(byte oldLevel, byte newLevel){
00136                 
00137                 if(newLevel >= oldLevel){
00138                         
00139                         return newLevel;
00140                 } else {
00141                         
00142                         return oldLevel;
00143                 }
00144         }
00145 }