1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.property;
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;
28 import java.util.function.Consumer;
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.ComboBoxCellEditor;
43 import org.eclipse.jface.viewers.EditingSupport;
44 import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
45 import org.eclipse.jface.viewers.ISelection;
46 import org.eclipse.jface.viewers.ISelectionChangedListener;
47 import org.eclipse.jface.viewers.ISelectionProvider;
48 import org.eclipse.jface.viewers.IStructuredContentProvider;
49 import org.eclipse.jface.viewers.SelectionChangedEvent;
50 import org.eclipse.jface.viewers.StructuredSelection;
51 import org.eclipse.jface.viewers.TableViewer;
52 import org.eclipse.jface.viewers.TableViewerColumn;
53 import org.eclipse.jface.viewers.TableViewerFocusCellManager;
54 import org.eclipse.jface.viewers.TextCellEditor;
55 import org.eclipse.jface.viewers.Viewer;
56 import org.eclipse.jface.viewers.ViewerCell;
57 import org.eclipse.jface.viewers.ViewerColumn;
58 import org.eclipse.jface.viewers.ViewerRow;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.custom.TableEditor;
61 import org.eclipse.swt.events.TraverseEvent;
62 import org.eclipse.swt.widgets.Composite;
63 import org.eclipse.swt.widgets.Control;
64 import org.eclipse.swt.widgets.Display;
65 import org.eclipse.swt.widgets.Event;
66 import org.eclipse.swt.widgets.Item;
67 import org.eclipse.swt.widgets.TableColumn;
68 import org.eclipse.swt.widgets.TableItem;
69 import org.eclipse.ui.IWorkbenchSite;
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;
90 public class AnnotatedPropertyTabContributorFactory implements PropertyTabContributorFactory {
92 private static final boolean DEBUG = false;
94 @SuppressWarnings("unchecked")
96 public List<PropertyTabContributor> getContributors(Object input) {
97 Map<String,IPropertyItem> items = new LinkedHashMap<String, IPropertyItem>();
98 List<String> blacklist = new ArrayList<String>();
100 collectItems(input.getClass(), items);
101 collectBlacklist(input.getClass(), blacklist);
102 } catch (InstantiationException e) {
103 // TODO Auto-generated catch block
105 } catch (IllegalAccessException e) {
106 // TODO Auto-generated catch block
110 if (items.size() == 0)
111 return Collections.EMPTY_LIST;
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);
123 for (String s : blacklist) {
127 List<PropertyTabContributor> contributors = new ArrayList<PropertyTabContributor>(tabs.size());
128 for (String tabId : tabs) {
129 contributors.add(new AnnotatedPropertyTabContributor(tabId, tabMap.getValues(tabId)));
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);
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());
148 item = new PropertyItem(get.value());
149 items.put(item.id, item);
153 item.manipulatorClass = get.manipulator().newInstance().get(m,null);
155 item.tabId = get.tabId();
157 item.name = get.name();
160 } else if (annotation.annotationType().equals(SetPropertyValue.class)) {
161 SetPropertyValue set = (SetPropertyValue)annotation;
162 PropertyItem item = (PropertyItem)items.get(set.value());
164 item = new PropertyItem(set.value());
165 items.put(item.id, item);
169 } else if (annotation.annotationType().equals(CompoundGetPropertyValue.class)) {
170 CompoundGetPropertyValue get = (CompoundGetPropertyValue)annotation;
171 CompoundPropertyItem item = (CompoundPropertyItem)items.get(get.value());
173 item = new CompoundPropertyItem(get.value());
174 items.put(item.id, item);
178 item.manipulatorFactory = get.manipulator().newInstance();
180 item.tabId = get.tabId();
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());
187 item = new CompoundPropertyItem(set.value());
188 items.put(item.id, item);
192 } else if (annotation.annotationType().equals(GetComboPropertyValue.class)) {
193 GetComboPropertyValue get = (GetComboPropertyValue)annotation;
194 ComboPropertyItem item = (ComboPropertyItem)items.get(get.value());
196 item = new ComboPropertyItem(get.value());
197 items.put(item.id, item);
200 item.manipulatorClass = ComboPropertyManipulator.class;
202 item.tabId = get.tabId();
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());
209 item = new ComboPropertyItem(set.value());
210 items.put(item.id, item);
213 } else if (annotation.annotationType().equals(GetComboProperty.class)) {
214 GetComboProperty get = (GetComboProperty)annotation;
215 ComboPropertyItem item = (ComboPropertyItem)items.get(get.value());
217 item = new ComboPropertyItem(get.value());
218 items.put(item.id, item);
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);
233 PropertyTabBlacklist ann = clazz.getAnnotation(PropertyTabBlacklist.class);
236 String s = ann.value();
241 for (String item : s.split(";")) {
248 private static Map<PropertyItem, PropertyManipulator> createManipulators(CompoundPropertyItem item, Object obj) {
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;
262 i.tabId = item.tabId;
263 result.put(i,manipulator);
266 } catch (Exception e) {
268 return Collections.emptyMap();
273 private static PropertyManipulator createManipulator(PropertyItem item, Object obj) {
275 MethodValueProvider provider = new MethodValueProvider(item.getter, item.setter);
276 PropertyManipulator manipulator = item.manipulatorClass.getConstructor(ValueProvider.class,Object.class).newInstance(provider,obj);
278 } catch (Exception e) {
284 private static PropertyManipulator createManipulator(ComboPropertyItem item, Object obj) {
286 MethodComboValueProvider provider = new MethodComboValueProvider(item.getter, item.setter,item.values);
287 PropertyManipulator manipulator = item.manipulatorClass.getConstructor(ValueProvider.class,Object.class).newInstance(provider,obj);
289 } catch (Exception e) {
295 private static interface IPropertyItem {
296 public String getTabId();
297 public String getName();
298 public String getId();
299 public boolean editable();
302 private static class PropertyItem implements IPropertyItem{
305 private String tabId;
306 private Method getter;
307 private Method setter;
308 private Class<? extends PropertyManipulator> manipulatorClass;
311 public PropertyItem(String id) {
313 throw new NullPointerException();
318 public String getId() {
323 public int hashCode() {
324 return id.hashCode();
328 public String getTabId() {
333 public String getName() {
338 public boolean editable() {
339 return setter != null;
343 private static class CompoundPropertyItem implements IPropertyItem{
346 private String tabId;
347 private Method getter;
348 private Method setter;
349 private PropertyManipulatorFactory manipulatorFactory;
352 public CompoundPropertyItem(String id) {
354 throw new NullPointerException();
359 public String getId() {
364 public int hashCode() {
365 return id.hashCode();
369 public String getTabId() {
374 public String getName() {
379 public boolean editable() {
380 return setter != null;
384 private static class ComboPropertyItem implements IPropertyItem{
387 private String tabId;
388 private Method getter;
389 private Method setter;
390 private Method values;
391 private Class<? extends ComboPropertyManipulator> manipulatorClass;
394 public ComboPropertyItem(String id) {
396 throw new NullPointerException();
401 public String getId() {
406 public int hashCode() {
407 return id.hashCode();
411 public String getTabId() {
416 public String getName() {
421 public boolean editable() {
422 return setter != null;
426 private static class AnnotatedPropertyTabContributor implements PropertyTabContributor {
428 List<IPropertyItem> items;
430 public AnnotatedPropertyTabContributor(String id, List<IPropertyItem> items) {
432 throw new NullPointerException();
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);
446 public String getId() {
452 private static class AnnotatedPropertyTab implements IPropertyTab2, NodeListener {
454 List<IPropertyItem> contibutedItems;
455 List<IPropertyItem> resolvedItems = new ArrayList<IPropertyItem>();
456 private Map<IPropertyItem,PropertyManipulator> manipulators = new HashMap<IPropertyItem, PropertyManipulator>();
458 private TableViewer viewer;
461 private NodeMap<?,?,?> nodeMap;
463 private List<TableViewerColumn> valueColumns = new ArrayList<TableViewerColumn>();
465 public AnnotatedPropertyTab(String id, List<IPropertyItem> items) {
467 this.contibutedItems = items;
473 public void createControl(Composite parent, ISessionContext context) {
474 //parent.setLayout(new FillLayout());
475 viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.SINGLE);
477 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(viewer.getTable());
479 //viewer.setLabelProvider(new AnnotatedTableLabelProvider(object))
481 viewer.setContentProvider(new PropertyItemContentsProvider());
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() {
494 public void selectionChanged(SelectionChangedEvent event) {
495 PropertyItem item = AdaptationUtils.adaptToSingle(event.getSelection(),PropertyItem.class);
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));
503 c.getColumn().setText("");
511 viewer.getTable().setHeaderVisible(true);
512 viewer.getTable().setLinesVisible(true);
513 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
516 public void selectionChanged(SelectionChangedEvent event) {
517 IPropertyItem item = AdaptationUtils.adaptToSingle(event.getSelection(), IPropertyItem.class);
519 if (!manipulators.get(selectedItem).getEditMode())
520 manipulators.get(selectedItem).setEditMode(true);
523 for (IPropertyItem i : delayedUpdate) {
524 if (!i.equals(selectedItem)) {
525 manipulators.get(i).setEditMode(false);
526 viewer.update(i,null);
529 if (delayedUpdate.contains(selectedItem)) {
530 delayedUpdate.clear();
531 delayedUpdate.add(selectedItem);
533 delayedUpdate.clear();
538 CellNavigationStrategy nStrategy = new CellNavigationStrategy() {
539 private ViewerCell internalFindSelectedCell(
540 ColumnViewer viewer, ViewerCell currentSelectedCell,
542 switch (event.keyCode) {
544 if (currentSelectedCell != null) {
545 return getNeighbor(currentSelectedCell,
546 ViewerCell.ABOVE, false);
550 if (currentSelectedCell != null) {
551 return getNeighbor(currentSelectedCell,
552 ViewerCell.BELOW, false);
556 if (currentSelectedCell != null) {
557 return getNeighbor(currentSelectedCell,
558 ViewerCell.LEFT, true);
561 case SWT.ARROW_RIGHT:
562 if (currentSelectedCell != null) {
563 return getNeighbor(currentSelectedCell,
564 ViewerCell.RIGHT, true);
571 public ViewerCell findSelectedCell(ColumnViewer viewer,
572 ViewerCell currentSelectedCell, Event event) {
573 ViewerCell cell = internalFindSelectedCell(viewer,
574 currentSelectedCell, event);
576 TableColumn t = AnnotatedPropertyTab.this.viewer.getTable().getColumn(
577 cell.getColumnIndex());
578 AnnotatedPropertyTab.this.viewer.getTable().showColumn(t);
584 TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(
585 viewer, new FocusCellOwnerDrawHighlighter(viewer));
587 Field f = focusCellManager.getClass().getSuperclass()
588 .getDeclaredField("navigationStrategy");
589 f.setAccessible(true);
590 f.set(focusCellManager, nStrategy);
591 } catch (SecurityException e) {
593 } catch (NoSuchFieldException e) {
595 } catch (IllegalArgumentException e) {
597 } catch (IllegalAccessException e) {
600 ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
602 protected boolean isEditorActivationEvent(
603 ColumnViewerEditorActivationEvent event) {
604 return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
605 || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
606 || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
607 || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
610 TableViewerEditor.create(viewer, focusCellManager, actSupport,
611 ColumnViewerEditor.TABBING_HORIZONTAL
612 | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
613 | ColumnViewerEditor.TABBING_VERTICAL
614 | ColumnViewerEditor.KEYBOARD_ACTIVATION);
615 viewer.getColumnViewerEditor().addEditorActivationListener(
616 new ColumnViewerEditorActivationListener() {
617 public void afterEditorActivated(
618 ColumnViewerEditorActivationEvent event) {
621 public void afterEditorDeactivated(
622 ColumnViewerEditorDeactivationEvent event) {
625 public void beforeEditorActivated(
626 ColumnViewerEditorActivationEvent event) {
627 ViewerCell cell = (ViewerCell) event.getSource();
628 viewer.getTable().showColumn(
629 viewer.getTable().getColumn(cell.getColumnIndex()));
632 public void beforeEditorDeactivated(
633 ColumnViewerEditorDeactivationEvent event) {
641 private IPropertyItem selectedItem = null;
642 private Set<IPropertyItem> delayedUpdate = new HashSet<IPropertyItem>();
646 public void setInput(ISessionContext context, ISelection selection,
648 Collection<INode> nodes = AdaptationUtils.adaptToCollection(selection, INode.class);
649 if (nodes.size() != 1) {
651 node.removeListener(this);
656 INode n = nodes.iterator().next();
658 if (!node.equals(n)) {
659 node.removeListener(this);
669 private void setInput(INode node) {
671 this.node.addListener(this);
679 if (n instanceof NodeMapProvider<?,?,?>) {
680 nodeMap = ((NodeMapProvider<?,?,?>) n).getNodeMap();
684 n = (INode)n.getParent();
686 boolean readOnly = (node instanceof IStructuralNode && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot());
688 resolvedItems.clear();
689 manipulators.clear();
690 for (IPropertyItem item : contibutedItems) {
691 if (item instanceof PropertyItem) {
692 resolvedItems.add((PropertyItem)item);
693 manipulators.put((PropertyItem)item, createManipulator((PropertyItem)item, node));
694 } else if (item instanceof ComboPropertyItem) {
695 resolvedItems.add((ComboPropertyItem)item);
696 manipulators.put((ComboPropertyItem)item, createManipulator((ComboPropertyItem)item, node));
697 } else if (item instanceof CompoundPropertyItem) {
698 CompoundPropertyItem compound = (CompoundPropertyItem)item;
699 Map<PropertyItem, PropertyManipulator> manipulators = createManipulators(compound, node);
700 for (PropertyItem i : manipulators.keySet()) {
701 resolvedItems.add(i);
702 this.manipulators.put(i, manipulators.get(i));
710 for (PropertyManipulator manipulator : manipulators.values()) {
711 if (valueCount < manipulator.getValueCount())
712 valueCount = manipulator.getValueCount();
714 for (int i = 0; i < valueCount; i++) {
715 TableViewerColumn value = new TableViewerColumn(viewer, SWT.LEFT);
716 //value.getColumn().setText("Value " + (i+1));
717 value.getColumn().setText("");
718 value.getColumn().setWidth(200);
719 valueColumns.add(value);
720 //value.setEditingSupport(new )
723 // create label providers
724 PropertyValueLabelProvider2 p = new PropertyValueLabelProvider2(this);
726 for (TableViewerColumn c : valueColumns) {
727 c.setLabelProvider(p);
729 PropertyEditingSupport support = new PropertyEditingSupport(this, viewer, index++, nodeMap);
730 c.setEditingSupport(support);
733 Collections.sort(resolvedItems, new Comparator<IPropertyItem>() {
735 public int compare(IPropertyItem o1, IPropertyItem o2) {
736 return o1.getName().compareTo(o2.getName());
739 viewer.getTable().setEnabled(!readOnly);
740 viewer.setInput(resolvedItems);
744 public void requestFocus() {
745 viewer.getTable().forceFocus();
749 public void dispose() {
751 node.removeListener(this);
758 public Control getControl() {
759 return viewer.getTable();
763 public ISelectionProvider getSelectionProvider() {
768 public boolean isDisposed() {
769 return viewer.getTable().isDisposed();
773 public <T extends INode> void nodeAdded(ParentNode<T> node,
774 INode child, String rel) {
779 public <T extends INode> void nodeRemoved(ParentNode<T> node,
780 INode child, String rel) {
785 public void propertyChanged(INode node, final String id) {
786 // for (final PropertyItem item : items) {
787 // if (item.id.equals(id)) {
788 // Display.getDefault().asyncExec(new Runnable() {
791 // public void run() {
792 // viewer.update(item, null);
798 if (Thread.currentThread() == Display.getDefault().getThread()) {
799 if (viewer.getTable().isDisposed())
801 if (DEBUG)System.out.println("Viewer refresh " + id);
802 for (IPropertyItem item : resolvedItems)
803 if (!item.equals(selectedItem))
804 viewer.refresh(item);
805 if (selectedItem != null)
806 delayedUpdate.add(selectedItem);
807 } else if (!editing){
808 // running delayed refresh when a cell editor is active would cancel cell editing.
809 Display.getDefault().asyncExec(new Runnable() {
812 if (viewer.getTable().isDisposed()) {
813 if (AnnotatedPropertyTab.this.node != null)
814 AnnotatedPropertyTab.this.node.removeListener(AnnotatedPropertyTab.this);
817 if (DEBUG) System.out.println("Viewer threaded refresh " + id);
818 for (IPropertyItem item : resolvedItems)
819 if (!item.equals(selectedItem))
820 viewer.refresh(item);
821 if (selectedItem != null)
822 delayedUpdate.add(selectedItem);
827 for (IPropertyItem item : resolvedItems) {
828 delayedUpdate.add(item);
837 public void updatePartName(Consumer<String> updateCallback) {
839 updateCallback.accept(node.toString());
843 public PropertyManipulator getManipulator(IPropertyItem item) {
844 return manipulators.get(item);
847 private boolean editing = false;
849 public void setEditing(boolean editing) {
850 this.editing = editing;
856 private static class PropertyEditingSupport extends EditingSupport {
857 AnnotatedPropertyTab tab;
859 NodeMap<?,?,?> nodeMap;
863 public PropertyEditingSupport(AnnotatedPropertyTab tab, TableViewer viewer, int index, NodeMap<?,?,?> nodeMap) {
867 this.viewer = viewer;
868 this.nodeMap = nodeMap;
872 protected boolean canEdit(Object element) {
873 IPropertyItem item = (IPropertyItem)element;
874 if (tab.getManipulator(item).getValueCount() <= index)
876 if (!item.editable())
878 if (getValue(element) == null)
884 protected CellEditor getCellEditor(Object element) {
885 IPropertyItem item = (IPropertyItem)element;
886 if (tab.getManipulator(item).getValueCount() <= index)
889 if (item instanceof PropertyItem)
890 editor = new TextCellEditor(viewer.getTable(),SWT.NONE) {
892 public void activate() {
893 tab.setEditing(true);
897 public void deactivate() {
899 tab.setEditing(false);
902 else if (item instanceof ComboPropertyItem) {
903 ComboPropertyItem comboPropertyItem = (ComboPropertyItem)item;
904 ComboPropertyManipulator manipulator = (ComboPropertyManipulator)tab.manipulators.get(comboPropertyItem);
905 editor = new ComboBoxCellEditor(viewer.getTable(), manipulator.getItems()) {
907 public void activate() {
908 tab.setEditing(true);
912 public void deactivate() {
914 tab.setEditing(false);
918 if (DEBUG)System.err.println("CELL EDITOR: " + element);
923 protected Object getValue(Object element) {
924 IPropertyItem item = (IPropertyItem)element;
925 PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, obj);
926 if (manipulator.getValueCount() <= index)
928 if (manipulator instanceof ComboPropertyManipulator) {
929 return ((ComboPropertyManipulator)manipulator).getValueIndex();
931 Object value = manipulator.getValue(index);
936 protected void setValue(Object element, Object value) {
938 IPropertyItem item = (IPropertyItem)element;
939 PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, obj);
940 if (manipulator.getValueCount() <= index)
941 throw new IllegalAccessError("Editing value in index " + index + " is not allowed.");
942 if (DEBUG)System.err.println("CELL SET VALUE: " + element + " " + value);
943 manipulator.setValue(value.toString(),index);
944 viewer.refresh(item);
945 nodeMap.commit("Set " + item.getId() + " value to " + value);
953 private static class PropertyItemNameProvider extends CellLabelProvider {
957 public void update(ViewerCell cell) {
958 IPropertyItem item = (IPropertyItem)cell.getElement();
960 if (item.getName().length() > 0)
961 cell.setText(item.getName());
963 cell.setText(item.getId());
969 private static class PropertyValueLabelProvider2 extends CellLabelProvider {
970 AnnotatedPropertyTab tab;
971 //private Object object;
973 public PropertyValueLabelProvider2(AnnotatedPropertyTab tab) {
978 public void update(ViewerCell cell) {
979 IPropertyItem item = (IPropertyItem)cell.getElement();
980 int index = cell.getColumnIndex() -1;
981 PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, object);
982 if (manipulator.getValueCount() <= index)
984 cell.setText(manipulator.getValue(index));
988 private static class PropertyItemContentsProvider implements IStructuredContentProvider {
989 @SuppressWarnings("unchecked")
991 public Object[] getElements(Object inputElement) {
992 List<PropertyItem> items = (List<PropertyItem>)inputElement;
993 return items.toArray();
997 public void dispose() {
1002 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
1009 private static ViewerCell getNeighbor(ViewerCell currentCell,
1010 int directionMask, boolean sameLevel) {
1012 if ((directionMask & ViewerCell.ABOVE) == ViewerCell.ABOVE) {
1013 row = currentCell.getViewerRow().getNeighbor(ViewerRow.ABOVE,
1015 } else if ((directionMask & ViewerCell.BELOW) == ViewerCell.BELOW) {
1016 row = currentCell.getViewerRow().getNeighbor(ViewerRow.BELOW,
1019 row = currentCell.getViewerRow();
1023 columnIndex = getVisualIndex(row, currentCell.getColumnIndex());
1025 if ((directionMask & ViewerCell.LEFT) == ViewerCell.LEFT) {
1027 } else if ((directionMask & ViewerCell.RIGHT) == ViewerCell.RIGHT) {
1030 columnIndex += modifier;
1031 if (columnIndex >= 0 && columnIndex < row.getColumnCount()) {
1032 ViewerCell cell = getCellAtVisualIndex(row, columnIndex);
1035 && columnIndex < row.getColumnCount() - 1
1036 && columnIndex > 0) {
1037 if (isVisible(cell)) {
1040 columnIndex += modifier;
1041 cell = getCellAtVisualIndex(row, columnIndex);
1055 public static class TableViewerEditor extends ColumnViewerEditor {
1057 * This viewer's table editor.
1059 private TableEditor tableEditor;
1060 private TableViewerFocusCellManager focusCellManager;
1061 private int feature;
1065 * the viewer the editor is attached to
1066 * @param focusCellManager
1067 * the cell focus manager if one used or <code>null</code>
1068 * @param editorActivationStrategy
1069 * the strategy used to decide about the editor activation
1073 TableViewerEditor(TableViewer viewer,
1074 TableViewerFocusCellManager focusCellManager,
1075 ColumnViewerEditorActivationStrategy editorActivationStrategy,
1077 super(viewer, editorActivationStrategy, feature);
1078 this.feature = feature;
1079 tableEditor = new TableEditor(viewer.getTable());
1080 this.focusCellManager = focusCellManager;
1084 * Create a customized editor with focusable cells
1087 * the viewer the editor is created for
1088 * @param focusCellManager
1089 * the cell focus manager if one needed else
1091 * @param editorActivationStrategy
1092 * activation strategy to control if an editor activated
1094 * bit mask controlling the editor
1096 * <li>{@link ColumnViewerEditor#DEFAULT}</li>
1097 * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
1098 * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
1100 * {@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
1101 * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
1103 * @see #create(TableViewer, ColumnViewerEditorActivationStrategy, int)
1105 public static void create(TableViewer viewer,
1106 TableViewerFocusCellManager focusCellManager,
1107 ColumnViewerEditorActivationStrategy editorActivationStrategy,
1109 TableViewerEditor editor = new TableViewerEditor(viewer,
1110 focusCellManager, editorActivationStrategy, feature);
1111 viewer.setColumnViewerEditor(editor);
1112 if (focusCellManager != null) {
1114 Method m = focusCellManager.getClass().getSuperclass()
1115 .getDeclaredMethod("init");
1116 m.setAccessible(true);
1117 m.invoke(focusCellManager);
1118 } catch (SecurityException e) {
1119 e.printStackTrace();
1120 } catch (IllegalArgumentException e) {
1121 e.printStackTrace();
1122 } catch (IllegalAccessException e) {
1123 e.printStackTrace();
1124 } catch (NoSuchMethodException e) {
1125 e.printStackTrace();
1126 } catch (InvocationTargetException e) {
1127 e.printStackTrace();
1129 // focusCellManager.init();
1134 * Create a customized editor whose activation process is customized
1137 * the viewer the editor is created for
1138 * @param editorActivationStrategy
1139 * activation strategy to control if an editor activated
1141 * bit mask controlling the editor
1143 * <li>{@link ColumnViewerEditor#DEFAULT}</li>
1144 * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
1145 * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
1147 * {@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
1148 * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
1151 public static void create(TableViewer viewer,
1152 ColumnViewerEditorActivationStrategy editorActivationStrategy,
1154 create(viewer, null, editorActivationStrategy, feature);
1157 protected void setEditor(Control w, Item item, int columnNumber) {
1158 tableEditor.setEditor(w, (TableItem) item, columnNumber);
1161 protected void setLayoutData(LayoutData layoutData) {
1162 tableEditor.grabHorizontal = layoutData.grabHorizontal;
1163 tableEditor.horizontalAlignment = layoutData.horizontalAlignment;
1164 tableEditor.minimumWidth = layoutData.minimumWidth;
1167 public ViewerCell getFocusCell() {
1168 if (focusCellManager != null) {
1169 return focusCellManager.getFocusCell();
1171 return super.getFocusCell();
1174 protected void updateFocusCell(ViewerCell focusCell,
1175 ColumnViewerEditorActivationEvent event) {
1176 // Update the focus cell when we activated the editor with these 2
1178 if (event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
1179 || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL) {
1180 if (focusCellManager != null) {
1182 if (DEBUG)System.err.println("FOCUS CELL: " + focusCell);
1184 Method m = AbstractTableViewer.class.getDeclaredMethod(
1185 "getSelectionFromWidget");
1186 m.setAccessible(true);
1187 @SuppressWarnings("rawtypes")
1188 List l = (List) m.invoke(getViewer());
1189 if (focusCellManager != null) {
1190 m = focusCellManager
1193 .getDeclaredMethod("setFocusCell",
1194 new Class[] { ViewerCell.class });
1195 m.setAccessible(true);
1196 m.invoke(focusCellManager,
1197 new Object[] { focusCell });
1199 if (!l.contains(focusCell.getElement())) {
1200 getViewer().setSelection(
1201 new StructuredSelection(focusCell
1204 } catch (SecurityException e) {
1205 e.printStackTrace();
1206 } catch (IllegalArgumentException e) {
1207 e.printStackTrace();
1208 } catch (IllegalAccessException e) {
1209 e.printStackTrace();
1210 } catch (NoSuchMethodException e) {
1211 e.printStackTrace();
1212 } catch (InvocationTargetException e) {
1213 e.printStackTrace();
1219 protected void processTraverseEvent(int columnIndex, ViewerRow row,
1220 TraverseEvent event) {
1221 ViewerCell cell2edit = null;
1222 if (event.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
1224 if ((event.stateMask & SWT.CTRL) == SWT.CTRL
1225 && (feature & TABBING_VERTICAL) == TABBING_VERTICAL) {
1226 cell2edit = searchCellAboveBelow(row, getViewer(),
1228 } else if ((feature & TABBING_HORIZONTAL) == TABBING_HORIZONTAL) {
1229 cell2edit = searchPreviousCell(row,
1230 row.getCell(columnIndex), row.getCell(columnIndex),
1233 } else if (event.detail == SWT.TRAVERSE_TAB_NEXT) {
1235 if ((event.stateMask & SWT.CTRL) == SWT.CTRL
1236 && (feature & TABBING_VERTICAL) == TABBING_VERTICAL) {
1237 cell2edit = searchCellAboveBelow(row, getViewer(),
1238 columnIndex, false);
1239 } else if ((feature & TABBING_HORIZONTAL) == TABBING_HORIZONTAL) {
1240 cell2edit = searchNextCell(row, row.getCell(columnIndex),
1241 row.getCell(columnIndex), getViewer());
1244 if (DEBUG) System.err.println("NEXT CELL: " + cell2edit);
1245 if (cell2edit != null) {
1246 getViewer().getControl().setRedraw(false);
1247 ColumnViewerEditorActivationEvent acEvent = new ColumnViewerEditorActivationEvent(
1250 Method m = ColumnViewer.class
1252 "triggerEditorActivationEvent",
1253 new Class[] { ColumnViewerEditorActivationEvent.class });
1254 m.setAccessible(true);
1255 m.invoke(getViewer(), new Object[] { acEvent });
1256 } catch (SecurityException e) {
1257 e.printStackTrace();
1258 } catch (NoSuchMethodException e) {
1259 e.printStackTrace();
1260 } catch (IllegalArgumentException e) {
1261 e.printStackTrace();
1262 } catch (IllegalAccessException e) {
1263 e.printStackTrace();
1264 } catch (InvocationTargetException e) {
1265 e.printStackTrace();
1267 getViewer().getControl().setRedraw(true);
1271 private ViewerCell searchCellAboveBelow(ViewerRow row,
1272 ColumnViewer viewer, int columnIndex, boolean above) {
1273 ViewerCell rv = null;
1274 ViewerRow newRow = null;
1276 newRow = row.getNeighbor(ViewerRow.ABOVE, false);
1278 newRow = row.getNeighbor(ViewerRow.BELOW, false);
1281 if (newRow != null) {
1282 Method m = ColumnViewer.class.getDeclaredMethod(
1283 "getViewerColumn", new Class[] { int.class });
1284 m.setAccessible(true);
1285 ViewerColumn column = (ViewerColumn) m.invoke(viewer,
1286 new Object[] { new Integer(columnIndex) });
1287 m = ViewerColumn.class.getDeclaredMethod(
1288 "getEditingSupport");
1289 m.setAccessible(true);
1290 EditingSupport es = (EditingSupport) m.invoke(column);
1291 if (column != null && es != null) {
1292 m = EditingSupport.class.getDeclaredMethod("canEdit",
1293 new Class[] { Object.class });
1294 m.setAccessible(true);
1295 Boolean b = (Boolean) m.invoke(es,
1296 new Object[] { newRow.getItem().getData() });
1297 if (b.booleanValue()) {
1298 rv = newRow.getCell(columnIndex);
1301 rv = searchCellAboveBelow(newRow, viewer, columnIndex,
1305 } catch (Exception e) {
1306 e.printStackTrace();
1311 private ViewerCell searchPreviousCell(ViewerRow row,
1312 ViewerCell currentCell, ViewerCell originalCell,
1313 ColumnViewer viewer) {
1314 ViewerCell rv = null;
1315 ViewerCell previousCell;
1316 if (currentCell != null) {
1317 previousCell = getNeighbor(currentCell, ViewerCell.LEFT, true);
1319 if (row.getColumnCount() != 0) {
1320 previousCell = row.getCell(getCreationIndex(row,
1321 row.getColumnCount() - 1));
1323 previousCell = row.getCell(0);
1327 if (originalCell.equals(previousCell)) {
1330 if (previousCell != null) {
1331 if (isCellEditable(viewer, previousCell)) {
1334 rv = searchPreviousCell(row, previousCell, originalCell,
1338 if ((feature & TABBING_CYCLE_IN_ROW) == TABBING_CYCLE_IN_ROW) {
1339 rv = searchPreviousCell(row, null, originalCell, viewer);
1340 } else if ((feature & TABBING_MOVE_TO_ROW_NEIGHBOR) == TABBING_MOVE_TO_ROW_NEIGHBOR) {
1341 ViewerRow rowAbove = row
1342 .getNeighbor(ViewerRow.ABOVE, false);
1343 if (rowAbove != null) {
1344 rv = searchPreviousCell(rowAbove, null, originalCell,
1352 private ViewerCell searchNextCell(ViewerRow row,
1353 ViewerCell currentCell, ViewerCell originalCell,
1354 ColumnViewer viewer) {
1355 ViewerCell rv = null;
1356 ViewerCell nextCell;
1357 if (currentCell != null) {
1358 nextCell = getNeighbor(currentCell, ViewerCell.RIGHT, true);
1360 nextCell = row.getCell(getCreationIndex(row, 0));
1363 if (originalCell.equals(nextCell)) {
1366 if (nextCell != null) {
1367 if (isCellEditable(viewer, nextCell)) {
1370 rv = searchNextCell(row, nextCell, originalCell, viewer);
1373 if ((feature & TABBING_CYCLE_IN_ROW) == TABBING_CYCLE_IN_ROW) {
1374 rv = searchNextCell(row, null, originalCell, viewer);
1375 } else if ((feature & TABBING_MOVE_TO_ROW_NEIGHBOR) == TABBING_MOVE_TO_ROW_NEIGHBOR) {
1376 ViewerRow rowBelow = row
1377 .getNeighbor(ViewerRow.BELOW, false);
1378 if (rowBelow != null) {
1379 rv = searchNextCell(rowBelow, null, originalCell,
1387 private boolean isCellEditable(ColumnViewer viewer, ViewerCell cell) {
1389 Method m = ColumnViewer.class.getDeclaredMethod(
1390 "getViewerColumn", new Class[] { int.class });
1391 m.setAccessible(true);
1392 ViewerColumn column = (ViewerColumn) m.invoke(viewer,
1393 new Object[] { new Integer(cell.getColumnIndex()) });
1394 m = ViewerColumn.class.getDeclaredMethod("getEditingSupport");
1395 m.setAccessible(true);
1396 EditingSupport es = (EditingSupport) m.invoke(column);
1397 if (column != null && es != null) {
1398 m = EditingSupport.class.getDeclaredMethod("canEdit",
1399 new Class[] { Object.class });
1400 m.setAccessible(true);
1402 Boolean b = (Boolean) m.invoke(es,
1403 new Object[] { cell.getElement() });
1404 return b.booleanValue();
1406 } catch (Exception e) {
1407 e.printStackTrace();
1413 // Reimplementation of ViewerCell-Methods
1414 private static int getVisualIndex(ViewerRow row, int creationIndex) {
1415 TableItem item = (TableItem) row.getItem();
1416 int[] order = item.getParent().getColumnOrder();
1417 for (int i = 0; i < order.length; i++) {
1418 if (order[i] == creationIndex) {
1422 return creationIndex;
1425 private static int getCreationIndex(ViewerRow row, int visualIndex) {
1426 TableItem item = (TableItem) row.getItem();
1427 if (item != null && !item.isDisposed() /*
1428 * && hasColumns() &&
1432 return item.getParent().getColumnOrder()[visualIndex];
1437 private static ViewerCell getCellAtVisualIndex(ViewerRow row,
1439 return getCell(row, getCreationIndex(row, visualIndex));
1442 private static boolean isVisible(ViewerCell cell) {
1443 return getWidth(cell) > 0;
1446 private static int getWidth(ViewerCell cell) {
1447 TableItem item = (TableItem) cell.getViewerRow().getItem();
1448 return item.getParent().getColumn(cell.getColumnIndex()).getWidth();
1451 private static ViewerCell getCell(ViewerRow row, int index) {
1452 return row.getCell(index);