GrxPropertyView.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * GrxPropertyView.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro Kawasumi (General Robotix, Inc.)
17  */
18 
19 package com.generalrobotix.ui.view;
20 
21 import java.util.Map;
22 import java.util.Properties;
23 
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.MenuManager;
26 import org.eclipse.jface.dialogs.InputDialog;
27 import org.eclipse.jface.viewers.CellEditor;
28 import org.eclipse.jface.viewers.ColumnWeightData;
29 import org.eclipse.jface.viewers.ComboBoxCellEditor;
30 import org.eclipse.jface.viewers.ICellModifier;
31 import org.eclipse.jface.viewers.ILabelProviderListener;
32 import org.eclipse.jface.viewers.IStructuredContentProvider;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.TableLayout;
35 import org.eclipse.jface.viewers.TableViewer;
36 import org.eclipse.jface.viewers.TextCellEditor;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.jface.viewers.ViewerSorter;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.custom.CCombo;
41 import org.eclipse.swt.events.KeyEvent;
42 import org.eclipse.swt.events.KeyListener;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.events.SelectionListener;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Event;
50 import org.eclipse.swt.widgets.Listener;
51 import org.eclipse.swt.widgets.Table;
52 import org.eclipse.swt.widgets.TableColumn;
53 import org.eclipse.swt.widgets.TableItem;
54 import org.eclipse.swt.widgets.Text;
55 
66 
67 @SuppressWarnings("serial") //$NON-NLS-1$
71 public class GrxPropertyView extends GrxBaseView {
72 
73  public static final String TITLE = "Property"; //$NON-NLS-1$
74 
75  private GrxBasePlugin currentPlugin_=null;
76 
77  private TableViewer viewer_ = null;
78 
79  private Table table_ = null;
80 
81  private Text nameText_ = null;
82  private String oldText = "";
83 
84  private final String[] clmName_ = { MessageBundle.get("GrxPropertyView.text.name"), MessageBundle.get("GrxPropertyView.text.value") }; //$NON-NLS-1$ //$NON-NLS-2$
85 
86  private MenuManager menuMgr_= new MenuManager();
87 
96  String name,
97  GrxPluginManager manager,
98  GrxBaseViewPart vp,
99  Composite parent) {
100 
101  super(name, manager, vp, parent);
102 
103  GrxDebugUtil.println("GrxPropertyView init"); //$NON-NLS-1$
104 
105  GridLayout gl = new GridLayout(1,false);
106  composite_.setLayout( gl );
107 
108  nameText_ = new Text(composite_, SWT.SINGLE|SWT.BORDER);
109  GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
110  textGridData.heightHint = 20;
111  nameText_.setLayoutData(textGridData);
112 
113  final GrxPropertyView view = this;
114  nameText_.addKeyListener(new KeyListener(){
115  public void keyPressed(KeyEvent e) {}
116  public void keyReleased(KeyEvent e) {
117  if (e.character == SWT.CR && !nameText_.getText().equals(oldText)) {
118  GrxBasePlugin p = null;
119  if ((p = manager_.getView(nameText_.getText(), true)) != null){
120  if(currentPlugin_ != p){
121  if(currentPlugin_ != null)
122  currentPlugin_.deleteObserver(view);
123  _setInput(p);
124  currentPlugin_ = p;
125  currentPlugin_.addObserver(view);
126  }
127  }else if ((p = manager_.getItem(nameText_.getText())) != null){
128  manager_.focusedItem((GrxBaseItem) p);
129  }else{
130  nameText_.setText(""); //$NON-NLS-1$
131  if(currentPlugin_ != null)
132  currentPlugin_.deleteObserver(view);
133  _setInput(null);
134  currentPlugin_ = null;
135  }
136  }
137  }
138  });
139 
140  viewer_ = new TableViewer(composite_, SWT.MULTI | SWT.FULL_SELECTION
141  | SWT.BORDER | SWT.VIRTUAL);
142 
143  TableLayout layout = new TableLayout();
144  layout.addColumnData(new ColumnWeightData(40, true));
145  layout.addColumnData(new ColumnWeightData(60, true));
146  table_ = viewer_.getTable();
147  table_.setLayout(layout);
148  GridData tableGridData = new GridData(GridData.FILL_BOTH);
149  table_.setLayoutData(tableGridData);
150 
151  TableColumn column1 = new TableColumn(table_, SWT.LEFT);
152  TableColumn column2 = new TableColumn(table_, SWT.LEFT);
153  column1.setText(clmName_[0]);
154  column2.setText(clmName_[1]);
155  column1.setMoveable(true);
156  column2.setMoveable(true);
157  column1.addSelectionListener(new ColumnSelectionListener());
158  column2.addSelectionListener(new ColumnSelectionListener());
159 
160  viewer_.setColumnProperties(clmName_);
161 
162  viewer_.setContentProvider(new PropertyTableContentProvider());
163  viewer_.setLabelProvider(new PropertyTableLabelProvider());
164  CellEditor[] editors = new CellEditor[] {
165  new TextCellEditor(table_),
166  new TextCellEditor(table_) };
167  viewer_.setCellEditors(editors);
168  viewer_.setCellModifier(new PropertyTableCellModifier());
169  viewer_.setSorter(new PropertyTableViewerSorter());
170 
171  table_.setLinesVisible(true);
172  table_.setHeaderVisible(true);
173 
174  setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
175 
176  setUp();
177  manager_.registerItemChangeListener(this, GrxBaseItem.class);
178 
179  // 右クリックメニュー
180  table_.setMenu(menuMgr_.createContextMenu( table_ ));
181  table_.addListener ( SWT.MenuDetect, new Listener () {
182  public void handleEvent (Event event) {
183  final TableItem[] selection = table_.getSelection();
184  menuMgr_.removeAll();
185  if(selection.length!=0)
186  menuMgr_.add(new Action(){
187  public String getText(){ return MessageBundle.get("GrxPropertyView.menu.InputValue"); }
188  public void run(){
189  InputDialog dialog = new InputDialog( null, getText(),
190  MessageBundle.get("GrxPropertyView.dialog.message.input"), "", null); //$NON-NLS-1$
191  if ( dialog.open() == InputDialog.OK && dialog.getValue() != null){
192  String _value = dialog.getValue();
193  String[] _key = new String[selection.length];
194  for(int i=0; i<selection.length; i++){ // value を変更するとソートがかかってselectionが変わってしまうので、まずkeyを保存
195  _key[i] = selection[i].getText(0);
196  }
197  for(int i=0; i<_key.length; i++){
198  if(isValidChangeValue(_key[i], _value) == false)
199  continue;
200 
201  if(!currentPlugin_.getProperty(_key[i]).equals(_value)){
202  if (!currentPlugin_.propertyChanged(_key[i], _value)){
203  currentPlugin_.setProperty(_key[i], _value);
204  }
205  }
206  }
207  }
208 
209  }
210  });
211  }
212  });
213 
214  updateTableFont();
215  }
216 
217  public void setUp(){
218  if(currentPlugin_!=null)
219  currentPlugin_.deleteObserver(this);
220  currentPlugin_ = manager_.focusedItem();
221  _setInput(currentPlugin_);
222  if(currentPlugin_ != null)
223  currentPlugin_.addObserver(this);
224  }
225 
226  private boolean isValidChangeValue(String key, String value){
227  GrxBasePlugin.ValueEditType editType = currentPlugin_.GetValueEditType(key);
228  if(editType instanceof ValueEditText){
229  return true;
230  }else if(editType instanceof ValueEditCombo){
231  String[] items = ((ValueEditCombo)editType).GetItems();
232  for(int idx=0; idx<items.length; idx++){
233  if(items[idx].equals(value)){
234  return true;
235  }
236  }
237  return false;
238  }
239  return false;
240  }
241 
242  public void registerItemChange(GrxBaseItem item, int event){
243  switch(event){
245  if(currentPlugin_ != item){
246  if(currentPlugin_ != null)
247  currentPlugin_.deleteObserver(this);
248  _setInput(item);
249  currentPlugin_ = item;
250  currentPlugin_.addObserver(this);
251  }
252  break;
254  if(currentPlugin_ == item){
255  currentPlugin_.deleteObserver(this);
256  _setInput(null);
257  currentPlugin_ = null;
258  }
259  break;
260  default:
261  break;
262  }
263  }
264 
265  public void update(GrxBasePlugin plugin, Object... arg) {
266  if((String)arg[0]!="PropertyChange") //$NON-NLS-1$
267  return;
268  _refresh();
269  }
270 
271  private class PropertyTableContentProvider implements
272  IStructuredContentProvider {
273 
274  public Object[] getElements(Object inputElement) {
275  if (inputElement instanceof Properties) {
276  return ((Properties)inputElement).entrySet().toArray();
277  }
278  return new Object[0];
279  }
280 
281  public void dispose() {
282  }
283 
284  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
285  }
286 
287  }
288 
289  private class PropertyTableLabelProvider implements ITableLabelProvider {
290 
291  public Image getColumnImage(Object element, int columnIndex) {
292  return null;
293  }
294 
295  public String getColumnText(Object element, int columnIndex) {
296  String str = null;
297  if (element instanceof Map.Entry && columnIndex < clmName_.length) {
298  switch (columnIndex) {
299  case 0:
300  str = (String) ((Map.Entry)element).getKey();
301  break;
302  case 1:
303  str = (String) ((Map.Entry)element).getValue();
304  break;
305  default:
306  str = "---"; //$NON-NLS-1$
307  break;
308  }
309  }
310  return str;
311  }
312 
313  public void addListener(ILabelProviderListener listener) {
314  }
315 
316  public void dispose() {
317  }
318 
319  public boolean isLabelProperty(Object element, String property) {
320  return false;
321  }
322 
323  public void removeListener(ILabelProviderListener listener) {
324  }
325 
326  }
327 
328  private class PropertyTableCellModifier implements ICellModifier {
329 
330  public boolean canModify(Object element, String property) {
331  if(currentPlugin_ instanceof GrxGraphItem || currentPlugin_ instanceof GrxCollisionPairItem )
332  return false;
333  if(property.equals(clmName_[1])){
334  if (element instanceof Map.Entry) {
335  Map.Entry<String, String> _element = (Map.Entry)element;
336  if(_element.getKey().equals("url"))
337  return false;
338  GrxBasePlugin.ValueEditType editType = currentPlugin_.GetValueEditType(_element.getKey());
339  if(editType instanceof ValueEditCombo){
340  CellEditor[] editors = new CellEditor[] {
341  new TextCellEditor(table_),
342  new ComboBoxCellEditor(table_, ((ValueEditCombo) editType).GetItems()) };
343  viewer_.setCellEditors(editors);
344  }else if(editType instanceof ValueEditText){
345  CellEditor[] editors = new CellEditor[] {
346  new TextCellEditor(table_),
347  new TextCellEditor(table_) };
348  viewer_.setCellEditors(editors);
349  }else{
350  return false;
351  }
352  }
353  return true;
354  }else
355  return false;
356  }
357 
358  public Object getValue(Object element, String property) {
359  if (property.equals(clmName_[1]) &&
360  element instanceof Map.Entry) {
361  Map.Entry<String, String> _element = (Map.Entry)element;
362  CellEditor[] ce = viewer_.getCellEditors();
363  if(2 <= ce.length && ce[1] instanceof ComboBoxCellEditor){
364  String[] items = ((ComboBoxCellEditor)ce[1]).getItems();
365  for(int i=0; i<items.length; i++)
366  if(_element.getValue().equals(items[i]))
367  return new Integer(i);
368  return new Integer(0);
369  }else{
370  return _element.getValue();
371  }
372  }
373  return null;
374  }
375 
376  @SuppressWarnings("unchecked") //$NON-NLS-1$
377  public void modify(Object element, String property, Object value) {
378  try{
379  if (element instanceof TableItem){
380  TableItem item = (TableItem) element;
381  String _value = null;
382  if( value instanceof String) {
383  _value = (String)value;
384  }else if(value instanceof Integer){
385  int idx = ((Integer)value).intValue();
386  CellEditor[] ce = viewer_.getCellEditors();
387  if(2 <= ce.length && ce[1] instanceof ComboBoxCellEditor){
388 
389  String[] items = ((ComboBoxCellEditor)ce[1]).getItems();
390  if(0 <= idx && idx < items.length)
391  _value = items[idx];
392  else
393  {
394  String str = ((CCombo)ce[1].getControl()).getText();
395  for(int i = 0; i < items.length; i++)
396  {
397  if(items[i].equals(str))
398  {
399  _value = str;
400  break;
401  }
402  }
403  }
404  }
405  }
406 
407  if(_value != null &&
408  !currentPlugin_.getProperty(item.getText()).equals(_value)){
409  if (!currentPlugin_.propertyChanged(item.getText(), _value)){
410  // if validity of value is not checked by currentPlugin_, it is set
411  currentPlugin_.setProperty(item.getText(), _value);
412  }
413  }
414  _refresh();
415  }
416  }catch(Exception ex){
417  ex.printStackTrace();
418  }
419  }
420 
421  }
422 
423  private void _setInput(GrxBasePlugin p){
424  if(p == null){
425  table_.setVisible(false);
426  nameText_.setText(""); //$NON-NLS-1$
427  oldText="";
428  }else if (p != currentPlugin_) {
429  table_.setVisible(false);
430  nameText_.setText(p.getName());
431  oldText=p.getName();
432  viewer_.setInput(p);
433  table_.setVisible(true);
434  }
435  }
439  private void _refresh(){
440  table_.setVisible(false);
441  nameText_.setText(currentPlugin_.getName());
442  oldText=currentPlugin_.getName();
443  viewer_.setInput(currentPlugin_);
444  table_.setVisible(true);
445  }
446 
447  private class PropertyTableViewerSorter extends ViewerSorter {
448 
449  public int compare(Viewer viewer, Object e1, Object e2) {
450  Table table = ((TableViewer)viewer).getTable();
451  if(table.getSortColumn() == null){
452  return 0;
453  }
454  TableColumn sortColumn = table.getSortColumn();
455  int direction = table.getSortDirection();
456 
457  String comperedFactor1 = ""; //$NON-NLS-1$
458  String comperedFactor2 = ""; //$NON-NLS-1$
459 
460  if(sortColumn.getText().equals(clmName_[0])){
461  comperedFactor1 = (String)((Map.Entry)e1).getKey();
462  comperedFactor2 = (String)((Map.Entry)e2).getKey();
463  }
464  else if(sortColumn.getText().equals(clmName_[1])){
465  comperedFactor1 = (String)((Map.Entry)e1).getValue();
466  comperedFactor2 = (String)((Map.Entry)e2).getValue();
467  }
468  else{
469  return 0;
470  }
471 
472  if (direction == SWT.NONE) {
473  return 0;
474  }
475  else if(direction == SWT.UP){
476  return comperedFactor1.compareTo(comperedFactor2);
477  }
478  else if(direction == SWT.DOWN){
479  return (-1) * comperedFactor1.compareTo(comperedFactor2);
480  }
481  else{
482  return 0;
483  }
484  }
485  }
486 
487 
488  private class ColumnSelectionListener implements SelectionListener{
489 
490  public void widgetDefaultSelected(SelectionEvent e) {
491  widgetSelected(e);
492  }
493 
494  public void widgetSelected(SelectionEvent e) {
495  TableColumn column = (TableColumn)e.widget;
496 
497  int currentDirection = table_.getSortDirection();
498 
499  if(column != table_.getSortColumn() || currentDirection == SWT.NONE){
500  table_.setSortDirection(SWT.UP);
501  }
502  else if(currentDirection == SWT.DOWN){
503  table_.setSortDirection(SWT.NONE);
504  }
505  else if(currentDirection == SWT.UP){
506  table_.setSortDirection(SWT.DOWN);
507  }
508  else{
509  table_.setSortDirection(SWT.NONE);
510  }
511  table_.setSortColumn(column);
512  viewer_.setSorter(new PropertyTableViewerSorter());
513  e.doit = false;
514  }
515  }
516 
517  public void shutdown(){
518  manager_.removeItemChangeListener(this, GrxBaseItem.class);
519  if(currentPlugin_ != null)
520  currentPlugin_.deleteObserver(this);
521  }
522 
523  public void updateTableFont(){
524  viewer_.getTable().setFont(Activator.getDefault().getFont("preference_table"));
525  }
526 }
static final String get(String key)
boolean propertyChanged(String property, String value)
check validity of new value of property and update if valid
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_infop png_charpp name
Definition: png.h:2382
png_voidp int value
Definition: png.h:2113
boolean equals(Object obj)
check whether obj equals to this
void update(GrxBasePlugin plugin, Object... arg)
png_uint_32 i
Definition: png.h:2735
GrxPropertyView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent)
constructor
ValueEditType GetValueEditType(String key)
Return editing type of the key item.
void registerItemChange(GrxBaseItem item, int event)
void inputChanged(Viewer viewer, Object oldInput, Object newInput)
def run(tree, args)
Object setProperty(String key, String value)
set property value associated with a keyword
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
def getText(self, nodes=None)
boolean isValidChangeValue(String key, String value)
org
void _refresh()
refresh contents of table
char * arg
Definition: cdjpeg.h:136


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03