00001
00002
00003
00004
00005
00006
00007
00008
00009
00017 package com.generalrobotix.ui.view.simulation;
00018
00019 import java.io.File;
00020 import java.util.Iterator;
00021 import java.util.List;
00022 import java.util.Vector;
00023
00024 import org.eclipse.jface.dialogs.MessageDialog;
00025 import org.eclipse.jface.viewers.ArrayContentProvider;
00026 import org.eclipse.jface.viewers.ColumnWeightData;
00027 import org.eclipse.jface.viewers.ILabelProviderListener;
00028 import org.eclipse.jface.viewers.ITableLabelProvider;
00029 import org.eclipse.jface.viewers.TableLayout;
00030 import org.eclipse.jface.viewers.TableViewer;
00031 import org.eclipse.swt.SWT;
00032 import org.eclipse.swt.events.SelectionEvent;
00033 import org.eclipse.swt.events.SelectionListener;
00034 import org.eclipse.swt.graphics.Image;
00035 import org.eclipse.swt.layout.FillLayout;
00036 import org.eclipse.swt.layout.GridData;
00037 import org.eclipse.swt.layout.GridLayout;
00038 import org.eclipse.swt.widgets.Button;
00039 import org.eclipse.swt.widgets.Combo;
00040 import org.eclipse.swt.widgets.Composite;
00041 import org.eclipse.swt.widgets.Control;
00042 import org.eclipse.swt.widgets.FileDialog;
00043 import org.eclipse.swt.widgets.Label;
00044 import org.eclipse.swt.widgets.TableColumn;
00045 import org.eclipse.swt.widgets.Text;
00046
00047 import com.generalrobotix.ui.grxui.Activator;
00048 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00049 import com.generalrobotix.ui.GrxBaseItem;
00050 import com.generalrobotix.ui.GrxPluginManager;
00051 import com.generalrobotix.ui.item.GrxModelItem;
00052 import com.generalrobotix.ui.util.GrxCorbaUtil;
00053 import com.generalrobotix.ui.util.GrxXmlUtil;
00054 import com.generalrobotix.ui.util.MessageBundle;
00055 import com.generalrobotix.ui.view.graph.SEDoubleTextWithSpinForSWT;
00056
00057 @SuppressWarnings("serial")
00058 public class ControllerPanel extends Composite{
00059 private GrxPluginManager manager_;
00060 private TableViewer viewer_;
00061
00062
00063 private Vector<GrxModelItem> vecRobot_;
00064
00065 private Button btnRemove_;
00066 private Button btnEdit_;
00067
00068 private ControllerEditorPanel editorPanel_;
00069
00070 private static final String ATTRIBUTE_CONTROLLER = "controller";
00071 private static final String ATTRIBUTE_CONTROL_TIME = "controlTime";
00072 private static final String ATTRIBUTE_SETUP_DIRECTORY = "setupDirectory";
00073 private static final String ATTRIBUTE_SETUP_COMMAND = "setupCommand";
00074
00075 private static final int BUTTONS_HEIGHT = 26;
00076
00077 private final String[] clmName_ ={
00078 MessageBundle.get("panel.controller.table.robot"),
00079 MessageBundle.get("panel.controller.table.controller"),
00080 MessageBundle.get("panel.controller.table.controlTime"),
00081 MessageBundle.get("panel.controller.table.setupDirectory"),
00082 MessageBundle.get("panel.controller.table.setupCommand")
00083 };
00084 private final String[] attrName_ = {
00085 "dummy",
00086 ATTRIBUTE_CONTROLLER,
00087 ATTRIBUTE_CONTROL_TIME,
00088 ATTRIBUTE_SETUP_DIRECTORY,
00089 ATTRIBUTE_SETUP_COMMAND
00090 };
00091
00092 public ControllerPanel(Composite parent,int style, GrxPluginManager manager) {
00093 super(parent, style);
00094
00095 manager_ = manager;
00096 vecRobot_ = new Vector<GrxModelItem>();
00097 setLayout(new GridLayout(1,false));
00098
00099 viewer_ = new TableViewer(this, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
00100 viewer_.setContentProvider(new ArrayContentProvider());
00101 viewer_.setLabelProvider(new ControllerPanelTableLabelProvider());
00102 TableLayout tableLayout = new TableLayout();
00103 for(int i=0;i<clmName_.length;i++){
00104 new TableColumn(viewer_.getTable(),i).setText(clmName_[i]);
00105 tableLayout.addColumnData(new ColumnWeightData(1,true));
00106 }
00107 viewer_.getTable().setLayout(tableLayout);
00108 viewer_.getTable().setHeaderVisible(true);
00109
00110 viewer_.getTable().addSelectionListener(new SelectionListener(){
00111
00112 public void widgetDefaultSelected(SelectionEvent e) {
00113 }
00114
00115 public void widgetSelected(SelectionEvent e) {
00116 int row = viewer_.getTable().getSelectionIndex();
00117 if(row>=0 && row<vecRobot_.size()){
00118 editorPanel_.setNode(vecRobot_.get(row));
00119 }
00120 }
00121 });
00122 viewer_.getTable().setLinesVisible(true);
00123 viewer_.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
00124 viewer_.setInput(vecRobot_);
00125 Composite pnlBttn = new Composite(this,SWT.NONE);
00126 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
00127 gridData.heightHint = BUTTONS_HEIGHT;
00128 pnlBttn.setLayoutData(gridData);
00129 pnlBttn.setLayout(new FillLayout(SWT.HORIZONTAL));
00130
00131 btnRemove_ = new Button(pnlBttn,SWT.PUSH);
00132 btnRemove_.setText(MessageBundle.get("panel.detach"));
00133 btnRemove_.addSelectionListener(new SelectionListener(){
00134
00135 public void widgetDefaultSelected(SelectionEvent e) {
00136 }
00137
00138 public void widgetSelected(SelectionEvent e) {
00139 int row = viewer_.getTable().getSelectionIndex();
00140 if (row >= 0 && row < vecRobot_.size()) {
00141 if (_checkDialog(MessageBundle.get("controller.remove")))
00142 {
00143 GrxModelItem node = vecRobot_.get(row);
00144 try {
00145 node.setProperty(ATTRIBUTE_CONTROLLER, "");
00146 node.setProperty(ATTRIBUTE_CONTROL_TIME, "");
00147 node.setProperty(ATTRIBUTE_SETUP_DIRECTORY, "");
00148 node.setProperty(ATTRIBUTE_SETUP_COMMAND, "");
00149 } catch (Exception ex) {
00150 ex.printStackTrace();
00151 }
00152 _repaint();
00153 }
00154 }
00155 }
00156
00157 });
00158
00159 btnEdit_ = new Button(pnlBttn,SWT.PUSH);
00160 btnEdit_.setText(MessageBundle.get("panel.edit"));
00161 btnEdit_.addSelectionListener(new SelectionListener(){
00162
00163 public void widgetDefaultSelected(SelectionEvent e) {
00164 }
00165
00166 public void widgetSelected(SelectionEvent e) {
00167 int row = viewer_.getTable().getSelectionIndex();
00168 if(row>=0 && row<vecRobot_.size()){
00169 _setButtonEnabled(false);
00170 editorPanel_.startEditMode(vecRobot_.get(row));
00171 }
00172 }
00173
00174 });
00175
00176 String[] names = GrxCorbaUtil.getObjectNameList();
00177 if(names == null){
00178 names = new String[0];
00179 }
00180
00181 Composite editorPanelComposite = new Composite(this,SWT.NONE);
00182 GridData editorPanelGridData = new GridData(GridData.FILL_HORIZONTAL);
00183 editorPanelComposite.setLayoutData(editorPanelGridData);
00184 editorPanelComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
00185
00186 editorPanel_ = new ControllerEditorPanel(editorPanelComposite,SWT.NONE,names);
00187
00188 updateTableFont();
00189 }
00190
00191
00192
00193 private boolean _checkDialog(String msg) {
00194 boolean overwrite = MessageDialog.openQuestion(getShell(), MessageBundle.get("dialog.overwrite"), msg);
00195 return overwrite;
00196 }
00197
00198 private void _setButtonEnabled(boolean flag) {
00199 btnRemove_.setEnabled(flag);
00200 btnEdit_.setEnabled(flag);
00201 viewer_.getTable().setEnabled(flag);
00202 _repaint();
00203 }
00204
00205 private void _repaint() {
00206 viewer_.setInput(vecRobot_);
00207 viewer_.getTable().update();
00208
00209
00210 }
00211
00212 public void setEnabled(boolean flag) {
00213 super.setEnabled(flag);
00214 editorPanel_.doCancel();
00215 _setButtonEnabled(flag);
00216 }
00217
00218 public double getMaxControllTime() {
00219 double maxTime = 0;
00220 boolean flag = false;
00221 Iterator it = manager_.getItemMap(GrxModelItem.class).values().iterator();
00222 while (it.hasNext()) {
00223 GrxBaseItem node = (GrxBaseItem)it.next();
00224 if (node instanceof GrxModelItem) {
00225 String controllName = node.getProperty(ATTRIBUTE_CONTROLLER);
00226 if (controllName != null) {
00227 double t = node.getDbl(ATTRIBUTE_CONTROL_TIME, 0.001);
00228 if (maxTime < t) {
00229 maxTime = t;
00230 flag = true;
00231 }
00232 }
00233 }
00234
00235 }
00236
00237 if (flag)
00238 return maxTime;
00239 return Double.MAX_VALUE;
00240 }
00241
00242
00243
00244 private class ControllerEditorPanel extends Composite {
00245
00246 private static final int MODE_EDIT = 1 ;
00247
00248 private static final int COMBO_WIDTH = 200;
00249 private static final int BUTTON_WIDTH = 50;
00250
00251 private int mode_;
00252 private GrxBaseItem node_;
00253 private Combo boxController_;
00254 private SEDoubleTextWithSpinForSWT spinControlTime_;
00255 private Text tfSetupDirectory_;
00256 private Text tfSetupCommand_;
00257 private Button btnOk_,btnCancel_;
00258 private ControllerBridgePanel controllerBridge_;
00259
00260 public ControllerEditorPanel(Composite parent,int style,String[] initialNames) {
00261 super(parent,style);
00262 setLayout(new GridLayout(5,true));
00263
00264 Label lbl = new Label(this,SWT.SHADOW_NONE);
00265 lbl.setText(MessageBundle.get("panel.controller.controller"));
00266 lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
00267
00268 boxController_ = new Combo(this,SWT.DROP_DOWN);
00269 boxController_.setItems(initialNames);
00270 boxController_.select(0);
00271 GridData gridData = new GridData();
00272 gridData.widthHint = COMBO_WIDTH;
00273 gridData.horizontalSpan = 2;
00274 boxController_.setLayoutData(gridData);
00275 boxController_.setItems(new String[0]);
00276
00277
00278 lbl = new Label(this,SWT.SHADOW_NONE);
00279 lbl.setText(MessageBundle.get("panel.controller.controlTime"));
00280 lbl.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER|GridData.HORIZONTAL_ALIGN_END));
00281
00282 spinControlTime_ = new SEDoubleTextWithSpinForSWT(this,SWT.NONE,0,10,0.001);
00283 gridData = new GridData(GridData.FILL_HORIZONTAL);
00284 spinControlTime_.setLayoutData(gridData);
00285
00286
00287 lbl = new Label(this,SWT.SHADOW_NONE);
00288 lbl.setText(MessageBundle.get("panel.controller.setupDirectory"));
00289 lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
00290
00291 tfSetupDirectory_ = new Text(this,SWT.DROP_DOWN);
00292 tfSetupDirectory_.setText(GrxXmlUtil.expandEnvVal("$(PROJECT_DIR)"));
00293 gridData = new GridData(GridData.FILL_HORIZONTAL);
00294 gridData.horizontalSpan = 2;
00295 tfSetupDirectory_.setLayoutData(gridData);
00296
00297 Button btnCmdRef = new Button(this, SWT.PUSH);
00298 GridData btnGridData = new GridData(GridData.VERTICAL_ALIGN_END);
00299 btnGridData.verticalSpan = 2;
00300 btnCmdRef.setLayoutData(btnGridData);
00301 btnCmdRef.setText(MessageBundle.get("panel.controller.selectFile"));
00302
00303 Button btnCreate = new Button(this, SWT.PUSH);
00304
00305 btnCreate.setText(MessageBundle.get("panel.controller.newFile"));
00306 btnCreate.addSelectionListener(new SelectionListener(){
00307 public void widgetDefaultSelected(SelectionEvent e) {
00308 }
00309 public void widgetSelected(SelectionEvent e) {
00310 controllerBridge_ = new ControllerBridgePanel(getShell());
00311 int row = viewer_.getTable().getSelectionIndex();
00312 GrxModelItem model = vecRobot_.get(row);
00313 controllerBridge_.setRobot(model);
00314 String controllerName = boxController_.getText();
00315 if(controllerName==""){
00316 MessageDialog.openError(getShell(), MessageBundle.get("dialog.error.title"),
00317 MessageBundle.get("panel.controller.setControllerName"));
00318 return;
00319 }
00320 controllerBridge_.setControllerName(controllerName);
00321 controllerBridge_.setProjectControllerName(controllerName);
00322 controllerBridge_.setControlTime(spinControlTime_.getValueDouble());
00323 controllerBridge_.open();
00324 }
00325 });
00326
00327
00328 lbl = new Label(this,SWT.SHADOW_NONE);
00329 lbl.setText(MessageBundle.get("panel.controller.setupCommand"));
00330 lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
00331
00332 tfSetupCommand_ = new Text(this, SWT.DROP_DOWN);
00333 tfSetupCommand_.setLayoutData(gridData);
00334
00335 btnCmdRef.addSelectionListener(new SelectionListener(){
00336 public void widgetDefaultSelected(SelectionEvent e) {
00337 }
00338
00339 public void widgetSelected(SelectionEvent e) {
00340 FileDialog fdlg = new FileDialog( GrxUIPerspectiveFactory.getCurrentShell(), SWT.OPEN );
00341 fdlg.setFilterPath(tfSetupDirectory_.getText());
00342 String fPath = fdlg.open();
00343 if( fPath != null ) {
00344 File selFile = new File(fPath);
00345 tfSetupDirectory_.setText(selFile.getParent());
00346 tfSetupCommand_.setText(selFile.getName());
00347 }
00348 }
00349 });
00350
00351 Button btnEdit_ = new Button(this, SWT.PUSH);
00352 btnEdit_.setText(MessageBundle.get("panel.controller.openFile"));
00353 btnEdit_.addSelectionListener(new SelectionListener(){
00354 public void widgetDefaultSelected(SelectionEvent e) {
00355 }
00356 public void widgetSelected(SelectionEvent e) {
00357 controllerBridge_ = new ControllerBridgePanel(getShell());
00358 if(!controllerBridge_.load(tfSetupDirectory_.getText()+File.separator+tfSetupCommand_.getText())){
00359 MessageDialog.openError(getShell(), MessageBundle.get("dialog.error.title"),
00360 MessageBundle.get("panel.controller.cantOpenFile"));
00361 return;
00362 }
00363 int row = viewer_.getTable().getSelectionIndex();
00364 GrxModelItem model = vecRobot_.get(row);
00365 controllerBridge_.setRobot(model);
00366 String controllerName = boxController_.getText();
00367 if(controllerName==""){
00368 MessageDialog.openError(getShell(), MessageBundle.get("dialog.error.title"),
00369 MessageBundle.get("panel.controller.setControllerName"));
00370 return;
00371 }
00372 if(!controllerBridge_.setProjectControllerName(controllerName)){
00373 if(MessageDialog.openQuestion(getShell(), MessageBundle.get("dialog.error.title"),
00374 MessageBundle.get("panel.controller.invalidControllerName")))
00375 controllerBridge_.setControllerName(controllerName);
00376 }
00377 if(!controllerBridge_.checkNameServer()){
00378 if(MessageDialog.openQuestion(getShell(), MessageBundle.get("dialog.error.title"),
00379 MessageBundle.get("panel.controller.invalidNameServer")))
00380 controllerBridge_.setNameServer();
00381 }
00382 controllerBridge_.setControlTime(spinControlTime_.getValueDouble());
00383 controllerBridge_.open();
00384
00385 }
00386 });
00387
00388 btnOk_ = new Button(this,SWT.PUSH);
00389 btnOk_.setText(MessageBundle.get("dialog.okButton"));
00390 btnOk_.addSelectionListener(new SelectionListener(){
00391
00392 public void widgetDefaultSelected(SelectionEvent e) {
00393 }
00394
00395 public void widgetSelected(SelectionEvent e) {
00396 switch (mode_) {
00397 case MODE_EDIT:
00398 _setAttribute(node_);
00399 break;
00400 }
00401 setEnabled(false);
00402 viewer_.refresh();
00403 }
00404
00405 private boolean _setAttribute(GrxBaseItem node) {
00406 try {
00407 node.setProperty(
00408 ATTRIBUTE_CONTROLLER,
00409 boxController_.getText()
00410 );
00411 node.setProperty(
00412 ATTRIBUTE_CONTROL_TIME,
00413 spinControlTime_.getValue().toString()
00414 );
00415 node.setProperty(
00416 ATTRIBUTE_SETUP_DIRECTORY,
00417 tfSetupDirectory_.getText()
00418 );
00419 node.setProperty(
00420 ATTRIBUTE_SETUP_COMMAND,
00421 tfSetupCommand_.getText()
00422 );
00423 } catch (Exception ex) {
00424 MessageDialog.openWarning(getShell(),"", MessageBundle.get("message.attributeerror"));
00425 return false;
00426
00427 }
00428 return true;
00429 }
00430
00431 });
00432 gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
00433 gridData.widthHint = BUTTON_WIDTH;
00434 btnOk_.setLayoutData(gridData);
00435
00436 btnCancel_ = new Button(this,SWT.PUSH);
00437 btnCancel_.setText(MessageBundle.get("dialog.cancelButton"));
00438 btnCancel_.addSelectionListener(new SelectionListener() {
00439 public void widgetDefaultSelected(SelectionEvent e) {
00440 }
00441
00442 public void widgetSelected(SelectionEvent e) {
00443 doCancel();
00444 }
00445 }
00446 );
00447 gridData = new GridData();
00448
00449 btnCancel_.setLayoutData(gridData);
00450 this.layout();
00451 }
00452
00453 public void startEditMode(GrxModelItem node) {
00454 mode_ = MODE_EDIT;
00455 String[] names = GrxCorbaUtil.getObjectNameList();
00456 if(names == null){
00457 names = new String[0];
00458 }
00459 boxController_.removeAll();
00460 boxController_.setItems(names);
00461 setNode(node);
00462 setEnabled(true);
00463 }
00464
00465 public void doCancel() {
00466 setEnabled(false);
00467 }
00468
00469 public void setNode(GrxModelItem node) {
00470 try {
00471 boolean is_set_boxctrl = false;
00472 String attr = node.getProperty(ATTRIBUTE_CONTROLLER, "");
00473 for (int i = 0; i < boxController_.getItemCount(); i ++) {
00474 if (attr.equals(boxController_.getItem(i).toString())) {
00475 is_set_boxctrl = true;
00476 boxController_.select(i);
00477 break;
00478 }
00479 }
00480 if(is_set_boxctrl == false)
00481 boxController_.setText(attr);
00482
00483 spinControlTime_.setValue(node.getProperty(ATTRIBUTE_CONTROL_TIME, "0.001"));
00484 tfSetupDirectory_.setText(node.getStr(ATTRIBUTE_SETUP_DIRECTORY ,GrxXmlUtil.expandEnvVal("$(PROJECT_DIR)")));
00485
00486 attr = node.getStr(ATTRIBUTE_SETUP_COMMAND, "");
00487 tfSetupCommand_.setText(attr);
00488 } catch (Exception ex) {
00489 ex.printStackTrace();
00490 }
00491 node_ = node;
00492 }
00493
00494 public void setEnabled(boolean flag) {
00495 super.setEnabled(flag);
00496 Control[] cmps = this.getChildren();
00497 for(int i = 0; i < cmps.length; i ++) {
00498 cmps[i].setEnabled(flag);
00499 }
00500 _setButtonEnabled(!flag);
00501 }
00502
00503 }
00504
00505 public void childAdded(GrxModelItem node) {
00506 int i;
00507 for (i = 0; i < vecRobot_.size(); i ++) {
00508 vecRobot_.get(i);
00509 if ((vecRobot_.get(i)).getName().compareTo(node.getName()) > 0)
00510 break;
00511
00512
00513 }
00514 vecRobot_.add(i, node);
00515 _repaint();
00516 return;
00517 }
00518
00519 private class ControllerPanelTableLabelProvider implements ITableLabelProvider{
00520
00521 public Image getColumnImage(Object element, int columnIndex) {
00522 return null;
00523 }
00524
00525 public String getColumnText(Object element, int columnIndex) {
00526 GrxModelItem node = (GrxModelItem)element;
00527 String str = null;
00528 if (columnIndex ==0){
00529 return node.getName();
00530 }
00531 try{
00532 str = node.getStr(attrName_[columnIndex]);
00533 }catch(Exception ex){
00534 ex.printStackTrace();
00535 return "";
00536 }
00537 if (str == null)
00538 str = "";
00539
00540 return str;
00541 }
00542
00543 public void addListener(ILabelProviderListener listener) {
00544 }
00545
00546 public void dispose() {
00547 }
00548
00549 public boolean isLabelProperty(Object element, String property) {
00550 return false;
00551 }
00552
00553 public void removeListener(ILabelProviderListener listener) {
00554 }
00555
00556
00557 }
00558
00559 public void childRemoved(GrxModelItem node) {
00560 if(node instanceof GrxModelItem){
00561 vecRobot_.remove(node);
00562 _repaint();
00563 editorPanel_.doCancel();
00564 return;
00565 }
00566 }
00567
00568 public void updateRobots(List<GrxModelItem> list) {
00569 editorPanel_.doCancel();
00570 vecRobot_ = new Vector<GrxModelItem>();
00571 for (int i=0; i<list.size(); i++) {
00572 GrxBaseItem item = list.get(i);
00573 if (item instanceof GrxModelItem && ((GrxModelItem)item).isRobot())
00574 vecRobot_.add(list.get(i));
00575 }
00576 setEnabled(vecRobot_.size() > 0);
00577 viewer_.setInput(vecRobot_);
00578 _repaint();
00579 }
00580
00581 public int getRobotNum() {
00582 return vecRobot_.size();
00583 }
00584
00585 public void updateTableFont(){
00586 viewer_.getTable().setFont(Activator.getDefault().getFont("preference_table"));
00587 }
00588 }