GrxLoggerView.java
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil; tab-width: 4; -*-
00002 /*
00003  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00004  * All rights reserved. This program is made available under the terms of the
00005  * Eclipse Public License v1.0 which accompanies this distribution, and is
00006  * available at http://www.eclipse.org/legal/epl-v10.html
00007  * Contributors:
00008  * General Robotix Inc.
00009  * National Institute of Advanced Industrial Science and Technology (AIST) 
00010  */
00011 /*
00012  *  GrxLoggerView.java
00013  *
00014  *  Copyright (C) 2007 GeneralRobotix, Inc.
00015  *  All Rights Reserved
00016  *
00017  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00018  */
00019 
00020 package com.generalrobotix.ui.view;
00021 
00022 import java.text.DecimalFormat;
00023 
00024 import org.eclipse.swt.SWT;
00025 import org.eclipse.swt.events.KeyEvent;
00026 import org.eclipse.swt.events.KeyListener;
00027 import org.eclipse.swt.events.SelectionAdapter;
00028 import org.eclipse.swt.events.SelectionEvent;
00029 import org.eclipse.swt.events.MouseEvent;
00030 import org.eclipse.swt.events.MouseListener;
00031 import org.eclipse.swt.events.MouseTrackListener;
00032 import org.eclipse.swt.events.MouseMoveListener;
00033 import org.eclipse.swt.layout.GridData;
00034 import org.eclipse.swt.layout.GridLayout;
00035 import org.eclipse.swt.widgets.Button;
00036 import org.eclipse.swt.widgets.Composite;
00037 import org.eclipse.swt.widgets.Display;
00038 import org.eclipse.swt.widgets.Label;
00039 import org.eclipse.swt.widgets.Scale;
00040 import org.eclipse.swt.widgets.Text;
00041 import org.eclipse.ui.IActionBars;
00042 import org.eclipse.jface.action.Action;
00043 
00044 import com.generalrobotix.ui.GrxBaseItem;
00045 import com.generalrobotix.ui.GrxBasePlugin;
00046 import com.generalrobotix.ui.GrxBaseView;
00047 import com.generalrobotix.ui.GrxBaseViewPart;
00048 import com.generalrobotix.ui.GrxPluginManager;
00049 import com.generalrobotix.ui.grxui.Activator;
00050 import com.generalrobotix.ui.item.GrxSimulationItem;
00051 import com.generalrobotix.ui.item.GrxWorldStateItem;
00052 import com.generalrobotix.ui.util.GrxDebugUtil;
00053 import com.generalrobotix.ui.util.MessageBundle;
00054 
00055 @SuppressWarnings("serial") //$NON-NLS-1$
00059 public class GrxLoggerView extends GrxBaseView {
00060     public static final String TITLE = "Logger"; //$NON-NLS-1$
00061 
00062         private GrxWorldStateItem currentItem_=null;
00063         private GrxSimulationItem simItem_=null;
00064 
00065         private int current_ = 0;    // current position
00066         private double playRate_ = 1.0; // playback rate
00067         private int frameRate_ = defaultFrameRate_;
00068         private boolean isPlaying_ = false;
00069     private boolean isControlDisabled_ = false; 
00070     private boolean inSimulation_ = false;
00071     private boolean buttonClick_ = false;
00072                 
00073     // widgets
00074         private Scale sliderFrameRate_;
00075         private Label  lblFrameRate_;
00076         private Scale sliderTime_;
00077     
00078 
00079         private Button btnFrameF_;
00080         private Button btnFrameR_;
00081         private Button[] btns_ = new Button[2];
00082 
00083         private String[] btnToolTips = new String[] {
00084                         MessageBundle.get("GrxLoggerView.text.oneRwd"), MessageBundle.get("GrxLoggerView.text.onePlay")  //$NON-NLS-1$ //$NON-NLS-2$
00085                 };
00086 
00087         private final static String[] icons_ = { "icon_frame-.png", "icon_frame+.png" };//$NON-NLS-1$ //$NON-NLS-2$
00088 
00089     private Text tFldTime_;
00090     
00091     private Action[] toolActs_ = new Action[6];
00092     private String[] actToolTips_ = new String[] {
00093         MessageBundle.get("GrxLoggerView.text.fastRwd"),MessageBundle.get("GrxLoggerView.text.slowRwd"), //$NON-NLS-1$ //$NON-NLS-2$
00094         MessageBundle.get("GrxLoggerView.text.pause"), MessageBundle.get("GrxLoggerView.text.play"), //$NON-NLS-1$ //$NON-NLS-2$
00095         MessageBundle.get("GrxLoggerView.text.slowFwd"), MessageBundle.get("GrxLoggerView.text.fastFwd") //$NON-NLS-1$ //$NON-NLS-2$
00096         };
00097     private final static String[] actIcons_ = {
00098         "icon_fastrwd.png", //$NON-NLS-1$
00099         "icon_slowrwd.png", //$NON-NLS-1$
00100         "icon_pause.png", //$NON-NLS-1$
00101         "icon_playback.png", //$NON-NLS-1$
00102         "icon_slowfwd.png", //$NON-NLS-1$
00103         "icon_fastfwd.png" }; //$NON-NLS-1$
00104     private final static String[] actDIcons_ = {
00105         "icond_fastrwd.png", //$NON-NLS-1$
00106         "icond_slowrwd.png", //$NON-NLS-1$
00107         "icond_pause.png", //$NON-NLS-1$
00108         "icond_playback.png", //$NON-NLS-1$
00109         "icond_slowfwd.png", //$NON-NLS-1$
00110         "icond_fastfwd.png" }; //$NON-NLS-1$
00111 
00112     private final static int    maxFrameRate_ = 50;
00113     private final static int    defaultFrameRate_ = 10;
00114         private Label  lblPlayRate_;
00115         
00116         private static DecimalFormat FORMAT_FAST  =  new DecimalFormat("Play x ##;Play x-##"); //$NON-NLS-1$
00117         private static DecimalFormat FORMAT_SLOW  =  new DecimalFormat("Play x 1/##;Play x-1/##"); //$NON-NLS-1$
00118         private static String timeFormat   = "%8.3f"; //"###0.000" //$NON-NLS-1$
00119 
00120         //private static final Font MONO_PLAIN_12 = new Font("Monospaced", Font.PLAIN, 12);
00121         
00122     private int mouseBtnActionNum = 0;
00123     private int mouseBtnRepeat_ = 200;
00124     private int mouseBtnAccelNeed_ = 5;
00125     private int mouseBtnAccelRepeat_ = 30;
00126     private boolean isMouseOverBtnR_ = false;
00127     private boolean isMouseOverBtnF_ = false;
00128     private Runnable backPosRun_;
00129     private Runnable forwardPosRun_;
00130 
00132     // ログ1コマ毎の間隔
00133     double logTimeStepMs_;
00134     // 前回描画時刻
00135     long prevDispNs_;
00136     // 次回タイマー起動時刻
00137     long nextTimerNs_;
00138     final long mills2nano = 1000 * 1000;
00139 
00147         public GrxLoggerView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent) {
00148                 super(name, manager, vp, parent);
00149 
00150                 GridLayout gl = new GridLayout(6,false);
00151                 gl.marginHeight = 0;
00152                 composite_.setLayout( gl );
00153                 
00154                 // playback rate label
00155                 lblPlayRate_ = new Label( composite_, SWT.NONE);
00156                 lblPlayRate_.setText(MessageBundle.get("GrxLoggerView.label.pause")); //$NON-NLS-1$
00157                 GridData lblGridData = new GridData();
00158         lblGridData.widthHint = 80;
00159         lblPlayRate_.setLayoutData(lblGridData);
00160 
00161 
00162         // playback controller
00163                 Composite btnComp = new Composite ( composite_, SWT.NONE);
00164                 GridLayout buttonLayout = new GridLayout(2,false);
00165                 buttonLayout.marginHeight = 0;
00166                 buttonLayout.marginWidth = 0;
00167                 btnComp.setLayout( buttonLayout );
00168 
00169         btnFrameR_ = new Button( btnComp, SWT.NONE );
00170         
00171         btnFrameR_.addMouseTrackListener(new MouseTrackListener(){
00172             public void mouseEnter(MouseEvent e){
00173                 isMouseOverBtnR_ = true;
00174             }
00175             public void mouseExit(MouseEvent e){
00176                 isMouseOverBtnR_ = false;
00177             }
00178             public void mouseHover(MouseEvent e){
00179             }
00180         });
00181 
00182         btnFrameR_.addMouseMoveListener(new MouseMoveListener(){
00183             public void mouseMove(MouseEvent e){
00184                 isMouseOverBtnR_ = (0 <= e.x && e.x < btnFrameR_.getSize().x && 0 <= e.y && e.y < btnFrameR_.getSize().y);
00185             }
00186         });
00187         btnFrameR_.addMouseListener(new MouseListener(){
00188             public void mouseDoubleClick(MouseEvent e){
00189             }
00190             public void mouseDown(MouseEvent e) {
00191                 if(e.button == 1){
00192                     backPosition();
00193                     startBackPosTimer();
00194                 }
00195             }
00196             public void mouseUp(MouseEvent e) {
00197                 if(e.button == 1){
00198                     stopBackPosTimer();
00199                 }
00200             }
00201         });
00202 
00203                 btnFrameF_ = new Button( btnComp, SWT.NONE );
00204         
00205         btnFrameF_.addMouseTrackListener(new MouseTrackListener(){
00206             public void mouseEnter(MouseEvent e){
00207                 isMouseOverBtnF_ = true;
00208             }
00209             public void mouseExit(MouseEvent e){
00210                 isMouseOverBtnF_ = false;
00211             }
00212             public void mouseHover(MouseEvent e){
00213             }
00214         });
00215 
00216         btnFrameF_.addMouseMoveListener(new MouseMoveListener(){
00217             public void mouseMove(MouseEvent e){
00218                 isMouseOverBtnF_ = (0 <= e.x && e.x < btnFrameF_.getSize().x && 0 <= e.y && e.y < btnFrameF_.getSize().y);
00219             }
00220         });
00221         btnFrameF_.addMouseListener(new MouseListener(){
00222             public void mouseDoubleClick(MouseEvent e){
00223             }
00224             public void mouseDown(MouseEvent e) {
00225                 if(e.button == 1){
00226                         forwardPosition();
00227                     startForwardPosTimer();
00228                 }
00229             }
00230             public void mouseUp(MouseEvent e) {
00231                 if(e.button == 1){
00232                     stopForwardPosTimer();
00233                 }
00234             }
00235         });
00236 
00237                 btns_[0] = btnFrameR_;
00238                 btns_[1] = btnFrameF_;
00239 
00240                 for(int i=0; i<btns_.length; i++) {
00241                         btns_[i].setImage( Activator.getDefault().getImage( icons_[i] ) );
00242                         btns_[i].setToolTipText( btnToolTips[i] );
00243                 }
00244 
00245                 // text field to display current time
00246                 tFldTime_ = new Text(composite_, SWT.SINGLE|SWT.BORDER);
00247         tFldTime_.setText(MessageBundle.get("GrxLoggerView.text.noData")); //$NON-NLS-1$
00248         GridData textGridData = new GridData();
00249         textGridData.widthHint = 60;
00250         tFldTime_.setLayoutData(textGridData);
00251         tFldTime_.addKeyListener(new KeyListener(){
00252             public void keyPressed(KeyEvent e) {}
00253             public void keyReleased(KeyEvent e) {
00254                 if (e.character == SWT.CR) {
00255                         String str = tFldTime_.getText();
00256                         try{
00257                                 double tm = Double.parseDouble(str);
00258                                 int newpos = currentItem_.getPositionAt(tm);
00259                                 if (newpos >= 0){
00260                                         currentItem_.setPosition(newpos);
00261                                 }
00262                         }catch(Exception ex){
00263                                 
00264                         }
00265                 }
00266             }
00267         });
00268 
00269                 //時間変更スライダ
00270                 sliderTime_ = new Scale( composite_, SWT.NONE );
00271                 sliderTime_.addSelectionListener(new SelectionAdapter(){
00272             public void widgetSelected(SelectionEvent e){
00273                 currentItem_.setPosition(sliderTime_.getSelection());
00274                 buttonClick_ = true;
00275             }
00276                 } );
00277                 sliderTime_.addKeyListener(new KeyListener(){
00278                         public void keyPressed(KeyEvent e) {
00279                                 if (e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_DOWN){
00280                                 if (isPlaying_)
00281                                     pause();
00282                                         currentItem_.setPosition(sliderTime_.getSelection());
00283                                 }else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == SWT.ARROW_UP){
00284                                 if (isPlaying_)
00285                                     pause();
00286                                         currentItem_.setPosition(sliderTime_.getSelection());
00287                                 }
00288                         }
00289                         public void keyReleased(KeyEvent e) {
00290                         }
00291                 });
00292                 GridData gd = new GridData(SWT.HORIZONTAL|SWT.FILL|GridData.FILL_HORIZONTAL);
00293                 sliderTime_.setLayoutData(gd);
00294                 
00295                 // frame rate
00296                 lblFrameRate_ = new Label(composite_, SWT.NONE);
00297                 lblFrameRate_.setText(MessageBundle.get("GrxLoggerView.label.FPS")+defaultFrameRate_); //$NON-NLS-1$
00298                 
00299                 // フレームレート変更スライダ
00300                 sliderFrameRate_ = new Scale( composite_, SWT.HORIZONTAL );
00301                 //sliderFrameRate_.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00302                 sliderFrameRate_.setSize(80, 27);
00303                 sliderFrameRate_.setMaximum(maxFrameRate_);
00304                 sliderFrameRate_.setMinimum(1);
00305                 sliderFrameRate_.setSelection(defaultFrameRate_);
00306         sliderFrameRate_.addSelectionListener(new SelectionAdapter(){
00307             public void widgetSelected(SelectionEvent e){
00308                 frameRate_ = sliderFrameRate_.getSelection();
00309                 lblFrameRate_.setText( MessageBundle.get("GrxLoggerView.label.FPS")+String.valueOf(frameRate_) ); //$NON-NLS-1$
00310             }
00311         } );
00312 
00313         Action actFRwd_ = new Action(){
00314             public void run(){
00315                 if (!isPlaying_)
00316                     playRate_ = -2.0;
00317                 else if (playRate_ > 0)
00318                     playRate_ *= -1;
00319                 else if (playRate_ > -64)
00320                     playRate_ *= 2.0;
00321                 buttonClick_ = true;
00322                 play();
00323             }
00324         };
00325         
00326         Action actSRwd_ = new Action(){
00327             public void run(){
00328                 if (!isPlaying_)
00329                     playRate_ = -1.0/8.0;
00330                 else if (playRate_ > 0)
00331                     playRate_ *= -1;
00332                 else if (1/playRate_ > -64)
00333                     playRate_ *= 0.5;
00334                 buttonClick_ = true;
00335                 play();
00336             }
00337         };
00338         Action actPause_ = new Action(){
00339             public void run(){
00340                 pause();
00341             }
00342         };
00343 
00344         Action actPlay_ = new Action(){
00345             public void run(){
00346                 playRate_ = 1.0;
00347                 buttonClick_ = true;
00348                 play();
00349             }
00350         };
00351 
00352         Action actSFwd_ = new Action(){
00353             public void run(){
00354                 if (!isPlaying_)
00355                     playRate_ = 1.0/8.0;
00356                 else if (playRate_ < 0)
00357                     playRate_ *= -1;
00358                 else if (1/playRate_ < 64)
00359                     playRate_ *= 0.5;
00360                 buttonClick_ = true;
00361                 play();
00362             }
00363         };
00364 
00365         Action actFFwd_ = new Action(){
00366             public void run(){
00367                 if (!isPlaying_)
00368                     playRate_ = 2.0;
00369                 else if (playRate_ < 0)
00370                     playRate_ *= -1;
00371                 else if (playRate_ < 64)
00372                     playRate_ *= 2.0;
00373                 buttonClick_ = true;
00374                 play();
00375             }
00376         };
00377 
00378         toolActs_[0] = actFRwd_;
00379         toolActs_[1] = actSRwd_; 
00380         toolActs_[2] = actPause_;
00381         toolActs_[3] = actPlay_;
00382         toolActs_[4] = actSFwd_;
00383         toolActs_[5] = actFFwd_;
00384         IActionBars bars = vp.getViewSite().getActionBars();
00385         for(int i = 0; i < toolActs_.length; i++) {
00386             toolActs_[i].setImageDescriptor( Activator.getDefault().getDescriptor(actIcons_[i]) );
00387             toolActs_[i].setDisabledImageDescriptor(Activator.getDefault().getDescriptor(actDIcons_[i]));
00388             toolActs_[i].setToolTipText( actToolTips_[i] );
00389             bars.getToolBarManager().add(toolActs_[i]);
00390         }
00391 
00392         
00393         backPosRun_ = new Runnable() {
00394             public void run() { 
00395                 Display display = btnFrameR_.getDisplay();
00396                 if (!display.isDisposed())
00397                 {
00398                     if(isMouseOverBtnR_)
00399                     {
00400                         mouseBtnActionNum += 1;
00401                         backPosition();
00402                     }
00403                     display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_), this);
00404                 }
00405             }
00406         };
00407         forwardPosRun_ = new Runnable() {
00408             public void run() { 
00409                 Display display = btnFrameF_.getDisplay();
00410                 if (!display.isDisposed())
00411                 {
00412                     if(isMouseOverBtnF_)
00413                     {
00414                         mouseBtnActionNum += 1;
00415                         forwardPosition();
00416                     }
00417                     display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_), this);
00418                 }
00419             }
00420         };
00421 
00422                 setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
00423                 
00424                 setUp();
00425                 manager_.registerItemChangeListener(this, GrxWorldStateItem.class);
00426                 manager_.registerItemChangeListener(this, GrxSimulationItem.class);
00427     }
00428 
00429         public void setUp(){
00430                 if(currentItem_ != null){
00431                         currentItem_.deleteObserver(this);
00432                         currentItem_.deletePosObserver(this);
00433                 }
00434                 currentItem_ = manager_.<GrxWorldStateItem>getSelectedItem(GrxWorldStateItem.class, null);
00435                 if(currentItem_!=null){
00436                         _setTimeSeriesItem(currentItem_);
00437                         currentItem_.addObserver(this);
00438                         currentItem_.addPosObserver(this);
00439                         updatePosition(currentItem_, currentItem_.getPosition());
00440                 }else
00441                         _setTimeSeriesItem(null);
00442                 if(simItem_ != null)
00443                         simItem_.deleteObserver(this);
00444                 simItem_ = manager_.<GrxSimulationItem>getSelectedItem(GrxSimulationItem.class, null);
00445                 if(simItem_!=null){
00446                         simItem_.addObserver(this);
00447                 }
00448         }
00449         
00450         private boolean _isAtTheEndAfterPlayback(){
00451                 if (current_ == currentItem_.getLogSize()-1 && playRate_ > 0){
00452                         return true;
00453                 }else{
00454                         return false;
00455                 }
00456         }
00457         
00458         
00459     private void play() {
00460         if(currentItem_ == null) return;
00461         if (!isPlaying_) {
00462             if (_isAtTheEndAfterPlayback()) currentItem_.setPosition(0);
00463             
00464             if(inSimulation_){
00465                 logTimeStepMs_ = currentItem_.getDbl("logTimeStep", 0.001)*1000; //$NON-NLS-1$
00466             }
00467             else{
00468                 // ログの時間間隔は一定として最初の1区間の間隔を読み込む。
00469                 if(currentItem_.getLogSize() < 2){
00470                     logTimeStepMs_ = currentItem_.getDbl("logTimeStep", 0.001)*1000;
00471                 }else{
00472                     logTimeStepMs_ = (currentItem_.getTime(1) - currentItem_.getTime(0))*1000;
00473                 }
00474             }
00475             
00476             Runnable playRun_ = new Runnable() {
00477                 public void run() {
00478                     if(!isPlaying_) return;
00479 
00480                     long nowNs = System.nanoTime();
00481                     boolean _continue = true;
00482 
00483                     int diffpos = (int)((nowNs - prevDispNs_) / (logTimeStepMs_ * mills2nano) * playRate_);
00484                     int sliderMax = currentItem_.getLogSize()-1;
00485                     int newpos;
00486                     if(inSimulation_ && !buttonClick_)
00487                         newpos = sliderMax;
00488                     else
00489                         newpos = current_ + diffpos;
00490                     if (sliderMax < newpos) {
00491                         newpos = sliderMax;
00492                         if(!inSimulation_)
00493                             _continue = false;
00494                     } else if (newpos < 0) {
00495                         newpos = 0;
00496                         if(!inSimulation_)
00497                                 _continue = false;
00498                     }
00499 
00500                     if(newpos != current_){
00501                         //GrxDebugUtil.println(String.format("nowNs : %15d, pos : %d, logtime : %12.3f",
00502                         //        (long)(nowNs/mills2nano), newpos, currentItem_.getTime(newpos)));
00503                         diffpos = newpos - current_;
00504                         currentItem_.setPosition(newpos);
00505                         prevDispNs_ += (long)((logTimeStepMs_ * mills2nano) * diffpos / playRate_);
00506                     }
00507 
00508                     if(_continue){
00509                         nowNs = System.nanoTime();
00510                         int sleepTime = (int)((1000.0d/frameRate_) - ((nowNs - nextTimerNs_) / mills2nano));
00511                         //GrxDebugUtil.println(String.format("nowNs : %15d, next : %15d, sleep: %6d",
00512                         //        (long)(nowNs/mills2nano), (long)(nextTimerNs_/mills2nano), sleepTime));
00513                         if(sleepTime < 1) sleepTime = 1;
00514                         nextTimerNs_ = nowNs + (sleepTime * mills2nano);
00515                         Display display = composite_.getDisplay();
00516                         if (!display.isDisposed())
00517                             display.timerExec(sleepTime, this);
00518                     }else{
00519                         pause();
00520                     }
00521                 }
00522             };
00523             
00524             isPlaying_ = true;
00525             long nowNs = System.nanoTime();
00526             prevDispNs_ = nowNs;
00527             int sleepTime = (int)(1000.0d/frameRate_);
00528             nextTimerNs_ = nowNs + (sleepTime * mills2nano);
00529             Display display = composite_.getDisplay();
00530             if (!display.isDisposed())
00531                 display.timerExec(sleepTime, playRun_);
00532         }
00533         if (Math.abs(playRate_)<1.0)
00534             lblPlayRate_.setText(FORMAT_SLOW.format(1/playRate_));
00535         else
00536             lblPlayRate_.setText(FORMAT_FAST.format(playRate_));
00537     }
00538 
00542         public void pause() {
00543                 isPlaying_ = false;
00544                 lblPlayRate_.setText(MessageBundle.get("GrxLoggerView.label.pause")); //$NON-NLS-1$
00545         }
00546         
00551         public boolean isPlaying() {
00552                 return isPlaying_;
00553         }
00554         
00558         public void restoreProperties() {
00559                 super.restoreProperties();
00560                 
00561                 timeFormat = getStr("timeFormat", "%8.3f"); //$NON-NLS-1$ //$NON-NLS-2$
00562                 GrxDebugUtil.println("LoggerView: timeFormat = \""+timeFormat+"\"\n"); //$NON-NLS-1$ //$NON-NLS-2$
00563         }
00564         
00565         private void _setTimeSeriesItem(GrxWorldStateItem item){
00566                 current_ = 0;
00567                 sliderTime_.setSelection(0);
00568                 if (item != null){
00569                         _updateTimeField(item);
00570                         int size = item.getLogSize();
00571                         if(size > 0){
00572                                 sliderTimeSetMaximam(item.getLogSize());
00573                                 setEnabled(true);
00574                         }else{
00575                                 sliderTimeSetMaximam(1);
00576                                 setEnabled(false);
00577                         }
00578                 }else{
00579                         sliderTimeSetMaximam(1);
00580                         setEnabled(false);
00581                 }
00582         }
00583         
00584         public void registerItemChange(GrxBaseItem item, int event){
00585                 if(item instanceof GrxWorldStateItem){
00586                         GrxWorldStateItem worldStateItem = (GrxWorldStateItem) item;
00587                 switch(event){
00588                 case GrxPluginManager.SELECTED_ITEM:
00589                         if(currentItem_!=worldStateItem){
00590                                 _setTimeSeriesItem(worldStateItem);
00591                                 currentItem_ = worldStateItem;
00592                                 currentItem_.addObserver(this);
00593                                 currentItem_.addPosObserver(this);
00594                                 updatePosition(currentItem_, currentItem_.getPosition());
00595                         }
00596                         break;
00597                 case GrxPluginManager.REMOVE_ITEM:
00598                 case GrxPluginManager.NOTSELECTED_ITEM:
00599                         if(currentItem_==worldStateItem){
00600                                 currentItem_.deleteObserver(this);
00601                                 currentItem_.deletePosObserver(this);
00602                                 _setTimeSeriesItem(null);
00603                                 currentItem_ = null;
00604                         }
00605                         break;
00606                 default:
00607                         break;
00608                 }
00609                 }else if(item instanceof GrxSimulationItem){
00610                 GrxSimulationItem simItem = (GrxSimulationItem) item;
00611                 switch(event){
00612                 case GrxPluginManager.SELECTED_ITEM:
00613                         if(simItem_!=simItem){
00614                                 simItem_ = simItem;
00615                                 simItem_.addObserver(this);
00616                         }
00617                         break;
00618                 case GrxPluginManager.REMOVE_ITEM:
00619                 case GrxPluginManager.NOTSELECTED_ITEM:
00620                         if(simItem_==simItem){
00621                                 simItem_.deleteObserver(this);
00622                                 simItem_ = null;
00623                         }
00624                         break;
00625                 default:
00626                         break;
00627                 }
00628         }
00629         }
00630         
00634         private void _updateTimeField(GrxWorldStateItem item) {
00635                 Double t = item.getTime();
00636                 if (t != null) {
00637                         tFldTime_.setText(String.format(timeFormat, t));
00638                         setEnabled(true);
00639                 } else if (item.getLogSize() == 0){
00640                         tFldTime_.setText(MessageBundle.get("GrxLoggerView.text.noData")); //$NON-NLS-1$
00641                         setEnabled(false);
00642                 }
00643         }
00644 
00649         public int getMaximum() {
00650                 return sliderTime_.getMaximum();
00651         }
00652 
00657         public int getCurrentPos() {
00658                 return current_;
00659         }
00660 
00661         public void update(GrxBasePlugin plugin, Object... arg) {
00662                 if(simItem_==plugin){
00663                         if((String)arg[0]=="StartSimulation"){ //$NON-NLS-1$
00664                                 inSimulation_ = true; 
00665                                 buttonClick_ = false;
00666                                 lblPlayRate_.setText(MessageBundle.get("GrxLoggerView.label.live")); //$NON-NLS-1$
00667                                 if((Boolean)arg[1])
00668                                         disableControl();
00669                                 else{
00670                                         playRate_ = 1;
00671                                         play();
00672                                 }
00673                         }else if((String)arg[0]=="StopSimulation"){ //$NON-NLS-1$
00674                                 inSimulation_ = false;
00675                                 lblPlayRate_.setText(MessageBundle.get("GrxLoggerView.label.pause")); //$NON-NLS-1$
00676                                 enableControl();
00677                                 pause();
00678                         }
00679                 }else if(currentItem_==plugin) {
00680                         if((String)arg[0]=="ClearLog"){ //$NON-NLS-1$
00681                                 if(isPlaying())
00682                                         pause();
00683                                 _setTimeSeriesItem(currentItem_);
00684                         }else if((String)arg[0]=="LoadLog"){
00685                                 _setTimeSeriesItem(currentItem_ );
00686                         updatePosition(currentItem_, currentItem_.getPosition());
00687                         }
00688                 }
00689         }
00690 
00691     public void updatePosition(GrxBasePlugin plugin, Integer arg_pos){
00692         if(currentItem_ != plugin)
00693             return;
00694         
00695         int pos = arg_pos.intValue();
00696         int logSize = currentItem_.getLogSize();
00697         if ( pos < 0 || logSize < pos){
00698             return;
00699         }
00700         current_ = pos;
00701         sliderTimeSetMaximam(logSize-1);
00702         sliderTime_.setSelection(pos);
00703         _updateTimeField(currentItem_);
00704     }
00705 
00710     public double getPlayRate() {
00711         return playRate_;
00712     }
00713         
00714         private void setEnabled(boolean b) {
00715         if (isControlDisabled_) return;
00716                 sliderTime_.setEnabled(b);
00717                 tFldTime_.setEnabled(b);
00718                 lblPlayRate_.setEnabled(b);
00719                 for (int i=0; i<btns_.length; i++)
00720                         btns_[i].setEnabled(b);
00721 
00722                 for (int i=0; i<toolActs_.length; i++)
00723                         toolActs_[i].setEnabled(b);
00724 
00725         }
00726         
00727     public void disableControl() {
00728         setEnabled(false);
00729         isControlDisabled_ = true;
00730     }
00731     
00732     public void enableControl() {
00733         isControlDisabled_ = false;
00734         setEnabled(true);
00735     }
00736     
00737     public void shutdown() {
00738         manager_.removeItemChangeListener(this, GrxWorldStateItem.class);
00739         if(currentItem_!=null)
00740         {
00741             currentItem_.deleteObserver(this);
00742             currentItem_.deletePosObserver(this);
00743         }
00744         if(simItem_!=null)
00745                         simItem_.deleteObserver(this);
00746                 manager_.removeItemChangeListener(this, GrxSimulationItem.class);
00747         }
00748     
00749     private void sliderTimeSetMaximam(int value)
00750     {
00751         sliderTime_.setMaximum(value);
00752 
00753         if(value < 150)
00754             sliderTime_.setPageIncrement(5);
00755         if(value < 1500)
00756             sliderTime_.setPageIncrement(50);
00757         else
00758             sliderTime_.setPageIncrement(500);
00759     }
00760     
00761     private void backPosition(){
00762         if (isPlaying_)
00763             pause();
00764         currentItem_.setPosition(sliderTime_.getSelection()-1);
00765     }
00766     private void forwardPosition(){
00767         if (isPlaying_)
00768             pause();
00769         currentItem_.setPosition(sliderTime_.getSelection()+1);
00770     }
00771     private void startBackPosTimer(){
00772         mouseBtnActionNum = 0;
00773 
00774         Display display = btnFrameR_.getDisplay();
00775         if (!display.isDisposed())
00776         {
00777             display.timerExec(mouseBtnRepeat_, backPosRun_);
00778         }
00779     }
00780     private void stopBackPosTimer(){
00781         mouseBtnActionNum = 0;
00782 
00783         Display display = btnFrameR_.getDisplay();
00784         if (!display.isDisposed())
00785         {
00786             display.timerExec(-1, backPosRun_);
00787         }
00788     }
00789     private void startForwardPosTimer(){
00790         mouseBtnActionNum = 0;
00791 
00792         Display display = btnFrameF_.getDisplay();
00793         if (!display.isDisposed())
00794         {
00795             display.timerExec(mouseBtnRepeat_, forwardPosRun_);
00796         }
00797     }
00798     private void stopForwardPosTimer(){
00799         mouseBtnActionNum = 0;
00800 
00801         Display display = btnFrameF_.getDisplay();
00802         if (!display.isDisposed())
00803         {
00804             display.timerExec(-1, forwardPosRun_);
00805         }
00806     }
00807 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:53