GrxRangeSensorView.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  *  GrxRangeSensorView.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.Iterator;
00023 import java.util.List;
00024 
00025 import jp.go.aist.hrp.simulator.SensorState;
00026 
00027 import org.eclipse.swt.SWT;
00028 import org.eclipse.swt.events.PaintEvent;
00029 import org.eclipse.swt.events.PaintListener;
00030 import org.eclipse.swt.events.SelectionEvent;
00031 import org.eclipse.swt.events.SelectionListener;
00032 import org.eclipse.swt.graphics.Rectangle;
00033 import org.eclipse.swt.layout.GridData;
00034 import org.eclipse.swt.layout.GridLayout;
00035 import org.eclipse.swt.widgets.Canvas;
00036 import org.eclipse.swt.widgets.Combo;
00037 import org.eclipse.swt.widgets.Composite;
00038 
00039 import com.generalrobotix.ui.GrxBaseItem;
00040 import com.generalrobotix.ui.GrxBasePlugin;
00041 import com.generalrobotix.ui.GrxBaseView;
00042 import com.generalrobotix.ui.GrxBaseViewPart;
00043 import com.generalrobotix.ui.GrxPluginManager;
00044 import com.generalrobotix.ui.grxui.Activator;
00045 import com.generalrobotix.ui.item.GrxModelItem;
00046 import com.generalrobotix.ui.item.GrxSensorItem;
00047 import com.generalrobotix.ui.item.GrxWorldStateItem;
00048 import com.generalrobotix.ui.item.GrxWorldStateItem.CharacterStateEx;
00049 import com.generalrobotix.ui.item.GrxWorldStateItem.WorldStateEx;
00050 
00054 @SuppressWarnings("serial")
00055 public class GrxRangeSensorView extends GrxBaseView implements PaintListener{
00056     public static final String TITLE = "Range Sensor View";
00057 
00058     private Composite canvas_;
00059     private GrxWorldStateItem currentWorld_ = null;
00060     private GrxModelItem currentModel_ = null;
00061     private GrxSensorItem currentSensor_ = null;
00062     private SensorState  currentSensorState_ = null;
00063     
00064     private Combo comboModelName_;
00065     private Combo comboSensorName_;
00066     private List<GrxModelItem> modelList_;
00067     private List<GrxSensorItem> sensorList_;
00068     
00076     public GrxRangeSensorView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent) {
00077         super(name, manager, vp, parent);
00078         
00079         GridLayout layout = new GridLayout(1, false);
00080         composite_.setLayout(layout);
00081         
00082         Composite northPane = new Composite(composite_,SWT.NONE);
00083         northPane.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00084         GridLayout gridLayout = new GridLayout(2,true);
00085         northPane.setLayout(gridLayout);
00086         
00087         comboModelName_ = new Combo(northPane,SWT.READ_ONLY);
00088         comboModelName_.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00089         modelList_ = new ArrayList<GrxModelItem>();
00090         
00091         comboModelName_.addSelectionListener(new SelectionListener(){
00092             public void widgetSelected(SelectionEvent e) {
00093                 GrxModelItem item = modelList_.get(comboModelName_.getSelectionIndex());
00094                 if (item == null || item == currentModel_)
00095                     return;
00096 
00097                 currentModel_ = item;
00098                 List<GrxSensorItem> sensors = currentModel_.getSensors("Range");
00099                 comboSensorName_.removeAll();
00100                 sensorList_.clear();
00101                 if (sensors != null){
00102                         for (int i=0; i<sensors.size(); i++){
00103                                 comboSensorName_.add(sensors.get(i).getName());
00104                                 sensorList_.add(sensors.get(i));
00105                         }
00106                         if(sensors.size()>0){
00107                                 comboSensorName_.select(0);
00108                                 comboSensorName_.notifyListeners(SWT.Selection, null);
00109                         }
00110                 }else
00111                         updateCanvas(null);
00112                         
00113             }
00114                         public void widgetDefaultSelected(SelectionEvent e) {
00115                         }
00116         });
00117 
00118         comboSensorName_ = new Combo(northPane,SWT.READ_ONLY);
00119         comboSensorName_.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00120         sensorList_ = new ArrayList<GrxSensorItem>();
00121         
00122         comboSensorName_.addSelectionListener(new SelectionListener(){
00123                 public void widgetSelected(SelectionEvent e){
00124                         currentSensor_ = sensorList_.get(comboSensorName_.getSelectionIndex());
00125                         if(currentWorld_!=null)
00126                                 updateCanvas(currentWorld_.getValue());
00127                 }
00128                         public void widgetDefaultSelected(SelectionEvent e) {
00129                         }
00130         });
00131         canvas_ = new Canvas(composite_,SWT.NONE);
00132         canvas_.addPaintListener(this);
00133         canvas_.setLayoutData(new GridData(GridData.FILL_BOTH));
00134 
00135         setUp();
00136         manager_.registerItemChangeListener(this, GrxModelItem.class);
00137         manager_.registerItemChangeListener(this, GrxWorldStateItem.class);
00138    }
00139 
00140     public void setUp(){
00141         Iterator<GrxModelItem> it = modelList_.iterator();
00142         while(it.hasNext())
00143                 comboModelName_.remove(it.next().getName());
00144         modelList_ = manager_.<GrxModelItem>getSelectedItemList(GrxModelItem.class);
00145         it = modelList_.iterator();
00146             while(it.hasNext())
00147                 comboModelName_.add(it.next().getName());
00148             if(comboModelName_.getItemCount()>0){
00149                         comboModelName_.select(0);
00150                         comboModelName_.notifyListeners(SWT.Selection, null);
00151             }
00152                 
00153                 if(currentWorld_ != null){
00154                         currentWorld_.deleteObserver(this);
00155                         currentWorld_.deletePosObserver(this);
00156                 }
00157         currentWorld_ = manager_.<GrxWorldStateItem>getSelectedItem(GrxWorldStateItem.class, null);
00158         if(currentWorld_!=null){
00159                 currentWorld_.addObserver(this);
00160                 currentWorld_.addPosObserver(this);
00161                 updateCanvas(currentWorld_.getValue());
00162         }else
00163                 updateCanvas(null);
00164 
00165     }
00166     
00171     public void paintControl(PaintEvent e) {
00172         if (currentSensor_ != null && currentSensorState_ != null){
00173                 double step = currentSensor_.getDbl("scanStep", 0.1);
00174                 double maxD = currentSensor_.getDbl("maxDistance", 10.0);
00175                 if (currentSensor_.id_ >= 0 && currentSensor_.id_ < currentSensorState_.range.length){
00176                 Rectangle bounds = canvas_.getBounds();
00177                 if(bounds.height < bounds.width) {
00178                         bounds.x = (bounds.width - bounds.height) / 2;
00179                         bounds.width = bounds.height;
00180                         bounds.y = 0;
00181                 } else {
00182                         bounds.y = (bounds.height - bounds.width) / 2;
00183                         bounds.height = bounds.width;
00184                         bounds.x = 0;
00185                 }
00186                 e.gc.drawArc(bounds.x, bounds.y, bounds.width-1, bounds.height-1,0,360);
00187                         double [] distances = currentSensorState_.range[currentSensor_.id_];
00188                         int centerx = bounds.x + bounds.width / 2;
00189                         int centery = bounds.y + bounds.height / 2;
00190                         double scale = bounds.width/(maxD*2);
00191                         e.gc.setBackground(Activator.getDefault().getColor("darkGray"));
00192                         int stepAngle = (int)(step*57.29579+0.5);
00193                         int half = (int)(distances.length/2);
00194                         double startAngle = 1.570796-step*half-step/2;
00195                         for(int i=0; i<distances.length; i++){
00196                                 double distance = distances[i];
00197                                 if(distance==0)
00198                                         distance = maxD;
00199                                 int iDistance = (int)(scale*distance);
00200                                 e.gc.fillArc(centerx-iDistance, centery-iDistance, iDistance*2, iDistance*2, (int)(startAngle*57.29579), stepAngle);
00201                                 startAngle += step;
00202                         }
00203                 }
00204         }
00205     }
00206     
00207     public void updatePosition(GrxBasePlugin plugin, Integer arg_pos){
00208         if(currentWorld_!=plugin) return;
00209 
00210         int pos = arg_pos.intValue();
00211         WorldStateEx state = currentWorld_.getValue(pos);
00212         updateCanvas(state);
00213     }
00214     
00215     private void updateCanvas( WorldStateEx state ){
00216         if (state != null  && currentModel_ != null) {
00217                 CharacterStateEx charStat = state.get(currentModel_.getName());
00218                 if (charStat != null ) {
00219                         currentSensorState_ = charStat.sensorState;
00220                         canvas_.redraw();
00221                 }
00222         }else{
00223                 currentSensorState_ = null;
00224                 canvas_.redraw();
00225         }
00226     }
00227     
00228     public void registerItemChange(GrxBaseItem item, int event){
00229         if(item instanceof GrxModelItem){
00230                 GrxModelItem modelItem = (GrxModelItem) item;
00231                 switch(event){
00232                 case GrxPluginManager.ADD_ITEM:
00233                         if(!modelList_.contains(modelItem)){
00234                                 modelList_.add(modelItem);
00235                                 comboModelName_.add(modelItem.getName());
00236                                 if(currentModel_==null){
00237                                         comboModelName_.select(0);
00238                                         comboModelName_.notifyListeners(SWT.Selection, null);
00239                                 }
00240                         }
00241                         break;
00242                 case GrxPluginManager.REMOVE_ITEM:
00243                         if(modelList_.contains(modelItem)){
00244                                 modelList_.remove(modelItem);
00245                                 comboModelName_.remove(modelItem.getName());
00246                                 if(currentModel_==modelItem){
00247                                         if(modelList_.size()>0){
00248                                                 comboModelName_.select(0);
00249                                                 comboModelName_.notifyListeners(SWT.Selection, null);
00250                                         }else
00251                                                 currentModel_=null;
00252                                 }
00253                         }
00254                         break;
00255                 default:
00256                         break;
00257                 }
00258         }else if(item instanceof GrxWorldStateItem){
00259                 GrxWorldStateItem worldStateItem = (GrxWorldStateItem) item;
00260                 switch(event){
00261                 case GrxPluginManager.SELECTED_ITEM:
00262                         if(currentWorld_!=worldStateItem){
00263                                 currentWorld_ = worldStateItem;
00264                                 currentWorld_.addObserver(this);
00265                                 currentWorld_.addPosObserver(this);
00266                                 updateCanvas(currentWorld_.getValue());
00267                         }
00268                         break;
00269                 case GrxPluginManager.REMOVE_ITEM:
00270                 case GrxPluginManager.NOTSELECTED_ITEM:
00271                         if(currentWorld_==worldStateItem){
00272                                 currentWorld_.deleteObserver(this);
00273                                 currentWorld_.deletePosObserver(this);
00274                                 currentWorld_ = null;
00275                                 updateCanvas(null);
00276                         }
00277                         break;
00278                 default:
00279                         break;
00280                 }
00281         }
00282     }
00283     
00284     public void shutdown() {
00285         manager_.removeItemChangeListener(this, GrxModelItem.class);
00286         manager_.removeItemChangeListener(this, GrxWorldStateItem.class);
00287         if(currentWorld_!=null) {
00288                          currentWorld_.deleteObserver(this);
00289                          currentWorld_.deletePosObserver(this);
00290         }
00291         }
00292 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16