ControllerPanel.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
17 package com.generalrobotix.ui.view.simulation;
18 
19 import java.io.File;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Vector;
23 
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.ArrayContentProvider;
26 import org.eclipse.jface.viewers.ColumnWeightData;
27 import org.eclipse.jface.viewers.ILabelProviderListener;
28 import org.eclipse.jface.viewers.ITableLabelProvider;
29 import org.eclipse.jface.viewers.TableLayout;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.layout.FillLayout;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.FileDialog;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.TableColumn;
45 import org.eclipse.swt.widgets.Text;
46 
56 
57 @SuppressWarnings("serial") //$NON-NLS-1$
58 public class ControllerPanel extends Composite{
60  private TableViewer viewer_;
61  //private JScrollPane scrollPane_;
62 
63  private Vector<GrxModelItem> vecRobot_;
64 
65  private Button btnRemove_;
66  private Button btnEdit_;
67 
69 
70  private static final String ATTRIBUTE_CONTROLLER = "controller"; //$NON-NLS-1$
71  private static final String ATTRIBUTE_CONTROL_TIME = "controlTime"; //$NON-NLS-1$
72  private static final String ATTRIBUTE_SETUP_DIRECTORY = "setupDirectory"; //$NON-NLS-1$
73  private static final String ATTRIBUTE_SETUP_COMMAND = "setupCommand"; //$NON-NLS-1$
74 
75  private static final int BUTTONS_HEIGHT = 26;
76 
77  private final String[] clmName_ ={
78  MessageBundle.get("panel.controller.table.robot"), //$NON-NLS-1$
79  MessageBundle.get("panel.controller.table.controller"), //$NON-NLS-1$
80  MessageBundle.get("panel.controller.table.controlTime"), //$NON-NLS-1$
81  MessageBundle.get("panel.controller.table.setupDirectory"), //$NON-NLS-1$
82  MessageBundle.get("panel.controller.table.setupCommand") //$NON-NLS-1$
83  };
84  private final String[] attrName_ = {
85  "dummy", //$NON-NLS-1$
86  ATTRIBUTE_CONTROLLER,
87  ATTRIBUTE_CONTROL_TIME,
88  ATTRIBUTE_SETUP_DIRECTORY,
89  ATTRIBUTE_SETUP_COMMAND
90  };
91 
92  public ControllerPanel(Composite parent,int style, GrxPluginManager manager) {
93  super(parent, style);
94 
95  manager_ = manager;
96  vecRobot_ = new Vector<GrxModelItem>();
97  setLayout(new GridLayout(1,false));
98 
99  viewer_ = new TableViewer(this, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
100  viewer_.setContentProvider(new ArrayContentProvider());
101  viewer_.setLabelProvider(new ControllerPanelTableLabelProvider());
102  TableLayout tableLayout = new TableLayout();
103  for(int i=0;i<clmName_.length;i++){
104  new TableColumn(viewer_.getTable(),i).setText(clmName_[i]);
105  tableLayout.addColumnData(new ColumnWeightData(1,true));
106  }
107  viewer_.getTable().setLayout(tableLayout);
108  viewer_.getTable().setHeaderVisible(true);
109 
110  viewer_.getTable().addSelectionListener(new SelectionListener(){
111 
112  public void widgetDefaultSelected(SelectionEvent e) {
113  }
114 
115  public void widgetSelected(SelectionEvent e) {
116  int row = viewer_.getTable().getSelectionIndex();
117  if(row>=0 && row<vecRobot_.size()){
118  editorPanel_.setNode(vecRobot_.get(row));
119  }
120  }
121  });
122  viewer_.getTable().setLinesVisible(true);
123  viewer_.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
124  viewer_.setInput(vecRobot_);
125  Composite pnlBttn = new Composite(this,SWT.NONE);
126  GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
127  gridData.heightHint = BUTTONS_HEIGHT;
128  pnlBttn.setLayoutData(gridData);
129  pnlBttn.setLayout(new FillLayout(SWT.HORIZONTAL));
130 
131  btnRemove_ = new Button(pnlBttn,SWT.PUSH);
132  btnRemove_.setText(MessageBundle.get("panel.detach")); //$NON-NLS-1$
133  btnRemove_.addSelectionListener(new SelectionListener(){
134 
135  public void widgetDefaultSelected(SelectionEvent e) {
136  }
137 
138  public void widgetSelected(SelectionEvent e) {
139  int row = viewer_.getTable().getSelectionIndex();
140  if (row >= 0 && row < vecRobot_.size()) {
141  if (_checkDialog(MessageBundle.get("controller.remove"))) //$NON-NLS-1$
142  {
143  GrxModelItem node = vecRobot_.get(row);
144  try {
145  node.setProperty(ATTRIBUTE_CONTROLLER, ""); //$NON-NLS-1$
146  node.setProperty(ATTRIBUTE_CONTROL_TIME, ""); //$NON-NLS-1$
147  node.setProperty(ATTRIBUTE_SETUP_DIRECTORY, ""); //$NON-NLS-1$
148  node.setProperty(ATTRIBUTE_SETUP_COMMAND, ""); //$NON-NLS-1$
149  } catch (Exception ex) {
150  ex.printStackTrace();
151  }
152  _repaint();
153  }
154  }
155  }
156 
157  });
158 
159  btnEdit_ = new Button(pnlBttn,SWT.PUSH);
160  btnEdit_.setText(MessageBundle.get("panel.edit")); //$NON-NLS-1$
161  btnEdit_.addSelectionListener(new SelectionListener(){
162 
163  public void widgetDefaultSelected(SelectionEvent e) {
164  }
165 
166  public void widgetSelected(SelectionEvent e) {
167  int row = viewer_.getTable().getSelectionIndex();
168  if(row>=0 && row<vecRobot_.size()){
169  _setButtonEnabled(false);
170  editorPanel_.startEditMode(vecRobot_.get(row));
171  }
172  }
173 
174  });
175 
176  String[] names = GrxCorbaUtil.getObjectNameList();
177  if(names == null){
178  names = new String[0];
179  }
180 
181  Composite editorPanelComposite = new Composite(this,SWT.NONE);
182  GridData editorPanelGridData = new GridData(GridData.FILL_HORIZONTAL);
183  editorPanelComposite.setLayoutData(editorPanelGridData);
184  editorPanelComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
185 
186  editorPanel_ = new ControllerEditorPanel(editorPanelComposite,SWT.NONE,names);
187 
188  updateTableFont();
189  }
190 
191 
192 
193  private boolean _checkDialog(String msg) {
194  boolean overwrite = MessageDialog.openQuestion(getShell(), MessageBundle.get("dialog.overwrite"), msg); //$NON-NLS-1$
195  return overwrite;
196  }
197 
198  private void _setButtonEnabled(boolean flag) {
199  btnRemove_.setEnabled(flag);
200  btnEdit_.setEnabled(flag);
201  viewer_.getTable().setEnabled(flag);
202  _repaint();
203  }
204 
205  private void _repaint() {
206  viewer_.setInput(vecRobot_);
207  viewer_.getTable().update();
208  //viewer_.columnMarginChanged(new ChangeEvent(viewer_) );
209  //scrollPane_.repaint();
210  }
211 
212  public void setEnabled(boolean flag) {
213  super.setEnabled(flag);
214  editorPanel_.doCancel();
215  _setButtonEnabled(flag);
216  }
217 
218  public double getMaxControllTime() {
219  double maxTime = 0;
220  boolean flag = false;
221  Iterator it = manager_.getItemMap(GrxModelItem.class).values().iterator();
222  while (it.hasNext()) {
223  GrxBaseItem node = (GrxBaseItem)it.next();
224  if (node instanceof GrxModelItem) {
225  String controllName = node.getProperty(ATTRIBUTE_CONTROLLER);
226  if (controllName != null) {
227  double t = node.getDbl(ATTRIBUTE_CONTROL_TIME, 0.001);
228  if (maxTime < t) {
229  maxTime = t;
230  flag = true;
231  }
232  }
233  }
234 
235  }
236 
237  if (flag)
238  return maxTime;
239  return Double.MAX_VALUE;
240  }
241 
242 
243  //--------------------------------------------------------------------
244  private class ControllerEditorPanel extends Composite {
245  //private static final int MODE_ADD = 0 ;
246  private static final int MODE_EDIT = 1 ;
247 
248  private static final int COMBO_WIDTH = 200;
249  private static final int BUTTON_WIDTH = 50;
250 
251  private int mode_;
253  private Combo boxController_;
255  private Text tfSetupDirectory_;
256  private Text tfSetupCommand_;
257  private Button btnOk_,btnCancel_;
259 
260  public ControllerEditorPanel(Composite parent,int style,String[] initialNames) {
261  super(parent,style);
262  setLayout(new GridLayout(5,true));
263 
264  Label lbl = new Label(this,SWT.SHADOW_NONE);
265  lbl.setText(MessageBundle.get("panel.controller.controller")); //$NON-NLS-1$
266  lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
267 
268  boxController_ = new Combo(this,SWT.DROP_DOWN);
269  boxController_.setItems(initialNames);
270  boxController_.select(0);
271  GridData gridData = new GridData();
272  gridData.widthHint = COMBO_WIDTH;
273  gridData.horizontalSpan = 2;
274  boxController_.setLayoutData(gridData);
275  boxController_.setItems(new String[0]);
276 
277  // Control Time
278  lbl = new Label(this,SWT.SHADOW_NONE);
279  lbl.setText(MessageBundle.get("panel.controller.controlTime")); //$NON-NLS-1$
280  lbl.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER|GridData.HORIZONTAL_ALIGN_END));
281 
282  spinControlTime_ = new SEDoubleTextWithSpinForSWT(this,SWT.NONE,0,10,0.001);
283  gridData = new GridData(GridData.FILL_HORIZONTAL);
284  spinControlTime_.setLayoutData(gridData);
285 
286  // Setup Command Working Directory
287  lbl = new Label(this,SWT.SHADOW_NONE);
288  lbl.setText(MessageBundle.get("panel.controller.setupDirectory")); //$NON-NLS-1$
289  lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
290 
291  tfSetupDirectory_ = new Text(this,SWT.DROP_DOWN);
292  tfSetupDirectory_.setText(GrxXmlUtil.expandEnvVal("$(PROJECT_DIR)")); //$NON-NLS-1$
293  gridData = new GridData(GridData.FILL_HORIZONTAL);
294  gridData.horizontalSpan = 2;
295  tfSetupDirectory_.setLayoutData(gridData);
296 
297  Button btnCmdRef = new Button(this, SWT.PUSH);
298  GridData btnGridData = new GridData(GridData.VERTICAL_ALIGN_END);
299  btnGridData.verticalSpan = 2;
300  btnCmdRef.setLayoutData(btnGridData);
301  btnCmdRef.setText(MessageBundle.get("panel.controller.selectFile")); //$NON-NLS-1$
302 
303  Button btnCreate = new Button(this, SWT.PUSH);
304  // btnCreate.setLayoutData(btnGridData);
305  btnCreate.setText(MessageBundle.get("panel.controller.newFile")); //$NON-NLS-1$
306  btnCreate.addSelectionListener(new SelectionListener(){
307  public void widgetDefaultSelected(SelectionEvent e) {
308  }
309  public void widgetSelected(SelectionEvent e) {
310  controllerBridge_ = new ControllerBridgePanel(getShell());
311  int row = viewer_.getTable().getSelectionIndex();
312  GrxModelItem model = vecRobot_.get(row);
313  controllerBridge_.setRobot(model);
314  String controllerName = boxController_.getText();
315  if(controllerName==""){
316  MessageDialog.openError(getShell(), MessageBundle.get("dialog.error.title"),
317  MessageBundle.get("panel.controller.setControllerName"));
318  return;
319  }
320  controllerBridge_.setControllerName(controllerName);
321  controllerBridge_.setProjectControllerName(controllerName);
322  controllerBridge_.setControlTime(spinControlTime_.getValueDouble());
323  controllerBridge_.open();
324  }
325  });
326 
327  // Setup Command
328  lbl = new Label(this,SWT.SHADOW_NONE);
329  lbl.setText(MessageBundle.get("panel.controller.setupCommand")); //$NON-NLS-1$
330  lbl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
331 
332  tfSetupCommand_ = new Text(this, SWT.DROP_DOWN);
333  tfSetupCommand_.setLayoutData(gridData);
334 
335  btnCmdRef.addSelectionListener(new SelectionListener(){
336  public void widgetDefaultSelected(SelectionEvent e) {
337  }
338 
339  public void widgetSelected(SelectionEvent e) {
340  FileDialog fdlg = new FileDialog( GrxUIPerspectiveFactory.getCurrentShell(), SWT.OPEN );
341  fdlg.setFilterPath(tfSetupDirectory_.getText());
342  String fPath = fdlg.open();
343  if( fPath != null ) {
344  File selFile = new File(fPath);
345  tfSetupDirectory_.setText(selFile.getParent());
346  tfSetupCommand_.setText(selFile.getName());
347  }
348  }
349  });
350 
351  Button btnEdit_ = new Button(this, SWT.PUSH);
352  btnEdit_.setText(MessageBundle.get("panel.controller.openFile"));
353  btnEdit_.addSelectionListener(new SelectionListener(){
354  public void widgetDefaultSelected(SelectionEvent e) {
355  }
356  public void widgetSelected(SelectionEvent e) {
357  controllerBridge_ = new ControllerBridgePanel(getShell());
358  if(!controllerBridge_.load(tfSetupDirectory_.getText()+File.separator+tfSetupCommand_.getText())){
359  MessageDialog.openError(getShell(), MessageBundle.get("dialog.error.title"),
360  MessageBundle.get("panel.controller.cantOpenFile"));
361  return;
362  }
363  int row = viewer_.getTable().getSelectionIndex();
364  GrxModelItem model = vecRobot_.get(row);
365  controllerBridge_.setRobot(model);
366  String controllerName = boxController_.getText();
367  if(controllerName==""){
368  MessageDialog.openError(getShell(), MessageBundle.get("dialog.error.title"),
369  MessageBundle.get("panel.controller.setControllerName"));
370  return;
371  }
372  if(!controllerBridge_.setProjectControllerName(controllerName)){
373  if(MessageDialog.openQuestion(getShell(), MessageBundle.get("dialog.error.title"),
374  MessageBundle.get("panel.controller.invalidControllerName")))
375  controllerBridge_.setControllerName(controllerName);
376  }
377  if(!controllerBridge_.checkNameServer()){
378  if(MessageDialog.openQuestion(getShell(), MessageBundle.get("dialog.error.title"),
379  MessageBundle.get("panel.controller.invalidNameServer")))
380  controllerBridge_.setNameServer();
381  }
382  controllerBridge_.setControlTime(spinControlTime_.getValueDouble());
383  controllerBridge_.open();
384 
385  }
386  });
387 
388  btnOk_ = new Button(this,SWT.PUSH);
389  btnOk_.setText(MessageBundle.get("dialog.okButton")); //$NON-NLS-1$
390  btnOk_.addSelectionListener(new SelectionListener(){
391 
392  public void widgetDefaultSelected(SelectionEvent e) {
393  }
394 
395  public void widgetSelected(SelectionEvent e) {
396  switch (mode_) {
397  case MODE_EDIT:
398  _setAttribute(node_);
399  break;
400  }
401  setEnabled(false);
402  viewer_.refresh();
403  }
404 
405  private boolean _setAttribute(GrxBaseItem node) {
406  try {
407  node.setProperty(
408  ATTRIBUTE_CONTROLLER,
409  boxController_.getText()
410  );
411  node.setProperty(
412  ATTRIBUTE_CONTROL_TIME,
413  spinControlTime_.getValue().toString()
414  );
415  node.setProperty(
416  ATTRIBUTE_SETUP_DIRECTORY,
417  tfSetupDirectory_.getText()
418  );
419  node.setProperty(
420  ATTRIBUTE_SETUP_COMMAND,
421  tfSetupCommand_.getText()
422  );
423  } catch (Exception ex) {
424  MessageDialog.openWarning(getShell(),"", MessageBundle.get("message.attributeerror")); //$NON-NLS-1$ //$NON-NLS-2$
425  return false;
426  //ex.printStackTrace();
427  }
428  return true;
429  }
430 
431  });
432  gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
433  gridData.widthHint = BUTTON_WIDTH;
434  btnOk_.setLayoutData(gridData);
435 
436  btnCancel_ = new Button(this,SWT.PUSH);
437  btnCancel_.setText(MessageBundle.get("dialog.cancelButton")); //$NON-NLS-1$
438  btnCancel_.addSelectionListener(new SelectionListener() {
439  public void widgetDefaultSelected(SelectionEvent e) {
440  }
441 
442  public void widgetSelected(SelectionEvent e) {
443  doCancel();
444  }
445  }
446  );
447  gridData = new GridData();
448  //gridData.widthHint = BUTTON_WIDTH;
449  btnCancel_.setLayoutData(gridData);
450  this.layout();
451  }
452 
453  public void startEditMode(GrxModelItem node) {
454  mode_ = MODE_EDIT;
455  String[] names = GrxCorbaUtil.getObjectNameList();
456  if(names == null){
457  names = new String[0];
458  }
459  boxController_.removeAll();
460  boxController_.setItems(names);
461  setNode(node);
462  setEnabled(true);
463  }
464 
465  public void doCancel() {
466  setEnabled(false);
467  }
468 
469  public void setNode(GrxModelItem node) {
470  try {
471  boolean is_set_boxctrl = false;
472  String attr = node.getProperty(ATTRIBUTE_CONTROLLER, "");
473  for (int i = 0; i < boxController_.getItemCount(); i ++) {
474  if (attr.equals(boxController_.getItem(i).toString())) {
475  is_set_boxctrl = true;
476  boxController_.select(i);
477  break;
478  }
479  }
480  if(is_set_boxctrl == false)
481  boxController_.setText(attr);
482 
483  spinControlTime_.setValue(node.getProperty(ATTRIBUTE_CONTROL_TIME, "0.001")); //$NON-NLS-1$
484  tfSetupDirectory_.setText(node.getStr(ATTRIBUTE_SETUP_DIRECTORY ,GrxXmlUtil.expandEnvVal("$(PROJECT_DIR)"))); //$NON-NLS-1$
485 
486  attr = node.getStr(ATTRIBUTE_SETUP_COMMAND, ""); //$NON-NLS-1$
487  tfSetupCommand_.setText(attr);
488  } catch (Exception ex) {
489  ex.printStackTrace();
490  }
491  node_ = node;
492  }
493 
494  public void setEnabled(boolean flag) {
495  super.setEnabled(flag);
496  Control[] cmps = this.getChildren();
497  for(int i = 0; i < cmps.length; i ++) {
498  cmps[i].setEnabled(flag);
499  }
500  _setButtonEnabled(!flag);
501  }
502 
503  }
504 
505  public void childAdded(GrxModelItem node) {
506  int i;
507  for (i = 0; i < vecRobot_.size(); i ++) {
508  vecRobot_.get(i);
509  if ((vecRobot_.get(i)).getName().compareTo(node.getName()) > 0)
510  break;
511  // if (viewable.compareTo(node) > 0)
512  // break;
513  }
514  vecRobot_.add(i, node);
515  _repaint();
516  return;
517  }
518 
519  private class ControllerPanelTableLabelProvider implements ITableLabelProvider{
520 
521  public Image getColumnImage(Object element, int columnIndex) {
522  return null;
523  }
524 
525  public String getColumnText(Object element, int columnIndex) {
526  GrxModelItem node = (GrxModelItem)element;
527  String str = null;
528  if (columnIndex ==0){
529  return node.getName();
530  }
531  try{
532  str = node.getStr(attrName_[columnIndex]);
533  }catch(Exception ex){
534  ex.printStackTrace();
535  return ""; //$NON-NLS-1$
536  }
537  if (str == null)
538  str = ""; //$NON-NLS-1$
539 
540  return str;
541  }
542 
543  public void addListener(ILabelProviderListener listener) {
544  }
545 
546  public void dispose() {
547  }
548 
549  public boolean isLabelProperty(Object element, String property) {
550  return false;
551  }
552 
553  public void removeListener(ILabelProviderListener listener) {
554  }
555 
556 
557  }
558 
559  public void childRemoved(GrxModelItem node) {
560  if(node instanceof GrxModelItem){
561  vecRobot_.remove(node);
562  _repaint();
563  editorPanel_.doCancel();
564  return;
565  }
566  }
567 
568  public void updateRobots(List<GrxModelItem> list) {
569  editorPanel_.doCancel();
570  vecRobot_ = new Vector<GrxModelItem>();
571  for (int i=0; i<list.size(); i++) {
572  GrxBaseItem item = list.get(i);
573  if (item instanceof GrxModelItem && ((GrxModelItem)item).isRobot())
574  vecRobot_.add(list.get(i));
575  }
576  setEnabled(vecRobot_.size() > 0);
577  viewer_.setInput(vecRobot_);
578  _repaint();
579  }
580 
581  public int getRobotNum() {
582  return vecRobot_.size();
583  }
584 
585  public void updateTableFont(){
586  viewer_.getTable().setFont(Activator.getDefault().getFont("preference_table"));
587  }
588 }
static final String get(String key)
ControllerEditorPanel(Composite parent, int style, String[] initialNames)
#define null
our own NULL pointer
Definition: IceTypes.h:57
item corresponds to a robot model
png_uint_32 i
Definition: png.h:2735
t
png_bytepp row
Definition: png.h:1759
static String expandEnvVal(String str)
Object setProperty(String key, String value)
set property value associated with a keyword
final Double getDbl(String key, Double defaultVal)
get double value associated to key
Map<?,?> getItemMap(Class<?extends GrxBaseItem > cls)
final String getStr(String key)
get value associated to keyword
ControllerPanel(Composite parent, int style, GrxPluginManager manager)
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
org
png_infop png_uint_32 flag
Definition: png.h:2159


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:20