ControllerBridgePanel.java
Go to the documentation of this file.
00001 package com.generalrobotix.ui.view.simulation;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.BufferedWriter;
00005 import java.io.File;
00006 import java.io.FileNotFoundException;
00007 import java.io.FileReader;
00008 import java.io.FileWriter;
00009 import java.io.IOException;
00010 import java.util.HashMap;
00011 import java.util.Iterator;
00012 import java.util.Vector;
00013 
00014 import org.eclipse.jface.dialogs.Dialog;
00015 import org.eclipse.jface.dialogs.IDialogConstants;
00016 import org.eclipse.jface.dialogs.MessageDialog;
00017 import org.eclipse.jface.viewers.ArrayContentProvider;
00018 import org.eclipse.jface.viewers.CellEditor;
00019 import org.eclipse.jface.viewers.ComboBoxCellEditor;
00020 import org.eclipse.jface.viewers.DialogCellEditor;
00021 import org.eclipse.jface.viewers.ICellModifier;
00022 import org.eclipse.jface.viewers.ILabelProviderListener;
00023 import org.eclipse.jface.viewers.ITableLabelProvider;
00024 import org.eclipse.jface.viewers.TableViewer;
00025 import org.eclipse.jface.viewers.TextCellEditor;
00026 import org.eclipse.swt.SWT;
00027 import org.eclipse.swt.custom.PopupList;
00028 import org.eclipse.swt.custom.StyledText;
00029 import org.eclipse.swt.dnd.DND;
00030 import org.eclipse.swt.dnd.DragSource;
00031 import org.eclipse.swt.dnd.DragSourceEvent;
00032 import org.eclipse.swt.dnd.DragSourceListener;
00033 import org.eclipse.swt.dnd.DropTarget;
00034 import org.eclipse.swt.dnd.DropTargetEvent;
00035 import org.eclipse.swt.dnd.DropTargetListener;
00036 import org.eclipse.swt.dnd.TextTransfer;
00037 import org.eclipse.swt.dnd.Transfer;
00038 import org.eclipse.swt.events.ModifyEvent;
00039 import org.eclipse.swt.events.ModifyListener;
00040 import org.eclipse.swt.events.MouseEvent;
00041 import org.eclipse.swt.events.MouseListener;
00042 import org.eclipse.swt.events.SelectionEvent;
00043 import org.eclipse.swt.events.SelectionListener;
00044 import org.eclipse.swt.graphics.Color;
00045 import org.eclipse.swt.graphics.Image;
00046 import org.eclipse.swt.graphics.Rectangle;
00047 import org.eclipse.swt.layout.FillLayout;
00048 import org.eclipse.swt.layout.GridData;
00049 import org.eclipse.swt.layout.GridLayout;
00050 import org.eclipse.swt.layout.RowLayout;
00051 import org.eclipse.swt.widgets.Button;
00052 import org.eclipse.swt.widgets.Composite;
00053 import org.eclipse.swt.widgets.Control;
00054 import org.eclipse.swt.widgets.FileDialog;
00055 import org.eclipse.swt.widgets.Item;
00056 import org.eclipse.swt.widgets.Label;
00057 import org.eclipse.swt.widgets.Shell;
00058 import org.eclipse.swt.widgets.Table;
00059 import org.eclipse.swt.widgets.TableColumn;
00060 import org.eclipse.swt.widgets.Text;
00061 
00062 import com.generalrobotix.ui.grxui.Activator;
00063 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00064 import com.generalrobotix.ui.item.GrxLinkItem;
00065 import com.generalrobotix.ui.item.GrxModelItem;
00066 import com.generalrobotix.ui.item.GrxSensorItem;
00067 import com.generalrobotix.ui.util.GrxDebugUtil;
00068 import com.generalrobotix.ui.util.MessageBundle;
00069 
00070 public class ControllerBridgePanel extends Dialog{
00071         private Vector<String> controllerBridgeFile_ = new Vector<String>();
00072         private String fileName_;
00073         private String rtcConfFileName_;
00074         private String controllerName_;
00075         private String projectControllerName_;
00076         private String robotName_;
00077         private GrxModelItem robot_;
00078         private String robotRtcName_;
00079         private Vector<Inport> inport_ = new Vector<Inport>();
00080         private Vector<Outport> outport_ = new Vector<Outport>();
00081         private HashMap<String, String> connection_ = new HashMap<String, String>();
00082         private NameServer nameServer_ = new NameServer();
00083         private Vector<Port> portTable_ = new Vector<Port>();
00084         private boolean useConfigFile_;
00085         private String configFileName_;   //Relative path//
00086         private StringBuffer bridgeConfFile_ = new StringBuffer();
00087         private double controlTime_ = 0;
00088         private enum DataTypeId {JOINT_VALUE, JOINT_VELOCITY, JOINT_ACCELERATION, JOINT_TORQUE, EXTERNAL_FORCE,
00089                                   ABS_TRANSFORM, ABS_VELOCITY, ABS_ACCELERATION, FORCE_SENSOR, RATE_GYRO_SENSOR, ACCELERATION_SENSOR,
00090                                   RANGE_SENSOR, CONSTRAINT_FORCE, COLOR_IMAGE, GRAYSCALE_IMAGE, DEPTH_IMAGE;
00091                                           public static enum NUMID_CODE {INVALID_TYPE, NOT_USED, USED, ONLY_ONE, NUMBER}; 
00092                                           public static String[] getNames(){
00093                                           String[] s = new String[values().length];
00094                                           int i=0;
00095                                           for(DataTypeId id : values()){
00096                                                   s[i++] = id.name();
00097                                           }
00098                                                 return s;
00099                                   }
00100                                   public static boolean isInport(String type){
00101                                           if(DataTypeId.valueOf(type).compareTo(DataTypeId.ABS_ACCELERATION) > 0 )
00102                                                   return false;
00103                                           else
00104                                                   return true;
00105                                   }
00106                                   public static boolean isOutport(String type){
00107                                           if(DataTypeId.valueOf(type).compareTo(DataTypeId.EXTERNAL_FORCE) == 0 )
00108                                                   return false;
00109                                           else
00110                                                   return true;
00111                                   }
00112                                   public static NUMID_CODE numId(String type){
00113                                           int ordinal = DataTypeId.valueOf(type).ordinal();
00114                                           if(ordinal <= 3 || (8<=ordinal && ordinal <=11) )
00115                                                   return NUMID_CODE.NOT_USED;
00116                                           if(4<=ordinal && ordinal<=7)
00117                                                   return NUMID_CODE.USED;
00118                                           if(ordinal==12)
00119                                                   return NUMID_CODE.ONLY_ONE;
00120                                           if(13<=ordinal && ordinal<=15)
00121                                                   return NUMID_CODE.NUMBER;
00122                                                 return NUMID_CODE.INVALID_TYPE;
00123                                   }
00124         }
00125         private String errorMessage_;
00126         private Vector<String> rtcConfFile_ = new Vector<String>();
00127         private NameServer rtcNameServer_ = new NameServer();
00128         private String moduleName_;
00129         private String controllerRtcName_;
00130         private String endChar_ = "";
00131         private String commentChar_ = "";
00132         private int commandLineStart_=0;
00133         private int commandLineEnd_=0;
00134         
00135         private StyledText cbtext_;
00136         private StyledText rctext_;
00137         private StyledText bctext_;
00138         private Text rrnText_;
00139         private Text nshText_;
00140         private Text nspText_;
00141         private TableViewer tableviewer_;
00142         private StyledText emText_;
00143         private Button saveButton_ ;
00144         private Button saveAsButton_ ; 
00145         private Label fileNameLabel_;
00146         private Label rtcConfFileNameLabel_;
00147         private Label bridgeConfFileNameLabel_;
00148         
00149         private int os_;
00150         private final int LINUX = 0;
00151         private final int WINDOWS = 1;
00152         
00153         ControllerBridgePanel(Shell shell) {
00154                 super(shell);
00155                 if(System.getProperty("os.name").equals("Linux") || System.getProperty("os.name").equals("Mac OS X")){
00156                         os_ = LINUX;
00157                         endChar_ = "\\";
00158                         commentChar_ = "#";
00159                 }else{
00160                         endChar_ = "^";
00161                         os_ = WINDOWS;
00162                         commentChar_ = "REM";
00163                 }
00164                 init();
00165         }
00166         
00167         protected void configureShell(Shell newShell) {   
00168         super.configureShell(newShell);
00169         newShell.setText(MessageBundle.get("panel.Bridge.title"));
00170     }
00171         
00172         protected Control createDialogArea(Composite parent) {
00173         Composite composite = (Composite)super.createDialogArea(parent);
00174         composite.setLayout(new GridLayout(1,true));
00175         
00176         Composite panel0 = new Composite(composite,SWT.NONE);
00177         panel0.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00178         panel0.setLayout(new GridLayout(3,false));
00179         Label label = new Label(panel0, SWT.LEFT );
00180         label.setText(MessageBundle.get("panel.Bridge.controllerName")+":");
00181         final Text text = new Text(panel0, SWT.BORDER);
00182         text.setText(controllerName_);
00183         text.addModifyListener(new ModifyListener(){
00184                         public void modifyText(ModifyEvent e) {
00185                                 String newName = text.getText();
00186                                 if(projectControllerName_.equals(newName))
00187                                         text.setForeground(Activator.getDefault().getColor("black"));
00188                                 else
00189                                         text.setForeground(Activator.getDefault().getColor("red"));
00190                                 controllerName_ = newName;
00191                                 rrnTextUpdate();
00192                                 saveButtonEnabled(false);
00193                         }
00194         });
00195         
00196         GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
00197         gridData.horizontalSpan = 2;
00198         text.setLayoutData(gridData);
00199         Label label0 = new Label(panel0, SWT.LEFT );
00200         label0.setText(MessageBundle.get("panel.Bridge.robotName")+":"+robotName_+"            ");
00201         Label label1 = new Label(panel0, SWT.LEFT);
00202         label1.setText(MessageBundle.get("panel.Bridge.robotRtcName")+":");
00203         label1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
00204         rrnText_ = new Text(panel0, SWT.BORDER);
00205         GridData gridData0 = new GridData();
00206         gridData0.horizontalAlignment = GridData.FILL;
00207         gridData0.grabExcessHorizontalSpace = true;
00208         rrnText_.setLayoutData(gridData0);
00209         rrnTextUpdate();
00210         rrnText_.addModifyListener(new ModifyListener(){
00211                         public void modifyText(ModifyEvent e) {
00212                                 if(rrnText_.isFocusControl()){
00213                                         robotRtcName_ = rrnText_.getText();
00214                                         saveButtonEnabled(false);
00215                                 }
00216                         }
00217         });
00218   
00219         tableviewer_ = new TableViewer(composite, SWT.FULL_SELECTION | SWT.BORDER);
00220         Table table = tableviewer_.getTable();
00221         table.setFont(Activator.getDefault().getFont("preference_table"));
00222         table.setLinesVisible(true);
00223         table.setHeaderVisible(true);
00224         TableColumn column = new TableColumn(table,SWT.NONE);
00225         column.setText("Type");
00226         column.setWidth(100);
00227         column = new TableColumn(table,SWT.NONE);
00228         column.setText("name");
00229         column.setWidth(100);
00230         column = new TableColumn(table,SWT.NONE);
00231         column.setText("property");
00232         column.setWidth(100);
00233         column = new TableColumn(table,SWT.NONE);
00234         column.setText("id");
00235         column.setWidth(100);
00236         column = new TableColumn(table,SWT.NONE);
00237         column.setText("output Time");
00238         column.setWidth(100);
00239         column = new TableColumn(table,SWT.NONE);
00240         column.setText("Controller : Port name");
00241         column.setWidth(200);
00242         GridData gridData1 = new GridData(GridData.FILL_VERTICAL);
00243         gridData1.minimumHeight = 200;
00244         tableviewer_.getControl().setLayoutData(gridData1);
00245         tableviewer_.setContentProvider(new ArrayContentProvider());
00246         tableviewer_.setLabelProvider(new InportLabelProvider());
00247         tableviewer_.setInput(portTable_);
00248         tableviewer_.setColumnProperties(new String[] {"type", "name", "property", "id", "time", "portName"});
00249         DialogCellEditor idCellEditor =new DialogCellEditor(tableviewer_.getTable()){
00250                         protected Object openDialogBox(Control cellEditorWindow) {
00251                                 IdCellDialog dialog = new IdCellDialog(cellEditorWindow.getShell());
00252                                 dialog.setId((String)getValue());
00253                         if(dialog.open() == IDialogConstants.OK_ID)
00254                                 setValue(dialog.getId());
00255                         return null;
00256                         }
00257         };
00258         CellEditor[] editors = new CellEditor[] {
00259                         new ComboBoxCellEditor(tableviewer_.getTable(), new String[]{"IN", "OUT" }), 
00260                         new TextCellEditor(tableviewer_.getTable()),
00261                         new ComboBoxCellEditor(tableviewer_.getTable(), DataTypeId.getNames()),
00262                         idCellEditor,
00263                         new TextCellEditor(tableviewer_.getTable()),
00264                         new TextCellEditor(tableviewer_.getTable())      };
00265         tableviewer_.setCellEditors(editors);
00266         tableviewer_.setCellModifier(new TableCellModifier());
00267         tableviewer_.getControl().addMouseListener(new MouseListener(){
00268                         public void mouseDoubleClick(MouseEvent e) {                    
00269                         }
00270                         public void mouseDown(MouseEvent e) {
00271                                 if(e.button!=1){
00272                                         PopupList list = new PopupList(getShell());
00273                                         list.setItems(new String[]{MessageBundle.get("panel.Bridge.add"),
00274                                                         MessageBundle.get("panel.Bridge.delete")});
00275                                         Rectangle rectangle = getShell().getBounds();
00276                                         String s = list.open(new Rectangle(rectangle.x+e.x, rectangle.y+e.y, 100, 100));
00277                                         if(s.equals(MessageBundle.get("panel.Bridge.add"))){
00278                                                 Port _port = new Port();
00279                                                 _port.type_ = "IN";
00280                                                 _port.name_ = "";
00281                                                 _port.propertyName_ = "JOINT_VALUE";
00282                                                 _port.id_ = "All joints";
00283                                                 _port.outputTime_ = "";
00284                                                 _port.controllerPortName_ = "";
00285                                                 portTable_.add(_port);
00286                                                 tableviewer_.refresh();
00287                                                 saveButtonEnabled(false);
00288                                         }else{
00289                                                 int row = tableviewer_.getTable().getSelectionIndex();
00290                                                 portTable_.remove(row);
00291                                                 tableviewer_.refresh();
00292                                                 saveButtonEnabled(false);
00293                                         }
00294                                 }
00295                         }
00296                         public void mouseUp(MouseEvent e) {
00297                         }
00298                 
00299         });
00300         
00301         Composite panel1 = new Composite(composite,SWT.NONE);
00302         panel1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00303         panel1.setLayout(new GridLayout(3,false));
00304         Label label2 = new Label(panel1, SWT.LEFT );
00305         label2.setText(MessageBundle.get("panel.Bridge.nameServer")+": ");
00306         Label label3 = new Label(panel1, SWT.LEFT );
00307         label3.setText(MessageBundle.get("panel.Bridge.host")+": ");
00308         nshText_ = new Text(panel1, SWT.BORDER);
00309         Label dumy = new Label(panel1, SWT.LEFT );
00310         Label label4 = new Label(panel1, SWT.LEFT );
00311         label4.setText(MessageBundle.get("panel.Bridge.port")+": ");
00312         nspText_ = new Text(panel1, SWT.BORDER);
00313         nshText_.setText(nameServer_.host_);
00314         nshText_.setLayoutData(gridData0);
00315         nspText_.setText(nameServer_.port_);
00316         nspText_.setLayoutData(gridData0);
00317         ModifyListener nsModifyListener = new ModifyListener(){
00318                         public void modifyText(ModifyEvent e) {
00319                                 nameServer_.host_ = nshText_.getText();
00320                                 nameServer_.port_ = nspText_.getText();
00321                                 saveButtonEnabled(false);
00322                         }
00323         };
00324         nshText_.addModifyListener(nsModifyListener);
00325         nspText_.addModifyListener(nsModifyListener);
00326         
00327         Composite panel5 = new Composite(composite,SWT.NONE);
00328         panel5.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00329         panel5.setLayout(new GridLayout(4,false));
00330         Label label8 = new Label(panel5, SWT.LEFT );
00331         label8.setText(MessageBundle.get("panel.Bridge.moduleName")+": ");
00332         final Text text4 = new Text(panel5, SWT.BORDER);
00333         text4.setLayoutData(gridData0);
00334         text4.setText(moduleName_);
00335         text4.addModifyListener(new ModifyListener(){
00336                         public void modifyText(ModifyEvent e) {
00337                                 moduleName_ = text4.getText();
00338                                 saveButtonEnabled(false);
00339                         }
00340         });
00341         Label label9 = new Label(panel5, SWT.LEFT );
00342         label9.setText(MessageBundle.get("panel.Bridge.controllerRTCName")+": ");
00343         final Text text5 = new Text(panel5, SWT.BORDER);
00344         text5.setLayoutData(gridData0);
00345         text5.setText(controllerRtcName_);
00346         text5.addModifyListener(new ModifyListener(){
00347                         public void modifyText(ModifyEvent e) {
00348                                 controllerRtcName_ = text5.getText();
00349                                 saveButtonEnabled(false);
00350                         }
00351         });
00352         
00353         Composite panel6 = new Composite(composite,SWT.NONE);
00354         panel6.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00355         panel6.setLayout(new GridLayout(2,false));
00356         final Button configFileButton = new Button(panel6, SWT.CHECK);
00357         configFileButton.setText(MessageBundle.get("panel.Bridge.useConfigFile"));
00358         configFileButton.addSelectionListener(new SelectionListener(){
00359                         public void widgetDefaultSelected(SelectionEvent e) {                   
00360                         }
00361                         public void widgetSelected(SelectionEvent e) {
00362                                 useConfigFile_ = configFileButton.getSelection();
00363                                 saveButtonEnabled(false);
00364                         }
00365         });
00366         if(useConfigFile_)
00367                 configFileButton.setSelection(true);
00368         final Text text6 = new Text(panel6, SWT.BORDER);
00369         text6.setLayoutData(gridData0);
00370         text6.setText(configFileName_);
00371         text6.addModifyListener(new ModifyListener(){
00372                         public void modifyText(ModifyEvent e) {
00373                                 configFileName_ = text6.getText();
00374                                 bridgeConfFileNameLabel_.setText(configFileName_);
00375                                 saveButtonEnabled(false);
00376                         }
00377         });
00378         
00379         Composite panel4 = new Composite(composite,SWT.NONE);
00380         panel4.setLayout(new GridLayout(1,false));
00381         Button button = new Button(panel4, SWT.PUSH);
00382         button.setText(MessageBundle.get("panel.Bridge.check"));
00383         button.addSelectionListener(new SelectionListener(){
00384                         public void widgetDefaultSelected(SelectionEvent e) {
00385                         }
00386                         public void widgetSelected(SelectionEvent e) {
00387                                 errorCheck();
00388                                 updateControllerBridgeFile();
00389                                 cbTextUpdate();
00390                                 updateRtcConfFile();
00391                                 rcTextUpdate();
00392                                 saveButtonEnabled(true);
00393                                 emText_.setText(errorMessage_);
00394                         }
00395         });
00396         fileNameLabel_ = new Label(panel4, SWT.LEFT);
00397         if(fileName_=="")
00398                 fileNameLabel_.setText("new file");
00399         else
00400                 fileNameLabel_.setText(fileName_);
00401         cbtext_ = new StyledText(panel4, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
00402         GridData gridData2 = new GridData();
00403         gridData2.horizontalAlignment = GridData.FILL;
00404         gridData2.grabExcessHorizontalSpace = true;
00405         gridData2.verticalAlignment = GridData.FILL;
00406         gridData2.grabExcessVerticalSpace = true;
00407         gridData2.minimumHeight = 100;
00408         gridData2.widthHint = 700;
00409         cbtext_.setLayoutData(gridData2);
00410         cbtext_.setBackground(Activator.getDefault().getColor("gray"));
00411         cbtext_.setEditable(false);
00412         bridgeConfFileNameLabel_ = new Label(panel4, SWT.LEFT);
00413         bridgeConfFileNameLabel_.setLayoutData(gridData0);
00414         bridgeConfFileNameLabel_.setText(configFileName_);
00415         bctext_ = new StyledText(panel4, SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
00416         GridData gridData5 = new GridData();
00417         gridData5.horizontalAlignment = GridData.FILL;
00418         gridData5.grabExcessHorizontalSpace = true;
00419         gridData5.verticalAlignment = GridData.FILL;
00420         gridData5.grabExcessVerticalSpace = true;
00421         gridData5.minimumHeight = 100;
00422         gridData5.widthHint = 700;
00423         bctext_.setLayoutData(gridData5);
00424         bctext_.setBackground(Activator.getDefault().getColor("gray"));
00425         bctext_.setEditable(false);
00426         cbTextUpdate();
00427         
00428         Composite panel3 = new Composite(composite,SWT.NONE);
00429         panel3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00430         panel3.setLayout(new GridLayout(1,false));
00431         rtcConfFileNameLabel_ = new Label(panel3, SWT.LEFT);
00432         if(rtcConfFileName_.equals(""))
00433                 rtcConfFileNameLabel_.setText("rtc.conf");
00434         else
00435                 rtcConfFileNameLabel_.setText(rtcConfFileName_);
00436         rctext_ = new StyledText(panel3,  SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL );
00437         GridData gridData4 = new GridData();
00438         gridData4.horizontalAlignment = GridData.FILL;
00439         gridData4.grabExcessHorizontalSpace = true;
00440         gridData4.verticalAlignment = GridData.FILL;
00441         gridData4.grabExcessVerticalSpace = true;
00442         gridData4.widthHint = 700;
00443         gridData4.minimumHeight = 50;
00444         rctext_.setLayoutData(gridData4);
00445         rctext_.setBackground(Activator.getDefault().getColor("gray"));
00446         rcTextUpdate();
00447         rctext_.setEditable(false);
00448         
00449         emText_ = new StyledText(panel3, SWT.NONE );
00450         GridData gridData3 = new GridData();
00451         gridData3.horizontalAlignment = GridData.FILL;
00452         gridData3.grabExcessHorizontalSpace = true;
00453         gridData3.verticalAlignment = GridData.FILL;
00454         gridData3.grabExcessVerticalSpace = true;
00455         gridData3.minimumHeight = 50;
00456         emText_.setLayoutData(gridData3);
00457         emText_.setBackground(panel3.getBackground());
00458         emText_.setEditable(false);
00459         emText_.setText(errorMessage_);
00460         emText_.setForeground(Activator.getDefault().getColor("red"));
00461         
00462                 return composite;
00463         }
00464         
00465         private final int SAVE_ID = IDialogConstants.CLIENT_ID;
00466         private final int SAVE_AS_ID = IDialogConstants.CLIENT_ID+1;
00467         protected void createButtonsForButtonBar(Composite parent) {
00468                 saveButton_ = createButton(parent, SAVE_ID, MessageBundle.get("GrxTextItem.menu.save"), false);
00469                 saveAsButton_ = createButton(parent, SAVE_AS_ID, MessageBundle.get("GrxTextItem.menu.saveAs"),  false);
00470                 saveButtonEnabled(false);
00471                 createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, false);           
00472         }
00473         
00474         protected void buttonPressed(int buttonId) {
00475         if (buttonId == SAVE_ID) {
00476                 save();
00477         }else if(buttonId == SAVE_AS_ID){
00478                 if(setFileName())
00479                         save();
00480         }else if(buttonId == IDialogConstants.CLOSE_ID){
00481                 close();
00482         }
00483         }
00484         
00485         private class InportLabelProvider implements ITableLabelProvider{
00486                 public Image getColumnImage(Object element, int columnIndex) {
00487                         return null;
00488                 }
00489                 public String getColumnText(Object element, int columnIndex) {
00490                         Port node = (Port)element;
00491                         switch(columnIndex){
00492                         case 0:
00493                                 return node.type_;
00494                         case 1:
00495                                 return node.name_;
00496                         case 2:
00497                                 return node.propertyName_;
00498                         case 3:
00499                                 return node.id_;
00500                         case 4:
00501                                 return node.outputTime_;
00502                         case 5:
00503                                 if(!node.controllerPortName_.equals(""))
00504                                         return node.type_.equals("IN") ? "<-- "+node.controllerPortName_ : "--> "+node.controllerPortName_;
00505                         }
00506                         return null;
00507                 }
00508                 public void addListener(ILabelProviderListener listener) {              
00509                 }
00510                 public void dispose() {
00511                 }
00512                 public boolean isLabelProperty(Object element, String property) {
00513                         return false;
00514                 }
00515                 public void removeListener(ILabelProviderListener listener) {                   
00516                 }       
00517         }
00518         
00519         private class TableCellModifier implements ICellModifier {
00520                 public boolean canModify(Object element, String property) {
00521                         return true;
00522                 }
00523                 
00524                 public Object getValue(Object element, String property) {
00525                         Port port = (Port)element;
00526                         if(property.equals("type"))
00527                                 return port.type_.equals("IN") ? 0 : 1 ;
00528                         else if(property.equals("name"))
00529                                 return port.name_;
00530                         else if(property.equals("property")){
00531                                 return DataTypeId.valueOf(port.propertyName_).ordinal();
00532                         }else if(property.equals("id"))
00533                                 return port.id_;
00534                         else if(property.equals("time"))
00535                                 return port.outputTime_;
00536                         else if(property.equals("portName"))
00537                                 return port.controllerPortName_;
00538                         return null;
00539                 }
00540                 
00541                 public void modify(Object element, String property, Object value) {
00542                         if (element instanceof Item) {
00543                               element = ((Item) element).getData();
00544                         }
00545                         Port port = (Port)element;
00546                         if(property.equals("type"))
00547                                 port.type_ = (Integer)value==0 ? "IN" : "OUT";
00548                         else if(property.equals("name"))
00549                                 port.name_ = (String)value;
00550                         else if(property.equals("property")){
00551                                 port.propertyName_ = DataTypeId.values()[(Integer)value].name();
00552                         }else if(property.equals("id"))
00553                                         port.id_ = (String)value;
00554                         else if(property.equals("time"))
00555                                 port.outputTime_ = (String)value;
00556                         else if(property.equals("portName"))
00557                                 port.controllerPortName_ = (String)value;
00558                         saveButtonEnabled(false);
00559                         tableviewer_.update(element, null);
00560                 }
00561         }
00562         
00563         public boolean load(String fileName){
00564                 File file = new File(fileName);
00565                 if (file == null || !file.isFile())
00566                         return false;
00567                 clear();
00568                 fileName_ = fileName;
00569                 try {
00570                         BufferedReader reader = new BufferedReader(new FileReader(file));
00571                         String option="";
00572                         boolean comline = false;
00573                         int i=0;
00574                         while (reader.ready()) {
00575                                 String string = reader.readLine();
00576                                 if(string.startsWith(commentChar_))
00577                                         continue;
00578                                 controllerBridgeFile_.add(string);
00579                                 int index = string.indexOf("openhrp-controller-bridge");
00580                                 if( index != -1 ){
00581                                         comline = true;
00582                                         string = string.substring(index+25).trim();
00583                                         commandLineStart_ = i;
00584                                 }
00585                                 if(comline){
00586                                         string = string.trim();
00587                                         if(string.endsWith(endChar_)){
00588                                                 option += string.substring(0,string.length()-1);
00589                                         }
00590                                         else{
00591                                                 option += string;
00592                                                 commandLineEnd_ =i;
00593                                                 comline = false;
00594                                         }
00595                                 }
00596                                 i++;
00597                         }
00598                         if(!option.equals("")){
00599                                 String[] options = option.split("--");
00600                                 rtcConfFileName_ = file.getCanonicalFile().getParent()+File.separator+"rtc.conf";
00601                                 boolean ret = loadRtcConf(rtcConfFileName_);
00602                                 parse(options);
00603                                 createPortTable();
00604                                 if(!ret){
00605                                         rtcConfFileName_="";
00606                                         rtcConfInit(nameServer_.host_, nameServer_.port_);
00607                                 }
00608                         }else
00609                                 ;// no command line
00610                 } catch (FileNotFoundException e) {
00611                         GrxDebugUtil.println("File Not Found. ("+file.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
00612                         return false;
00613                 } catch (IOException e) {
00614                         e.printStackTrace();
00615                         return false;
00616                 }
00617                 return true;
00618         }
00619 
00620         private boolean loadRtcConf(String fileName){
00621                 File file = new File(fileName);
00622                 if (file == null || !file.isFile())
00623                         return false;
00624                 try {
00625                         BufferedReader reader = new BufferedReader(new FileReader(file));
00626                         while (reader.ready()) {
00627                                 String string = reader.readLine();
00628                                 if(string.startsWith("#"))
00629                                         continue;
00630                                 rtcConfFile_.add(string);
00631                                 String string0 = string.trim();
00632                                 if(string0.startsWith("corba.nameservers:")){
00633                                         String[] s=string0.substring(18).trim().split(":");
00634                                         if(s.length == 2){
00635                                                 rtcNameServer_.host_ = s[0];
00636                                                 rtcNameServer_.port_ = s[1];
00637                                         }
00638                                 }else if(string.trim().startsWith("manager.modules.preload:"))
00639                                         moduleName_ = string0.substring(24).trim();
00640                                 else if(string.trim().startsWith("manager.components.precreate:"))
00641                                         controllerRtcName_ = string0.substring(29).trim();
00642                                 //else if(string.trim().startsWith("exec_cxt.periodic.type"))
00643                                 //      ;
00644                         }
00645                 } catch (FileNotFoundException e) {
00646                         GrxDebugUtil.println("File Not Found. ("+file.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
00647                         return false;
00648                 } catch (IOException e) {
00649                         e.printStackTrace();
00650                         return false;
00651                 }
00652                 return true;
00653         }
00654         
00655         private void init(){
00656                 fileName_ = "";
00657                 rtcConfFileName_ = "";
00658                 controllerBridgeFile_.clear();
00659                 if(os_==LINUX){
00660                         controllerBridgeFile_.add("#!/bin/sh");
00661                         commandLineStart_ = 1;
00662                         commandLineEnd_ = 1;
00663                 }else{
00664                         commandLineStart_ = 0;
00665                         commandLineEnd_ = 0;
00666                 }
00667                 controllerBridgeFile_.add("openhrp-controller-bridge "+endChar_);
00668                 
00669                 portTable_.clear();
00670                 controllerName_ = "";
00671                 robotRtcName_ = "";
00672                 inport_.clear();
00673                 outport_.clear();
00674                 connection_.clear();
00675                 nameServer_.host_ = "127.0.0.1";
00676                 nameServer_.port_ = "2809";
00677                 errorMessage_ = "";
00678                 useConfigFile_ = false;
00679                 configFileName_ = "";
00680                 rtcConfInit(nameServer_.host_, nameServer_.port_);
00681         }
00682         
00683         private void rtcConfInit(String host, String port){
00684                 rtcConfFile_.clear();
00685                 rtcConfFile_.add("corba.nameservers: "+host+":"+port);
00686                 rtcConfFile_.add("naming.formats: %n.rtc");
00687                 rtcConfFile_.add("logger.log_level: TRACE");
00688                 rtcConfFile_.add("exec_cxt.periodic.type: SynchExtTriggerEC");
00689                 rtcConfFile_.add("exec_cxt.periodic.rate: 1000000");
00690                 rtcConfFile_.add("manager.modules.load_path: .");
00691                 rtcConfFile_.add("manager.modules.abs_path_allowed: yes");
00692                 rtcConfFile_.add("manager.modules.preload: ");
00693                 rtcConfFile_.add("manager.components.precreate: ");
00694                 rtcNameServer_.host_ = host;
00695                 rtcNameServer_.port_ = port;
00696                 moduleName_ = "";
00697                 controllerRtcName_ = "";
00698 
00699         }
00700         
00701         private void clear(){
00702                 fileName_ = "";
00703                 controllerBridgeFile_.clear();
00704                 portTable_.clear();
00705                 controllerName_ = "";
00706                 robotRtcName_ = "";
00707                 inport_.clear();
00708                 outport_.clear();
00709                 connection_.clear();
00710                 nameServer_.host_ = "127.0.0.1";
00711                 nameServer_.port_ = "2809";
00712                 errorMessage_ = "";
00713                 
00714                 rtcConfFile_.clear();
00715                 rtcNameServer_.host_ = "";
00716                 rtcNameServer_.port_ = "";
00717                 moduleName_ = "";
00718                 controllerRtcName_ = "";
00719                 useConfigFile_ = false;
00720                 configFileName_ = "";
00721         }
00722         
00723         private void parse(String[] options){
00724                 for(int i=0; i<options.length; i++){
00725                         options[i] = options[i].trim();
00726                         int endIndex = options[i].indexOf(" ");
00727                         if(endIndex == -1)
00728                                 continue;
00729                         String option = options[i].substring(0,endIndex);
00730                         String parameter = options[i].substring(endIndex).trim();
00731                         if(option.equals("server-name")){
00732                                 controllerName_ = parameter;
00733                         }else if(option.equals("in-port")){
00734                                 Inport _inport = new Inport();
00735                                 String[] s = parameter.split(":");
00736                                 if(s.length==2){
00737                                         _inport.name_ = s[0].trim();
00738                                         _inport.propertyName_ = s[1].trim();
00739                                         _inport.id_ = null;
00740                                 }else if(s.length==3){
00741                                         _inport.name_ = s[0].trim();
00742                                         _inport.propertyName_ = s[2].trim();
00743                                         _inport.id_ = s[1].split(",");
00744                                         for(int j=0; j<_inport.id_.length; j++)
00745                                                 _inport.id_[j] = _inport.id_[j].trim();
00746                                 }else
00747                                         continue;
00748                                 inport_.add(_inport);
00749                                 
00750                         }else if(option.equals("out-port")){
00751                                 Outport _outport = new Outport();
00752                                 String[] s = parameter.split(":");
00753                                 _outport.name_ = s[0].trim();
00754                                 _outport.idName_ = null;
00755                             int j;
00756                             for(j=1; j<3; j++){
00757                                 try{
00758                                         DataTypeId type = DataTypeId.valueOf(s[j].trim());
00759                                         // プロパティ名 //
00760                                         _outport.propertyName_ = s[j].trim();
00761                                         j++;
00762                                         break;
00763                                 }catch(IllegalArgumentException e){
00764                                          // プロパティ名でないので識別名  //
00765                                     if(j==2)    // 3番目までにプロパティ名がないのでエラー //
00766                                         break;
00767                                     String[] _id = s[j].split(",");
00768                                     _outport.idName_ = new String[_id.length];
00769                                     for(int k=0; k<_id.length; k++){
00770                                         try{
00771                                                 _outport.idNo_ = Integer.parseInt(_id[k]);
00772                                                 _outport.idName_[k] = "";
00773                                         }catch(NumberFormatException ex){
00774                                                 _outport.idName_[k] = _id[k].trim();
00775                                                 _outport.idNo_ = -1;
00776                                         }
00777                                     }
00778                                 }
00779                             }
00780                             if(j<s.length){    // まだパラメターがあるならサンプリング時間 //
00781                                 _outport.outputTime_ = Double.parseDouble(s[j]);
00782                             }else{
00783                                 _outport.outputTime_ = -1;
00784                             }
00785                                 outport_.add(_outport);
00786                         }else if(option.equals("connection")){
00787                                 String[] s = parameter.split(":");
00788                                 String controllerPortName="";
00789                                 if(s.length == 2){
00790                                         controllerPortName = controllerRtcName_+"0:"+s[1].trim();
00791                                 }else if(s.length ==3){
00792                                         controllerPortName = s[1].trim()+":"+s[2].trim();
00793                                 }
00794                                 connection_.put(s[0].trim(), controllerPortName);
00795                         }else if(option.equals("robot-name")){
00796                                 robotRtcName_ = parameter;
00797                         }else if(option.equals("name-server")){
00798                                 String[] s = parameter.split(":");
00799                                 nameServer_.host_ = s[0].trim();
00800                                 nameServer_.port_ = s[1].trim();
00801                         }else if(option.equals("config-file")){
00802                                 loadConfigFile(parameter);
00803                                 useConfigFile_ = true;
00804                                 configFileName_ = parameter;
00805                         }
00806                 }
00807         }
00808         
00809         private void loadConfigFile(String fileName){
00810                 File file = new File(fileName_);
00811                 try {
00812                         fileName = file.getCanonicalFile().getParent()+File.separator+fileName;
00813                 } catch (IOException e) {
00814                         e.printStackTrace();
00815                 }
00816                 file = new File(fileName);
00817                 if (file == null || !file.isFile())
00818                         return;
00819                 try {
00820                         BufferedReader reader = new BufferedReader(new FileReader(file));
00821                         Vector<String> options = new Vector<String>();
00822                         while (reader.ready()) {
00823                                 String string = reader.readLine();
00824                                 if(string.startsWith("#"))
00825                                         continue;
00826                                 bridgeConfFile_.append(string+"\n");
00827                                 String[] s = string.split("=");
00828                                 if(s.length==2)
00829                                         options.add(s[0]+" "+s[1]);
00830                         }
00831                         parse(options.toArray(new String[0]));
00832                 
00833                 } catch (FileNotFoundException e) {
00834                         GrxDebugUtil.println("File Not Found. ("+file.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
00835                         return;
00836                 } catch (IOException e) {
00837                         e.printStackTrace();
00838                         return;
00839                 }
00840                 return;
00841         }
00842         
00843         private void createPortTable() {
00844                 Iterator<Inport> it = inport_.iterator();
00845                 while(it.hasNext()){
00846                         Inport inport = it.next();
00847                         Port _port = new Port();
00848                         _port.type_ = "IN";
00849                         _port.name_ = inport.name_;
00850                         _port.propertyName_ = inport.propertyName_;
00851                         if(inport.id_ == null)
00852                                 _port.id_ = "All joints";
00853                         else{
00854                                 _port.id_ = "";
00855                                 for(int i=0; i<inport.id_.length-1; i++)
00856                                         _port.id_ += inport.id_[i] + ",";
00857                                 _port.id_ += inport.id_[inport.id_.length-1];
00858                         }
00859                         _port.outputTime_ = "";
00860                         String controllerPort = connection_.get(inport.name_);
00861                         if(controllerPort==null)
00862                                 controllerPort = "";
00863                         _port.controllerPortName_ = controllerPort;
00864                         portTable_.add(_port);
00865                 }
00866                 Iterator<Outport> it0 = outport_.iterator();
00867                 while(it0.hasNext()){
00868                         Outport outport = it0.next();
00869                         Port _port = new Port();
00870                         _port.type_ = "OUT";
00871                         _port.name_ = outport.name_;
00872                         _port.propertyName_ = outport.propertyName_;
00873                         if(outport.idName_ == null)
00874                                 _port.id_ = "All joints";
00875                         else if(outport.idNo_ != -1){
00876                                 _port.id_ = String.valueOf(outport.idNo_);
00877                         }else{
00878                                 _port.id_ = "";
00879                                 for(int i=0; i<outport.idName_.length-1; i++)
00880                                         _port.id_ += outport.idName_[i] + ",";
00881                                 _port.id_ += outport.idName_[outport.idName_.length-1];
00882                         }
00883                         if(outport.outputTime_<0)
00884                                 _port.outputTime_ = "control Time";
00885                         else
00886                                 _port.outputTime_ = String.valueOf(outport.outputTime_);
00887                         String controllerPort = connection_.get(outport.name_);
00888                         if(controllerPort==null)
00889                                 controllerPort = "";
00890                         _port.controllerPortName_ = controllerPort;
00891                         portTable_.add(_port);
00892                 }
00893         }
00894 
00895         private class Inport {
00896                 public String name_;
00897                 public String propertyName_;
00898                 public String[] id_;
00899         }
00900         
00901         private class Outport {
00902                 public String name_;
00903                 public String propertyName_;
00904                 public String[] idName_;
00905                 public int idNo_;
00906                 public double outputTime_;
00907         }
00908 
00909         private class Port {
00910                 public String type_;
00911                 public String name_;
00912                 public String propertyName_;
00913                 public String id_;
00914                 public String outputTime_;
00915                 public String controllerPortName_;
00916         }
00917         
00918         private class NameServer {
00919                 public String host_;
00920                 public String port_;
00921         }
00922 
00923         public void setRobot(GrxModelItem robot) {
00924                 robot_ = robot;
00925                 robotName_ = robot_.getName();
00926         }
00927 
00928         public boolean setProjectControllerName(String _controllerName) {
00929                 projectControllerName_ = _controllerName;
00930                 if(projectControllerName_.equals(controllerName_))
00931                         return true;
00932                 else
00933                         return false;
00934         }
00935         
00936         public void setControllerName(String _controllerName) {
00937                 controllerName_ = _controllerName;
00938         }
00939         
00940         public boolean checkNameServer(){
00941                 if(nameServer_.host_.equals(rtcNameServer_.host_) && nameServer_.port_.equals(rtcNameServer_.port_))
00942                         return true;
00943                 else
00944                         return false;
00945         }
00946         
00947         public void setNameServer(){
00948                 nameServer_.host_ = rtcNameServer_.host_;
00949                 nameServer_.port_ = rtcNameServer_.port_;
00950         }
00951         
00952         public void save(){
00953                 if(fileName_.equals(""))
00954                         if(!setFileName())
00955                                 return;
00956                 File file = new File(fileName_);
00957                 if(rtcConfFileName_.equals("")){
00958                         try {
00959                                 rtcConfFileName_ = file.getCanonicalFile().getParent()+File.separator+"rtc.conf";
00960                         } catch (IOException e) {
00961                                 e.printStackTrace();
00962                         }
00963                 }
00964                 String bridgeConfFileName=null;
00965                 if(useConfigFile_){
00966                         try {
00967                                 bridgeConfFileName = file.getCanonicalFile().getParent()+File.separator+configFileName_;
00968                         } catch (IOException e) {
00969                                 e.printStackTrace();
00970                         }
00971                 }
00972                 try {
00973                         BufferedWriter writer = new BufferedWriter(new FileWriter(fileName_));
00974                         Iterator<String> it = controllerBridgeFile_.iterator();
00975                         while(it.hasNext()){
00976                                 writer.write(it.next());
00977                                 writer.newLine();
00978                         }
00979                         writer.flush();
00980                         writer.close();
00981                         
00982                         writer = new BufferedWriter(new FileWriter(rtcConfFileName_));
00983                         it = rtcConfFile_.iterator();
00984                         while(it.hasNext()){
00985                                 writer.write(it.next());
00986                                 writer.newLine();
00987                         }
00988                         writer.flush();
00989                         writer.close();
00990                         
00991                         if(useConfigFile_){
00992                                 writer = new BufferedWriter(new FileWriter(bridgeConfFileName));
00993                                 writer.write(bridgeConfFile_.toString());
00994                                 writer.flush();
00995                                 writer.close();
00996                         }
00997                 } catch (IOException e) {
00998                         e.printStackTrace();
00999                 }
01000                 fileNameLabel_.setText(fileName_);
01001                 rtcConfFileNameLabel_.setText(rtcConfFileName_);
01002                 
01003         }
01004         
01005         private boolean setFileName(){
01006                 FileDialog fdlg = new FileDialog( GrxUIPerspectiveFactory.getCurrentShell(), SWT.SAVE);
01007                 String fileName = fdlg.open();
01008                 if( fileName != null ) {
01009                         fileName_ = fileName;
01010                         File file = new File(fileName_);
01011                         try {
01012                                 rtcConfFileName_ = file.getCanonicalFile().getParent()+File.separator+"rtc.conf";
01013                                 file = new File(rtcConfFileName_);
01014                                 if(file.exists()){
01015                                         if(!MessageDialog.openQuestion(getShell(), rtcConfFileName_, MessageBundle.get("Grx3DView.dialog.message.fileExist"))){
01016                                                 fdlg.setFileName(rtcConfFileName_);
01017                                                 rtcConfFileName_ = fdlg.open();
01018                                                 if(rtcConfFileName_==null)
01019                                                         return false;
01020                                         }
01021                                 }
01022                         } catch (IOException e) {
01023                                 e.printStackTrace();
01024                         }
01025                         return true;
01026                 }
01027                 return false;
01028         }
01029         
01030         private void updateControllerBridgeFile(){
01031                 String commandLine = controllerBridgeFile_.get(commandLineStart_);
01032                 int index = commandLine.indexOf("openhrp-controller-bridge");
01033                 if( index != -1 ){
01034                         commandLine = commandLine.substring(0,index+25)+" ";
01035                 }
01036                 for(int i=commandLineEnd_; i>=commandLineStart_; i--)
01037                         controllerBridgeFile_.remove(i);
01038                 bridgeConfFile_ = new StringBuffer();
01039                 
01040                 if(useConfigFile_){
01041                         controllerBridgeFile_.insertElementAt( commandLine+"--config-file "+configFileName_,commandLineStart_);
01042                         commandLineEnd_ = commandLineStart_;
01043                         updateBridgeConfFile();
01044                 }else{
01045                         int i = commandLineStart_;
01046                         controllerBridgeFile_.insertElementAt(commandLine+endChar_, i++);
01047                         if(!controllerName_.equals("")){
01048                                 String string = "--server-name "+controllerName_ +" "+endChar_;
01049                                 controllerBridgeFile_.insertElementAt(string, i++);
01050                         }
01051                         Iterator<Port> it = portTable_.iterator();
01052                         while(it.hasNext()){
01053                                 String string="";
01054                                 Port port = it.next();
01055                                 if(port.type_.equals("IN")){
01056                                         string += "--in-port "+port.name_;
01057                                         if(!port.id_.equals("All joints") && !port.id_.equals(""))
01058                                                 string += ":"+port.id_;
01059                                         string += ":"+port.propertyName_;
01060                                 }else{
01061                                         string += "--out-port "+port.name_;
01062                                         if(!port.id_.equals("All joints") && !port.id_.equals(""))
01063                                                 string += ":"+port.id_;
01064                                         string += ":"+port.propertyName_;
01065                                         if(!port.outputTime_.equals("control Time"))
01066                                                 string += ":"+port.outputTime_;
01067                                 }
01068                                 string += " "+endChar_;
01069                                 controllerBridgeFile_.insertElementAt(string, i++);
01070                                 if(!port.controllerPortName_.equals("")){
01071                                         String[] s = port.controllerPortName_.split(":");
01072                                         String controllerPortName = port.controllerPortName_;
01073                                         if(s.length==2 && s[0].equals(controllerRtcName_+"0"))
01074                                                 controllerPortName = s[1];
01075                                         string = "--connection "+port.name_+":"+controllerPortName+" "+endChar_;
01076                                         controllerBridgeFile_.insertElementAt(string, i++);
01077                                 }
01078                         }
01079                         if(!robotRtcName_.equals("")){
01080                                 String string = "--robot-name "+robotRtcName_ +" "+endChar_;
01081                                 controllerBridgeFile_.insertElementAt(string, i++);
01082                         }
01083                         if(!nameServer_.host_.equals("127.0.0.1") || !nameServer_.port_.equals("2809")){
01084                                 String string = "--name-server "+nameServer_.host_+":"+nameServer_.port_+" " +endChar_;
01085                                 controllerBridgeFile_.insertElementAt(string, i++);
01086                         }
01087         
01088                         commandLineEnd_ = i-1;
01089                         String string = controllerBridgeFile_.get(commandLineEnd_);
01090                         string = string.substring(0, string.length()-1);
01091                         controllerBridgeFile_.setElementAt(string, commandLineEnd_);
01092                 }
01093         }
01094         
01095         private void updateBridgeConfFile(){
01096                 String lineSeparator = System.getProperty("line.separator");
01097                 if(!controllerName_.equals("")){
01098                         bridgeConfFile_.append("server-name = "+controllerName_ +lineSeparator);
01099                 }
01100                 Iterator<Port> it = portTable_.iterator();
01101                 while(it.hasNext()){
01102                         String string="";
01103                         Port port = it.next();
01104                         if(port.type_.equals("IN")){
01105                                 string += "in-port = "+port.name_;
01106                                 if(!port.id_.equals("All joints") && !port.id_.equals(""))
01107                                         string += ":"+port.id_;
01108                                 string += ":"+port.propertyName_;
01109                         }else{
01110                                 string += "out-port = "+port.name_;
01111                                 if(!port.id_.equals("All joints") && !port.id_.equals(""))
01112                                         string += ":"+port.id_;
01113                                 string += ":"+port.propertyName_;
01114                                 if(!port.outputTime_.equals("control Time"))
01115                                         string += ":"+port.outputTime_;
01116                         }
01117                         bridgeConfFile_.append(string+lineSeparator);
01118                         if(!port.controllerPortName_.equals("")){
01119                                 String[] s = port.controllerPortName_.split(":");
01120                                 String controllerPortName = port.controllerPortName_;
01121                                 if(s.length==2 && s[0].equals(controllerRtcName_+"0"))
01122                                         controllerPortName = s[1];
01123                                 string = "connection = "+port.name_+":"+controllerPortName+lineSeparator;
01124                                 bridgeConfFile_.append(string);
01125                         }
01126                 }
01127                 if(!robotRtcName_.equals("")){
01128                         String string = "robot-name = "+robotRtcName_ +lineSeparator;
01129                         bridgeConfFile_.append(string);
01130                 }
01131                 if(!nameServer_.host_.equals("127.0.0.1") || !nameServer_.port_.equals("2809")){
01132                         String string = "name-server = "+nameServer_.host_+":"+nameServer_.port_+lineSeparator;
01133                         bridgeConfFile_.append(string);
01134                 }
01135         }
01136         
01137         private void updateRtcConfFile(){
01138                 for(int i=0; i<rtcConfFile_.size(); i++){
01139                         String string = rtcConfFile_.get(i).trim();
01140                         if(string.startsWith("corba.nameservers:")){
01141                                 String s=string.substring(0,18)+" "+ nameServer_.host_+":"+nameServer_.port_;
01142                                 rtcConfFile_.setElementAt(s, i);
01143                         }else if(string.startsWith("manager.modules.preload:")){
01144                                 String s=string.substring(0,24)+" "+ moduleName_;
01145                                 rtcConfFile_.setElementAt(s, i);
01146                         }else if(string.startsWith("manager.components.precreate:")){
01147                                 String s=string.substring(0,29)+" "+ controllerRtcName_;
01148                                 rtcConfFile_.setElementAt(s, i);
01149                         }
01150                 }
01151         }
01152         
01153         private void cbTextUpdate(){
01154                 String s="";
01155         Iterator<String> it = controllerBridgeFile_.iterator();
01156         while(it.hasNext())
01157                 s += it.next() + "\n";
01158         cbtext_.setText(s);
01159         bctext_.setText(bridgeConfFile_.toString());
01160         }
01161         
01162         private void rcTextUpdate(){
01163                 String s="";
01164         Iterator<String> it = rtcConfFile_.iterator();
01165         while(it.hasNext())
01166                 s += it.next() + "\n";
01167         rctext_.setText(s);
01168         }
01169         
01170         private void rrnTextUpdate(){
01171                 if(robotRtcName_.equals(""))
01172                         rrnText_.setText(controllerName_+"(robot)");
01173                 else
01174                         rrnText_.setText(robotRtcName_);
01175         }
01176 
01177         private void saveButtonEnabled(boolean b){
01178                 saveButton_.setEnabled(b);
01179                 saveAsButton_.setEnabled(b);
01180         }
01181         
01182         private void errorCheck(){
01183                 errorMessage_ = "";
01184                 if(!projectControllerName_.equals(controllerName_))
01185                         errorMessage_ += MessageBundle.get("panel.Bridge.error.controlName")+"\n";
01186                 Iterator<Port> it = portTable_.iterator();
01187                 while(it.hasNext()){
01188                         Port port = it.next();
01189                         if(port.name_.equals(""))
01190                                 errorMessage_ += MessageBundle.get("panel.Bridge.error.setPortName")+"\n";
01191                         DataTypeId.NUMID_CODE numId = DataTypeId.numId(port.propertyName_);
01192                         if(!(numId==DataTypeId.NUMID_CODE.NOT_USED) && (port.id_.equals("All joints") || port.id_.equals("")))
01193                                 errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" "+MessageBundle.get("panel.Bridge.error.useID")+"\n";
01194                         if(numId==DataTypeId.NUMID_CODE.ONLY_ONE){
01195                                 String[] s = port.id_.split(",");
01196                                 if(s.length > 1)
01197                                         errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" "+MessageBundle.get("panel.Bridge.error.oneID")+"\n";
01198                         }
01199                         if(numId==DataTypeId.NUMID_CODE.NUMBER){
01200                                 try{
01201                                         Integer.valueOf(port.id_);
01202                                 }catch(NumberFormatException e){
01203                                         errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" "+MessageBundle.get("panel.Bridge.error.sensorId")+"\n";
01204                                 }
01205                         }
01206                         if(port.type_.equals("IN")){
01207                                 if(!DataTypeId.isInport(port.propertyName_))
01208                                         errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" : "+port.propertyName_+MessageBundle.get("panel.Bridge.error.inportPropertyName")+"\n";
01209                                 if(!port.outputTime_.equals(""))
01210                                         errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.inportTime")+"\n";
01211                         }else{
01212                                 if(!DataTypeId.isOutport(port.propertyName_))
01213                                         errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+" : "+port.propertyName_+MessageBundle.get("panel.Bridge.error.outportPropertyName")+"\n";
01214                                 if(!port.outputTime_.equals("control Time") && !port.outputTime_.equals("")){
01215                                         try{
01216                                                 double time = Double.valueOf(port.outputTime_);
01217                                                 if(time < controlTime_){
01218                                                         errorMessage_ +=  MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.outportTimeValue")+"\n";
01219                                                 }
01220                                         }catch(NumberFormatException e){
01221                                                 errorMessage_ +=  MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.outportTime")+"\n";
01222                                         }
01223                                 }
01224                         }
01225                         if(port.controllerPortName_.startsWith("0:"))
01226                                 errorMessage_ += MessageBundle.get("panel.Bridge.errorPort")+" "+port.name_+ MessageBundle.get("panel.Bridge.error.controllerPortName")+"\n";
01227                 }
01228                 
01229                 if(moduleName_.equals(""))
01230                         errorMessage_ += MessageBundle.get("panel.Bridge.error.moduleName")+"\n";
01231                 if(controllerRtcName_.equals(""))
01232                         errorMessage_ += MessageBundle.get("panel.Bridge.error.controlRTCName")+"\n";
01233                 if(useConfigFile_ && configFileName_.equals(""))
01234                         errorMessage_ += MessageBundle.get("panel.Bridge.error.configFileName")+"\n";
01235         }
01236         
01237         @SuppressWarnings("unchecked")
01238         private class IdCellDialog extends Dialog {
01239                 private class IdData {
01240                         public String name_;
01241                         public int id_;
01242                         public boolean selection_;
01243                         public IdData(String name, int id){
01244                                 name_ = name;
01245                                 id_ = id;
01246                                 selection_ = false;
01247                         }
01248                         public boolean equals(Object object){
01249                                 if(id_ < 0)
01250                                         return name_.equals(((IdData)object).name_);
01251                                 else
01252                                         return name_.equals(((IdData)object).name_) || id_ == ((IdData)object).id_ ;
01253                         }
01254                 }
01255                 private class Link extends IdData {
01256                         public Link(String name, int jointId) {
01257                                 super(name, jointId);
01258                         }
01259                         public boolean isJoint(){
01260                                 if(id_ < 0)
01261                                         return false;
01262                                 else
01263                                         return true;
01264                         }
01265                 }
01266                 
01267                 private Vector<Link> links_ = new Vector<Link>();
01268                 private Vector<IdData>[] sensors_ = new Vector[GrxSensorItem.sensorType.length];
01269                 private String id_;
01270                 private Vector<Button> linkButtons_ = new Vector<Button>();
01271                 private Vector<Button>[] sensorButtons_ = new Vector[GrxSensorItem.sensorType.length];
01272                 private Button allJointButton_;
01273                 
01274                 protected IdCellDialog(Shell parentShell) {
01275                         super(parentShell);
01276                         String[] joints = robot_.getJointNames();
01277                         for(int i=0; i<joints.length; i++)
01278                                 links_.add(new Link(joints[i], i));
01279                         Iterator<GrxLinkItem> it = robot_.links_.iterator();
01280                         while(it.hasNext()){
01281                                 GrxLinkItem link = it.next();
01282                                 if(link.jointId_ < 0)
01283                                         links_.add(new Link(link.getName(), -1));
01284                         }                       
01285                         for(int i=0; i<GrxSensorItem.sensorType.length; i++){
01286                         sensors_[i] = new Vector<IdData>(); 
01287                         String[] names = robot_.getSensorNames(GrxSensorItem.sensorType[i]);
01288                         if(names!=null){
01289                                 for(int j=0; j<names.length; j++)
01290                                         sensors_[i].add(new IdData(names[j], j));
01291                         }
01292                         sensorButtons_[i] = new Vector<Button>();
01293                 }
01294                 }
01295                 
01296                 protected Control createDialogArea(Composite parent) {
01297                 Composite composite = (Composite)super.createDialogArea(parent);
01298                 composite.setLayout(new GridLayout(2,true));
01299                 
01300                 Composite panel0 = new Composite(composite,SWT.NONE);
01301                 panel0.setLayoutData(new GridData(GridData.FILL_VERTICAL));
01302                 panel0.setLayout(new FillLayout(SWT.VERTICAL));
01303                 Label label0 = new Label(panel0, SWT.NONE);
01304                         label0.setText("Link");
01305                         label0.setForeground(Activator.getDefault().getColor("blue"));
01306                         allJointButton_ = new Button(panel0, SWT.TOGGLE|SWT.CENTER);
01307                         allJointButton_.setText("All joints");
01308                         allJointButton_.addSelectionListener(new SelectionListener(){
01309                                 public void widgetDefaultSelected(SelectionEvent e) {
01310                                 }
01311                                 public void widgetSelected(SelectionEvent e) {
01312                                         if(allJointButton_.getSelection()){
01313                                                 for(int i=0; i<links_.size(); i++){
01314                                                         if(links_.get(i).isJoint())
01315                                                                 linkButtons_.get(i).setSelection(true);
01316                                                 }
01317                                         }else{
01318                                                 for(int i=0; i<links_.size(); i++){
01319                                                         if(links_.get(i).isJoint())
01320                                                                 linkButtons_.get(i).setSelection(false);
01321                                                 }
01322                                         }
01323                                 }                       
01324                         });
01325                 for(int i=0; i<links_.size(); i++){
01326                         linkButtons_.add(new Button(panel0, SWT.CHECK|SWT.LEFT));
01327                         linkButtons_.get(i).addSelectionListener(new SelectionListener(){
01328                                 public void widgetDefaultSelected(SelectionEvent e) {
01329                                 }
01330                                 public void widgetSelected(SelectionEvent e) {
01331                                         updateAllJointButton();
01332                                 }                               
01333                         });
01334                 }
01335                 updateButton(true, 0);
01336                 
01337                 Composite panel1 = new Composite(composite,SWT.NONE);
01338                 panel1.setLayoutData(new GridData(GridData.FILL_VERTICAL));
01339                 panel1.setLayout(new RowLayout(SWT.VERTICAL));
01340                 for(int i=0; i<GrxSensorItem.sensorType.length; i++){
01341                         Label label = new Label(panel1, SWT.NONE);
01342                         label.setText(GrxSensorItem.sensorType[i]);
01343                         label.setForeground(Activator.getDefault().getColor("blue"));
01344                         for(int j=0; j<sensors_[i].size(); j++){
01345                                 sensorButtons_[i].add(new Button(panel1, SWT.CHECK|SWT.LEFT));
01346                         }
01347                         Label dumy = new Label(panel1, SWT.NONE);
01348                 }
01349                 updateButton(false, -1);
01350                 
01351                 Transfer [] transfers = new Transfer[]{ TextTransfer.getInstance() };
01352                 DragSourceListener dragSourceListener = new DragSourceListener() {
01353                                 public void dragFinished(DragSourceEvent event) {
01354                                 }
01355                                 public void dragSetData(DragSourceEvent event) {
01356                                         Button sourceButton = (Button)((DragSource) event.getSource()).getControl();
01357                                         int index = linkButtons_.indexOf(sourceButton);
01358                                         if(index>=0)
01359                                                 event.data = "link "+String.valueOf(index);//links_.get(index).name_;
01360                                         else
01361                                                 for(int i=0; i<sensorButtons_.length; i++){
01362                                                         index = sensorButtons_[i].indexOf(sourceButton);
01363                                                         if(index >= 0){
01364                                                                 event.data = "sensor "+String.valueOf(i)+" "+String.valueOf(index);
01365                                                                 break;
01366                                                         }
01367                                                 }
01368                                 }
01369                                 public void dragStart(DragSourceEvent event) {
01370                                 }
01371                 };
01372                 DropTargetListener dropTargetListener = new DropTargetListener(){
01373                     public void drop(DropTargetEvent event){
01374                         if (event != null){
01375                                 Button targetButton = (Button)((DropTarget) event.getSource()).getControl();
01376                                 String string = (String)event.data;
01377                                 String[] source = string.split(" ");
01378                                 if(source[0].equals("link")){
01379                                         for(int i=0; i<links_.size(); i++)
01380                                                 links_.get(i).selection_ = linkButtons_.get(i).getSelection();
01381                                         int sourceIndex = Integer.parseInt(source[1]);
01382                                         int targetIndex = linkButtons_.indexOf(targetButton);
01383                                         if(targetIndex >=0){
01384                                                 Link sourceLink = links_.get(sourceIndex);
01385                                                 Link targetLink = links_.get(targetIndex);
01386                                                 links_.remove(sourceLink);
01387                                                 links_.insertElementAt(sourceLink, links_.indexOf(targetLink));
01388                                                 updateButton(true, 0);
01389                                         }
01390                                 }else{  //sensor
01391                                         int type = Integer.parseInt(source[1]);
01392                                         for(int i=0; i<sensors_[type].size(); i++)
01393                                                 sensors_[type].get(i).selection_ = sensorButtons_[type].get(i).getSelection();
01394                                         int sourceIndex = Integer.parseInt(source[2]);
01395                                         int targetIndex =  sensorButtons_[type].indexOf(targetButton);
01396                                         if(targetIndex >=0){
01397                                                 IdData sourceData = sensors_[type].get(sourceIndex);
01398                                                 IdData targetData = sensors_[type].get(targetIndex);
01399                                                 sensors_[type].remove(sourceData);
01400                                                 sensors_[type].insertElementAt(sourceData, sensors_[type].indexOf(targetData));
01401                                                 updateButton(false, type);
01402                                         }
01403                                 }
01404                         }
01405                     }
01406                                 private Color buttonColor_;
01407                                 public void dragEnter(DropTargetEvent event) {
01408                                         Button targetButton = (Button)((DropTarget) event.getSource()).getControl();
01409                                         buttonColor_ = targetButton.getBackground(); 
01410                                         targetButton.setBackground(Activator.getDefault().getColor("darkGray"));
01411                                 }
01412                                 public void dragLeave(DropTargetEvent event) {
01413                                         Button targetButton = (Button)((DropTarget) event.getSource()).getControl();
01414                                         targetButton.setBackground(buttonColor_);
01415                                 }
01416                                 public void dragOperationChanged(DropTargetEvent event) {
01417                                 }
01418                                 public void dragOver(DropTargetEvent event) {   
01419                                 }
01420                                 public void dropAccept(DropTargetEvent event) {
01421                                 }
01422                 };
01423                 for(int i=0; i<links_.size(); i++){
01424                         DragSource source = new DragSource(linkButtons_.get(i), DND.DROP_MOVE );
01425                         source.setTransfer(transfers);
01426                         source.addDragListener(dragSourceListener);
01427                         DropTarget target = new DropTarget(linkButtons_.get(i), DND.DROP_MOVE);
01428                         target.setTransfer(transfers);
01429                         target.addDropListener(dropTargetListener);
01430                 }
01431                 
01432                 for(int i=0; i<sensors_.length; i++){
01433                         for(int j=0; j<sensors_[i].size(); j++){
01434                                 DragSource source = new DragSource(sensorButtons_[i].get(j), DND.DROP_MOVE );
01435                                 source.setTransfer(transfers);
01436                                 source.addDragListener(dragSourceListener);
01437                                 DropTarget target = new DropTarget(sensorButtons_[i].get(j), DND.DROP_MOVE);
01438                                 target.setTransfer(transfers);
01439                                 target.addDropListener(dropTargetListener);
01440                         }
01441                 }
01442                 
01443                         return composite;
01444                 }
01445 
01446                 protected void buttonPressed(int buttonId) {
01447                 if (buttonId == IDialogConstants.OK_ID) {
01448                         boolean b = false;
01449                         for(int i=0; i<links_.size(); i++){
01450                                 if(links_.get(i).isJoint()){
01451                                         b = true;
01452                                         if(!linkButtons_.get(i).getSelection()){
01453                                                 b = false;
01454                                                         break;
01455                                         }
01456                                 }
01457                         }
01458                                 if(b){
01459                                         id_ = "All joints";
01460                                 }else{  
01461                                         id_ = "";
01462                                         for(int i=0; i<linkButtons_.size(); i++)
01463                                                 if(linkButtons_.get(i).getSelection())
01464                                                         id_ += links_.get(i).name_ + ",";
01465                                         for(int i=1; i<sensorButtons_.length; i++)
01466                                                 for(int j=0; j<sensorButtons_[i].size(); j++)
01467                                                         if(sensorButtons_[i].get(j).getSelection())
01468                                                                 id_ += sensors_[i].get(j).name_ + ",";
01469                                         if(id_.length()!=0)
01470                                                 id_ = id_.substring(0, id_.length()-1);
01471                                         else
01472                                                 for(int i=0; i<sensorButtons_[0].size(); i++)
01473                                                         if(sensorButtons_[0].get(i).getSelection())
01474                                                                 id_ = String.valueOf(sensors_[0].get(i).id_);
01475                                                                 
01476                                 }
01477                 }
01478                 super.buttonPressed(buttonId);
01479                 }
01480 
01481                 public String getId(){
01482                         return id_;
01483                 }
01484                 
01485                 public void setId(String value) {
01486                         id_ = value;
01487                         if(id_.equals("All joints")){
01488                                 for(int i=0; i<links_.size(); i++){
01489                                         if(links_.get(i).isJoint())
01490                                                 links_.get(i).selection_ = true;
01491                                 }
01492                         }else{
01493                                 String[] names = id_.split(",");
01494                                 for(int i=names.length; i>0; ){
01495                                         String name = names[--i].trim();
01496                                         int index = links_.indexOf(new Link(name, -1));
01497                                         if(index >= 0){
01498                                                 Link link = links_.get(index);
01499                                                 link.selection_ = true;
01500                                                 links_.remove(link);
01501                                                 links_.insertElementAt(link, 0);
01502                                                 continue;
01503                                         }
01504                                         for(int j=1; j<GrxSensorItem.sensorType.length; j++){
01505                                                 index = sensors_[j].indexOf(new IdData(name, -1));
01506                                                 if(index >= 0){
01507                                                         IdData idData = sensors_[j].get(index);
01508                                                         idData.selection_ = true;
01509                                                         sensors_[j].remove(idData);
01510                                                         sensors_[j].insertElementAt(idData, 0);
01511                                                         continue;
01512                                                 }
01513                                         }
01514                                         try{
01515                                                 int id = Integer.valueOf(name);
01516                                                 index = sensors_[0].indexOf(new IdData("", id));
01517                                                 if(index >= 0){
01518                                                         IdData idData = sensors_[0].get(index);
01519                                                         idData.selection_ = true;
01520                                                         sensors_[0].remove(idData);
01521                                                         sensors_[0].insertElementAt(idData, 0);
01522                                                 }
01523                                         }catch(NumberFormatException e){
01524                                                 ;
01525                                         }
01526                                 }
01527                         }
01528                 }
01529                 
01530                 private void updateButton(boolean isLink, int type){
01531                         if(isLink){
01532                                 for(int i=0; i<links_.size(); i++){
01533                                 String string = links_.get(i).name_;
01534                                 if(links_.get(i).isJoint())
01535                                         string += " (id= "+links_.get(i).id_+")";
01536                                 linkButtons_.get(i).setText(string);
01537                                 linkButtons_.get(i).setSelection(links_.get(i).selection_);
01538                         }
01539                                 updateAllJointButton();
01540                         }else{
01541                                 if(type < 0)
01542                                         for(int i=0; i<GrxSensorItem.sensorType.length; i++){
01543                                         for(int j=0; j<sensors_[i].size(); j++){
01544                                                 sensorButtons_[i].get(j).setText(sensors_[i].get(j).name_+" (id= "+sensors_[i].get(j).id_+")");
01545                                                 sensorButtons_[i].get(j).setSelection(sensors_[i].get(j).selection_);
01546                                         }
01547                                 }
01548                                 else{
01549                                         for(int j=0; j<sensors_[type].size(); j++){
01550                                         sensorButtons_[type].get(j).setText(sensors_[type].get(j).name_+" (id= "+sensors_[type].get(j).id_+")");
01551                                         sensorButtons_[type].get(j).setSelection(sensors_[type].get(j).selection_);
01552                                 }
01553                                 }
01554                         }
01555                 }
01556                 
01557                 private void updateAllJointButton() {
01558                         boolean b = true;
01559                         for(int i=0; i<links_.size(); i++)
01560                                 if(links_.get(i).isJoint())
01561                                         b = b && linkButtons_.get(i).getSelection();
01562                         if(b)
01563                                 allJointButton_.setSelection(true);
01564                         else
01565                                 allJointButton_.setSelection(false);
01566                 }
01567         }
01568 
01569         public void setControlTime(double time) {
01570                 controlTime_ = time;
01571         }
01572         
01573 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:53