]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d45b1a2af03a3daa885e42bd8b0a8d3704fb6440
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.arrays;\r
2 \r
3 \r
4 import java.util.ArrayList;\r
5 import java.util.List;\r
6 \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
21 \r
22 public abstract class ComboBoxModifier<T> implements CustomModifier {\r
23 \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
29         int select;\r
30 \r
31         public ComboBoxModifier(Session session, Enumeration<T> enumeration, T value) {\r
32                 this(session, enumeration, enumeration.find(value));\r
33         }\r
34 \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
43 \r
44                 this.enumeration = enumeration;\r
45                 this.value = value;\r
46                 this.session = session;\r
47 \r
48                 select = 0;\r
49                 boolean found = false;\r
50                 \r
51                 this.values = new ArrayList<String>();\r
52                 for (EnumeratedValue<T> v : enumeration.values()) {\r
53                         values.add(v.getName());\r
54                         \r
55                         if(v == value) \r
56                                 found = true;\r
57                         \r
58                         if(found == false)\r
59                                 select++;\r
60                 }\r
61         }\r
62 \r
63         @Override\r
64         public Object createControl(Object parentControl, Object controlItem,\r
65                         int columnIndex, NodeContext context) {\r
66 \r
67                 combo = new Combo((Composite) parentControl, SWT.DROP_DOWN\r
68                                 | SWT.BORDER | SWT.READ_ONLY);\r
69 \r
70                 for (String item : values) {\r
71                         combo.add(item);\r
72                 }\r
73                 \r
74                 combo.addSelectionListener(new SelectionAdapter() {\r
75                         @Override\r
76                         public void widgetSelected(SelectionEvent e) {\r
77                                 String index = ((Combo) e.getSource()).getText();\r
78                                 modify(index);\r
79                         }\r
80                 });\r
81                 \r
82                 combo.select(select);\r
83                 \r
84                 return combo;\r
85 \r
86         }\r
87 \r
88         @Override\r
89         public String getValue() {\r
90                 return enumeration.values().get(0).getName();\r
91         }\r
92 \r
93     @Override\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
97         return null;\r
98     }\r
99 \r
100         @Override\r
101         public void modify(String label) {\r
102                 \r
103         int index = values.indexOf(label);\r
104         if (index == -1)\r
105             throw new IllegalArgumentException("Cannot modify enumeration with value '" + label + "', not among the enumerated values " + values);\r
106                 \r
107         EnumeratedValue<T> oldEnumValue = value;\r
108         EnumeratedValue<T> newEnumValue = enumeration.values().get(index);\r
109         \r
110         final T oldObject = oldEnumValue != null ? oldEnumValue.getObject() : null;\r
111         final T newObject = newEnumValue != null ? newEnumValue.getObject() : null;\r
112         \r
113         if (ObjectUtils.objectEquals(oldObject, newObject))\r
114             return;\r
115 \r
116         try {\r
117             session.getSession().syncRequest(new WriteRequest() {\r
118                 @Override\r
119                 public void perform(WriteGraph graph) throws DatabaseException {\r
120                     modifyWithObject(graph, oldObject, newObject);\r
121                 }\r
122             });\r
123         } catch (DatabaseException e) {\r
124             handleException(e);\r
125         }\r
126         \r
127                 combo.dispose();                \r
128         }\r
129         \r
130         protected abstract void modifyWithObject(WriteGraph graph, T oldEnumObject, T enumObject) throws DatabaseException;\r
131         \r
132     protected void handleException(DatabaseException e) {\r
133         throw new RuntimeException(e);\r
134     }\r
135 \r
136 }\r