GrxPropertyView.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxPropertyView.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  */
00018 
00019 package com.generalrobotix.ui.view;
00020 
00021 import java.util.Map;
00022 import java.util.Properties;
00023 
00024 import org.eclipse.jface.action.Action;
00025 import org.eclipse.jface.action.MenuManager;
00026 import org.eclipse.jface.dialogs.InputDialog;
00027 import org.eclipse.jface.viewers.CellEditor;
00028 import org.eclipse.jface.viewers.ColumnWeightData;
00029 import org.eclipse.jface.viewers.ComboBoxCellEditor;
00030 import org.eclipse.jface.viewers.ICellModifier;
00031 import org.eclipse.jface.viewers.ILabelProviderListener;
00032 import org.eclipse.jface.viewers.IStructuredContentProvider;
00033 import org.eclipse.jface.viewers.ITableLabelProvider;
00034 import org.eclipse.jface.viewers.TableLayout;
00035 import org.eclipse.jface.viewers.TableViewer;
00036 import org.eclipse.jface.viewers.TextCellEditor;
00037 import org.eclipse.jface.viewers.Viewer;
00038 import org.eclipse.jface.viewers.ViewerSorter;
00039 import org.eclipse.swt.SWT;
00040 import org.eclipse.swt.custom.CCombo;
00041 import org.eclipse.swt.events.KeyEvent;
00042 import org.eclipse.swt.events.KeyListener;
00043 import org.eclipse.swt.events.SelectionEvent;
00044 import org.eclipse.swt.events.SelectionListener;
00045 import org.eclipse.swt.graphics.Image;
00046 import org.eclipse.swt.layout.GridData;
00047 import org.eclipse.swt.layout.GridLayout;
00048 import org.eclipse.swt.widgets.Composite;
00049 import org.eclipse.swt.widgets.Event;
00050 import org.eclipse.swt.widgets.Listener;
00051 import org.eclipse.swt.widgets.Table;
00052 import org.eclipse.swt.widgets.TableColumn;
00053 import org.eclipse.swt.widgets.TableItem;
00054 import org.eclipse.swt.widgets.Text;
00055 
00056 import com.generalrobotix.ui.GrxBaseItem;
00057 import com.generalrobotix.ui.GrxBasePlugin;
00058 import com.generalrobotix.ui.GrxBaseView;
00059 import com.generalrobotix.ui.GrxBaseViewPart;
00060 import com.generalrobotix.ui.GrxPluginManager;
00061 import com.generalrobotix.ui.grxui.Activator;
00062 import com.generalrobotix.ui.item.GrxCollisionPairItem;
00063 import com.generalrobotix.ui.item.GrxGraphItem;
00064 import com.generalrobotix.ui.util.GrxDebugUtil;
00065 import com.generalrobotix.ui.util.MessageBundle;
00066 
00067 @SuppressWarnings("serial") //$NON-NLS-1$
00071 public class GrxPropertyView extends GrxBaseView {
00072     
00073     public static final String TITLE = "Property"; //$NON-NLS-1$
00074 
00075     private GrxBasePlugin currentPlugin_=null;
00076 
00077     private TableViewer viewer_ = null;
00078 
00079     private Table table_ = null;
00080     
00081     private Text nameText_ = null;
00082     private String oldText = "";
00083 
00084     private final String[] clmName_ = { MessageBundle.get("GrxPropertyView.text.name"), MessageBundle.get("GrxPropertyView.text.value") }; //$NON-NLS-1$ //$NON-NLS-2$
00085     
00086     private MenuManager menuMgr_= new MenuManager();
00087 
00095     public GrxPropertyView(
00096         String name,
00097         GrxPluginManager manager,
00098         GrxBaseViewPart vp,
00099         Composite parent) {
00100 
00101         super(name, manager, vp, parent);
00102         
00103         GrxDebugUtil.println("GrxPropertyView init"); //$NON-NLS-1$
00104 
00105                 GridLayout gl = new GridLayout(1,false);
00106                 composite_.setLayout( gl );
00107 
00108                 nameText_ = new Text(composite_, SWT.SINGLE|SWT.BORDER);
00109         GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
00110         textGridData.heightHint = 20;
00111         nameText_.setLayoutData(textGridData);
00112                 
00113         final GrxPropertyView view = this;
00114         nameText_.addKeyListener(new KeyListener(){
00115             public void keyPressed(KeyEvent e) {}
00116             public void keyReleased(KeyEvent e) {
00117                 if (e.character == SWT.CR && !nameText_.getText().equals(oldText)) {
00118                         GrxBasePlugin p = null;
00119                         if ((p = manager_.getView(nameText_.getText(), true)) != null){
00120                                 if(currentPlugin_ != p){
00121                                         if(currentPlugin_ != null)
00122                                                 currentPlugin_.deleteObserver(view);
00123                                         _setInput(p);
00124                                         currentPlugin_ = p;
00125                                         currentPlugin_.addObserver(view);
00126                                 }
00127                         }else if ((p = manager_.getItem(nameText_.getText())) != null){
00128                                 manager_.focusedItem((GrxBaseItem) p);
00129                         }else{
00130                                 nameText_.setText(""); //$NON-NLS-1$
00131                                 if(currentPlugin_ != null)
00132                                         currentPlugin_.deleteObserver(view);
00133                                 _setInput(null);
00134                                 currentPlugin_ = null;
00135                         }
00136                 }
00137             }
00138         });
00139 
00140         viewer_ = new TableViewer(composite_, SWT.MULTI | SWT.FULL_SELECTION
00141             | SWT.BORDER | SWT.VIRTUAL);
00142         
00143         TableLayout layout = new TableLayout();
00144         layout.addColumnData(new ColumnWeightData(40, true));
00145         layout.addColumnData(new ColumnWeightData(60, true));
00146         table_ = viewer_.getTable();
00147         table_.setLayout(layout);
00148         GridData tableGridData = new GridData(GridData.FILL_BOTH);
00149         table_.setLayoutData(tableGridData);
00150         
00151         TableColumn column1 = new TableColumn(table_, SWT.LEFT);
00152         TableColumn column2 = new TableColumn(table_, SWT.LEFT);
00153         column1.setText(clmName_[0]);
00154         column2.setText(clmName_[1]);
00155         column1.setMoveable(true);
00156         column2.setMoveable(true);
00157         column1.addSelectionListener(new ColumnSelectionListener());
00158         column2.addSelectionListener(new ColumnSelectionListener());
00159         
00160         viewer_.setColumnProperties(clmName_);
00161 
00162         viewer_.setContentProvider(new PropertyTableContentProvider());
00163         viewer_.setLabelProvider(new PropertyTableLabelProvider());
00164         CellEditor[] editors = new CellEditor[] {
00165             new TextCellEditor(table_),
00166             new TextCellEditor(table_) };
00167         viewer_.setCellEditors(editors);
00168         viewer_.setCellModifier(new PropertyTableCellModifier());
00169         viewer_.setSorter(new PropertyTableViewerSorter());
00170         
00171         table_.setLinesVisible(true);
00172         table_.setHeaderVisible(true);
00173 
00174         setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
00175         
00176         setUp();
00177         manager_.registerItemChangeListener(this, GrxBaseItem.class);
00178         
00179         //      右クリックメニュー
00180         table_.setMenu(menuMgr_.createContextMenu( table_ ));
00181         table_.addListener ( SWT.MenuDetect, new Listener () {
00182                 public void handleEvent (Event event) {
00183                         final TableItem[] selection = table_.getSelection();
00184                 menuMgr_.removeAll(); 
00185                 if(selection.length!=0)
00186                         menuMgr_.add(new Action(){
00187                                 public String getText(){ return MessageBundle.get("GrxPropertyView.menu.InputValue"); }
00188                                 public void run(){
00189                                         InputDialog dialog = new InputDialog( null, getText(),
00190                                                         MessageBundle.get("GrxPropertyView.dialog.message.input"), "", null); //$NON-NLS-1$
00191                                         if ( dialog.open() == InputDialog.OK && dialog.getValue() != null){
00192                                                 String _value = dialog.getValue();
00193                                                 String[] _key = new String[selection.length];
00194                                                 for(int i=0; i<selection.length; i++){  // value を変更するとソートがかかってselectionが変わってしまうので、まずkeyを保存
00195                                                         _key[i] = selection[i].getText(0);
00196                                                 }
00197                                                 for(int i=0; i<_key.length; i++){
00198                                                         if(isValidChangeValue(_key[i], _value) == false)
00199                                                                 continue;
00200                                                         
00201                                                         if(!currentPlugin_.getProperty(_key[i]).equals(_value)){
00202                                                 if (!currentPlugin_.propertyChanged(_key[i], _value)){
00203                                                         currentPlugin_.setProperty(_key[i], _value);
00204                                                 }
00205                                         }
00206                                                 }
00207                                         }
00208                                                         
00209                                 }
00210                         });
00211                 }
00212         });
00213 
00214         updateTableFont();
00215     }
00216 
00217     public void setUp(){
00218         if(currentPlugin_!=null)
00219                 currentPlugin_.deleteObserver(this);
00220         currentPlugin_ = manager_.focusedItem();
00221         _setInput(currentPlugin_);
00222         if(currentPlugin_ != null)
00223                 currentPlugin_.addObserver(this);
00224     }
00225     
00226     private boolean isValidChangeValue(String key, String value){
00227         GrxBasePlugin.ValueEditType editType = currentPlugin_.GetValueEditType(key);
00228         if(editType instanceof ValueEditText){
00229                         return true;
00230         }else if(editType instanceof ValueEditCombo){
00231                 String[] items = ((ValueEditCombo)editType).GetItems();
00232                 for(int idx=0; idx<items.length; idx++){
00233                         if(items[idx].equals(value)){
00234                                 return true;
00235                         }
00236                 }
00237                 return false;
00238         }
00239         return false;
00240     }
00241 
00242     public void registerItemChange(GrxBaseItem item, int event){
00243         switch(event){
00244         case GrxPluginManager.FOCUSED_ITEM:
00245                 if(currentPlugin_ != item){
00246                         if(currentPlugin_ != null)
00247                                 currentPlugin_.deleteObserver(this);
00248                         _setInput(item);
00249                         currentPlugin_ = item;
00250                         currentPlugin_.addObserver(this);
00251                 }
00252                 break;
00253         case GrxPluginManager.REMOVE_ITEM:
00254                 if(currentPlugin_ == item){
00255                                 currentPlugin_.deleteObserver(this);
00256                         _setInput(null);
00257                         currentPlugin_ = null;
00258                 }
00259                 break;
00260         default:
00261                 break;
00262         }
00263     }
00264    
00265     public void update(GrxBasePlugin plugin, Object... arg) {
00266         if((String)arg[0]!="PropertyChange") //$NON-NLS-1$
00267                 return;
00268         _refresh();
00269     }
00270     
00271     private class PropertyTableContentProvider implements
00272         IStructuredContentProvider {
00273 
00274         public Object[] getElements(Object inputElement) {
00275             if (inputElement instanceof Properties) {
00276                 return ((Properties)inputElement).entrySet().toArray();
00277             }
00278             return new Object[0];
00279         }
00280 
00281         public void dispose() {
00282         }
00283 
00284         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
00285         }
00286 
00287     }
00288 
00289     private class PropertyTableLabelProvider implements ITableLabelProvider {
00290 
00291         public Image getColumnImage(Object element, int columnIndex) {
00292             return null;
00293         }
00294 
00295         public String getColumnText(Object element, int columnIndex) {
00296             String str = null;
00297             if (element instanceof Map.Entry && columnIndex < clmName_.length) {
00298                 switch (columnIndex) {
00299                     case 0:
00300                         str = (String) ((Map.Entry)element).getKey();
00301                         break;
00302                     case 1:
00303                         str = (String) ((Map.Entry)element).getValue();
00304                         break;
00305                     default:
00306                         str = "---"; //$NON-NLS-1$
00307                         break;
00308                 }
00309             }
00310             return str;
00311         }
00312 
00313         public void addListener(ILabelProviderListener listener) {
00314         }
00315 
00316         public void dispose() {
00317         }
00318 
00319         public boolean isLabelProperty(Object element, String property) {
00320             return false;
00321         }
00322 
00323         public void removeListener(ILabelProviderListener listener) {
00324         }
00325 
00326     }
00327 
00328     private class PropertyTableCellModifier implements ICellModifier {
00329 
00330         public boolean canModify(Object element, String property) {
00331             if(currentPlugin_ instanceof GrxGraphItem || currentPlugin_ instanceof GrxCollisionPairItem )
00332                 return false;
00333             if(property.equals(clmName_[1])){
00334                 if (element instanceof Map.Entry) {
00335                         Map.Entry<String, String> _element = (Map.Entry)element;
00336                         if(_element.getKey().equals("url"))     
00337                                 return false;
00338                     GrxBasePlugin.ValueEditType editType = currentPlugin_.GetValueEditType(_element.getKey());
00339                     if(editType instanceof ValueEditCombo){
00340                                 CellEditor[] editors = new CellEditor[] { 
00341                                                 new TextCellEditor(table_),
00342                                     new ComboBoxCellEditor(table_, ((ValueEditCombo) editType).GetItems()) };
00343                                 viewer_.setCellEditors(editors);
00344                     }else if(editType instanceof ValueEditText){
00345                                 CellEditor[] editors = new CellEditor[] { 
00346                                                 new TextCellEditor(table_),
00347                                                 new TextCellEditor(table_) };
00348                                 viewer_.setCellEditors(editors);
00349                     }else{
00350                         return false;
00351                     }
00352                     }
00353                 return true;
00354                 }else 
00355                         return false;
00356         }
00357 
00358         public Object getValue(Object element, String property) {
00359             if (property.equals(clmName_[1]) &&
00360                 element instanceof Map.Entry) {
00361                 Map.Entry<String, String> _element = (Map.Entry)element;
00362                 CellEditor[] ce = viewer_.getCellEditors();
00363                 if(2 <= ce.length && ce[1] instanceof ComboBoxCellEditor){
00364                         String[] items = ((ComboBoxCellEditor)ce[1]).getItems();
00365                         for(int i=0; i<items.length; i++)
00366                                 if(_element.getValue().equals(items[i]))
00367                                         return new Integer(i);
00368                         return new Integer(0);
00369                 }else{
00370                         return _element.getValue();
00371                 }
00372             }
00373             return null;
00374         }
00375 
00376         @SuppressWarnings("unchecked") //$NON-NLS-1$
00377         public void modify(Object element, String property, Object value) {
00378             try{
00379             if (element instanceof TableItem){
00380                 TableItem item = (TableItem) element;
00381                 String _value = null;
00382                 if( value instanceof String) {
00383                         _value = (String)value;
00384                 }else if(value instanceof Integer){
00385                         int idx = ((Integer)value).intValue();
00386                         CellEditor[] ce = viewer_.getCellEditors();
00387                         if(2 <= ce.length && ce[1] instanceof ComboBoxCellEditor){
00388                                 
00389                                 String[] items = ((ComboBoxCellEditor)ce[1]).getItems();
00390                                 if(0 <= idx && idx < items.length)
00391                                         _value = items[idx];
00392                                 else
00393                                 {
00394                                 String str = ((CCombo)ce[1].getControl()).getText();
00395                                 for(int i = 0; i < items.length; i++)
00396                                 {
00397                                         if(items[i].equals(str))
00398                                         {
00399                                                 _value = str;
00400                                                 break;
00401                                         }
00402                                 }
00403                                 }
00404                         }
00405                 }
00406 
00407                 if(_value != null && 
00408                    !currentPlugin_.getProperty(item.getText()).equals(_value)){
00409                         if (!currentPlugin_.propertyChanged(item.getText(), _value)){
00410                                 // if validity of value is not checked by currentPlugin_, it is set
00411                                 currentPlugin_.setProperty(item.getText(), _value);
00412                         }
00413                 }
00414                 _refresh();
00415             }
00416             }catch(Exception ex){
00417                 ex.printStackTrace();
00418             }
00419         }
00420 
00421     }
00422 
00423     private void _setInput(GrxBasePlugin p){
00424         if(p == null){
00425                 table_.setVisible(false);
00426             nameText_.setText(""); //$NON-NLS-1$
00427             oldText="";
00428         }else if (p != currentPlugin_) {
00429             table_.setVisible(false);
00430             nameText_.setText(p.getName());
00431             oldText=p.getName();
00432             viewer_.setInput(p);
00433             table_.setVisible(true);
00434         }       
00435     }
00439     private void _refresh(){
00440         table_.setVisible(false);
00441         nameText_.setText(currentPlugin_.getName());
00442         oldText=currentPlugin_.getName();
00443         viewer_.setInput(currentPlugin_);
00444         table_.setVisible(true);
00445     }
00446     
00447     private class PropertyTableViewerSorter extends ViewerSorter {
00448 
00449         public int compare(Viewer viewer, Object e1, Object e2) {
00450             Table table = ((TableViewer)viewer).getTable();
00451             if(table.getSortColumn() == null){
00452                 return 0;
00453             }
00454             TableColumn sortColumn = table.getSortColumn();
00455             int direction = table.getSortDirection();
00456             
00457             String comperedFactor1 = ""; //$NON-NLS-1$
00458             String comperedFactor2 = ""; //$NON-NLS-1$
00459             
00460             if(sortColumn.getText().equals(clmName_[0])){
00461                 comperedFactor1 = (String)((Map.Entry)e1).getKey();
00462                 comperedFactor2 = (String)((Map.Entry)e2).getKey();
00463             }
00464             else if(sortColumn.getText().equals(clmName_[1])){
00465                 comperedFactor1 = (String)((Map.Entry)e1).getValue();
00466                 comperedFactor2 = (String)((Map.Entry)e2).getValue();
00467             }
00468             else{
00469                 return 0;
00470             }
00471             
00472             if (direction == SWT.NONE) {
00473                 return 0;
00474             }
00475             else if(direction == SWT.UP){
00476                 return comperedFactor1.compareTo(comperedFactor2);
00477             }
00478             else if(direction == SWT.DOWN){
00479                 return (-1) * comperedFactor1.compareTo(comperedFactor2);
00480             }
00481             else{
00482                 return 0;
00483             }
00484         }
00485     }
00486     
00487     
00488     private class ColumnSelectionListener implements SelectionListener{
00489 
00490         public void widgetDefaultSelected(SelectionEvent e) {
00491             widgetSelected(e);
00492         }
00493 
00494         public void widgetSelected(SelectionEvent e) {
00495             TableColumn column = (TableColumn)e.widget;
00496             
00497             int currentDirection = table_.getSortDirection(); 
00498             
00499             if(column != table_.getSortColumn() || currentDirection == SWT.NONE){
00500                 table_.setSortDirection(SWT.UP);
00501             }
00502             else if(currentDirection == SWT.DOWN){
00503                 table_.setSortDirection(SWT.NONE);
00504             }
00505             else if(currentDirection == SWT.UP){
00506                 table_.setSortDirection(SWT.DOWN);
00507             }
00508             else{
00509                 table_.setSortDirection(SWT.NONE);
00510             }
00511             table_.setSortColumn(column);
00512             viewer_.setSorter(new PropertyTableViewerSorter());
00513             e.doit = false;
00514         }
00515     }
00516     
00517     public void shutdown(){
00518         manager_.removeItemChangeListener(this, GrxBaseItem.class);
00519         if(currentPlugin_ != null)
00520                         currentPlugin_.deleteObserver(this);
00521     }
00522 
00523     public void updateTableFont(){
00524         viewer_.getTable().setFont(Activator.getDefault().getFont("preference_table"));
00525     }
00526 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16