]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
8fa748ddb8ff3998b132fb9f520c8f071b6a266b
[simantics/sysdyn.git] /
1 package org.simantics.jfreechart.chart.properties;\r
2 \r
3 import org.eclipse.jface.viewers.ISelection;\r
4 import org.eclipse.swt.widgets.Text;\r
5 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
6 import org.simantics.db.WriteGraph;\r
7 import org.simantics.db.common.request.WriteRequest;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.management.ISessionContext;\r
10 import org.simantics.utils.ReflectionUtils;\r
11 import org.simantics.utils.ui.ISelectionUtils;\r
12 \r
13 public abstract class StringChooserModifyListenerImpl<T,T2> implements StringChooserModifyListener<T2>, Widget {\r
14         protected ISessionContext context;\r
15         protected T lastInput = null;\r
16 \r
17         protected final Class<?> clazz;\r
18 \r
19         public StringChooserModifyListenerImpl() {\r
20                 clazz = ReflectionUtils.getSingleParameterType(getClass());\r
21         }\r
22 \r
23         private Object getInputContents(Object input, Class<?> inputClass) {\r
24                 if (inputClass.isInstance(input))\r
25                         return input;\r
26                 if (input instanceof ISelection)\r
27                         return ISelectionUtils.filterSingleSelection(input, inputClass);\r
28                 return null;\r
29         }\r
30 \r
31         @Override\r
32         public void modifySelection(StringChooserModifyEvent<T2> e) {\r
33 \r
34                 Text text = (Text)e.getWidget();\r
35                 final T2 object = e.getObject();\r
36                 final String textValue = text.getText();\r
37                 final T input = lastInput;\r
38 \r
39                 try {\r
40                         context.getSession().syncRequest(new WriteRequest() {\r
41 \r
42                                 @SuppressWarnings("unchecked")\r
43                                 @Override\r
44                                 public void perform(WriteGraph graph) throws DatabaseException {\r
45                                         \r
46                                         if(clazz.isInstance(input)) {\r
47                                                 applyObject(graph, (T)input, object, textValue);\r
48                                         } else {\r
49                                                 T single = (T)getInputContents(input, clazz);\r
50                                                 if(single != null)\r
51                                                         applyObject(graph, single, object, textValue);\r
52                                         }\r
53 \r
54                                         \r
55                                 }\r
56                                 \r
57                         });\r
58                 } catch (DatabaseException e1) {\r
59                         e1.printStackTrace();\r
60                 }\r
61         }\r
62 \r
63         @Override\r
64         public void setInput(ISessionContext context, Object parameter) {\r
65                 this.context = context;\r
66                 lastInput = (T)parameter;\r
67         }\r
68 \r
69         abstract public void applyObject(WriteGraph graph, T input, T2 object, String text) throws DatabaseException;\r
70 \r
71 }\r