-package org.simantics.views.swt.client.impl;\r
-\r
-import java.util.List;\r
-\r
-import org.eclipse.swt.events.ModifyEvent;\r
-import org.eclipse.swt.events.ModifyListener;\r
-import org.eclipse.swt.widgets.Combo;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.simantics.scl.runtime.tuple.Tuple;\r
-\r
-public class SWTCombo extends SWTComboBase<Combo> {\r
- \r
- private static final long serialVersionUID = 3355806440902480450L;\r
-\r
- private final ModifyListener listener = new ModifyListener() {\r
-\r
- @Override\r
- public void modifyText(ModifyEvent e) {\r
- \r
- int selectionIndex = control.getSelectionIndex();\r
- if (selectionIndex == -1)\r
- return;\r
-\r
- String[] items = control.getItems();\r
- \r
- String key = items[selectionIndex];\r
- \r
- if(modifier != null)\r
- modifier.apply(key);\r
- \r
- selected = key;\r
- \r
- }\r
- \r
- };\r
- \r
- @Override\r
- public void createControls(Composite parent) {\r
- \r
- control = new Combo(parent, style);\r
- // Must initially add modifylistener before invoking setProperties\r
- // since setProperties will invoke synchronize*-methods, which do\r
- // removeModifyListener + addModifyListener. Otherwise modify listener\r
- // would get added twice to the control.\r
- control.addModifyListener(listener);\r
- \r
- setProperties();\r
- \r
- }\r
- \r
- public void synchronizeAvailable(List<Tuple> available) {\r
- \r
- if(available != null) {\r
- \r
- control.removeModifyListener(listener);\r
- control.setData(available);\r
- control.clearSelection();\r
- try {\r
- control.removeAll();\r
- } catch (Throwable t) {\r
- t.printStackTrace();\r
- }\r
- if (available != null) {\r
- int index = 0;\r
- for(Tuple key : available) {\r
- control.add((String)key.get(0));\r
- control.setData((String)key.get(0), index++);\r
- }\r
- String selectionKey = (String)control.getData("_SelectionKey");\r
- if(selectionKey != null) {\r
- Integer selectionIndex = (Integer)control.getData(selectionKey);\r
- if(selectionIndex != null) control.select(selectionIndex);\r
- }\r
- }\r
- control.addModifyListener(listener);\r
- \r
-// // This seems to be necessary for correct size computations\r
-// widget.getControl().getParent().layout(true);\r
- }\r
- }\r
-\r
- public void synchronizeSelected(String selected) {\r
- \r
- if(selected != null) {\r
- \r
- control.removeModifyListener(listener);\r
- control.setData("_SelectionKey", selected);\r
- Integer selectionIndex = (Integer)control.getData(selected);\r
- if(selectionIndex != null) control.select(selectionIndex);\r
- control.addModifyListener(listener);\r
- \r
- }\r
- \r
- }\r
- \r
-}\r
+package org.simantics.views.swt.client.impl;
+
+import java.util.List;
+
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.simantics.scl.runtime.tuple.Tuple;
+
+public class SWTCombo extends SWTComboBase<Combo> {
+
+ private static final long serialVersionUID = 3355806440902480450L;
+
+ private final ModifyListener listener = new ModifyListener() {
+
+ @Override
+ public void modifyText(ModifyEvent e) {
+
+ int selectionIndex = control.getSelectionIndex();
+ if (selectionIndex == -1)
+ return;
+
+ String[] items = control.getItems();
+
+ String key = items[selectionIndex];
+
+ if(modifier != null)
+ modifier.apply(key);
+
+ selected = key;
+
+ }
+
+ };
+
+ @Override
+ public void createControls(Composite parent) {
+
+ control = new Combo(parent, style);
+ // Must initially add modifylistener before invoking setProperties
+ // since setProperties will invoke synchronize*-methods, which do
+ // removeModifyListener + addModifyListener. Otherwise modify listener
+ // would get added twice to the control.
+ control.addModifyListener(listener);
+
+ setProperties();
+
+ }
+
+ public void synchronizeAvailable(List<Tuple> available) {
+
+ if(available != null) {
+
+ control.removeModifyListener(listener);
+ control.setData(available);
+ control.clearSelection();
+ try {
+ control.removeAll();
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ if (available != null) {
+ int index = 0;
+ for(Tuple key : available) {
+ control.add((String)key.get(0));
+ control.setData((String)key.get(0), index++);
+ }
+ String selectionKey = (String)control.getData("_SelectionKey");
+ if(selectionKey != null) {
+ Integer selectionIndex = (Integer)control.getData(selectionKey);
+ if(selectionIndex != null) control.select(selectionIndex);
+ }
+ }
+ control.addModifyListener(listener);
+
+// // This seems to be necessary for correct size computations
+// widget.getControl().getParent().layout(true);
+ }
+ }
+
+ public void synchronizeSelected(String selected) {
+
+ if(selected != null) {
+
+ control.removeModifyListener(listener);
+ control.setData("_SelectionKey", selected);
+ Integer selectionIndex = (Integer)control.getData(selected);
+ if(selectionIndex != null) control.select(selectionIndex);
+ control.addModifyListener(listener);
+
+ }
+
+ }
+
+}