ControllerBridgePanel.java
Go to the documentation of this file.
1 package com.generalrobotix.ui.view.simulation;
2 
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileReader;
8 import java.io.FileWriter;
9 import java.io.IOException;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.Vector;
13 
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.CellEditor;
19 import org.eclipse.jface.viewers.ComboBoxCellEditor;
20 import org.eclipse.jface.viewers.DialogCellEditor;
21 import org.eclipse.jface.viewers.ICellModifier;
22 import org.eclipse.jface.viewers.ILabelProviderListener;
23 import org.eclipse.jface.viewers.ITableLabelProvider;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.TextCellEditor;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.custom.PopupList;
28 import org.eclipse.swt.custom.StyledText;
29 import org.eclipse.swt.dnd.DND;
30 import org.eclipse.swt.dnd.DragSource;
31 import org.eclipse.swt.dnd.DragSourceEvent;
32 import org.eclipse.swt.dnd.DragSourceListener;
33 import org.eclipse.swt.dnd.DropTarget;
34 import org.eclipse.swt.dnd.DropTargetEvent;
35 import org.eclipse.swt.dnd.DropTargetListener;
36 import org.eclipse.swt.dnd.TextTransfer;
37 import org.eclipse.swt.dnd.Transfer;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.events.MouseEvent;
41 import org.eclipse.swt.events.MouseListener;
42 import org.eclipse.swt.events.SelectionEvent;
43 import org.eclipse.swt.events.SelectionListener;
44 import org.eclipse.swt.graphics.Color;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.graphics.Rectangle;
47 import org.eclipse.swt.layout.FillLayout;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.layout.RowLayout;
51 import org.eclipse.swt.widgets.Button;
52 import org.eclipse.swt.widgets.Composite;
53 import org.eclipse.swt.widgets.Control;
54 import org.eclipse.swt.widgets.FileDialog;
55 import org.eclipse.swt.widgets.Item;
56 import org.eclipse.swt.widgets.Label;
57 import org.eclipse.swt.widgets.Shell;
58 import org.eclipse.swt.widgets.Table;
59 import org.eclipse.swt.widgets.TableColumn;
60 import org.eclipse.swt.widgets.Text;
61 
69 
70 public class ControllerBridgePanel extends Dialog{
71  private Vector<String> controllerBridgeFile_ = new Vector<String>();
72  private String fileName_;
73  private String rtcConfFileName_;
74  private String controllerName_;
75  private String projectControllerName_;
76  private String robotName_;
78  private String robotRtcName_;
79  private Vector<Inport> inport_ = new Vector<Inport>();
80  private Vector<Outport> outport_ = new Vector<Outport>();
81  private HashMap<String, String> connection_ = new HashMap<String, String>();
83  private Vector<Port> portTable_ = new Vector<Port>();
84  private boolean useConfigFile_;
85  private String configFileName_; //Relative path//
86  private StringBuffer bridgeConfFile_ = new StringBuffer();
87  private double controlTime_ = 0;
91  public static enum NUMID_CODE {INVALID_TYPE, NOT_USED, USED, ONLY_ONE, NUMBER};
92  public static String[] getNames(){
93  String[] s = new String[values().length];
94  int i=0;
95  for(DataTypeId id : values()){
96  s[i++] = id.name();
97  }
98  return s;
99  }
100  public static boolean isInport(String type){
101  if(DataTypeId.valueOf(type).compareTo(DataTypeId.ABS_ACCELERATION) > 0 )
102  return false;
103  else
104  return true;
105  }
106  public static boolean isOutport(String type){
107  if(DataTypeId.valueOf(type).compareTo(DataTypeId.EXTERNAL_FORCE) == 0 )
108  return false;
109  else
110  return true;
111  }
112  public static NUMID_CODE numId(String type){
113  int ordinal = DataTypeId.valueOf(type).ordinal();
114  if(ordinal <= 3 || (8<=ordinal && ordinal <=11) )
115  return NUMID_CODE.NOT_USED;
116  if(4<=ordinal && ordinal<=7)
117  return NUMID_CODE.USED;
118  if(ordinal==12)
119  return NUMID_CODE.ONLY_ONE;
120  if(13<=ordinal && ordinal<=15)
121  return NUMID_CODE.NUMBER;
122  return NUMID_CODE.INVALID_TYPE;
123  }
124  }
125  private String errorMessage_;
126  private Vector<String> rtcConfFile_ = new Vector<String>();
128  private String moduleName_;
129  private String controllerRtcName_;
130  private String endChar_ = "";
131  private String commentChar_ = "";
132  private int commandLineStart_=0;
133  private int commandLineEnd_=0;
134 
135  private StyledText cbtext_;
136  private StyledText rctext_;
137  private StyledText bctext_;
138  private Text rrnText_;
139  private Text nshText_;
140  private Text nspText_;
141  private TableViewer tableviewer_;
142  private StyledText emText_;
143  private Button saveButton_ ;
144  private Button saveAsButton_ ;
145  private Label fileNameLabel_;
146  private Label rtcConfFileNameLabel_;
148 
149  private int os_;
150  private final int LINUX = 0;
151  private final int WINDOWS = 1;
152 
153  ControllerBridgePanel(Shell shell) {
154  super(shell);
155  if(System.getProperty("os.name").equals("Linux") || System.getProperty("os.name").equals("Mac OS X")){
156  os_ = LINUX;
157  endChar_ = "\\";
158  commentChar_ = "#";
159  }else{
160  endChar_ = "^";
161  os_ = WINDOWS;
162  commentChar_ = "REM";
163  }
164  init();
165  }
166 
167  protected void configureShell(Shell newShell) {
168  super.configureShell(newShell);
169  newShell.setText(MessageBundle.get("panel.Bridge.title"));
170  }
171 
172  protected Control createDialogArea(Composite parent) {
173  Composite composite = (Composite)super.createDialogArea(parent);
174  composite.setLayout(new GridLayout(1,true));
175 
176  Composite panel0 = new Composite(composite,SWT.NONE);
177  panel0.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
178  panel0.setLayout(new GridLayout(3,false));
179  Label label = new Label(panel0, SWT.LEFT );
180  label.setText(MessageBundle.get("panel.Bridge.controllerName")+":");
181  final Text text = new Text(panel0, SWT.BORDER);
182  text.setText(controllerName_);
183  text.addModifyListener(new ModifyListener(){
184  public void modifyText(ModifyEvent e) {
185  String newName = text.getText();
186  if(projectControllerName_.equals(newName))
187  text.setForeground(Activator.getDefault().getColor("black"));
188  else
189  text.setForeground(Activator.getDefault().getColor("red"));
190  controllerName_ = newName;
191  rrnTextUpdate();
192  saveButtonEnabled(false);
193  }
194  });
195 
196  GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
197  gridData.horizontalSpan = 2;
198  text.setLayoutData(gridData);
199  Label label0 = new Label(panel0, SWT.LEFT );
200  label0.setText(MessageBundle.get("panel.Bridge.robotName")+":"+robotName_+" ");
201  Label label1 = new Label(panel0, SWT.LEFT);
202  label1.setText(MessageBundle.get("panel.Bridge.robotRtcName")+":");
203  label1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
204  rrnText_ = new Text(panel0, SWT.BORDER);
205  GridData gridData0 = new GridData();
206  gridData0.horizontalAlignment = GridData.FILL;
207  gridData0.grabExcessHorizontalSpace = true;
208  rrnText_.setLayoutData(gridData0);
209  rrnTextUpdate();
210  rrnText_.addModifyListener(new ModifyListener(){
211  public void modifyText(ModifyEvent e) {
212  if(rrnText_.isFocusControl()){
213  robotRtcName_ = rrnText_.getText();
214  saveButtonEnabled(false);
215  }
216  }
217  });
218 
219  tableviewer_ = new TableViewer(composite, SWT.FULL_SELECTION | SWT.BORDER);
220  Table table = tableviewer_.getTable();
221  table.setFont(Activator.getDefault().getFont("preference_table"));
222  table.setLinesVisible(true);
223  table.setHeaderVisible(true);
224  TableColumn column = new TableColumn(table,SWT.NONE);
225  column.setText("Type");
226  column.setWidth(100);
227  column = new TableColumn(table,SWT.NONE);
228  column.setText("name");
229  column.setWidth(100);
230  column = new TableColumn(table,SWT.NONE);
231  column.setText("property");
232  column.setWidth(100);
233  column = new TableColumn(table,SWT.NONE);
234  column.setText("id");
235  column.setWidth(100);
236  column = new TableColumn(table,SWT.NONE);
237  column.setText("output Time");
238  column.setWidth(100);
239  column = new TableColumn(table,SWT.NONE);
240  column.setText("Controller : Port name");
241  column.setWidth(200);
242  GridData gridData1 = new GridData(GridData.FILL_VERTICAL);
243  gridData1.minimumHeight = 200;
244  tableviewer_.getControl().setLayoutData(gridData1);
245  tableviewer_.setContentProvider(new ArrayContentProvider());
246  tableviewer_.setLabelProvider(new InportLabelProvider());
247  tableviewer_.setInput(portTable_);
248  tableviewer_.setColumnProperties(new String[] {"type", "name", "property", "id", "time", "portName"});
249  DialogCellEditor idCellEditor =new DialogCellEditor(tableviewer_.getTable()){
250  protected Object openDialogBox(Control cellEditorWindow) {
251  IdCellDialog dialog = new IdCellDialog(cellEditorWindow.getShell());
252  dialog.setId((String)getValue());
253  if(dialog.open() == IDialogConstants.OK_ID)
254  setValue(dialog.getId());
255  return null;
256  }
257  };
258  CellEditor[] editors = new CellEditor[] {
259  new ComboBoxCellEditor(tableviewer_.getTable(), new String[]{"IN", "OUT" }),
260  new TextCellEditor(tableviewer_.getTable()),
261  new ComboBoxCellEditor(tableviewer_.getTable(), DataTypeId.getNames()),
262  idCellEditor,
263  new TextCellEditor(tableviewer_.getTable()),
264  new TextCellEditor(tableviewer_.getTable()) };
265  tableviewer_.setCellEditors(editors);
266  tableviewer_.setCellModifier(new TableCellModifier());
267  tableviewer_.getControl().addMouseListener(new MouseListener(){
268  public void mouseDoubleClick(MouseEvent e) {
269  }
270  public void mouseDown(MouseEvent e) {
271  if(e.button!=1){
272  PopupList list = new PopupList(getShell());
273  list.setItems(new String[]{MessageBundle.get("panel.Bridge.add"),
274  MessageBundle.get("panel.Bridge.delete")});
275  Rectangle rectangle = getShell().getBounds();
276  String s = list.open(new Rectangle(rectangle.x+e.x, rectangle.y+e.y, 100, 100));
277  if(s.equals(MessageBundle.get("panel.Bridge.add"))){
278  Port _port = new Port();
279  _port.type_ = "IN";
280  _port.name_ = "";
281  _port.propertyName_ = "JOINT_VALUE";
282  _port.id_ = "All joints";
283  _port.outputTime_ = "";
284  _port.controllerPortName_ = "";
285  portTable_.add(_port);
286  tableviewer_.refresh();
287  saveButtonEnabled(false);
288  }else{
289  int row = tableviewer_.getTable().getSelectionIndex();
290  portTable_.remove(row);
291  tableviewer_.refresh();
292  saveButtonEnabled(false);
293  }
294  }
295  }
296  public void mouseUp(MouseEvent e) {
297  }
298 
299  });
300 
301  Composite panel1 = new Composite(composite,SWT.NONE);
302  panel1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
303  panel1.setLayout(new GridLayout(3,false));
304  Label label2 = new Label(panel1, SWT.LEFT );
305  label2.setText(MessageBundle.get("panel.Bridge.nameServer")+": ");
306  Label label3 = new Label(panel1, SWT.LEFT );
307  label3.setText(MessageBundle.get("panel.Bridge.host")+": ");
308  nshText_ = new Text(panel1, SWT.BORDER);
309  Label dumy = new Label(panel1, SWT.LEFT );
310  Label label4 = new Label(panel1, SWT.LEFT );
311  label4.setText(MessageBundle.get("panel.Bridge.port")+": ");
312  nspText_ = new Text(panel1, SWT.BORDER);
313  nshText_.setText(nameServer_.host_);
314  nshText_.setLayoutData(gridData0);
315  nspText_.setText(nameServer_.port_);
316  nspText_.setLayoutData(gridData0);
317  ModifyListener nsModifyListener = new ModifyListener(){
318  public void modifyText(ModifyEvent e) {
319  nameServer_.host_ = nshText_.getText();
320  nameServer_.port_ = nspText_.getText();
321  saveButtonEnabled(false);
322  }
323  };
324  nshText_.addModifyListener(nsModifyListener);
325  nspText_.addModifyListener(nsModifyListener);
326 
327  Composite panel5 = new Composite(composite,SWT.NONE);
328  panel5.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
329  panel5.setLayout(new GridLayout(4,false));
330  Label label8 = new Label(panel5, SWT.LEFT );
331  label8.setText(MessageBundle.get("panel.Bridge.moduleName")+": ");
332  final Text text4 = new Text(panel5, SWT.BORDER);
333  text4.setLayoutData(gridData0);
334  text4.setText(moduleName_);
335  text4.addModifyListener(new ModifyListener(){
336  public void modifyText(ModifyEvent e) {
337  moduleName_ = text4.getText();
338  saveButtonEnabled(false);
339  }
340  });
341  Label label9 = new Label(panel5, SWT.LEFT );
342  label9.setText(MessageBundle.get("panel.Bridge.controllerRTCName")+": ");
343  final Text text5 = new Text(panel5, SWT.BORDER);
344  text5.setLayoutData(gridData0);
345  text5.setText(controllerRtcName_);
346  text5.addModifyListener(new ModifyListener(){
347  public void modifyText(ModifyEvent e) {
348  controllerRtcName_ = text5.getText();
349  saveButtonEnabled(false);
350  }
351  });
352 
353  Composite panel6 = new Composite(composite,SWT.NONE);
354  panel6.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
355  panel6.setLayout(new GridLayout(2,false));
356  final Button configFileButton = new Button(panel6, SWT.CHECK);
357  configFileButton.setText(MessageBundle.get("panel.Bridge.useConfigFile"));
358  configFileButton.addSelectionListener(new SelectionListener(){
359  public void widgetDefaultSelected(SelectionEvent e) {
360  }
361  public void widgetSelected(SelectionEvent e) {
362  useConfigFile_ = configFileButton.getSelection();
363  saveButtonEnabled(false);
364  }
365  });
366  if(useConfigFile_)
367  configFileButton.setSelection(true);
368  final Text text6 = new Text(panel6, SWT.BORDER);
369  text6.setLayoutData(gridData0);
370  text6.setText(configFileName_);
371  text6.addModifyListener(new ModifyListener(){
372  public void modifyText(ModifyEvent e) {
373  configFileName_ = text6.getText();
374  bridgeConfFileNameLabel_.setText(configFileName_);
375  saveButtonEnabled(false);
376  }
377  });
378 
379  Composite panel4 = new Composite(composite,SWT.NONE);
380  panel4.setLayout(new GridLayout(1,false));
381  Button button = new Button(panel4, SWT.PUSH);
382  button.setText(MessageBundle.get("panel.Bridge.check"));
383  button.addSelectionListener(new SelectionListener(){
384  public void widgetDefaultSelected(SelectionEvent e) {
385  }
386  public void widgetSelected(SelectionEvent e) {
387  errorCheck();
389  cbTextUpdate();
391  rcTextUpdate();
392  saveButtonEnabled(true);
393  emText_.setText(errorMessage_);
394  }
395  });
396  fileNameLabel_ = new Label(panel4, SWT.LEFT);
397  if(fileName_=="")
398  fileNameLabel_.setText("new file");
399  else
400  fileNameLabel_.setText(fileName_);
401  cbtext_ = new StyledText(panel4, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
402  GridData gridData2 = new GridData();
403  gridData2.horizontalAlignment = GridData.FILL;
404  gridData2.grabExcessHorizontalSpace = true;
405  gridData2.verticalAlignment = GridData.FILL;
406  gridData2.grabExcessVerticalSpace = true;
407  gridData2.minimumHeight = 100;
408  gridData2.widthHint = 700;
409  cbtext_.setLayoutData(gridData2);
410  cbtext_.setBackground(Activator.getDefault().getColor("gray"));
411  cbtext_.setEditable(false);
412  bridgeConfFileNameLabel_ = new Label(panel4, SWT.LEFT);
413  bridgeConfFileNameLabel_.setLayoutData(gridData0);
414  bridgeConfFileNameLabel_.setText(configFileName_);
415  bctext_ = new StyledText(panel4, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
416  GridData gridData5 = new GridData();
417  gridData5.horizontalAlignment = GridData.FILL;
418  gridData5.grabExcessHorizontalSpace = true;
419  gridData5.verticalAlignment = GridData.FILL;
420  gridData5.grabExcessVerticalSpace = true;
421  gridData5.minimumHeight = 100;
422  gridData5.widthHint = 700;
423  bctext_.setLayoutData(gridData5);
424  bctext_.setBackground(Activator.getDefault().getColor("gray"));
425  bctext_.setEditable(false);
426  cbTextUpdate();
427 
428  Composite panel3 = new Composite(composite,SWT.NONE);
429  panel3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
430  panel3.setLayout(new GridLayout(1,false));
431  rtcConfFileNameLabel_ = new Label(panel3, SWT.LEFT);
432  if(rtcConfFileName_.equals(""))
433  rtcConfFileNameLabel_.setText("rtc.conf");
434  else
435  rtcConfFileNameLabel_.setText(rtcConfFileName_);
436  rctext_ = new StyledText(panel3, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL );
437  GridData gridData4 = new GridData();
438  gridData4.horizontalAlignment = GridData.FILL;
439  gridData4.grabExcessHorizontalSpace = true;
440  gridData4.verticalAlignment = GridData.FILL;
441  gridData4.grabExcessVerticalSpace = true;
442  gridData4.widthHint = 700;
443  gridData4.minimumHeight = 50;
444  rctext_.setLayoutData(gridData4);
445  rctext_.setBackground(Activator.getDefault().getColor("gray"));
446  rcTextUpdate();
447  rctext_.setEditable(false);
448 
449  emText_ = new StyledText(panel3, SWT.NONE );
450  GridData gridData3 = new GridData();
451  gridData3.horizontalAlignment = GridData.FILL;
452  gridData3.grabExcessHorizontalSpace = true;
453  gridData3.verticalAlignment = GridData.FILL;
454  gridData3.grabExcessVerticalSpace = true;
455  gridData3.minimumHeight = 50;
456  emText_.setLayoutData(gridData3);
457  emText_.setBackground(panel3.getBackground());
458  emText_.setEditable(false);
459  emText_.setText(errorMessage_);
460  emText_.setForeground(Activator.getDefault().getColor("red"));
461 
462  return composite;
463  }
464 
465  private final int SAVE_ID = IDialogConstants.CLIENT_ID;
466  private final int SAVE_AS_ID = IDialogConstants.CLIENT_ID+1;
467  protected void createButtonsForButtonBar(Composite parent) {
468  saveButton_ = createButton(parent, SAVE_ID, MessageBundle.get("GrxTextItem.menu.save"), false);
469  saveAsButton_ = createButton(parent, SAVE_AS_ID, MessageBundle.get("GrxTextItem.menu.saveAs"), false);
470  saveButtonEnabled(false);
471  createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, false);
472  }
473 
474  protected void buttonPressed(int buttonId) {
475  if (buttonId == SAVE_ID) {
476  save();
477  }else if(buttonId == SAVE_AS_ID){
478  if(setFileName())
479  save();
480  }else if(buttonId == IDialogConstants.CLOSE_ID){
481  close();
482  }
483  }
484 
485  private class InportLabelProvider implements ITableLabelProvider{
486  public Image getColumnImage(Object element, int columnIndex) {
487  return null;
488  }
489  public String getColumnText(Object element, int columnIndex) {
490  Port node = (Port)element;
491  switch(columnIndex){
492  case 0:
493  return node.type_;
494  case 1:
495  return node.name_;
496  case 2:
497  return node.propertyName_;
498  case 3:
499  return node.id_;
500  case 4:
501  return node.outputTime_;
502  case 5:
503  if(!node.controllerPortName_.equals(""))
504  return node.type_.equals("IN") ? "<-- "+node.controllerPortName_ : "--> "+node.controllerPortName_;
505  }
506  return null;
507  }
508  public void addListener(ILabelProviderListener listener) {
509  }
510  public void dispose() {
511  }
512  public boolean isLabelProperty(Object element, String property) {
513  return false;
514  }
515  public void removeListener(ILabelProviderListener listener) {
516  }
517  }
518 
519  private class TableCellModifier implements ICellModifier {
520  public boolean canModify(Object element, String property) {
521  return true;
522  }
523 
524  public Object getValue(Object element, String property) {
525  Port port = (Port)element;
526  if(property.equals("type"))
527  return port.type_.equals("IN") ? 0 : 1 ;
528  else if(property.equals("name"))
529  return port.name_;
530  else if(property.equals("property")){
531  return DataTypeId.valueOf(port.propertyName_).ordinal();
532  }else if(property.equals("id"))
533  return port.id_;
534  else if(property.equals("time"))
535  return port.outputTime_;
536  else if(property.equals("portName"))
537  return port.controllerPortName_;
538  return null;
539  }
540 
541  public void modify(Object element, String property, Object value) {
542  if (element instanceof Item) {
543  element = ((Item) element).getData();
544  }
545  Port port = (Port)element;
546  if(property.equals("type"))
547  port.type_ = (Integer)value==0 ? "IN" : "OUT";
548  else if(property.equals("name"))
549  port.name_ = (String)value;
550  else if(property.equals("property")){
551  port.propertyName_ = DataTypeId.values()[(Integer)value].name();
552  }else if(property.equals("id"))
553  port.id_ = (String)value;
554  else if(property.equals("time"))
555  port.outputTime_ = (String)value;
556  else if(property.equals("portName"))
557  port.controllerPortName_ = (String)value;
558  saveButtonEnabled(false);
559  tableviewer_.update(element, null);
560  }
561  }
562 
563  public boolean load(String fileName){
564  File file = new File(fileName);
565  if (file == null || !file.isFile())
566  return false;
567  clear();
568  fileName_ = fileName;
569  try {
570  BufferedReader reader = new BufferedReader(new FileReader(file));
571  String option="";
572  boolean comline = false;
573  int i=0;
574  while (reader.ready()) {
575  String string = reader.readLine();
576  if(string.startsWith(commentChar_))
577  continue;
578  controllerBridgeFile_.add(string);
579  int index = string.indexOf("openhrp-controller-bridge");
580  if( index != -1 ){
581  comline = true;
582  string = string.substring(index+25).trim();
583  commandLineStart_ = i;
584  }
585  if(comline){
586  string = string.trim();
587  if(string.endsWith(endChar_)){
588  option += string.substring(0,string.length()-1);
589  }
590  else{
591  option += string;
592  commandLineEnd_ =i;
593  comline = false;
594  }
595  }
596  i++;
597  }
598  if(!option.equals("")){
599  String[] options = option.split("--");
600  rtcConfFileName_ = file.getCanonicalFile().getParent()+File.separator+"rtc.conf";
601  boolean ret = loadRtcConf(rtcConfFileName_);
602  parse(options);
603  createPortTable();
604  if(!ret){
605  rtcConfFileName_="";
606  rtcConfInit(nameServer_.host_, nameServer_.port_);
607  }
608  }else
609  ;// no command line
610  } catch (FileNotFoundException e) {
611  GrxDebugUtil.println("File Not Found. ("+file.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
612  return false;
613  } catch (IOException e) {
614  e.printStackTrace();
615  return false;
616  }
617  return true;
618  }
619 
620  private boolean loadRtcConf(String fileName){
621  File file = new File(fileName);
622  if (file == null || !file.isFile())
623  return false;
624  try {
625  BufferedReader reader = new BufferedReader(new FileReader(file));
626  while (reader.ready()) {
627  String string = reader.readLine();
628  if(string.startsWith("#"))
629  continue;
630  rtcConfFile_.add(string);
631  String string0 = string.trim();
632  if(string0.startsWith("corba.nameservers:")){
633  String[] s=string0.substring(18).trim().split(":");
634  if(s.length == 2){
635  rtcNameServer_.host_ = s[0];
636  rtcNameServer_.port_ = s[1];
637  }
638  }else if(string.trim().startsWith("manager.modules.preload:"))
639  moduleName_ = string0.substring(24).trim();
640  else if(string.trim().startsWith("manager.components.precreate:"))
641  controllerRtcName_ = string0.substring(29).trim();
642  //else if(string.trim().startsWith("exec_cxt.periodic.type"))
643  // ;
644  }
645  } catch (FileNotFoundException e) {
646  GrxDebugUtil.println("File Not Found. ("+file.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
647  return false;
648  } catch (IOException e) {
649  e.printStackTrace();
650  return false;
651  }
652  return true;
653  }
654 
655  private void init(){
656  fileName_ = "";
657  rtcConfFileName_ = "";
658  controllerBridgeFile_.clear();
659  if(os_==LINUX){
660  controllerBridgeFile_.add("#!/bin/sh");
661  commandLineStart_ = 1;
662  commandLineEnd_ = 1;
663  }else{
664  commandLineStart_ = 0;
665  commandLineEnd_ = 0;
666  }
667  controllerBridgeFile_.add("openhrp-controller-bridge "+endChar_);
668 
669  portTable_.clear();
670  controllerName_ = "";
671  robotRtcName_ = "";
672  inport_.clear();
673  outport_.clear();
674  connection_.clear();
675  nameServer_.host_ = "127.0.0.1";
676  nameServer_.port_ = "2809";
677  errorMessage_ = "";
678  useConfigFile_ = false;
679  configFileName_ = "";
680  rtcConfInit(nameServer_.host_, nameServer_.port_);
681  }
682 
683  private void rtcConfInit(String host, String port){
684  rtcConfFile_.clear();
685  rtcConfFile_.add("corba.nameservers: "+host+":"+port);
686  rtcConfFile_.add("naming.formats: %n.rtc");
687  rtcConfFile_.add("logger.log_level: TRACE");
688  rtcConfFile_.add("exec_cxt.periodic.type: SynchExtTriggerEC");
689  rtcConfFile_.add("exec_cxt.periodic.rate: 1000000");
690  rtcConfFile_.add("manager.modules.load_path: .");
691  rtcConfFile_.add("manager.modules.abs_path_allowed: yes");
692  rtcConfFile_.add("manager.modules.preload: ");
693  rtcConfFile_.add("manager.components.precreate: ");
694  rtcNameServer_.host_ = host;
695  rtcNameServer_.port_ = port;
696  moduleName_ = "";
697  controllerRtcName_ = "";
698 
699  }
700 
701  private void clear(){
702  fileName_ = "";
703  controllerBridgeFile_.clear();
704  portTable_.clear();
705  controllerName_ = "";
706  robotRtcName_ = "";
707  inport_.clear();
708  outport_.clear();
709  connection_.clear();
710  nameServer_.host_ = "127.0.0.1";
711  nameServer_.port_ = "2809";
712  errorMessage_ = "";
713 
714  rtcConfFile_.clear();
715  rtcNameServer_.host_ = "";
716  rtcNameServer_.port_ = "";
717  moduleName_ = "";
718  controllerRtcName_ = "";
719  useConfigFile_ = false;
720  configFileName_ = "";
721  }
722 
723  private void parse(String[] options){
724  for(int i=0; i<options.length; i++){
725  options[i] = options[i].trim();
726  int endIndex = options[i].indexOf(" ");
727  if(endIndex == -1)
728  continue;
729  String option = options[i].substring(0,endIndex);
730  String parameter = options[i].substring(endIndex).trim();
731  if(option.equals("server-name")){
732  controllerName_ = parameter;
733  }else if(option.equals("in-port")){
734  Inport _inport = new Inport();
735  String[] s = parameter.split(":");
736  if(s.length==2){
737  _inport.name_ = s[0].trim();
738  _inport.propertyName_ = s[1].trim();
739  _inport.id_ = null;
740  }else if(s.length==3){
741  _inport.name_ = s[0].trim();
742  _inport.propertyName_ = s[2].trim();
743  _inport.id_ = s[1].split(",");
744  for(int j=0; j<_inport.id_.length; j++)
745  _inport.id_[j] = _inport.id_[j].trim();
746  }else
747  continue;
748  inport_.add(_inport);
749 
750  }else if(option.equals("out-port")){
751  Outport _outport = new Outport();
752  String[] s = parameter.split(":");
753  _outport.name_ = s[0].trim();
754  _outport.idName_ = null;
755  int j;
756  for(j=1; j<3; j++){
757  try{
758  DataTypeId type = DataTypeId.valueOf(s[j].trim());
759  // プロパティ名 //
760  _outport.propertyName_ = s[j].trim();
761  j++;
762  break;
763  }catch(IllegalArgumentException e){
764  // プロパティ名でないので識別名  //
765  if(j==2) // 3番目までにプロパティ名がないのでエラー //
766  break;
767  String[] _id = s[j].split(",");
768  _outport.idName_ = new String[_id.length];
769  for(int k=0; k<_id.length; k++){
770  try{
771  _outport.idNo_ = Integer.parseInt(_id[k]);
772  _outport.idName_[k] = "";
773  }catch(NumberFormatException ex){
774  _outport.idName_[k] = _id[k].trim();
775  _outport.idNo_ = -1;
776  }
777  }
778  }
779  }
780  if(j<s.length){ // まだパラメターがあるならサンプリング時間 //
781  _outport.outputTime_ = Double.parseDouble(s[j]);
782  }else{
783  _outport.outputTime_ = -1;
784  }
785  outport_.add(_outport);
786  }else if(option.equals("connection")){
787  String[] s = parameter.split(":");
788  String controllerPortName="";
789  if(s.length == 2){
790  controllerPortName = controllerRtcName_+"0:"+s[1].trim();
791  }else if(s.length ==3){
792  controllerPortName = s[1].trim()+":"+s[2].trim();
793  }
794  connection_.put(s[0].trim(), controllerPortName);
795  }else if(option.equals("robot-name")){
796  robotRtcName_ = parameter;
797  }else if(option.equals("name-server")){
798  String[] s = parameter.split(":");
799  nameServer_.host_ = s[0].trim();
800  nameServer_.port_ = s[1].trim();
801  }else if(option.equals("config-file")){
802  loadConfigFile(parameter);
803  useConfigFile_ = true;
804  configFileName_ = parameter;
805  }
806  }
807  }
808 
809  private void loadConfigFile(String fileName){
810  File file = new File(fileName_);
811  try {
812  fileName = file.getCanonicalFile().getParent()+File.separator+fileName;
813  } catch (IOException e) {
814  e.printStackTrace();
815  }
816  file = new File(fileName);
817  if (file == null || !file.isFile())
818  return;
819  try {
820  BufferedReader reader = new BufferedReader(new FileReader(file));
821  Vector<String> options = new Vector<String>();
822  while (reader.ready()) {
823  String string = reader.readLine();
824  if(string.startsWith("#"))
825  continue;
826  bridgeConfFile_.append(string+"\n");
827  String[] s = string.split("=");
828  if(s.length==2)
829  options.add(s[0]+" "+s[1]);
830  }
831  parse(options.toArray(new String[0]));
832 
833  } catch (FileNotFoundException e) {
834  GrxDebugUtil.println("File Not Found. ("+file.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
835  return;
836  } catch (IOException e) {
837  e.printStackTrace();
838  return;
839  }
840  return;
841  }
842 
843  private void createPortTable() {
844  Iterator<Inport> it = inport_.iterator();
845  while(it.hasNext()){
846  Inport inport = it.next();
847  Port _port = new Port();
848  _port.type_ = "IN";
849  _port.name_ = inport.name_;
850  _port.propertyName_ = inport.propertyName_;
851  if(inport.id_ == null)
852  _port.id_ = "All joints";
853  else{
854  _port.id_ = "";
855  for(int i=0; i<inport.id_.length-1; i++)
856  _port.id_ += inport.id_[i] + ",";
857  _port.id_ += inport.id_[inport.id_.length-1];
858  }
859  _port.outputTime_ = "";
860  String controllerPort = connection_.get(inport.name_);
861  if(controllerPort==null)
862  controllerPort = "";
863  _port.controllerPortName_ = controllerPort;
864  portTable_.add(_port);
865  }
866  Iterator<Outport> it0 = outport_.iterator();
867  while(it0.hasNext()){
868  Outport outport = it0.next();
869  Port _port = new Port();
870  _port.type_ = "OUT";
871  _port.name_ = outport.name_;
872  _port.propertyName_ = outport.propertyName_;
873  if(outport.idName_ == null)
874  _port.id_ = "All joints";
875  else if(outport.idNo_ != -1){
876  _port.id_ = String.valueOf(outport.idNo_);
877  }else{
878  _port.id_ = "";
879  for(int i=0; i<outport.idName_.length-1; i++)
880  _port.id_ += outport.idName_[i] + ",";
881  _port.id_ += outport.idName_[outport.idName_.length-1];
882  }
883  if(outport.outputTime_<0)
884  _port.outputTime_ = "control Time";
885  else
886  _port.outputTime_ = String.valueOf(outport.outputTime_);
887  String controllerPort = connection_.get(outport.name_);
888  if(controllerPort==null)
889  controllerPort = "";
890  _port.controllerPortName_ = controllerPort;
891  portTable_.add(_port);
892  }
893  }
894 
895  private class Inport {
896  public String name_;
897  public String propertyName_;
898  public String[] id_;
899  }
900 
901  private class Outport {
902  public String name_;
903  public String propertyName_;
904  public String[] idName_;
905  public int idNo_;
906  public double outputTime_;
907  }
908 
909  private class Port {
910  public String type_;
911  public String name_;
912  public String propertyName_;
913  public String id_;
914  public String outputTime_;
915  public String controllerPortName_;
916  }
917 
918  private class NameServer {
919  public String host_;
920  public String port_;
921  }
922 
923  public void setRobot(GrxModelItem robot) {
924  robot_ = robot;
925  robotName_ = robot_.getName();
926  }
927 
928  public boolean setProjectControllerName(String _controllerName) {
929  projectControllerName_ = _controllerName;
930  if(projectControllerName_.equals(controllerName_))
931  return true;
932  else
933  return false;
934  }
935 
936  public void setControllerName(String _controllerName) {
937  controllerName_ = _controllerName;
938  }
939 
940  public boolean checkNameServer(){
941  if(nameServer_.host_.equals(rtcNameServer_.host_) && nameServer_.port_.equals(rtcNameServer_.port_))
942  return true;
943  else
944  return false;
945  }
946 
947  public void setNameServer(){
948  nameServer_.host_ = rtcNameServer_.host_;
949  nameServer_.port_ = rtcNameServer_.port_;
950  }
951 
952  public void save(){
953  if(fileName_.equals(""))
954  if(!setFileName())
955  return;
956  File file = new File(fileName_);
957  if(rtcConfFileName_.equals("")){
958  try {
959  rtcConfFileName_ = file.getCanonicalFile().getParent()+File.separator+"rtc.conf";
960  } catch (IOException e) {
961  e.printStackTrace();
962  }
963  }
964  String bridgeConfFileName=null;
965  if(useConfigFile_){
966  try {
967  bridgeConfFileName = file.getCanonicalFile().getParent()+File.separator+configFileName_;
968  } catch (IOException e) {
969  e.printStackTrace();
970  }
971  }
972  try {
973  BufferedWriter writer = new BufferedWriter(new FileWriter(fileName_));
974  Iterator<String> it = controllerBridgeFile_.iterator();
975  while(it.hasNext()){
976  writer.write(it.next());
977  writer.newLine();
978  }
979  writer.flush();
980  writer.close();
981 
982  writer = new BufferedWriter(new FileWriter(rtcConfFileName_));
983  it = rtcConfFile_.iterator();
984  while(it.hasNext()){
985  writer.write(it.next());
986  writer.newLine();
987  }
988  writer.flush();
989  writer.close();
990 
991  if(useConfigFile_){
992  writer = new BufferedWriter(new FileWriter(bridgeConfFileName));
993  writer.write(bridgeConfFile_.toString());
994  writer.flush();
995  writer.close();
996  }
997  } catch (IOException e) {
998  e.printStackTrace();
999  }
1000  fileNameLabel_.setText(fileName_);
1001  rtcConfFileNameLabel_.setText(rtcConfFileName_);
1002 
1003  }
1004 
1005  private boolean setFileName(){
1006  FileDialog fdlg = new FileDialog( GrxUIPerspectiveFactory.getCurrentShell(), SWT.SAVE);
1007  String fileName = fdlg.open();
1008  if( fileName != null ) {
1009  fileName_ = fileName;
1010  File file = new File(fileName_);
1011  try {
1012  rtcConfFileName_ = file.getCanonicalFile().getParent()+File.separator+"rtc.conf";
1013  file = new File(rtcConfFileName_);
1014  if(file.exists()){
1015  if(!MessageDialog.openQuestion(getShell(), rtcConfFileName_, MessageBundle.get("Grx3DView.dialog.message.fileExist"))){
1016  fdlg.setFileName(rtcConfFileName_);
1017  rtcConfFileName_ = fdlg.open();
1018  if(rtcConfFileName_==null)
1019  return false;
1020  }
1021  }
1022  } catch (IOException e) {
1023  e.printStackTrace();
1024  }
1025  return true;
1026  }
1027  return false;
1028  }
1029 
1031  String commandLine = controllerBridgeFile_.get(commandLineStart_);
1032  int index = commandLine.indexOf("openhrp-controller-bridge");
1033  if( index != -1 ){
1034  commandLine = commandLine.substring(0,index+25)+" ";
1035  }
1036  for(int i=commandLineEnd_; i>=commandLineStart_; i--)
1037  controllerBridgeFile_.remove(i);
1038  bridgeConfFile_ = new StringBuffer();
1039 
1040  if(useConfigFile_){
1041  controllerBridgeFile_.insertElementAt( commandLine+"--config-file "+configFileName_,commandLineStart_);
1042  commandLineEnd_ = commandLineStart_;
1044  }else{
1045  int i = commandLineStart_;
1046  controllerBridgeFile_.insertElementAt(commandLine+endChar_, i++);
1047  if(!controllerName_.equals("")){
1048  String string = "--server-name "+controllerName_ +" "+endChar_;
1049  controllerBridgeFile_.insertElementAt(string, i++);
1050  }
1051  Iterator<Port> it = portTable_.iterator();
1052  while(it.hasNext()){
1053  String string="";
1054  Port port = it.next();
1055  if(port.type_.equals("IN")){
1056  string += "--in-port "+port.name_;
1057  if(!port.id_.equals("All joints") && !port.id_.equals(""))
1058  string += ":"+port.id_;
1059  string += ":"+port.propertyName_;
1060  }else{
1061  string += "--out-port "+port.name_;
1062  if(!port.id_.equals("All joints") && !port.id_.equals(""))
1063  string += ":"+port.id_;
1064  string += ":"+port.propertyName_;
1065  if(!port.outputTime_.equals("control Time"))
1066  string += ":"+port.outputTime_;
1067  }
1068  string += " "+endChar_;
1069  controllerBridgeFile_.insertElementAt(string, i++);
1070  if(!port.controllerPortName_.equals("")){
1071  String[] s = port.controllerPortName_.split(":");
1072  String controllerPortName = port.controllerPortName_;
1073  if(s.length==2 && s[0].equals(controllerRtcName_+"0"))
1074  controllerPortName = s[1];
1075  string = "--connection "+port.name_+":"+controllerPortName+" "+endChar_;
1076  controllerBridgeFile_.insertElementAt(string, i++);
1077  }
1078  }
1079  if(!robotRtcName_.equals("")){
1080  String string = "--robot-name "+robotRtcName_ +" "+endChar_;
1081  controllerBridgeFile_.insertElementAt(string, i++);
1082  }
1083  if(!nameServer_.host_.equals("127.0.0.1") || !nameServer_.port_.equals("2809")){
1084  String string = "--name-server "+nameServer_.host_+":"+nameServer_.port_+" " +endChar_;
1085  controllerBridgeFile_.insertElementAt(string, i++);
1086  }
1087 
1088  commandLineEnd_ = i-1;
1089  String string = controllerBridgeFile_.get(commandLineEnd_);
1090  string = string.substring(0, string.length()-1);
1091  controllerBridgeFile_.setElementAt(string, commandLineEnd_);
1092  }
1093  }
1094 
1095  private void updateBridgeConfFile(){
1096  String lineSeparator = System.getProperty("line.separator");
1097  if(!controllerName_.equals("")){
1098  bridgeConfFile_.append("server-name = "+controllerName_ +lineSeparator);
1099  }
1100  Iterator<Port> it = portTable_.iterator();
1101  while(it.hasNext()){
1102  String string="";
1103  Port port = it.next();
1104  if(port.type_.equals("IN")){
1105  string += "in-port = "+port.name_;
1106  if(!port.id_.equals("All joints") && !port.id_.equals(""))
1107  string += ":"+port.id_;
1108  string += ":"+port.propertyName_;
1109  }else{
1110  string += "out-port = "+port.name_;
1111  if(!port.id_.equals("All joints") && !port.id_.equals(""))
1112  string += ":"+port.id_;
1113  string += ":"+port.propertyName_;
1114  if(!port.outputTime_.equals("control Time"))
1115  string += ":"+port.outputTime_;
1116  }
1117  bridgeConfFile_.append(string+lineSeparator);
1118  if(!port.controllerPortName_.equals("")){
1119  String[] s = port.controllerPortName_.split(":");
1120  String controllerPortName = port.controllerPortName_;
1121  if(s.length==2 && s[0].equals(controllerRtcName_+"0"))
1122  controllerPortName = s[1];
1123  string = "connection = "+port.name_+":"+controllerPortName+lineSeparator;
1124  bridgeConfFile_.append(string);
1125  }
1126  }
1127  if(!robotRtcName_.equals("")){
1128  String string = "robot-name = "+robotRtcName_ +lineSeparator;
1129  bridgeConfFile_.append(string);
1130  }
1131  if(!nameServer_.host_.equals("127.0.0.1") || !nameServer_.port_.equals("2809")){
1132  String string = "name-server = "+nameServer_.host_+":"+nameServer_.port_+lineSeparator;
1133  bridgeConfFile_.append(string);
1134  }
1135  }
1136 
1137  private void updateRtcConfFile(){
1138  for(int i=0; i<rtcConfFile_.size(); i++){
1139  String string = rtcConfFile_.get(i).trim();
1140  if(string.startsWith("corba.nameservers:")){
1141  String s=string.substring(0,18)+" "+ nameServer_.host_+":"+nameServer_.port_;
1142  rtcConfFile_.setElementAt(s, i);
1143  }else if(string.startsWith("manager.modules.preload:")){
1144  String s=string.substring(0,24)+" "+ moduleName_;
1145  rtcConfFile_.setElementAt(s, i);
1146  }else if(string.startsWith("manager.components.precreate:")){
1147  String s=string.substring(0,29)+" "+ controllerRtcName_;
1148  rtcConfFile_.setElementAt(s, i);
1149  }
1150  }
1151  }
1152 
1153  private void cbTextUpdate(){
1154  String s="";
1155  Iterator<String> it = controllerBridgeFile_.iterator();
1156  while(it.hasNext())
1157  s += it.next() + "\n";
1158  cbtext_.setText(s);
1159  bctext_.setText(bridgeConfFile_.toString());
1160  }
1161 
1162  private void rcTextUpdate(){
1163  String s="";
1164  Iterator<String> it = rtcConfFile_.iterator();
1165  while(it.hasNext())
1166  s += it.next() + "\n";
1167  rctext_.setText(s);
1168  }
1169 
1170  private void rrnTextUpdate(){
1171  if(robotRtcName_.equals(""))
1172  rrnText_.setText(controllerName_+"(robot)");
1173  else
1174  rrnText_.setText(robotRtcName_);
1175  }
1176 
1177  private void saveButtonEnabled(boolean b){
1178  saveButton_.setEnabled(b);
1179  saveAsButton_.setEnabled(b);
1180  }
1181 
1182  private void errorCheck(){
1183  errorMessage_ = "";
1184  if(!projectControllerName_.equals(controllerName_))
1185  errorMessage_ += MessageBundle.get("panel.Bridge.error.controlName")+"\n";
1186  Iterator<Port> it = portTable_.iterator();
1187  while(it.hasNext()){
1188  Port port = it.next();
1189  if(port.name_.equals(""))
1190  errorMessage_ += MessageBundle.get("panel.Bridge.error.setPortName")+"\n";
1192  if(!(numId==DataTypeId.NUMID_CODE.NOT_USED) && (port.id_.equals("All joints") || port.id_.equals("")))
1193  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" "+MessageBundle.get("panel.Bridge.error.useID")+"\n";
1194  if(numId==DataTypeId.NUMID_CODE.ONLY_ONE){
1195  String[] s = port.id_.split(",");
1196  if(s.length > 1)
1197  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" "+MessageBundle.get("panel.Bridge.error.oneID")+"\n";
1198  }
1199  if(numId==DataTypeId.NUMID_CODE.NUMBER){
1200  try{
1201  Integer.valueOf(port.id_);
1202  }catch(NumberFormatException e){
1203  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" "+MessageBundle.get("panel.Bridge.error.sensorId")+"\n";
1204  }
1205  }
1206  if(port.type_.equals("IN")){
1207  if(!DataTypeId.isInport(port.propertyName_))
1208  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" : "+port.propertyName_+MessageBundle.get("panel.Bridge.error.inportPropertyName")+"\n";
1209  if(!port.outputTime_.equals(""))
1210  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.inportTime")+"\n";
1211  }else{
1213  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" : "+port.propertyName_+MessageBundle.get("panel.Bridge.error.outportPropertyName")+"\n";
1214  if(!port.outputTime_.equals("control Time") && !port.outputTime_.equals("")){
1215  try{
1216  double time = Double.valueOf(port.outputTime_);
1217  if(time < controlTime_){
1218  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.outportTimeValue")+"\n";
1219  }
1220  }catch(NumberFormatException e){
1221  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.outportTime")+"\n";
1222  }
1223  }
1224  }
1225  if(port.controllerPortName_.startsWith("0:"))
1226  errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.controllerPortName")+"\n";
1227  }
1228 
1229  if(moduleName_.equals(""))
1230  errorMessage_ += MessageBundle.get("panel.Bridge.error.moduleName")+"\n";
1231  if(controllerRtcName_.equals(""))
1232  errorMessage_ += MessageBundle.get("panel.Bridge.error.controlRTCName")+"\n";
1233  if(useConfigFile_ && configFileName_.equals(""))
1234  errorMessage_ += MessageBundle.get("panel.Bridge.error.configFileName")+"\n";
1235  }
1236 
1237  @SuppressWarnings("unchecked")
1238  private class IdCellDialog extends Dialog {
1239  private class IdData {
1240  public String name_;
1241  public int id_;
1242  public boolean selection_;
1243  public IdData(String name, int id){
1244  name_ = name;
1245  id_ = id;
1246  selection_ = false;
1247  }
1248  public boolean equals(Object object){
1249  if(id_ < 0)
1250  return name_.equals(((IdData)object).name_);
1251  else
1252  return name_.equals(((IdData)object).name_) || id_ == ((IdData)object).id_ ;
1253  }
1254  }
1255  private class Link extends IdData {
1256  public Link(String name, int jointId) {
1257  super(name, jointId);
1258  }
1259  public boolean isJoint(){
1260  if(id_ < 0)
1261  return false;
1262  else
1263  return true;
1264  }
1265  }
1266 
1267  private Vector<Link> links_ = new Vector<Link>();
1268  private Vector<IdData>[] sensors_ = new Vector[GrxSensorItem.sensorType.length];
1269  private String id_;
1270  private Vector<Button> linkButtons_ = new Vector<Button>();
1271  private Vector<Button>[] sensorButtons_ = new Vector[GrxSensorItem.sensorType.length];
1272  private Button allJointButton_;
1273 
1274  protected IdCellDialog(Shell parentShell) {
1275  super(parentShell);
1276  String[] joints = robot_.getJointNames();
1277  for(int i=0; i<joints.length; i++)
1278  links_.add(new Link(joints[i], i));
1279  Iterator<GrxLinkItem> it = robot_.links_.iterator();
1280  while(it.hasNext()){
1281  GrxLinkItem link = it.next();
1282  if(link.jointId_ < 0)
1283  links_.add(new Link(link.getName(), -1));
1284  }
1285  for(int i=0; i<GrxSensorItem.sensorType.length; i++){
1286  sensors_[i] = new Vector<IdData>();
1287  String[] names = robot_.getSensorNames(GrxSensorItem.sensorType[i]);
1288  if(names!=null){
1289  for(int j=0; j<names.length; j++)
1290  sensors_[i].add(new IdData(names[j], j));
1291  }
1292  sensorButtons_[i] = new Vector<Button>();
1293  }
1294  }
1295 
1296  protected Control createDialogArea(Composite parent) {
1297  Composite composite = (Composite)super.createDialogArea(parent);
1298  composite.setLayout(new GridLayout(2,true));
1299 
1300  Composite panel0 = new Composite(composite,SWT.NONE);
1301  panel0.setLayoutData(new GridData(GridData.FILL_VERTICAL));
1302  panel0.setLayout(new FillLayout(SWT.VERTICAL));
1303  Label label0 = new Label(panel0, SWT.NONE);
1304  label0.setText("Link");
1305  label0.setForeground(Activator.getDefault().getColor("blue"));
1306  allJointButton_ = new Button(panel0, SWT.TOGGLE|SWT.CENTER);
1307  allJointButton_.setText("All joints");
1308  allJointButton_.addSelectionListener(new SelectionListener(){
1309  public void widgetDefaultSelected(SelectionEvent e) {
1310  }
1311  public void widgetSelected(SelectionEvent e) {
1312  if(allJointButton_.getSelection()){
1313  for(int i=0; i<links_.size(); i++){
1314  if(links_.get(i).isJoint())
1315  linkButtons_.get(i).setSelection(true);
1316  }
1317  }else{
1318  for(int i=0; i<links_.size(); i++){
1319  if(links_.get(i).isJoint())
1320  linkButtons_.get(i).setSelection(false);
1321  }
1322  }
1323  }
1324  });
1325  for(int i=0; i<links_.size(); i++){
1326  linkButtons_.add(new Button(panel0, SWT.CHECK|SWT.LEFT));
1327  linkButtons_.get(i).addSelectionListener(new SelectionListener(){
1328  public void widgetDefaultSelected(SelectionEvent e) {
1329  }
1330  public void widgetSelected(SelectionEvent e) {
1331  updateAllJointButton();
1332  }
1333  });
1334  }
1335  updateButton(true, 0);
1336 
1337  Composite panel1 = new Composite(composite,SWT.NONE);
1338  panel1.setLayoutData(new GridData(GridData.FILL_VERTICAL));
1339  panel1.setLayout(new RowLayout(SWT.VERTICAL));
1340  for(int i=0; i<GrxSensorItem.sensorType.length; i++){
1341  Label label = new Label(panel1, SWT.NONE);
1342  label.setText(GrxSensorItem.sensorType[i]);
1343  label.setForeground(Activator.getDefault().getColor("blue"));
1344  for(int j=0; j<sensors_[i].size(); j++){
1345  sensorButtons_[i].add(new Button(panel1, SWT.CHECK|SWT.LEFT));
1346  }
1347  Label dumy = new Label(panel1, SWT.NONE);
1348  }
1349  updateButton(false, -1);
1350 
1351  Transfer [] transfers = new Transfer[]{ TextTransfer.getInstance() };
1352  DragSourceListener dragSourceListener = new DragSourceListener() {
1353  public void dragFinished(DragSourceEvent event) {
1354  }
1355  public void dragSetData(DragSourceEvent event) {
1356  Button sourceButton = (Button)((DragSource) event.getSource()).getControl();
1357  int index = linkButtons_.indexOf(sourceButton);
1358  if(index>=0)
1359  event.data = "link "+String.valueOf(index);//links_.get(index).name_;
1360  else
1361  for(int i=0; i<sensorButtons_.length; i++){
1362  index = sensorButtons_[i].indexOf(sourceButton);
1363  if(index >= 0){
1364  event.data = "sensor "+String.valueOf(i)+" "+String.valueOf(index);
1365  break;
1366  }
1367  }
1368  }
1369  public void dragStart(DragSourceEvent event) {
1370  }
1371  };
1372  DropTargetListener dropTargetListener = new DropTargetListener(){
1373  public void drop(DropTargetEvent event){
1374  if (event != null){
1375  Button targetButton = (Button)((DropTarget) event.getSource()).getControl();
1376  String string = (String)event.data;
1377  String[] source = string.split(" ");
1378  if(source[0].equals("link")){
1379  for(int i=0; i<links_.size(); i++)
1380  links_.get(i).selection_ = linkButtons_.get(i).getSelection();
1381  int sourceIndex = Integer.parseInt(source[1]);
1382  int targetIndex = linkButtons_.indexOf(targetButton);
1383  if(targetIndex >=0){
1384  Link sourceLink = links_.get(sourceIndex);
1385  Link targetLink = links_.get(targetIndex);
1386  links_.remove(sourceLink);
1387  links_.insertElementAt(sourceLink, links_.indexOf(targetLink));
1388  updateButton(true, 0);
1389  }
1390  }else{ //sensor
1391  int type = Integer.parseInt(source[1]);
1392  for(int i=0; i<sensors_[type].size(); i++)
1393  sensors_[type].get(i).selection_ = sensorButtons_[type].get(i).getSelection();
1394  int sourceIndex = Integer.parseInt(source[2]);
1395  int targetIndex = sensorButtons_[type].indexOf(targetButton);
1396  if(targetIndex >=0){
1397  IdData sourceData = sensors_[type].get(sourceIndex);
1398  IdData targetData = sensors_[type].get(targetIndex);
1399  sensors_[type].remove(sourceData);
1400  sensors_[type].insertElementAt(sourceData, sensors_[type].indexOf(targetData));
1401  updateButton(false, type);
1402  }
1403  }
1404  }
1405  }
1406  private Color buttonColor_;
1407  public void dragEnter(DropTargetEvent event) {
1408  Button targetButton = (Button)((DropTarget) event.getSource()).getControl();
1409  buttonColor_ = targetButton.getBackground();
1410  targetButton.setBackground(Activator.getDefault().getColor("darkGray"));
1411  }
1412  public void dragLeave(DropTargetEvent event) {
1413  Button targetButton = (Button)((DropTarget) event.getSource()).getControl();
1414  targetButton.setBackground(buttonColor_);
1415  }
1416  public void dragOperationChanged(DropTargetEvent event) {
1417  }
1418  public void dragOver(DropTargetEvent event) {
1419  }
1420  public void dropAccept(DropTargetEvent event) {
1421  }
1422  };
1423  for(int i=0; i<links_.size(); i++){
1424  DragSource source = new DragSource(linkButtons_.get(i), DND.DROP_MOVE );
1425  source.setTransfer(transfers);
1426  source.addDragListener(dragSourceListener);
1427  DropTarget target = new DropTarget(linkButtons_.get(i), DND.DROP_MOVE);
1428  target.setTransfer(transfers);
1429  target.addDropListener(dropTargetListener);
1430  }
1431 
1432  for(int i=0; i<sensors_.length; i++){
1433  for(int j=0; j<sensors_[i].size(); j++){
1434  DragSource source = new DragSource(sensorButtons_[i].get(j), DND.DROP_MOVE );
1435  source.setTransfer(transfers);
1436  source.addDragListener(dragSourceListener);
1437  DropTarget target = new DropTarget(sensorButtons_[i].get(j), DND.DROP_MOVE);
1438  target.setTransfer(transfers);
1439  target.addDropListener(dropTargetListener);
1440  }
1441  }
1442 
1443  return composite;
1444  }
1445 
1446  protected void buttonPressed(int buttonId) {
1447  if (buttonId == IDialogConstants.OK_ID) {
1448  boolean b = false;
1449  for(int i=0; i<links_.size(); i++){
1450  if(links_.get(i).isJoint()){
1451  b = true;
1452  if(!linkButtons_.get(i).getSelection()){
1453  b = false;
1454  break;
1455  }
1456  }
1457  }
1458  if(b){
1459  id_ = "All joints";
1460  }else{
1461  id_ = "";
1462  for(int i=0; i<linkButtons_.size(); i++)
1463  if(linkButtons_.get(i).getSelection())
1464  id_ += links_.get(i).name_ + ",";
1465  for(int i=1; i<sensorButtons_.length; i++)
1466  for(int j=0; j<sensorButtons_[i].size(); j++)
1467  if(sensorButtons_[i].get(j).getSelection())
1468  id_ += sensors_[i].get(j).name_ + ",";
1469  if(id_.length()!=0)
1470  id_ = id_.substring(0, id_.length()-1);
1471  else
1472  for(int i=0; i<sensorButtons_[0].size(); i++)
1473  if(sensorButtons_[0].get(i).getSelection())
1474  id_ = String.valueOf(sensors_[0].get(i).id_);
1475 
1476  }
1477  }
1478  super.buttonPressed(buttonId);
1479  }
1480 
1481  public String getId(){
1482  return id_;
1483  }
1484 
1485  public void setId(String value) {
1486  id_ = value;
1487  if(id_.equals("All joints")){
1488  for(int i=0; i<links_.size(); i++){
1489  if(links_.get(i).isJoint())
1490  links_.get(i).selection_ = true;
1491  }
1492  }else{
1493  String[] names = id_.split(",");
1494  for(int i=names.length; i>0; ){
1495  String name = names[--i].trim();
1496  int index = links_.indexOf(new Link(name, -1));
1497  if(index >= 0){
1498  Link link = links_.get(index);
1499  link.selection_ = true;
1500  links_.remove(link);
1501  links_.insertElementAt(link, 0);
1502  continue;
1503  }
1504  for(int j=1; j<GrxSensorItem.sensorType.length; j++){
1505  index = sensors_[j].indexOf(new IdData(name, -1));
1506  if(index >= 0){
1507  IdData idData = sensors_[j].get(index);
1508  idData.selection_ = true;
1509  sensors_[j].remove(idData);
1510  sensors_[j].insertElementAt(idData, 0);
1511  continue;
1512  }
1513  }
1514  try{
1515  int id = Integer.valueOf(name);
1516  index = sensors_[0].indexOf(new IdData("", id));
1517  if(index >= 0){
1518  IdData idData = sensors_[0].get(index);
1519  idData.selection_ = true;
1520  sensors_[0].remove(idData);
1521  sensors_[0].insertElementAt(idData, 0);
1522  }
1523  }catch(NumberFormatException e){
1524  ;
1525  }
1526  }
1527  }
1528  }
1529 
1530  private void updateButton(boolean isLink, int type){
1531  if(isLink){
1532  for(int i=0; i<links_.size(); i++){
1533  String string = links_.get(i).name_;
1534  if(links_.get(i).isJoint())
1535  string += " (id= "+links_.get(i).id_+")";
1536  linkButtons_.get(i).setText(string);
1537  linkButtons_.get(i).setSelection(links_.get(i).selection_);
1538  }
1539  updateAllJointButton();
1540  }else{
1541  if(type < 0)
1542  for(int i=0; i<GrxSensorItem.sensorType.length; i++){
1543  for(int j=0; j<sensors_[i].size(); j++){
1544  sensorButtons_[i].get(j).setText(sensors_[i].get(j).name_+" (id= "+sensors_[i].get(j).id_+")");
1545  sensorButtons_[i].get(j).setSelection(sensors_[i].get(j).selection_);
1546  }
1547  }
1548  else{
1549  for(int j=0; j<sensors_[type].size(); j++){
1550  sensorButtons_[type].get(j).setText(sensors_[type].get(j).name_+" (id= "+sensors_[type].get(j).id_+")");
1551  sensorButtons_[type].get(j).setSelection(sensors_[type].get(j).selection_);
1552  }
1553  }
1554  }
1555  }
1556 
1557  private void updateAllJointButton() {
1558  boolean b = true;
1559  for(int i=0; i<links_.size(); i++)
1560  if(links_.get(i).isJoint())
1561  b = b && linkButtons_.get(i).getSelection();
1562  if(b)
1563  allJointButton_.setSelection(true);
1564  else
1565  allJointButton_.setSelection(false);
1566  }
1567  }
1568 
1569  public void setControlTime(double time) {
1570  controlTime_ = time;
1571  }
1572 
1573 }
static final String get(String key)
png_infop png_charp png_int_32 png_int_32 int * type
Definition: png.h:2332
#define null
our own NULL pointer
Definition: IceTypes.h:57
void * reader(void *arg)
png_infop png_charpp name
Definition: png.h:2382
png_voidp int value
Definition: png.h:2113
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
item corresponds to a robot model
png_bytep png_bytep png_size_t length
Definition: png.h:1541
png_uint_32 i
Definition: png.h:2735
JCOPY_OPTION option
Definition: transupp.h:131
long b
Definition: jpegint.h:371
string outport
string inport
def j(str, encoding="cp932")
list index
png_bytepp row
Definition: png.h:1759
org
void * writer(void *arg)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:37