StartSimulate.java
Go to the documentation of this file.
00001 package com.generalrobotix.ui.actions;
00002 
00003 import org.eclipse.jface.action.ActionContributionItem;
00004 import org.eclipse.jface.action.IAction;
00005 import org.eclipse.jface.action.IContributionItem;
00006 import org.eclipse.jface.action.ICoolBarManager;
00007 import org.eclipse.jface.action.IToolBarManager;
00008 import org.eclipse.jface.action.ToolBarContributionItem;
00009 import org.eclipse.jface.viewers.ISelection;
00010 import org.eclipse.jface.window.ApplicationWindow;
00011 import org.eclipse.ui.IWorkbenchWindow;
00012 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
00013 import org.eclipse.ui.PlatformUI;
00014 
00015 import com.generalrobotix.ui.GrxBaseItem;
00016 import com.generalrobotix.ui.GrxBasePlugin;
00017 import com.generalrobotix.ui.GrxItemChangeListener;
00018 import com.generalrobotix.ui.GrxObserver;
00019 import com.generalrobotix.ui.GrxPluginManager;
00020 import com.generalrobotix.ui.grxui.Activator;
00021 import com.generalrobotix.ui.item.GrxSimulationItem;
00022 import com.generalrobotix.ui.item.GrxWorldStateItem;
00023 import com.generalrobotix.ui.util.MessageBundle;
00024 
00025 public class StartSimulate implements IWorkbenchWindowActionDelegate, GrxItemChangeListener, GrxObserver {
00026         private IAction action_ = null;
00027         private GrxSimulationItem simItem_=null;
00028         
00029         public StartSimulate() {
00030                 setUp();
00031                 GrxPluginManager manager_ = Activator.getDefault().manager_;
00032                 manager_.registerItemChangeListener(this, GrxSimulationItem.class);
00033         }
00034 
00035         public void setUp(){
00036                 if(simItem_ != null)
00037                         simItem_.deleteObserver(this);
00038                 GrxPluginManager manager_ = Activator.getDefault().manager_;
00039                 simItem_ = manager_.<GrxSimulationItem>getSelectedItem(GrxSimulationItem.class, null);
00040                 if(simItem_!=null){
00041                         simItem_.addObserver(this);
00042                 }
00043         }
00044         
00045         public void run(IAction action) {
00046                 action_ = action;
00047                 GrxPluginManager manager_ = Activator.getDefault().manager_;
00048                 if(simItem_==null){
00049                         simItem_ = (GrxSimulationItem)manager_.createItem(GrxSimulationItem.class, null);
00050                         simItem_.addObserver(this);
00051                         manager_.itemChange(simItem_, GrxPluginManager.ADD_ITEM);
00052                         manager_.setSelectedItem(simItem_, true);
00053                 }
00054                 if(simItem_.isSimulating()){
00055                         action_.setEnabled(false);
00056                         simItem_.stopSimulation();
00057                 }else {
00058                         action_.setEnabled(false);
00059                         if(!simItem_.startSimulation(true))
00060                                 action_.setEnabled(true);
00061                 }
00062         }
00063 
00064         public void selectionChanged(IAction action, ISelection selection) {}
00065         public void dispose() {
00066         }
00067         public void init(IWorkbenchWindow window) {}
00068         
00069         public void setActionImage(boolean start){
00070                 if(action_==null)
00071                         action_ = getStartSimulationAction();
00072                 if(start){
00073                         action_.setToolTipText(MessageBundle.get("GrxOpenHRPView.text.stop")); //$NON-NLS-1$
00074                         action_.setText(MessageBundle.get("GrxOpenHRPView.menu.stop")); //$NON-NLS-1$
00075                         action_.setImageDescriptor(Activator.getDefault().getDescriptor("sim_stop.png")); //$NON-NLS-1$
00076                 }else{  
00077                         action_.setToolTipText(MessageBundle.get("GrxOpenHRPView.text.start")); //$NON-NLS-1$
00078                         action_.setText(MessageBundle.get("GrxOpenHRPView.menu.start")); //$NON-NLS-1$
00079                         action_.setImageDescriptor(Activator.getDefault().getDescriptor("sim_start.png")); //$NON-NLS-1$
00080                 }
00081         }
00082         
00083         private IAction getStartSimulationAction()
00084         {
00085                 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
00086                 for(IWorkbenchWindow w : windows){
00087                         if(!(w instanceof ApplicationWindow))
00088                                 continue;
00089                         ApplicationWindow window = (ApplicationWindow)w;
00090                         ICoolBarManager coolbar = window.getCoolBarManager2();
00091                         if(coolbar == null)
00092                                 continue;
00093                         IContributionItem setitem = coolbar.find("com.generalrobotix.ui.actionSet");
00094                         if(setitem != null && setitem instanceof ToolBarContributionItem)
00095                         {
00096                                 IToolBarManager toolbar = ((ToolBarContributionItem)setitem).getToolBarManager();
00097                                 if(toolbar == null)
00098                                         continue;
00099                                 IContributionItem actitem = toolbar.find("com.generalrobotix.ui.actions.StartSimulate");
00100                                 if(actitem != null && actitem instanceof ActionContributionItem)
00101                                         return ((ActionContributionItem)actitem).getAction();
00102                         }
00103                 }
00104                 return null;
00105         }
00106 
00107         public void registerItemChange(GrxBaseItem item, int event) {
00108                 if(item instanceof GrxSimulationItem){
00109                 GrxSimulationItem simItem = (GrxSimulationItem) item;
00110                 switch(event){
00111                 case GrxPluginManager.SELECTED_ITEM:
00112                         if(simItem_!=simItem){
00113                                 simItem_ = simItem;
00114                                 simItem_.addObserver(this);
00115                         }
00116                         break;
00117                 case GrxPluginManager.REMOVE_ITEM:
00118                 case GrxPluginManager.NOTSELECTED_ITEM:
00119                         if(simItem_==simItem){
00120                                 simItem_.deleteObserver(this);
00121                                 simItem_ = null;
00122                         }
00123                         break;
00124                 default:
00125                         break;
00126                 }
00127         }
00128                 
00129         }
00130 
00131         public void update(GrxBasePlugin plugin, Object... arg) {
00132                 if(simItem_==plugin){
00133                         if(arg[0].equals("StartSimulation")){
00134                                 setActionImage(true);
00135                                 action_.setEnabled(true);
00136                         }else if(arg[0].equals("StopSimulation")){
00137                                 setActionImage(false);  
00138                                 action_.setEnabled(true);
00139                         }
00140                 }
00141         }
00142 
00143 }


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:57