]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/AnnotatedPropertyTabContributorFactory.java
Allow property editing with just a single mouse click
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / AnnotatedPropertyTabContributorFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.property;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Field;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.function.Consumer;
29
30 import org.eclipse.jface.layout.GridDataFactory;
31 import org.eclipse.jface.viewers.AbstractTableViewer;
32 import org.eclipse.jface.viewers.CellEditor;
33 import org.eclipse.jface.viewers.CellEditor.LayoutData;
34 import org.eclipse.jface.viewers.CellLabelProvider;
35 import org.eclipse.jface.viewers.CellNavigationStrategy;
36 import org.eclipse.jface.viewers.ColumnViewer;
37 import org.eclipse.jface.viewers.ColumnViewerEditor;
38 import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
39 import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
40 import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
41 import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
42 import org.eclipse.jface.viewers.EditingSupport;
43 import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
44 import org.eclipse.jface.viewers.ISelection;
45 import org.eclipse.jface.viewers.ISelectionChangedListener;
46 import org.eclipse.jface.viewers.ISelectionProvider;
47 import org.eclipse.jface.viewers.IStructuredContentProvider;
48 import org.eclipse.jface.viewers.SelectionChangedEvent;
49 import org.eclipse.jface.viewers.StructuredSelection;
50 import org.eclipse.jface.viewers.TableViewer;
51 import org.eclipse.jface.viewers.TableViewerColumn;
52 import org.eclipse.jface.viewers.TableViewerFocusCellManager;
53 import org.eclipse.jface.viewers.TextCellEditor;
54 import org.eclipse.jface.viewers.Viewer;
55 import org.eclipse.jface.viewers.ViewerCell;
56 import org.eclipse.jface.viewers.ViewerColumn;
57 import org.eclipse.jface.viewers.ViewerRow;
58 import org.eclipse.swt.SWT;
59 import org.eclipse.swt.custom.TableEditor;
60 import org.eclipse.swt.events.TraverseEvent;
61 import org.eclipse.swt.widgets.Composite;
62 import org.eclipse.swt.widgets.Control;
63 import org.eclipse.swt.widgets.Display;
64 import org.eclipse.swt.widgets.Event;
65 import org.eclipse.swt.widgets.Item;
66 import org.eclipse.swt.widgets.TableColumn;
67 import org.eclipse.swt.widgets.TableItem;
68 import org.eclipse.ui.IWorkbenchSite;
69 import org.simantics.browsing.ui.swt.ComboBoxCellEditor2;
70 import org.simantics.db.management.ISessionContext;
71 import org.simantics.g3d.property.annotations.CompoundGetPropertyValue;
72 import org.simantics.g3d.property.annotations.CompoundSetPropertyValue;
73 import org.simantics.g3d.property.annotations.GetComboProperty;
74 import org.simantics.g3d.property.annotations.GetComboPropertyValue;
75 import org.simantics.g3d.property.annotations.GetPropertyValue;
76 import org.simantics.g3d.property.annotations.PropertyTabBlacklist;
77 import org.simantics.g3d.property.annotations.SetComboPropertyValue;
78 import org.simantics.g3d.property.annotations.SetPropertyValue;
79 import org.simantics.g3d.scenegraph.NodeMap;
80 import org.simantics.g3d.scenegraph.NodeMapProvider;
81 import org.simantics.g3d.scenegraph.base.INode;
82 import org.simantics.g3d.scenegraph.base.NodeListener;
83 import org.simantics.g3d.scenegraph.base.ParentNode;
84 import org.simantics.g3d.scenegraph.structural.IStructuralNode;
85 import org.simantics.g3d.tools.AdaptationUtils;
86 import org.simantics.selectionview.IPropertyTab;
87 import org.simantics.selectionview.IPropertyTab2;
88 import org.simantics.utils.datastructures.MapList;
89
90 public class AnnotatedPropertyTabContributorFactory implements PropertyTabContributorFactory {
91
92         private static final boolean DEBUG = false;
93         
94         @SuppressWarnings("unchecked")
95         @Override
96         public List<PropertyTabContributor> getContributors(Object input) {
97                 Map<String,IPropertyItem> items = new LinkedHashMap<String, IPropertyItem>();
98                 List<String> blacklist = new ArrayList<String>();
99                 try {
100                         collectItems(input.getClass(), items);
101                         collectBlacklist(input.getClass(), blacklist);
102                 } catch (InstantiationException e) {
103                         // TODO Auto-generated catch block
104                         e.printStackTrace();
105                 } catch (IllegalAccessException e) {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                 }
109                 
110                 if (items.size() == 0)
111                         return Collections.EMPTY_LIST;
112                 
113                 MapList<String, IPropertyItem> tabMap = new MapList<String, IPropertyItem>();
114                 List<String> tabs = new ArrayList<String>();
115                 for (String id : items.keySet()) {
116                         IPropertyItem item = items.get(id);
117                         tabMap.add(item.getTabId(), item);
118                         if (!tabs.contains(item.getTabId())) {
119                                 tabs.add(item.getTabId());
120                                 //System.out.println(item.tabId + " " + item.name + " " + item.id);
121                         }
122                 }
123                 for (String s : blacklist) {
124                         tabs.remove(s);
125                 }
126                 
127                 List<PropertyTabContributor> contributors = new ArrayList<PropertyTabContributor>(tabs.size());
128                 for (String tabId : tabs) {
129                         contributors.add(new AnnotatedPropertyTabContributor(tabId, tabMap.getValues(tabId)));
130                 }
131                 
132                 return contributors;
133         }
134         
135         
136         private static void collectItems(Class<?> clazz, Map<String, IPropertyItem> items) throws InstantiationException, IllegalAccessException {
137                 Class<?> superclass = clazz.getSuperclass();
138                 if(superclass != null)
139                         collectItems(superclass, items);
140                 
141                 for (Method m : clazz.getDeclaredMethods()) {
142                         m.setAccessible(true);
143                         for (Annotation annotation : m.getAnnotations()) {
144                                 if (annotation.annotationType().equals(GetPropertyValue.class)) {
145                                         GetPropertyValue get = (GetPropertyValue)annotation;
146                                         PropertyItem item = (PropertyItem)items.get(get.value());
147                                         if (item == null) {
148                                                 item = new PropertyItem(get.value());
149                                                 items.put(item.id, item);
150                                         }
151
152                                         item.getter = m;
153                                         item.manipulatorClass = get.manipulator().newInstance().get(m,null);
154
155                                         item.tabId = get.tabId();
156
157                                         item.name = get.name();
158                                         
159                                         
160                                 } else if (annotation.annotationType().equals(SetPropertyValue.class)) {
161                                         SetPropertyValue set = (SetPropertyValue)annotation;
162                                         PropertyItem item = (PropertyItem)items.get(set.value());
163                                         if (item == null) {
164                                                 item = new PropertyItem(set.value());
165                                                 items.put(item.id, item);
166                                         }
167                                         
168                                         item.setter = m;
169                                 } else if (annotation.annotationType().equals(CompoundGetPropertyValue.class)) {
170                                         CompoundGetPropertyValue get = (CompoundGetPropertyValue)annotation;
171                                         CompoundPropertyItem item = (CompoundPropertyItem)items.get(get.value());
172                                         if (item == null) {
173                                                 item = new CompoundPropertyItem(get.value());
174                                                 items.put(item.id, item);
175                                         }
176
177                                         item.getter = m;
178                                         item.manipulatorFactory = get.manipulator().newInstance();
179
180                                         item.tabId = get.tabId();
181
182                                         item.name = get.name();
183                                 } else if (annotation.annotationType().equals(CompoundSetPropertyValue.class)) {
184                                         CompoundSetPropertyValue set = (CompoundSetPropertyValue)annotation;
185                                         CompoundPropertyItem item = (CompoundPropertyItem)items.get(set.value());
186                                         if (item == null) {
187                                                 item = new CompoundPropertyItem(set.value());
188                                                 items.put(item.id, item);
189                                         }
190                                         
191                                         item.setter = m;
192                                 } else if (annotation.annotationType().equals(GetComboPropertyValue.class)) {
193                                     GetComboPropertyValue get = (GetComboPropertyValue)annotation;
194                                     ComboPropertyItem item = (ComboPropertyItem)items.get(get.value());
195                     if (item == null) {
196                         item = new ComboPropertyItem(get.value());
197                         items.put(item.id, item);
198                     }
199                     item.getter = m;
200                     item.manipulatorClass = ComboPropertyManipulator.class;
201
202                     item.tabId = get.tabId();
203
204                     item.name = get.name();
205                                 } else if (annotation.annotationType().equals(SetComboPropertyValue.class)) {
206                                     SetComboPropertyValue set = (SetComboPropertyValue)annotation;
207                     ComboPropertyItem item = (ComboPropertyItem)items.get(set.value());
208                     if (item == null) {
209                         item = new ComboPropertyItem(set.value());
210                         items.put(item.id, item);
211                     }
212                     item.setter = m;
213                                 } else if (annotation.annotationType().equals(GetComboProperty.class)) {
214                                     GetComboProperty get = (GetComboProperty)annotation;
215                     ComboPropertyItem item = (ComboPropertyItem)items.get(get.value());
216                     if (item == null) {
217                         item = new ComboPropertyItem(get.value());
218                         items.put(item.id, item);
219                     }
220                     item.values = m;
221                                 }
222                         }
223                 }
224                 
225                 
226         }
227         
228         private static void collectBlacklist(Class<?> clazz, List<String> blacklist) throws InstantiationException, IllegalAccessException {
229                 Class<?> superclass = clazz.getSuperclass();
230                 if(superclass != null)
231                         collectBlacklist(superclass, blacklist);
232                 
233                 PropertyTabBlacklist ann = clazz.getAnnotation(PropertyTabBlacklist.class);
234                 if (ann == null)
235                         return;
236                 String s = ann.value();
237                 if (s == null)
238                         return;
239                 if (s.length() == 0)
240                         return;
241                 for (String item : s.split(";")) {
242                         blacklist.add(item);
243                 }
244                 
245                 
246         }
247         
248         private static Map<PropertyItem, PropertyManipulator> createManipulators(CompoundPropertyItem item, Object obj) {
249                 try {
250                         
251                         @SuppressWarnings("unchecked")
252                         Map<String,Object> map = (Map<String, Object>)item.getter.invoke(obj);
253                         Map<PropertyItem, PropertyManipulator> result = new HashMap<AnnotatedPropertyTabContributorFactory.PropertyItem, PropertyManipulator>();
254                         for (String key : map.keySet()) {
255                                 MethodWithMapValueProvider provider = new MethodWithMapValueProvider(item.getter, item.setter, key);
256                                 Class<? extends PropertyManipulator> clazz = item.manipulatorFactory.get(null,map.get(key));
257                                 PropertyManipulator manipulator = clazz.getConstructor(ValueProvider.class,Object.class).newInstance(provider,obj);
258                                 PropertyItem i = new PropertyItem(item.id+"."+key);
259                                 i.getter = item.getter;
260                                 i.setter = item.setter;
261                                 i.name = key;
262                                 i.tabId = item.tabId;
263                                 result.put(i,manipulator);
264                         }
265                         return result;
266                 } catch (Exception e) {
267                         e.printStackTrace();
268                         return Collections.emptyMap();
269                 } 
270                 
271         }
272         
273         private static PropertyManipulator createManipulator(PropertyItem item, Object obj) {
274                 try {
275                         MethodValueProvider provider = new MethodValueProvider(item.getter, item.setter);
276                         PropertyManipulator manipulator = item.manipulatorClass.getConstructor(ValueProvider.class,Object.class).newInstance(provider,obj);
277                         return manipulator;
278                 } catch (Exception e) {
279                         e.printStackTrace();
280                         return null;
281                 } 
282         }
283         
284         private static PropertyManipulator createManipulator(ComboPropertyItem item, Object obj) {
285         try {
286             MethodComboValueProvider provider = new MethodComboValueProvider(item.getter, item.setter,item.values);
287             PropertyManipulator manipulator = item.manipulatorClass.getConstructor(ValueProvider.class,Object.class).newInstance(provider,obj);
288             return manipulator;
289         } catch (Exception e) {
290             e.printStackTrace();
291             return null;
292         } 
293     }
294         
295         private static interface IPropertyItem {
296                 public String getTabId();
297                 public String getName();
298                 public String getId();
299                 public boolean editable();
300         }
301         
302         private static class PropertyItem implements IPropertyItem{
303                 private String id;
304                 private String name;
305                 private String tabId;
306                 private Method getter;
307                 private Method setter;
308                 private Class<? extends PropertyManipulator> manipulatorClass;
309                 
310                 
311                 public PropertyItem(String id) {
312                         if (id == null)
313                                 throw new NullPointerException();
314                         this.id = id;
315                 }
316                 
317                 @Override
318                 public String getId() {
319                     return id;
320                 }
321                 
322                 @Override
323                 public int hashCode() {
324                         return id.hashCode();
325                 }
326                 
327                 @Override
328                 public String getTabId() {
329                         return tabId;
330                 }
331                 
332                 @Override
333                 public String getName() {
334                     return name;
335                 }
336                 
337                 @Override
338                 public boolean editable() {
339                     return setter != null;
340                 }
341         }
342         
343         private static class CompoundPropertyItem implements IPropertyItem{
344                 private String id;
345                 private String name;
346                 private String tabId;
347                 private Method getter;
348                 private Method setter;
349                 private PropertyManipulatorFactory manipulatorFactory;
350                 
351                 
352                 public CompoundPropertyItem(String id) {
353                         if (id == null)
354                                 throw new NullPointerException();
355                         this.id = id;
356                 }
357                 
358                 @Override
359         public String getId() {
360             return id;
361         }
362                 
363                 @Override
364                 public int hashCode() {
365                         return id.hashCode();
366                 }
367                 
368                 @Override
369                 public String getTabId() {
370                         return tabId;
371                 }
372                 
373                 @Override
374         public String getName() {
375             return name;
376         }
377                 
378                 @Override
379         public boolean editable() {
380             return setter != null;
381         }
382         }
383         
384         private static class ComboPropertyItem implements IPropertyItem{
385         private String id;
386         private String name;
387         private String tabId;
388         private Method getter;
389         private Method setter;
390         private Method values;
391         private Class<? extends ComboPropertyManipulator> manipulatorClass;
392         
393         
394         public ComboPropertyItem(String id) {
395             if (id == null)
396                 throw new NullPointerException();
397             this.id = id;
398         }
399         
400         @Override
401         public String getId() {
402             return id;
403         }
404         
405         @Override
406         public int hashCode() {
407             return id.hashCode();
408         }
409         
410         @Override
411         public String getTabId() {
412             return tabId;
413         }
414         
415         @Override
416         public String getName() {
417             return name;
418         }
419         
420         @Override
421         public boolean editable() {
422             return setter != null;
423         }
424     }
425         
426         private static class AnnotatedPropertyTabContributor implements PropertyTabContributor {
427                 private String id; 
428                 List<IPropertyItem> items;
429                 
430                 public AnnotatedPropertyTabContributor(String id, List<IPropertyItem> items) {
431                         if (id == null)
432                                 throw new NullPointerException();
433                         this.id = id;
434                         this.items = items;
435                 }
436                 
437                 @Override
438                 public IPropertyTab create(Composite parent, IWorkbenchSite site,
439                                 ISessionContext context, Object input) {
440                         AnnotatedPropertyTab tab = new AnnotatedPropertyTab(id, items);
441                         tab.createControl(parent, context);
442                         return tab;
443                 }
444                 
445                 @Override
446                 public String getId() {
447                         return id;
448                 }
449                 
450         }
451         
452         private static class AnnotatedPropertyTab implements IPropertyTab2, NodeListener {
453                 //private String id; 
454                 List<IPropertyItem> contibutedItems;
455                 List<IPropertyItem> resolvedItems = new ArrayList<IPropertyItem>();
456                 private Map<IPropertyItem,PropertyManipulator> manipulators = new HashMap<IPropertyItem, PropertyManipulator>();
457                 
458                 private TableViewer viewer;
459                 
460                 private INode node;
461                 private NodeMap<?,?,?> nodeMap;
462                 
463                 private List<TableViewerColumn> valueColumns = new ArrayList<TableViewerColumn>();
464                 
465                 public AnnotatedPropertyTab(String id, List<IPropertyItem> items) {
466                         //this.id = id;
467                         this.contibutedItems = items;
468                         
469                                         
470                 }
471                 
472                 @Override
473                 public void createControl(Composite parent, ISessionContext context) {
474                         //parent.setLayout(new FillLayout());
475                         viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.SINGLE);
476                         
477                         GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(viewer.getTable());
478                         
479                         //viewer.setLabelProvider(new AnnotatedTableLabelProvider(object))
480                         
481                         viewer.setContentProvider(new PropertyItemContentsProvider());
482                         
483                         TableViewerColumn name = new TableViewerColumn(viewer, SWT.LEFT);
484                         //TableViewerColumn value = new TableViewerColumn(viewer, SWT.LEFT);
485                         name.setLabelProvider(new PropertyItemNameProvider());
486                         //value.setLabelProvider(new PropertyValueLabelProvider(null));
487                         name.getColumn().setText("Property");
488                         //value.getColumn().setText("Value");
489                         name.getColumn().setWidth(200);
490                         //value.getColumn().setWidth(200);
491                         name.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {
492                                 
493                                 @Override
494                                 public void selectionChanged(SelectionChangedEvent event) {
495                                         PropertyItem item = AdaptationUtils.adaptToSingle(event.getSelection(),PropertyItem.class);
496                                         if (item != null) {
497                                                 PropertyManipulator manipulator = manipulators.get(item);//createManipulator(item, null);
498                                                 for (int i = 0; i < valueColumns.size(); i++) {
499                                                         TableViewerColumn c = valueColumns.get(i);
500                                                         if (i < manipulator.getValueCount()) {
501                                                                 c.getColumn().setText(manipulator.getDescription(i));
502                                                         } else {
503                                                                 c.getColumn().setText("");
504                                                         }
505                                                 }
506                                         }
507                                         
508                                 }
509                         });
510                         
511                         viewer.getTable().setHeaderVisible(true);
512                         viewer.getTable().setLinesVisible(true);
513                         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
514                                 
515                                 @Override
516                                 public void selectionChanged(SelectionChangedEvent event) {
517                                         IPropertyItem item = AdaptationUtils.adaptToSingle(event.getSelection(), IPropertyItem.class);
518                                         selectedItem = item;
519                                         if (!manipulators.get(selectedItem).getEditMode())
520                                                 manipulators.get(selectedItem).setEditMode(true);
521                                         
522
523                                         for (IPropertyItem i : delayedUpdate) {
524                                                 if (!i.equals(selectedItem)) {
525                                                         manipulators.get(i).setEditMode(false);
526                                                         viewer.update(i,null);
527                                                 }
528                                         }
529                                         if (delayedUpdate.contains(selectedItem)) {
530                                                 delayedUpdate.clear();
531                                                 delayedUpdate.add(selectedItem);
532                                         } else {
533                                                 delayedUpdate.clear();
534                                         }
535                                 }
536                         });
537                         
538                         CellNavigationStrategy nStrategy = new CellNavigationStrategy() {
539                                 private ViewerCell internalFindSelectedCell(
540                                                 ColumnViewer viewer, ViewerCell currentSelectedCell,
541                                                 Event event) {
542                                         switch (event.keyCode) {
543                                         case SWT.ARROW_UP:
544                                                 if (currentSelectedCell != null) {
545                                                         return getNeighbor(currentSelectedCell,
546                                                                         ViewerCell.ABOVE, false);
547                                                 }
548                                                 break;
549                                         case SWT.ARROW_DOWN:
550                                                 if (currentSelectedCell != null) {
551                                                         return getNeighbor(currentSelectedCell,
552                                                                         ViewerCell.BELOW, false);
553                                                 }
554                                                 break;
555                                         case SWT.ARROW_LEFT:
556                                                 if (currentSelectedCell != null) {
557                                                         return getNeighbor(currentSelectedCell,
558                                                                         ViewerCell.LEFT, true);
559                                                 }
560                                                 break;
561                                         case SWT.ARROW_RIGHT:
562                                                 if (currentSelectedCell != null) {
563                                                         return getNeighbor(currentSelectedCell,
564                                                                         ViewerCell.RIGHT, true);
565                                                 }
566                                                 break;
567                                         }
568                                         return null;
569                                 }
570                                 
571                                 public ViewerCell findSelectedCell(ColumnViewer viewer,
572                                                 ViewerCell currentSelectedCell, Event event) {
573                                         ViewerCell cell = internalFindSelectedCell(viewer,
574                                                         currentSelectedCell, event);
575                                         if (cell != null) {
576                                                 TableColumn t = AnnotatedPropertyTab.this.viewer.getTable().getColumn(
577                                                                 cell.getColumnIndex());
578                                                 AnnotatedPropertyTab.this.viewer.getTable().showColumn(t);
579                                         }
580                                         return cell;
581                                 }
582                         };
583                         
584                         TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(
585                                         viewer, new FocusCellOwnerDrawHighlighter(viewer));
586                         try {
587                                 Field f = focusCellManager.getClass().getSuperclass()
588                                                 .getDeclaredField("navigationStrategy");
589                                 f.setAccessible(true);
590                                 f.set(focusCellManager, nStrategy);
591                         } catch (SecurityException e) {
592                                 e.printStackTrace();
593                         } catch (NoSuchFieldException e) {
594                                 e.printStackTrace();
595                         } catch (IllegalArgumentException e) {
596                                 e.printStackTrace();
597                         } catch (IllegalAccessException e) {
598                                 e.printStackTrace();
599                         }
600                         ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
601                                         viewer) {
602                                 protected boolean isEditorActivationEvent(
603                                                 ColumnViewerEditorActivationEvent event) {
604                                         return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
605                                                         || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION
606                                                         || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
607                                                         || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && (event.keyCode == SWT.CR || event.keyCode == SWT.F2))
608                                                         || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
609                                 }
610                         };
611                         TableViewerEditor.create(viewer, focusCellManager, actSupport,
612                                         ColumnViewerEditor.TABBING_HORIZONTAL
613                                                         | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
614                                                         | ColumnViewerEditor.TABBING_VERTICAL
615                                                         | ColumnViewerEditor.KEYBOARD_ACTIVATION);
616                         viewer.getColumnViewerEditor().addEditorActivationListener(
617                                         new ColumnViewerEditorActivationListener() {
618                                                 public void afterEditorActivated(
619                                                                 ColumnViewerEditorActivationEvent event) {
620                                                 }
621
622                                                 public void afterEditorDeactivated(
623                                                                 ColumnViewerEditorDeactivationEvent event) {
624                                                 }
625
626                                                 public void beforeEditorActivated(
627                                                                 ColumnViewerEditorActivationEvent event) {
628                                                         ViewerCell cell = (ViewerCell) event.getSource();
629                                                         viewer.getTable().showColumn(
630                                                                         viewer.getTable().getColumn(cell.getColumnIndex()));
631                                                 }
632
633                                                 public void beforeEditorDeactivated(
634                                                                 ColumnViewerEditorDeactivationEvent event) {
635                                                 }
636                                         });
637                 }
638                 
639                 
640                 
641                 
642                 private IPropertyItem selectedItem = null;
643                 private Set<IPropertyItem> delayedUpdate = new HashSet<IPropertyItem>();
644                 
645                 
646                 @Override
647                 public void setInput(ISessionContext context, ISelection selection,
648                                 boolean force) {
649                         Collection<INode> nodes = AdaptationUtils.adaptToCollection(selection, INode.class);
650                         if (nodes.size() != 1) {
651                                 if (node != null) {
652                                         node.removeListener(this);
653                                         node = null;
654                                 }
655                                 return;
656                         }
657                         INode n = nodes.iterator().next();
658                         if (node != null) {
659                                 if (!node.equals(n)) {
660                                         node.removeListener(this);
661                                         setInput(n);
662                                 }
663                         } else {
664                                 setInput(n);
665                         }
666                 }
667                 
668                 
669                 
670                 private void setInput(INode node) {
671                         this.node = node;
672                         this.node.addListener(this);
673                         // resolve nodemap
674                         INode n = node;
675                         while (true) {
676                                 if (n == null) {
677                                         nodeMap = null;
678                                         break;
679                                 }
680                                 if (n instanceof NodeMapProvider<?,?,?>) {
681                                         nodeMap = ((NodeMapProvider<?,?,?>) n).getNodeMap();
682                                         if (nodeMap != null)
683                                                 break;
684                                 }
685                                 n = (INode)n.getParent();
686                         }
687                         boolean readOnly =  (node instanceof IStructuralNode && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot());
688                         
689                         resolvedItems.clear();
690                         manipulators.clear();
691                         for (IPropertyItem item : contibutedItems) {
692                                 if (item instanceof PropertyItem) {
693                                         resolvedItems.add((PropertyItem)item);
694                                         manipulators.put((PropertyItem)item, createManipulator((PropertyItem)item, node));
695                                 } else if (item instanceof ComboPropertyItem) {
696                                     resolvedItems.add((ComboPropertyItem)item);
697                                     manipulators.put((ComboPropertyItem)item, createManipulator((ComboPropertyItem)item, node));
698                                 } else if (item instanceof CompoundPropertyItem) {
699                                         CompoundPropertyItem compound = (CompoundPropertyItem)item;
700                                         Map<PropertyItem, PropertyManipulator> manipulators = createManipulators(compound, node);
701                                         for (PropertyItem i : manipulators.keySet()) {
702                                                 resolvedItems.add(i);
703                                                 this.manipulators.put(i, manipulators.get(i));
704                                         }
705                                 } else {
706                                     
707                                 }
708                         }
709                         
710                         int valueCount = 0;
711                         for (PropertyManipulator manipulator : manipulators.values()) {
712                             if (valueCount < manipulator.getValueCount())
713                    valueCount = manipulator.getValueCount();
714                         }
715             for (int i = 0; i < valueCount; i++) {
716                 TableViewerColumn value = new TableViewerColumn(viewer, SWT.LEFT);
717                 //value.getColumn().setText("Value " + (i+1));
718                 value.getColumn().setText("");
719                 value.getColumn().setWidth(200);
720                 valueColumns.add(value);
721                 //value.setEditingSupport(new )
722             }
723             
724             // create label providers
725             PropertyValueLabelProvider2 p = new PropertyValueLabelProvider2(this);
726             int index = 0;
727             for (TableViewerColumn c : valueColumns) {
728                 c.setLabelProvider(p);
729                 if (!readOnly) {
730                     PropertyEditingSupport support = new PropertyEditingSupport(this, viewer, index++, nodeMap);
731                     c.setEditingSupport(support);
732                 }
733             }
734                         Collections.sort(resolvedItems, new Comparator<IPropertyItem>() {
735                             @Override
736                             public int compare(IPropertyItem o1, IPropertyItem o2) {
737                                 return o1.getName().compareTo(o2.getName());
738                             }
739             });
740                         viewer.getTable().setEnabled(!readOnly);
741                         viewer.setInput(resolvedItems);
742                 }
743                 
744                 @Override
745                 public void requestFocus() {
746                         viewer.getTable().forceFocus();
747                 }
748                 
749                 @Override
750                 public void dispose() {
751                         if (node != null) {
752                                 node.removeListener(this);
753                                 node = null;
754                         }
755                         
756                 }
757                 
758                 @Override
759                 public Control getControl() {
760                         return viewer.getTable();
761                 }
762                 
763                 @Override
764                 public ISelectionProvider getSelectionProvider() {
765                         return null;
766                 }
767                 
768                 @Override
769                 public boolean isDisposed() {
770                         return viewer.getTable().isDisposed();
771                 }
772                 
773                 @Override
774                 public <T extends INode> void nodeAdded(ParentNode<T> node,
775                                 INode child, String rel) {
776                         
777                 }
778                 
779                 @Override
780                 public <T extends INode> void nodeRemoved(ParentNode<T> node,
781                                 INode child, String rel) {
782                         
783                 }
784                 
785                 @Override
786                 public void propertyChanged(INode node, final String id) {
787 //                      for (final PropertyItem item : items) {
788 //                              if (item.id.equals(id)) {
789 //                                      Display.getDefault().asyncExec(new Runnable() {
790 //                                              
791 //                                              @Override
792 //                                              public void run() {
793 //                                                      viewer.update(item, null);
794 //                                                      
795 //                                              }
796 //                                      });
797 //                              }
798 //                      }
799                         if (Thread.currentThread() == Display.getDefault().getThread()) {
800                                 if (viewer.getTable().isDisposed())
801                                         return;
802                                 if (DEBUG)System.out.println("Viewer refresh " + id);
803                                 for (IPropertyItem item : resolvedItems)
804                                         if (!item.equals(selectedItem))
805                                                 viewer.refresh(item);
806                                 if (selectedItem != null)
807                                         delayedUpdate.add(selectedItem);
808                         } else if (!editing){
809                                 // running delayed refresh when a cell editor is active would cancel cell editing.
810                                 Display.getDefault().asyncExec(new Runnable() {
811                                         @Override
812                                         public void run() {
813                                                 if (viewer.getTable().isDisposed()) {
814                                                         if (AnnotatedPropertyTab.this.node != null)
815                                                                 AnnotatedPropertyTab.this.node.removeListener(AnnotatedPropertyTab.this);
816                                                         return;
817                                                 }
818                                                 if (DEBUG) System.out.println("Viewer threaded refresh " + id);
819                                                 for (IPropertyItem item : resolvedItems)
820                                                         if (!item.equals(selectedItem))
821                                                                 viewer.refresh(item);
822                                                 if (selectedItem != null)
823                                                         delayedUpdate.add(selectedItem);
824                                                 
825                                         }
826                                 });
827                         } else {
828                                 for (IPropertyItem item : resolvedItems) {
829                                         delayedUpdate.add(item);
830                                 }
831                         }
832                         
833                 }
834                 
835
836                 
837                 @Override
838                 public void updatePartName(Consumer<String> updateCallback) {
839                         if (node != null)
840                                 updateCallback.accept(node.toString()); 
841                         
842                 }
843                 
844                 public PropertyManipulator getManipulator(IPropertyItem item) {
845                         return manipulators.get(item);
846                 }
847                 
848                 private boolean editing = false;
849                 
850                 public void setEditing(boolean editing) {
851                         this.editing = editing;
852                 }
853         }
854         
855         
856         
857         private static class PropertyEditingSupport extends EditingSupport {
858                 AnnotatedPropertyTab tab;
859                 int index;
860                 NodeMap<?,?,?> nodeMap;
861                 TableViewer viewer;
862                 CellEditor propertyItemEditor;
863                 Map<ComboPropertyItem,CellEditor> comboEditors = new HashMap<>();
864
865                 public PropertyEditingSupport(AnnotatedPropertyTab tab, TableViewer viewer, int index, NodeMap<?,?,?> nodeMap) {
866                         super(viewer);
867                         this.tab = tab;
868                         this.index = index;
869                         this.viewer = viewer;
870                         this.nodeMap = nodeMap;
871                 }
872                 
873                 @Override
874                 protected boolean canEdit(Object element) {
875                         IPropertyItem item = (IPropertyItem)element;
876                         if (tab.getManipulator(item).getValueCount() <= index)
877                                 return false;
878                         if (!item.editable())
879                                 return false;
880                         if (getValue(element) == null)
881                                 return false;
882                         return true;
883                 }
884                 
885                 @Override
886                 protected CellEditor getCellEditor(Object element) {
887                         IPropertyItem item = (IPropertyItem)element;
888                         if (tab.getManipulator(item).getValueCount() <= index)
889                                 return null;
890                         if (item instanceof PropertyItem) {
891                                 if (propertyItemEditor == null) {
892                                 propertyItemEditor = new TextCellEditor(viewer.getTable(),SWT.NONE) {
893                                         @Override
894                                         public void activate() {
895                                                 tab.setEditing(true);
896                                         }
897                                         
898                                         @Override
899                                         public void deactivate() {
900                                                 super.deactivate();
901                                                 tab.setEditing(false);
902                                         }
903                                 };
904                                 }
905                                 
906                                 if (DEBUG) System.err.println("CELL EDITOR: " + element);
907                                 return propertyItemEditor;
908                         }
909                         else if (item instanceof ComboPropertyItem) {
910                             ComboPropertyItem comboPropertyItem = (ComboPropertyItem)item;
911                             CellEditor editor = comboEditors.get(comboPropertyItem);
912                             if (editor == null) {
913                                     ComboPropertyManipulator manipulator = (ComboPropertyManipulator)tab.manipulators.get(comboPropertyItem);
914                                     editor = new ComboBoxCellEditor2(viewer.getTable(), manipulator.getItems(), SWT.DROP_DOWN | SWT.READ_ONLY) {
915                                         @Override
916                             public void activate() {
917                                 tab.setEditing(true);
918                             }
919                             
920                             @Override
921                             public void deactivate() {
922                                 super.deactivate();
923                                 tab.setEditing(false);
924                             }    
925                                     };
926                                     
927                                     comboEditors.put(comboPropertyItem, editor);
928                             }
929                             
930                             if (DEBUG) System.err.println("CELL EDITOR: " + element);
931                             return editor;
932                         }
933                         else {
934                                 throw new IllegalStateException("Unsupported property item type " + item.getClass().getName());
935                         }
936                 }
937                 
938                 @Override
939                 protected Object getValue(Object element) {
940                         IPropertyItem item = (IPropertyItem)element;
941                         PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, obj);
942                         if (manipulator.getValueCount() <= index)
943                                 return null;
944                         if (manipulator instanceof ComboPropertyManipulator) {
945                             return ((ComboPropertyManipulator)manipulator).getValueIndex();
946                         }
947                         Object value = manipulator.getValue(index);
948                         return value;
949                 }
950                 
951                 @Override
952                 protected void setValue(Object element, Object value) {
953                         
954                         IPropertyItem item = (IPropertyItem)element;
955                         PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, obj);
956                         if (manipulator.getValueCount() <= index)
957                                 throw new IllegalAccessError("Editing value in index " + index + " is not allowed.");
958                         if (DEBUG)System.err.println("CELL SET VALUE: " + element + " " + value);
959                         manipulator.setValue(value.toString(),index);
960                         viewer.refresh(item);
961                         nodeMap.commit("Set " + item.getId() + " value to " + value);
962                         
963                 }
964                 
965                 
966
967         }
968         
969         private static class PropertyItemNameProvider extends CellLabelProvider {
970
971                 
972                 @Override
973                 public void update(ViewerCell cell) {
974                         IPropertyItem item = (IPropertyItem)cell.getElement();
975
976                         if (item.getName().length() > 0)
977                                 cell.setText(item.getName());
978                         else
979                                 cell.setText(item.getId());
980                         
981                         
982                 }
983         }
984         
985         private static class PropertyValueLabelProvider2 extends CellLabelProvider {
986                 AnnotatedPropertyTab tab;
987                 //private Object object;
988                 
989                 public PropertyValueLabelProvider2(AnnotatedPropertyTab tab) {
990                         this.tab = tab;
991                 }
992                 
993                 @Override
994                 public void update(ViewerCell cell) {
995                         IPropertyItem item = (IPropertyItem)cell.getElement();
996                         int index = cell.getColumnIndex() -1;
997                         PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, object);
998                         if (manipulator.getValueCount() <= index)
999                                 return;
1000                         cell.setText(manipulator.getValue(index));
1001                 }
1002         }
1003         
1004         private static class PropertyItemContentsProvider implements IStructuredContentProvider {
1005                 @SuppressWarnings("unchecked")
1006                 @Override
1007                 public Object[] getElements(Object inputElement) {
1008                         List<PropertyItem> items = (List<PropertyItem>)inputElement;
1009                         return items.toArray();
1010                 }
1011                 
1012                 @Override
1013                 public void dispose() {
1014                         
1015                 }
1016                 
1017                 @Override
1018                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
1019                         
1020                 }
1021         }
1022         
1023         
1024         
1025         private static ViewerCell getNeighbor(ViewerCell currentCell,
1026                         int directionMask, boolean sameLevel) {
1027                 ViewerRow row;
1028                 if ((directionMask & ViewerCell.ABOVE) == ViewerCell.ABOVE) {
1029                         row = currentCell.getViewerRow().getNeighbor(ViewerRow.ABOVE,
1030                                         sameLevel);
1031                 } else if ((directionMask & ViewerCell.BELOW) == ViewerCell.BELOW) {
1032                         row = currentCell.getViewerRow().getNeighbor(ViewerRow.BELOW,
1033                                         sameLevel);
1034                 } else {
1035                         row = currentCell.getViewerRow();
1036                 }
1037                 if (row != null) {
1038                         int columnIndex;
1039                         columnIndex = getVisualIndex(row, currentCell.getColumnIndex());
1040                         int modifier = 0;
1041                         if ((directionMask & ViewerCell.LEFT) == ViewerCell.LEFT) {
1042                                 modifier = -1;
1043                         } else if ((directionMask & ViewerCell.RIGHT) == ViewerCell.RIGHT) {
1044                                 modifier = 1;
1045                         }
1046                         columnIndex += modifier;
1047                         if (columnIndex >= 0 && columnIndex < row.getColumnCount()) {
1048                                 ViewerCell cell = getCellAtVisualIndex(row, columnIndex);
1049                                 if (cell != null) {
1050                                         while (cell != null
1051                                                         && columnIndex < row.getColumnCount() - 1
1052                                                         && columnIndex > 0) {
1053                                                 if (isVisible(cell)) {
1054                                                         break;
1055                                                 }
1056                                                 columnIndex += modifier;
1057                                                 cell = getCellAtVisualIndex(row, columnIndex);
1058                                                 if (cell == null) {
1059                                                         break;
1060                                                 }
1061                                         }
1062                                 }
1063                                 return cell;
1064                         }
1065                 }
1066                 return null;
1067         }
1068         
1069         
1070         
1071         public static class TableViewerEditor extends ColumnViewerEditor {
1072                 /**
1073                  * This viewer's table editor.
1074                  */
1075                 private TableEditor tableEditor;
1076                 private TableViewerFocusCellManager focusCellManager;
1077                 private int feature;
1078
1079                 /**
1080                  * @param viewer
1081                  *            the viewer the editor is attached to
1082                  * @param focusCellManager
1083                  *            the cell focus manager if one used or <code>null</code>
1084                  * @param editorActivationStrategy
1085                  *            the strategy used to decide about the editor activation
1086                  * @param feature
1087                  *            the feature mask
1088                  */
1089                 TableViewerEditor(TableViewer viewer,
1090                                 TableViewerFocusCellManager focusCellManager,
1091                                 ColumnViewerEditorActivationStrategy editorActivationStrategy,
1092                                 int feature) {
1093                         super(viewer, editorActivationStrategy, feature);
1094                         this.feature = feature;
1095                         tableEditor = new TableEditor(viewer.getTable());
1096                         this.focusCellManager = focusCellManager;
1097                 }
1098
1099                 /**
1100                  * Create a customized editor with focusable cells
1101                  * 
1102                  * @param viewer
1103                  *            the viewer the editor is created for
1104                  * @param focusCellManager
1105                  *            the cell focus manager if one needed else
1106                  *            <code>null</code>
1107                  * @param editorActivationStrategy
1108                  *            activation strategy to control if an editor activated
1109                  * @param feature
1110                  *            bit mask controlling the editor
1111                  *            <ul>
1112                  *            <li>{@link ColumnViewerEditor#DEFAULT}</li>
1113                  *            <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
1114                  *            <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
1115                  *            <li>
1116                  *            {@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
1117                  *            <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
1118                  *            </ul>
1119                  * @see #create(TableViewer, ColumnViewerEditorActivationStrategy, int)
1120                  */
1121                 public static void create(TableViewer viewer,
1122                                 TableViewerFocusCellManager focusCellManager,
1123                                 ColumnViewerEditorActivationStrategy editorActivationStrategy,
1124                                 int feature) {
1125                         TableViewerEditor editor = new TableViewerEditor(viewer,
1126                                         focusCellManager, editorActivationStrategy, feature);
1127                         viewer.setColumnViewerEditor(editor);
1128                         if (focusCellManager != null) {
1129                                 try {
1130                                         Method m = focusCellManager.getClass().getSuperclass()
1131                                                         .getDeclaredMethod("init");
1132                                         m.setAccessible(true);
1133                                         m.invoke(focusCellManager);
1134                                 } catch (SecurityException e) {
1135                                         e.printStackTrace();
1136                                 } catch (IllegalArgumentException e) {
1137                                         e.printStackTrace();
1138                                 } catch (IllegalAccessException e) {
1139                                         e.printStackTrace();
1140                                 } catch (NoSuchMethodException e) {
1141                                         e.printStackTrace();
1142                                 } catch (InvocationTargetException e) {
1143                                         e.printStackTrace();
1144                                 }
1145                                 // focusCellManager.init();
1146                         }
1147                 }
1148
1149                 /**
1150                  * Create a customized editor whose activation process is customized
1151                  * 
1152                  * @param viewer
1153                  *            the viewer the editor is created for
1154                  * @param editorActivationStrategy
1155                  *            activation strategy to control if an editor activated
1156                  * @param feature
1157                  *            bit mask controlling the editor
1158                  *            <ul>
1159                  *            <li>{@link ColumnViewerEditor#DEFAULT}</li>
1160                  *            <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
1161                  *            <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
1162                  *            <li>
1163                  *            {@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
1164                  *            <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
1165                  *            </ul>
1166                  */
1167                 public static void create(TableViewer viewer,
1168                                 ColumnViewerEditorActivationStrategy editorActivationStrategy,
1169                                 int feature) {
1170                         create(viewer, null, editorActivationStrategy, feature);
1171                 }
1172
1173                 protected void setEditor(Control w, Item item, int columnNumber) {
1174                         tableEditor.setEditor(w, (TableItem) item, columnNumber);
1175                 }
1176
1177                 protected void setLayoutData(LayoutData layoutData) {
1178                         tableEditor.grabHorizontal = layoutData.grabHorizontal;
1179                         tableEditor.horizontalAlignment = layoutData.horizontalAlignment;
1180                         tableEditor.minimumWidth = layoutData.minimumWidth;
1181                 }
1182
1183                 public ViewerCell getFocusCell() {
1184                         if (focusCellManager != null) {
1185                                 return focusCellManager.getFocusCell();
1186                         }
1187                         return super.getFocusCell();
1188                 }
1189
1190                 protected void updateFocusCell(ViewerCell focusCell,
1191                                 ColumnViewerEditorActivationEvent event) {
1192                         // Update the focus cell when we activated the editor with these 2
1193                         // events
1194                         if (event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
1195                                         || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL) {
1196                                 if (focusCellManager != null) {
1197                                         try {
1198                                                 if (DEBUG)System.err.println("FOCUS CELL: " + focusCell);
1199                                                 
1200                                                 Method m = AbstractTableViewer.class.getDeclaredMethod(
1201                                                                 "getSelectionFromWidget");
1202                                                 m.setAccessible(true);
1203                                                 @SuppressWarnings("rawtypes")
1204                                                 List l = (List) m.invoke(getViewer());
1205                                                 if (focusCellManager != null) {
1206                                                         m = focusCellManager
1207                                                                         .getClass()
1208                                                                         .getSuperclass()
1209                                                                         .getDeclaredMethod("setFocusCell",
1210                                                                                         new Class[] { ViewerCell.class });
1211                                                         m.setAccessible(true);
1212                                                         m.invoke(focusCellManager,
1213                                                                         new Object[] { focusCell });
1214                                                 }
1215                                                 if (!l.contains(focusCell.getElement())) {
1216                                                         getViewer().setSelection(
1217                                                                         new StructuredSelection(focusCell
1218                                                                                         .getElement()));
1219                                                 }
1220                                         } catch (SecurityException e) {
1221                                                 e.printStackTrace();
1222                                         } catch (IllegalArgumentException e) {
1223                                                 e.printStackTrace();
1224                                         } catch (IllegalAccessException e) {
1225                                                 e.printStackTrace();
1226                                         } catch (NoSuchMethodException e) {
1227                                                 e.printStackTrace();
1228                                         } catch (InvocationTargetException e) {
1229                                                 e.printStackTrace();
1230                                         }
1231                                 }
1232                         }
1233                 }
1234
1235                 protected void processTraverseEvent(int columnIndex, ViewerRow row,
1236                                 TraverseEvent event) {
1237                         ViewerCell cell2edit = null;
1238                         if (event.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
1239                                 event.doit = false;
1240                                 if ((event.stateMask & SWT.CTRL) == SWT.CTRL
1241                                                 && (feature & TABBING_VERTICAL) == TABBING_VERTICAL) {
1242                                         cell2edit = searchCellAboveBelow(row, getViewer(),
1243                                                         columnIndex, true);
1244                                 } else if ((feature & TABBING_HORIZONTAL) == TABBING_HORIZONTAL) {
1245                                         cell2edit = searchPreviousCell(row,
1246                                                         row.getCell(columnIndex), row.getCell(columnIndex),
1247                                                         getViewer());
1248                                 }
1249                         } else if (event.detail == SWT.TRAVERSE_TAB_NEXT) {
1250                                 event.doit = false;
1251                                 if ((event.stateMask & SWT.CTRL) == SWT.CTRL
1252                                                 && (feature & TABBING_VERTICAL) == TABBING_VERTICAL) {
1253                                         cell2edit = searchCellAboveBelow(row, getViewer(),
1254                                                         columnIndex, false);
1255                                 } else if ((feature & TABBING_HORIZONTAL) == TABBING_HORIZONTAL) {
1256                                         cell2edit = searchNextCell(row, row.getCell(columnIndex),
1257                                                         row.getCell(columnIndex), getViewer());
1258                                 }
1259                         }
1260                         if (DEBUG) System.err.println("NEXT CELL: " + cell2edit);
1261                         if (cell2edit != null) {
1262                                 getViewer().getControl().setRedraw(false);
1263                                 ColumnViewerEditorActivationEvent acEvent = new ColumnViewerEditorActivationEvent(
1264                                                 cell2edit, event);
1265                                 try {
1266                                         Method m = ColumnViewer.class
1267                                                         .getDeclaredMethod(
1268                                                                         "triggerEditorActivationEvent",
1269                                                                         new Class[] { ColumnViewerEditorActivationEvent.class });
1270                                         m.setAccessible(true);
1271                                         m.invoke(getViewer(), new Object[] { acEvent });
1272                                 } catch (SecurityException e) {
1273                                         e.printStackTrace();
1274                                 } catch (NoSuchMethodException e) {
1275                                         e.printStackTrace();
1276                                 } catch (IllegalArgumentException e) {
1277                                         e.printStackTrace();
1278                                 } catch (IllegalAccessException e) {
1279                                         e.printStackTrace();
1280                                 } catch (InvocationTargetException e) {
1281                                         e.printStackTrace();
1282                                 }
1283                                 getViewer().getControl().setRedraw(true);
1284                         }
1285                 }
1286
1287                 private ViewerCell searchCellAboveBelow(ViewerRow row,
1288                                 ColumnViewer viewer, int columnIndex, boolean above) {
1289                         ViewerCell rv = null;
1290                         ViewerRow newRow = null;
1291                         if (above) {
1292                                 newRow = row.getNeighbor(ViewerRow.ABOVE, false);
1293                         } else {
1294                                 newRow = row.getNeighbor(ViewerRow.BELOW, false);
1295                         }
1296                         try {
1297                                 if (newRow != null) {
1298                                         Method m = ColumnViewer.class.getDeclaredMethod(
1299                                                         "getViewerColumn", new Class[] { int.class });
1300                                         m.setAccessible(true);
1301                                         ViewerColumn column = (ViewerColumn) m.invoke(viewer,
1302                                                         new Object[] { new Integer(columnIndex) });
1303                                         m = ViewerColumn.class.getDeclaredMethod(
1304                                                         "getEditingSupport");
1305                                         m.setAccessible(true);
1306                                         EditingSupport es = (EditingSupport) m.invoke(column);
1307                                         if (column != null && es != null) {
1308                                                 m = EditingSupport.class.getDeclaredMethod("canEdit",
1309                                                                 new Class[] { Object.class });
1310                                                 m.setAccessible(true);
1311                                                 Boolean b = (Boolean) m.invoke(es,
1312                                                                 new Object[] { newRow.getItem().getData() });
1313                                                 if (b.booleanValue()) {
1314                                                         rv = newRow.getCell(columnIndex);
1315                                                 }
1316                                         } else {
1317                                                 rv = searchCellAboveBelow(newRow, viewer, columnIndex,
1318                                                                 above);
1319                                         }
1320                                 }
1321                         } catch (Exception e) {
1322                                 e.printStackTrace();
1323                         }
1324                         return rv;
1325                 }
1326
1327                 private ViewerCell searchPreviousCell(ViewerRow row,
1328                                 ViewerCell currentCell, ViewerCell originalCell,
1329                                 ColumnViewer viewer) {
1330                         ViewerCell rv = null;
1331                         ViewerCell previousCell;
1332                         if (currentCell != null) {
1333                                 previousCell = getNeighbor(currentCell, ViewerCell.LEFT, true);
1334                         } else {
1335                                 if (row.getColumnCount() != 0) {
1336                                         previousCell = row.getCell(getCreationIndex(row,
1337                                                         row.getColumnCount() - 1));
1338                                 } else {
1339                                         previousCell = row.getCell(0);
1340                                 }
1341                         }
1342                         // No endless loop
1343                         if (originalCell.equals(previousCell)) {
1344                                 return null;
1345                         }
1346                         if (previousCell != null) {
1347                                 if (isCellEditable(viewer, previousCell)) {
1348                                         rv = previousCell;
1349                                 } else {
1350                                         rv = searchPreviousCell(row, previousCell, originalCell,
1351                                                         viewer);
1352                                 }
1353                         } else {
1354                                 if ((feature & TABBING_CYCLE_IN_ROW) == TABBING_CYCLE_IN_ROW) {
1355                                         rv = searchPreviousCell(row, null, originalCell, viewer);
1356                                 } else if ((feature & TABBING_MOVE_TO_ROW_NEIGHBOR) == TABBING_MOVE_TO_ROW_NEIGHBOR) {
1357                                         ViewerRow rowAbove = row
1358                                                         .getNeighbor(ViewerRow.ABOVE, false);
1359                                         if (rowAbove != null) {
1360                                                 rv = searchPreviousCell(rowAbove, null, originalCell,
1361                                                                 viewer);
1362                                         }
1363                                 }
1364                         }
1365                         return rv;
1366                 }
1367
1368                 private ViewerCell searchNextCell(ViewerRow row,
1369                                 ViewerCell currentCell, ViewerCell originalCell,
1370                                 ColumnViewer viewer) {
1371                         ViewerCell rv = null;
1372                         ViewerCell nextCell;
1373                         if (currentCell != null) {
1374                                 nextCell = getNeighbor(currentCell, ViewerCell.RIGHT, true);
1375                         } else {
1376                                 nextCell = row.getCell(getCreationIndex(row, 0));
1377                         }
1378                         // No endless loop
1379                         if (originalCell.equals(nextCell)) {
1380                                 return null;
1381                         }
1382                         if (nextCell != null) {
1383                                 if (isCellEditable(viewer, nextCell)) {
1384                                         rv = nextCell;
1385                                 } else {
1386                                         rv = searchNextCell(row, nextCell, originalCell, viewer);
1387                                 }
1388                         } else {
1389                                 if ((feature & TABBING_CYCLE_IN_ROW) == TABBING_CYCLE_IN_ROW) {
1390                                         rv = searchNextCell(row, null, originalCell, viewer);
1391                                 } else if ((feature & TABBING_MOVE_TO_ROW_NEIGHBOR) == TABBING_MOVE_TO_ROW_NEIGHBOR) {
1392                                         ViewerRow rowBelow = row
1393                                                         .getNeighbor(ViewerRow.BELOW, false);
1394                                         if (rowBelow != null) {
1395                                                 rv = searchNextCell(rowBelow, null, originalCell,
1396                                                                 viewer);
1397                                         }
1398                                 }
1399                         }
1400                         return rv;
1401                 }
1402
1403                 private boolean isCellEditable(ColumnViewer viewer, ViewerCell cell) {
1404                         try {
1405                                 Method m = ColumnViewer.class.getDeclaredMethod(
1406                                                 "getViewerColumn", new Class[] { int.class });
1407                                 m.setAccessible(true);
1408                                 ViewerColumn column = (ViewerColumn) m.invoke(viewer,
1409                                                 new Object[] { new Integer(cell.getColumnIndex()) });
1410                                 m = ViewerColumn.class.getDeclaredMethod("getEditingSupport");
1411                                 m.setAccessible(true);
1412                                 EditingSupport es = (EditingSupport) m.invoke(column);
1413                                 if (column != null && es != null) {
1414                                         m = EditingSupport.class.getDeclaredMethod("canEdit",
1415                                                         new Class[] { Object.class });
1416                                         m.setAccessible(true);
1417                                         // return true;
1418                                         Boolean b = (Boolean) m.invoke(es,
1419                                                         new Object[] { cell.getElement() });
1420                                         return b.booleanValue();
1421                                 }
1422                         } catch (Exception e) {
1423                                 e.printStackTrace();
1424                         }
1425                         return false;
1426                 }
1427         }
1428         
1429         // Reimplementation of ViewerCell-Methods
1430         private static int getVisualIndex(ViewerRow row, int creationIndex) {
1431                 TableItem item = (TableItem) row.getItem();
1432                 int[] order = item.getParent().getColumnOrder();
1433                 for (int i = 0; i < order.length; i++) {
1434                         if (order[i] == creationIndex) {
1435                                 return i;
1436                         }
1437                 }
1438                 return creationIndex;
1439         }
1440         
1441         private static int getCreationIndex(ViewerRow row, int visualIndex) {
1442                 TableItem item = (TableItem) row.getItem();
1443                 if (item != null && !item.isDisposed() /*
1444                                                                                                  * && hasColumns() &&
1445                                                                                                  * isValidOrderIndex
1446                                                                                                  * (visualIndex)
1447                                                                                                  */) {
1448                         return item.getParent().getColumnOrder()[visualIndex];
1449                 }
1450                 return visualIndex;
1451         }
1452         
1453         private static ViewerCell getCellAtVisualIndex(ViewerRow row,
1454                         int visualIndex) {
1455                 return getCell(row, getCreationIndex(row, visualIndex));
1456         }
1457
1458         private static boolean isVisible(ViewerCell cell) {
1459                 return getWidth(cell) > 0;
1460         }
1461
1462         private static int getWidth(ViewerCell cell) {
1463                 TableItem item = (TableItem) cell.getViewerRow().getItem();
1464                 return item.getParent().getColumn(cell.getColumnIndex()).getWidth();
1465         }
1466
1467         private static ViewerCell getCell(ViewerRow row, int index) {
1468                 return row.getCell(index);
1469         }
1470         
1471         
1472 }