GrxGraphView.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxGraphView.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  */
00018 
00019 package com.generalrobotix.ui.view;
00020 
00021 import java.util.ArrayList;
00022 import java.util.List;
00023 
00024 import org.eclipse.jface.resource.StringConverter;
00025 import org.eclipse.swt.SWT;
00026 import org.eclipse.swt.graphics.RGB;
00027 import org.eclipse.swt.widgets.Composite;
00028 
00029 import com.generalrobotix.ui.GrxBaseItem;
00030 import com.generalrobotix.ui.GrxBasePlugin;
00031 import com.generalrobotix.ui.GrxBaseView;
00032 import com.generalrobotix.ui.GrxBaseViewPart;
00033 import com.generalrobotix.ui.GrxPluginManager;
00034 import com.generalrobotix.ui.GrxTimeSeriesItem;
00035 import com.generalrobotix.ui.item.GrxGraphItem;
00036 import com.generalrobotix.ui.item.GrxModelItem;
00037 import com.generalrobotix.ui.item.GrxSimulationItem;
00038 import com.generalrobotix.ui.item.GrxWorldStateItem;
00039 import com.generalrobotix.ui.item.GrxWorldStateItem.WorldStateEx;
00040 import com.generalrobotix.ui.view.graph.DataItem;
00041 import com.generalrobotix.ui.view.graph.DataItemInfo;
00042 import com.generalrobotix.ui.view.graph.GraphPanel;
00043 import com.generalrobotix.ui.view.graph.Time;
00044 import com.generalrobotix.ui.view.graph.TrendGraph;
00045 import com.generalrobotix.ui.view.graph.TrendGraphModel;
00046 
00047 @SuppressWarnings("serial")
00048 public class GrxGraphView extends GrxBaseView {
00049     public static final String TITLE = "Graph";
00050         public static final int NumOfGraph = 3;
00051         
00052         private GrxWorldStateItem currentWorld_ = null;
00053     private GrxSimulationItem simItem_ = null;
00054         private GrxGraphItem currentGraph_ = null;
00055         private List<GrxModelItem> currentModels_ = new ArrayList<GrxModelItem>();
00056         private TrendGraphModel graphManager_;
00057         private GraphPanel gpanel_;
00058         
00059         public GrxGraphView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent)  {
00060                 super(name, manager, vp, parent);
00061                 isScrollable_ = false;
00062                 graphManager_ = new TrendGraphModel(NumOfGraph );
00063                 
00064                 gpanel_ = new GraphPanel(manager_, graphManager_, composite_ );
00065         setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
00066         graphManager_.setPanel(gpanel_);
00067         
00068         setUp();
00069         manager_.registerItemChangeListener(this, GrxSimulationItem.class);
00070         manager_.registerItemChangeListener(this, GrxWorldStateItem.class);
00071         manager_.registerItemChangeListener(this, GrxGraphItem.class);   
00072         manager_.registerItemChangeListener(this, GrxModelItem.class);
00073         }
00074 
00075         public void setUp(){
00076                 if(currentGraph_!=null){
00077                 _updateGraph(null);
00078         }
00079         currentGraph_ = manager_.<GrxGraphItem>getSelectedItem(GrxGraphItem.class, null);
00080         if(currentGraph_!=null){
00081                 _updateGraph(currentGraph_);
00082         }
00083                 if(currentWorld_!=null){
00084                         currentWorld_.deleteObserver(this);
00085             currentWorld_.deletePosObserver(this);
00086                 }
00087                 currentWorld_ = manager_.<GrxWorldStateItem>getSelectedItem(GrxWorldStateItem.class, null);
00088         if(currentWorld_!=null){
00089                 currentWorld_.addObserver(this);
00090                 currentWorld_.addPosObserver(this);
00091                 graphManager_.setWorldState(currentWorld_);
00092         }else
00093                 graphManager_.setWorldState(null);
00094         graphManager_.updateGraph();
00095         
00096         if(simItem_!=null)
00097                 simItem_.deleteObserver(this);
00098         simItem_ = manager_.<GrxSimulationItem>getSelectedItem(GrxSimulationItem.class, null);
00099         if(simItem_!=null)
00100             simItem_.addObserver(this);
00101         
00102         currentModels_ = manager_.<GrxModelItem>getSelectedItemList(GrxModelItem.class);
00103         gpanel_.setModelList(currentModels_);
00104         }
00105         
00106     public void registerItemChange(GrxBaseItem item, int event){
00107         if(item instanceof GrxWorldStateItem){
00108             GrxWorldStateItem worldStateItem = (GrxWorldStateItem) item;
00109             switch(event){
00110             case GrxPluginManager.SELECTED_ITEM:
00111                 if(currentWorld_!=worldStateItem){
00112                     currentWorld_ = worldStateItem;
00113                     currentWorld_.addObserver(this);
00114                     currentWorld_.addPosObserver(this);
00115                     graphManager_.setWorldState(currentWorld_);
00116                     graphManager_.updateGraph();
00117                 }
00118                 break;
00119             case GrxPluginManager.REMOVE_ITEM:
00120             case GrxPluginManager.NOTSELECTED_ITEM:
00121                 if(currentWorld_==worldStateItem){
00122                     currentWorld_.deleteObserver(this);
00123                     currentWorld_.deletePosObserver(this);
00124                     currentWorld_ = null;
00125                     graphManager_.setWorldState(null);
00126                     graphManager_.updateGraph();
00127                 }
00128                 break;
00129             default:
00130                 break;
00131             }
00132         }else if(item instanceof GrxGraphItem){
00133             GrxGraphItem graphItem = (GrxGraphItem) item;
00134             switch(event){
00135             case GrxPluginManager.SELECTED_ITEM:
00136                 _updateGraph(graphItem);
00137                 currentGraph_ = graphItem;
00138                 break;
00139             case GrxPluginManager.REMOVE_ITEM:
00140             case GrxPluginManager.NOTSELECTED_ITEM:
00141                 _updateGraph(null);
00142                 currentGraph_ = null;
00143                 break;
00144             default:
00145                 break;
00146             }
00147         }else if(item instanceof GrxModelItem){
00148             GrxModelItem modelItem = (GrxModelItem) item;
00149             switch(event){
00150             case GrxPluginManager.SELECTED_ITEM:
00151                 if(!currentModels_.contains(modelItem)){
00152                     currentModels_.add(modelItem);
00153                     gpanel_.setModelList(currentModels_);
00154                 }
00155                 break;
00156             case GrxPluginManager.REMOVE_ITEM:
00157             case GrxPluginManager.NOTSELECTED_ITEM:
00158                 if(currentModels_.contains(modelItem)){
00159                     currentModels_.remove(modelItem);
00160                     gpanel_.setModelList(currentModels_);
00161                 }
00162                 break;
00163             default:
00164                 break;
00165             }
00166         }else if(item instanceof GrxSimulationItem){
00167             GrxSimulationItem simItem = (GrxSimulationItem) item;
00168             switch(event){
00169             case GrxPluginManager.SELECTED_ITEM:
00170                 if(simItem_!=simItem){
00171                     simItem_ = simItem;
00172                     simItem_.addObserver(this);
00173                 }
00174                 break;
00175             case GrxPluginManager.REMOVE_ITEM:
00176             case GrxPluginManager.NOTSELECTED_ITEM:
00177                 if(simItem_==simItem){
00178                     simItem_.deleteObserver(this);
00179                     simItem_ = null;
00180                 }
00181                 break;
00182             default:
00183                 break;
00184             }
00185         }
00186     }
00187         
00188         
00189         private void _updateGraph(GrxBasePlugin p) {
00190                 graphManager_.clearDataItem();
00191         
00192                 if (p == null) {
00193                         gpanel_.setEnabledRangeButton(false);
00194                         graphManager_.updateGraph();
00195                         return;
00196                 }
00197                 
00198                 gpanel_.setEnabledRangeButton(true);
00199                 
00200                 for (int i = 0; i < graphManager_.getNumGraph(); i++) {
00201                         String graphName = "Graph" + i;
00202                         String header = graphName + ".";
00203                         String ditems = p.getStr(header + "dataItems");
00204                         if (ditems == null)
00205                                 continue;
00206 
00207                         TrendGraph tgraph = graphManager_.getTrendGraph(i);
00208                         List<String> addedList = new ArrayList<String>();
00209                         String[] str = ditems.split(",");
00210                         for (int j = 0; j < str.length; j++) {
00211                                 if (str[j].equals(""))
00212                                         continue;
00213                                 String object= p.getStr(header + str[j] + ".object");
00214                                 String node  = p.getStr(header + str[j] + ".node");
00215                                 String attr  = p.getStr(header + str[j] + ".attr");
00216                                 int index    = p.getInt(header + str[j] + ".index", -1);
00217                                 String sColor= p.getStr(header + str[j] + ".color");
00218                                 RGB color=null;
00219                                 if(sColor!=null)
00220                                         color=StringConverter.asRGB(sColor);
00221                                 String legend= p.getStr(header + str[j] + ".legend");
00222                                 String type = "";
00223                                 if(attr.equals("angle"))
00224                                         type = "Joint";
00225                                 else if (attr.equals("force")) 
00226                                         type = "ForceSensor";
00227                                 else if (attr.equals("torque")) 
00228                                         type = "ForceSensor";
00229                                 else if (attr.equals("acceleration"))
00230                                         type = "AccelerationSensor";
00231                                 else if (attr.equals("angularVelocity"))
00232                                         type = "Gyro";
00233 
00234                                 DataItem ditem = new  DataItem(object, node, attr, index, type);
00235                                 if (!addedList.contains(ditem.toString())){
00236                                         addedList.add(ditem.toString());
00237                                         tgraph.addDataItem(new DataItemInfo(ditem, color, legend));
00238                                 }
00239                         }
00240 
00241                         double[] vRange = p.getDblAry(header + "vRange", null);
00242                         if (vRange != null) {
00243                                 tgraph.setRange(vRange[0], vRange[1]);
00244                         }
00245                 }
00246                 
00247                 double[] timeRange = p.getDblAry("timeRange", new double[]{1.0, 0.8});
00248                 graphManager_.setRangeAndPos(timeRange[0], timeRange[1]);
00249                 
00250                 graphManager_.updateGraph();
00251         }
00252 
00253         private boolean isSimulation = false;
00254     public void update(GrxBasePlugin plugin, Object... arg) {
00255         if(simItem_==plugin){
00256             if((String)arg[0]=="StartSimulation"){ //$NON-NLS-1$
00257                                 double step = currentWorld_.getDbl("logTimeStep", 0.001);
00258                                 graphManager_.setStepTime((long)(1000000*step));
00259                                 graphManager_.initGetData();
00260                                 isSimulation = true;
00261             }else if((String)arg[0]=="StopSimulation"){
00262                 isSimulation = false;
00263                 graphManager_.setTotalTime(currentWorld_.getTime(currentWorld_.getLogSize()-1));
00264             }
00265         } else if(currentWorld_==plugin) {
00266             if((String)arg[0]=="ClearLog"){ //$NON-NLS-1$
00267                 graphManager_.setWorldState(currentWorld_);
00268                 graphManager_.updateGraph();
00269             }else if((String)arg[0]=="LoadLog"){
00270                 graphManager_.setWorldState(currentWorld_);
00271                 graphManager_.updateGraph();
00272             }
00273         }
00274     }
00275     
00276     public void updatePosition(GrxBasePlugin plugin, Integer arg_pos){
00277         if(currentWorld_!=plugin) return;
00278 
00279         int pos = arg_pos.intValue();
00280         WorldStateEx state = currentWorld_.getValue(pos);
00281         if (state == null){
00282                 updateGraph(new Time(0));
00283         }else{
00284                 Time time_ = new Time();
00285                         time_.set(state.time);
00286                 updateGraph(time_);
00287         }
00288     }
00289 
00290         private void updateGraph(Time time_){
00291                 if(isSimulation)
00292                         graphManager_.setTotalTime(((GrxTimeSeriesItem)currentWorld_).getTime(currentWorld_.getLogSize()-1));
00293                 graphManager_.worldTimeChanged(time_);
00294         }
00295 
00296     public void shutdown() {
00297         manager_.removeItemChangeListener(this, GrxGraphItem.class);
00298         manager_.removeItemChangeListener(this, GrxModelItem.class);
00299         manager_.removeItemChangeListener(this, GrxWorldStateItem.class);
00300         manager_.removeItemChangeListener(this, GrxSimulationItem.class);
00301         if(currentWorld_!=null)
00302         {
00303             currentWorld_.deleteObserver(this);
00304             currentWorld_.deletePosObserver(this);
00305         }
00306 
00307         if(simItem_!=null)
00308             simItem_.deleteObserver(this);
00309     }
00310     
00311 }


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