1 package org.simantics.views.swt.client.impl;
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;
11 public class SWTCombo extends SWTComboBase<Combo> {
13 private static final long serialVersionUID = 3355806440902480450L;
15 private final ModifyListener listener = new ModifyListener() {
18 public void modifyText(ModifyEvent e) {
20 int selectionIndex = control.getSelectionIndex();
21 if (selectionIndex == -1)
24 String[] items = control.getItems();
26 String key = items[selectionIndex];
38 public void createControls(Composite parent) {
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);
51 public void synchronizeAvailable(List<Tuple> available) {
53 if(available != null) {
55 control.removeModifyListener(listener);
56 control.setData(available);
57 control.clearSelection();
60 } catch (Throwable t) {
63 if (available != null) {
65 for(Tuple key : available) {
66 control.add((String)key.get(0));
67 control.setData((String)key.get(0), index++);
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);
75 control.addModifyListener(listener);
77 // // This seems to be necessary for correct size computations
78 // widget.getControl().getParent().layout(true);
82 public void synchronizeSelected(String selected) {
84 if(selected != null) {
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);