]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTCCombo.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTCCombo.java
1 package org.simantics.views.swt.client.impl;
2
3 import java.util.List;
4
5 import org.eclipse.swt.custom.CCombo;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.events.SelectionListener;
8 import org.eclipse.swt.widgets.Composite;
9 import org.simantics.scl.runtime.tuple.Tuple;
10
11
12 public class SWTCCombo extends SWTComboBase<CCombo> {
13         
14         private static final long serialVersionUID = 2529612234578912642L;
15
16         private final SelectionListener listener = new SelectionListener() {
17
18                 @Override
19                 public void widgetSelected(SelectionEvent e) {
20                         
21                         int selectionIndex = control.getSelectionIndex();
22                         if (selectionIndex == -1)
23                             return;
24
25                         String[] items = control.getItems();
26                         
27                         String key = items[selectionIndex];
28                         
29                         if(modifier != null)
30                                 modifier.apply(key);
31                         
32                         selected = key;
33                         
34                 }
35
36                 @Override
37                 public void widgetDefaultSelected(SelectionEvent e) {
38                         
39                         widgetSelected(e);
40                         
41                 }
42                 
43         };
44
45         @Override
46         public void createControls(Composite parent) {
47                 
48                 control = new CCombo(parent, style);
49                 control.addSelectionListener(listener);
50                 
51                 setProperties();
52                 
53         }
54         
55         public void synchronizeAvailable(List<Tuple> available) {
56                 if(available != null) {
57                         
58                 control.removeSelectionListener(listener);
59                 control.setData(available);
60                 control.clearSelection();
61                 try {
62                         control.removeAll();
63                 } catch (Throwable t) {
64                     t.printStackTrace();
65                 }
66                 if (available != null) {
67                     int index = 0;
68                     for(Tuple key : available) {
69                         control.add((String)key.toArray()[0]);
70                         control.setData((String)key.toArray()[0], index++);
71                     }
72                     String selectionKey = (String)control.getData("_SelectionKey");
73                     if(selectionKey != null) {
74                         Integer selectionIndex = (Integer)control.getData(selectionKey);
75                         if(selectionIndex != null) control.select(selectionIndex);
76                     }
77                 }
78                 control.addSelectionListener(listener);
79                         
80 //                      // This seems to be necessary for correct size computations
81 //                      widget.getControl().getParent().layout(true);
82                 }
83         }
84
85         public void synchronizeSelected(String selected) {
86                 if(selected != null) {
87                         
88             control.removeSelectionListener(listener);
89                 control.setData("_SelectionKey", selected);
90                 Integer selectionIndex = (Integer)control.getData(selected);
91                 if(selectionIndex != null) control.select(selectionIndex);
92             control.addSelectionListener(listener);
93                         
94 //                      // This seems to be necessary for correct size computations
95 //                      widget.getControl().getParent().layout(true);
96                 }
97         }
98         
99         public String readSelected() {
100                 return selected;
101         }
102         
103         public List<Tuple> readAvailable() {
104                 return available;
105         }
106         
107 }