]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTCombo.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTCombo.java
1 package org.simantics.views.swt.client.impl;\r
2 \r
3 import java.util.List;\r
4 \r
5 import org.eclipse.swt.events.ModifyEvent;\r
6 import org.eclipse.swt.events.ModifyListener;\r
7 import org.eclipse.swt.widgets.Combo;\r
8 import org.eclipse.swt.widgets.Composite;\r
9 import org.simantics.scl.runtime.tuple.Tuple;\r
10 \r
11 public class SWTCombo extends SWTComboBase<Combo> {\r
12         \r
13         private static final long serialVersionUID = 3355806440902480450L;\r
14 \r
15         private final ModifyListener listener = new ModifyListener() {\r
16 \r
17                 @Override\r
18                 public void modifyText(ModifyEvent e) {\r
19                         \r
20                         int selectionIndex = control.getSelectionIndex();\r
21                         if (selectionIndex == -1)\r
22                             return;\r
23 \r
24                         String[] items = control.getItems();\r
25                         \r
26                         String key = items[selectionIndex];\r
27                         \r
28                         if(modifier != null)\r
29                                 modifier.apply(key);\r
30                         \r
31                         selected = key;\r
32                         \r
33                 }\r
34                 \r
35         };\r
36         \r
37         @Override\r
38         public void createControls(Composite parent) {\r
39                 \r
40                 control = new Combo(parent, style);\r
41                 // Must initially add modifylistener before invoking setProperties\r
42                 // since setProperties will invoke synchronize*-methods, which do\r
43                 // removeModifyListener + addModifyListener. Otherwise modify listener\r
44                 // would get added twice to the control.\r
45                 control.addModifyListener(listener);\r
46                 \r
47                 setProperties();\r
48                 \r
49         }\r
50         \r
51         public void synchronizeAvailable(List<Tuple> available) {\r
52                 \r
53                 if(available != null) {\r
54                         \r
55                 control.removeModifyListener(listener);\r
56                 control.setData(available);\r
57                 control.clearSelection();\r
58                 try {\r
59                         control.removeAll();\r
60                 } catch (Throwable t) {\r
61                     t.printStackTrace();\r
62                 }\r
63                 if (available != null) {\r
64                     int index = 0;\r
65                     for(Tuple key : available) {\r
66                         control.add((String)key.get(0));\r
67                         control.setData((String)key.get(0), index++);\r
68                     }\r
69                     String selectionKey = (String)control.getData("_SelectionKey");\r
70                     if(selectionKey != null) {\r
71                         Integer selectionIndex = (Integer)control.getData(selectionKey);\r
72                         if(selectionIndex != null) control.select(selectionIndex);\r
73                     }\r
74                 }\r
75                 control.addModifyListener(listener);\r
76                         \r
77 //                      // This seems to be necessary for correct size computations\r
78 //                      widget.getControl().getParent().layout(true);\r
79                 }\r
80         }\r
81 \r
82         public void synchronizeSelected(String selected) {\r
83                 \r
84                 if(selected != null) {\r
85                         \r
86                 control.removeModifyListener(listener);\r
87                 control.setData("_SelectionKey", selected);\r
88                 Integer selectionIndex = (Integer)control.getData(selected);\r
89                 if(selectionIndex != null) control.select(selectionIndex);\r
90                 control.addModifyListener(listener);\r
91                                 \r
92                 }\r
93                 \r
94         }\r
95         \r
96 }\r