1 package org.simantics.sysdyn.ui.properties.widgets.arrays;
\r
4 import java.util.ArrayList;
\r
5 import java.util.List;
\r
7 import org.eclipse.swt.SWT;
\r
8 import org.eclipse.swt.events.SelectionAdapter;
\r
9 import org.eclipse.swt.events.SelectionEvent;
\r
10 import org.eclipse.swt.widgets.Combo;
\r
11 import org.eclipse.swt.widgets.Composite;
\r
12 import org.simantics.browsing.ui.NodeContext;
\r
13 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;
\r
14 import org.simantics.browsing.ui.common.modifiers.Enumeration;
\r
15 import org.simantics.browsing.ui.content.Labeler.CustomModifier;
\r
16 import org.simantics.db.Session;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.common.request.WriteRequest;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.utils.ObjectUtils;
\r
22 public abstract class ComboBoxModifier<T> implements CustomModifier {
\r
24 private final Enumeration<T> enumeration;
\r
25 private final EnumeratedValue<T> value;
\r
26 private final List<String> values;
\r
27 private final Session session;
\r
28 private Combo combo;
\r
31 public ComboBoxModifier(Session session, Enumeration<T> enumeration, T value) {
\r
32 this(session, enumeration, enumeration.find(value));
\r
35 public ComboBoxModifier(Session session, Enumeration<T> enumeration,
\r
36 EnumeratedValue<T> value) {
\r
37 if (session == null)
\r
38 throw new NullPointerException("null session");
\r
39 if (enumeration == null)
\r
40 throw new NullPointerException("null enumeration");
\r
41 if (enumeration.size() == 0)
\r
42 throw new IllegalArgumentException("");
\r
44 this.enumeration = enumeration;
\r
46 this.session = session;
\r
49 boolean found = false;
\r
51 this.values = new ArrayList<String>();
\r
52 for (EnumeratedValue<T> v : enumeration.values()) {
\r
53 values.add(v.getName());
\r
64 public Object createControl(Object parentControl, Object controlItem,
\r
65 int columnIndex, NodeContext context) {
\r
67 combo = new Combo((Composite) parentControl, SWT.DROP_DOWN
\r
68 | SWT.BORDER | SWT.READ_ONLY);
\r
70 for (String item : values) {
\r
74 combo.addSelectionListener(new SelectionAdapter() {
\r
76 public void widgetSelected(SelectionEvent e) {
\r
77 String index = ((Combo) e.getSource()).getText();
\r
82 combo.select(select);
\r
89 public String getValue() {
\r
90 return enumeration.values().get(0).getName();
\r
94 public final String isValid(String label) {
\r
95 if (enumeration.findByName(label) == null)
\r
96 return "Value '" + label + "' is not among the enumerated values " + enumeration.values();
\r
101 public void modify(String label) {
\r
103 int index = values.indexOf(label);
\r
105 throw new IllegalArgumentException("Cannot modify enumeration with value '" + label + "', not among the enumerated values " + values);
\r
107 EnumeratedValue<T> oldEnumValue = value;
\r
108 EnumeratedValue<T> newEnumValue = enumeration.values().get(index);
\r
110 final T oldObject = oldEnumValue != null ? oldEnumValue.getObject() : null;
\r
111 final T newObject = newEnumValue != null ? newEnumValue.getObject() : null;
\r
113 if (ObjectUtils.objectEquals(oldObject, newObject))
\r
117 session.getSession().syncRequest(new WriteRequest() {
\r
119 public void perform(WriteGraph graph) throws DatabaseException {
\r
120 modifyWithObject(graph, oldObject, newObject);
\r
123 } catch (DatabaseException e) {
\r
124 handleException(e);
\r
130 protected abstract void modifyWithObject(WriteGraph graph, T oldEnumObject, T enumObject) throws DatabaseException;
\r
132 protected void handleException(DatabaseException e) {
\r
133 throw new RuntimeException(e);
\r