From: Marko Luukkainen Date: Fri, 15 Nov 2019 13:44:00 +0000 (+0200) Subject: Combo property support for annotated property tabs X-Git-Tag: v1.43.0~122 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F30%2F3530%2F1;p=simantics%2F3d.git Combo property support for annotated property tabs gitlab #55 Change-Id: I4fde018ed47ccdc9f82e59bac51272ffebd8244b (cherry picked from commit 79528578707a2103753d895b520fdc6439f52d3e) --- diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/AnnotatedPropertyTabContributorFactory.java b/org.simantics.g3d/src/org/simantics/g3d/property/AnnotatedPropertyTabContributorFactory.java index 7e3f8533..65b267cb 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/property/AnnotatedPropertyTabContributorFactory.java +++ b/org.simantics.g3d/src/org/simantics/g3d/property/AnnotatedPropertyTabContributorFactory.java @@ -39,6 +39,7 @@ import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent; import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener; import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy; import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent; +import org.eclipse.jface.viewers.ComboBoxCellEditor; import org.eclipse.jface.viewers.EditingSupport; import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter; import org.eclipse.jface.viewers.ISelection; @@ -69,8 +70,11 @@ import org.eclipse.ui.IWorkbenchSite; import org.simantics.db.management.ISessionContext; import org.simantics.g3d.property.annotations.CompoundGetPropertyValue; import org.simantics.g3d.property.annotations.CompoundSetPropertyValue; +import org.simantics.g3d.property.annotations.GetComboProperty; +import org.simantics.g3d.property.annotations.GetComboPropertyValue; import org.simantics.g3d.property.annotations.GetPropertyValue; import org.simantics.g3d.property.annotations.PropertyTabBlacklist; +import org.simantics.g3d.property.annotations.SetComboPropertyValue; import org.simantics.g3d.property.annotations.SetPropertyValue; import org.simantics.g3d.scenegraph.IG3DNode; import org.simantics.g3d.scenegraph.NodeMap; @@ -186,6 +190,35 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri } item.setter = m; + } else if (annotation.annotationType().equals(GetComboPropertyValue.class)) { + GetComboPropertyValue get = (GetComboPropertyValue)annotation; + ComboPropertyItem item = (ComboPropertyItem)items.get(get.value()); + if (item == null) { + item = new ComboPropertyItem(get.value()); + items.put(item.id, item); + } + item.getter = m; + item.manipulatorClass = ComboPropertyManipulator.class; + + item.tabId = get.tabId(); + + item.name = get.name(); + } else if (annotation.annotationType().equals(SetComboPropertyValue.class)) { + SetComboPropertyValue set = (SetComboPropertyValue)annotation; + ComboPropertyItem item = (ComboPropertyItem)items.get(set.value()); + if (item == null) { + item = new ComboPropertyItem(set.value()); + items.put(item.id, item); + } + item.setter = m; + } else if (annotation.annotationType().equals(GetComboProperty.class)) { + GetComboProperty get = (GetComboProperty)annotation; + ComboPropertyItem item = (ComboPropertyItem)items.get(get.value()); + if (item == null) { + item = new ComboPropertyItem(get.value()); + items.put(item.id, item); + } + item.values = m; } } } @@ -249,9 +282,22 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri } } + private static PropertyManipulator createManipulator(ComboPropertyItem item, Object obj) { + try { + MethodComboValueProvider provider = new MethodComboValueProvider(item.getter, item.setter,item.values); + PropertyManipulator manipulator = item.manipulatorClass.getConstructor(ValueProvider.class,Object.class).newInstance(provider,obj); + return manipulator; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + private static interface IPropertyItem { public String getTabId(); public String getName(); + public String getId(); + public boolean editable(); } private static class PropertyItem implements IPropertyItem{ @@ -269,6 +315,11 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri this.id = id; } + @Override + public String getId() { + return id; + } + @Override public int hashCode() { return id.hashCode(); @@ -283,6 +334,11 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri public String getName() { return name; } + + @Override + public boolean editable() { + return setter != null; + } } private static class CompoundPropertyItem implements IPropertyItem{ @@ -300,6 +356,11 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri this.id = id; } + @Override + public String getId() { + return id; + } + @Override public int hashCode() { return id.hashCode(); @@ -314,8 +375,55 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri public String getName() { return name; } + + @Override + public boolean editable() { + return setter != null; + } } + private static class ComboPropertyItem implements IPropertyItem{ + private String id; + private String name; + private String tabId; + private Method getter; + private Method setter; + private Method values; + private Class manipulatorClass; + + + public ComboPropertyItem(String id) { + if (id == null) + throw new NullPointerException(); + this.id = id; + } + + @Override + public String getId() { + return id; + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public String getTabId() { + return tabId; + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean editable() { + return setter != null; + } + } + private static class AnnotatedPropertyTabContributor implements PropertyTabContributor { private String id; List items; @@ -345,8 +453,8 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri private static class AnnotatedPropertyTab implements IPropertyTab2, NodeListener { //private String id; List contibutedItems; - List resolvedItems = new ArrayList(); - private Map manipulators = new HashMap(); + List resolvedItems = new ArrayList(); + private Map manipulators = new HashMap(); private TableViewer viewer; @@ -407,7 +515,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri @Override public void selectionChanged(SelectionChangedEvent event) { - PropertyItem item = AdaptationUtils.adaptToSingle(event.getSelection(), PropertyItem.class); + IPropertyItem item = AdaptationUtils.adaptToSingle(event.getSelection(), IPropertyItem.class); selectedItem = item; if (!manipulators.get(selectedItem).getEditMode()) manipulators.get(selectedItem).setEditMode(true); @@ -584,14 +692,18 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri if (item instanceof PropertyItem) { resolvedItems.add((PropertyItem)item); manipulators.put((PropertyItem)item, createManipulator((PropertyItem)item, node)); - } - else { + } else if (item instanceof ComboPropertyItem) { + resolvedItems.add((ComboPropertyItem)item); + manipulators.put((ComboPropertyItem)item, createManipulator((ComboPropertyItem)item, node)); + } else if (item instanceof CompoundPropertyItem) { CompoundPropertyItem compound = (CompoundPropertyItem)item; Map manipulators = createManipulators(compound, node); for (PropertyItem i : manipulators.keySet()) { resolvedItems.add(i); this.manipulators.put(i, manipulators.get(i)); } + } else { + } } @@ -688,7 +800,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri if (viewer.getTable().isDisposed()) return; if (DEBUG)System.out.println("Viewer refresh " + id); - for (PropertyItem item : resolvedItems) + for (IPropertyItem item : resolvedItems) if (!item.equals(selectedItem)) viewer.refresh(item); if (selectedItem != null) @@ -704,7 +816,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri return; } if (DEBUG) System.out.println("Viewer threaded refresh " + id); - for (PropertyItem item : resolvedItems) + for (IPropertyItem item : resolvedItems) if (!item.equals(selectedItem)) viewer.refresh(item); if (selectedItem != null) @@ -713,7 +825,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri } }); } else { - for (PropertyItem item : resolvedItems) { + for (IPropertyItem item : resolvedItems) { delayedUpdate.add(item); } } @@ -729,7 +841,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri } - public PropertyManipulator getManipulator(PropertyItem item) { + public PropertyManipulator getManipulator(IPropertyItem item) { return manipulators.get(item); } @@ -759,10 +871,10 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri @Override protected boolean canEdit(Object element) { - PropertyItem item = (PropertyItem)element; + IPropertyItem item = (IPropertyItem)element; if (tab.getManipulator(item).getValueCount() <= index) return false; - if (item.setter == null) + if (!item.editable()) return false; if (getValue(element) == null) return false; @@ -771,32 +883,52 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri @Override protected CellEditor getCellEditor(Object element) { - - if (tab.getManipulator((PropertyItem)element).getValueCount() <= index) + IPropertyItem item = (IPropertyItem)element; + if (tab.getManipulator(item).getValueCount() <= index) return null; if (editor == null) - editor = new TextCellEditor(viewer.getTable(),SWT.NONE) { - @Override - public void activate() { - tab.setEditing(true); - } - - @Override - public void deactivate() { - super.deactivate(); - tab.setEditing(false); - } - }; + if (item instanceof PropertyItem) + editor = new TextCellEditor(viewer.getTable(),SWT.NONE) { + @Override + public void activate() { + tab.setEditing(true); + } + + @Override + public void deactivate() { + super.deactivate(); + tab.setEditing(false); + } + }; + else if (item instanceof ComboPropertyItem) { + ComboPropertyItem comboPropertyItem = (ComboPropertyItem)item; + ComboPropertyManipulator manipulator = (ComboPropertyManipulator)tab.manipulators.get(comboPropertyItem); + editor = new ComboBoxCellEditor(viewer.getTable(), manipulator.getItems()) { + @Override + public void activate() { + tab.setEditing(true); + } + + @Override + public void deactivate() { + super.deactivate(); + tab.setEditing(false); + } + }; + } if (DEBUG)System.err.println("CELL EDITOR: " + element); return editor; } @Override protected Object getValue(Object element) { - PropertyItem item = (PropertyItem)element; + IPropertyItem item = (IPropertyItem)element; PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, obj); if (manipulator.getValueCount() <= index) return null; + if (manipulator instanceof ComboPropertyManipulator) { + return ((ComboPropertyManipulator)manipulator).getValueIndex(); + } Object value = manipulator.getValue(index); return value; } @@ -804,14 +936,14 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri @Override protected void setValue(Object element, Object value) { - PropertyItem item = (PropertyItem)element; + IPropertyItem item = (IPropertyItem)element; PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, obj); if (manipulator.getValueCount() <= index) throw new IllegalAccessError("Editing value in index " + index + " is not allowed."); if (DEBUG)System.err.println("CELL SET VALUE: " + element + " " + value); - manipulator.setValue((String)value,index); + manipulator.setValue(value.toString(),index); viewer.refresh(item); - nodeMap.commit("Set " + item.id + " value to " + value); + nodeMap.commit("Set " + item.getId() + " value to " + value); } @@ -824,12 +956,12 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri @Override public void update(ViewerCell cell) { - PropertyItem item = (PropertyItem)cell.getElement(); + IPropertyItem item = (IPropertyItem)cell.getElement(); - if (item.name.length() > 0) - cell.setText(item.name); + if (item.getName().length() > 0) + cell.setText(item.getName()); else - cell.setText(item.id); + cell.setText(item.getId()); } @@ -845,7 +977,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri @Override public void update(ViewerCell cell) { - PropertyItem item = (PropertyItem)cell.getElement(); + IPropertyItem item = (IPropertyItem)cell.getElement(); int index = cell.getColumnIndex() -1; PropertyManipulator manipulator = tab.getManipulator(item);//createManipulator(item, object); if (manipulator.getValueCount() <= index) diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/ComboPropertyManipulator.java b/org.simantics.g3d/src/org/simantics/g3d/property/ComboPropertyManipulator.java new file mode 100644 index 00000000..45a3b99a --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/property/ComboPropertyManipulator.java @@ -0,0 +1,89 @@ +package org.simantics.g3d.property; + +import java.util.List; + +public class ComboPropertyManipulator implements PropertyManipulator{ + + ComboValueProvider provider; + Object input; + + boolean editMode; + + public ComboPropertyManipulator(ValueProvider provider, Object input) { + this.provider = (ComboValueProvider)provider; + this.input = input; + } + + @Override + public int getValueCount() { + return 1; + } + + @Override + public String getDescription(int i) { + if (i == 0) + return "Value"; + return null; + } + + @Override + public String getValue(int i) { + try { + Integer value = (Integer)provider.getValue(input); + if (value == null) return null; + return getItems()[value]; + } catch (Exception e) { + return null; + } + } + + @Override + public String setValue(String value, int i) { + try { + provider.setValue(input, Integer.parseInt(value)); + } catch (Exception e) { + return e.getMessage(); + } + return null; + } + + @Override + public boolean getEditMode() { + return editMode; + } + + @Override + public void setEditMode(boolean b) { + editMode = b; + } + + public String[] getItems() { + try { + List vals = provider.getValues(input); + String arr[] = new String[vals.size()]; + for (int i = 0; i < vals.size(); i++) { + arr[i] = vals.get(i).toString(); + } + return arr; + } catch (Exception e) { + return new String[0]; + } + } + + public Integer getValueIndex() { + try { + Integer value = (Integer)provider.getValue(input); + return value; + } catch (Exception e) { + return null; + } + } + + public Integer indexOf(String value) { + String[] items = getItems(); + for (int i = 0; i < items.length; i++) + if (items[i].equals(value)) + return i; + return -1; + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/ComboValueProvider.java b/org.simantics.g3d/src/org/simantics/g3d/property/ComboValueProvider.java new file mode 100644 index 00000000..30d2e33c --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/property/ComboValueProvider.java @@ -0,0 +1,10 @@ +package org.simantics.g3d.property; + +import java.util.List; + +public interface ComboValueProvider extends ValueProvider{ + + + public List getValues(Object obj) throws Exception; + +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/MethodComboValueProvider.java b/org.simantics.g3d/src/org/simantics/g3d/property/MethodComboValueProvider.java new file mode 100644 index 00000000..c1e21e07 --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/property/MethodComboValueProvider.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.property; + +import java.lang.reflect.Method; +import java.util.List; + +public class MethodComboValueProvider implements ComboValueProvider { + + Method getter; + Method setter; + Method values; + + public MethodComboValueProvider(Method getter, Method setter, Method values) { + this.getter = getter; + this.setter = setter; + this.values = values; + } + + @Override + public Object getValue(Object obj) throws Exception{ + return getter.invoke(obj); + } + @Override + public void setValue(Object obj, Object value) throws Exception { + setter.invoke(obj,value); + } + + public List getValues(Object obj) throws Exception { + return (List)values.invoke(obj); + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/annotations/GetComboProperty.java b/org.simantics.g3d/src/org/simantics/g3d/property/annotations/GetComboProperty.java new file mode 100644 index 00000000..ef019ae1 --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/property/annotations/GetComboProperty.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.property.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface GetComboProperty { + String value(); +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/annotations/GetComboPropertyValue.java b/org.simantics.g3d/src/org/simantics/g3d/property/annotations/GetComboPropertyValue.java new file mode 100644 index 00000000..5497b8e5 --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/property/annotations/GetComboPropertyValue.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.property.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface GetComboPropertyValue { + String value(); + String name() default ""; + String tabId() default "Default"; + +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/property/annotations/SetComboPropertyValue.java b/org.simantics.g3d/src/org/simantics/g3d/property/annotations/SetComboPropertyValue.java new file mode 100644 index 00000000..08377372 --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/property/annotations/SetComboPropertyValue.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.property.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface SetComboPropertyValue { + String value(); +}