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