--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt.widgets;\r
+\r
+import java.util.List;\r
+import java.util.concurrent.CopyOnWriteArrayList;\r
+\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.common.request.ParametrizedRead;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class CCombo2 extends WidgetImpl {\r
+\r
+ private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();\r
+ private List<Pair<String, Object>> itemList;\r
+\r
+ final private Display display;\r
+ final private org.eclipse.swt.custom.CCombo combo;\r
+\r
+ public CCombo2(Composite parent, WidgetSupport support, int style) {\r
+ super(support);\r
+ this.display = parent.getDisplay();\r
+ combo = new org.eclipse.swt.custom.CCombo(parent, style);\r
+ combo.setData("org.simantics.browsing.ui.widgets.Combo", this);\r
+ support.register(this);\r
+ }\r
+\r
+ public <T> void setAvailable(final ParametrizedRead<T, List<Pair<String, Object>>> read) {\r
+ \r
+ Simantics.getSession().async(new UniqueRead<List<Pair<String, Object>>>() {\r
+\r
+ @Override\r
+ public List<Pair<String, Object>> perform(ReadGraph graph) throws DatabaseException {\r
+ T input = support.getInput(graph);\r
+ System.err.println("read: " + read);\r
+ return graph.syncRequest(read.get(input));\r
+ }\r
+ \r
+ }, new Listener<List<Pair<String, Object>>>() {\r
+\r
+ @Override\r
+ public void exception(Throwable t) {\r
+ t.printStackTrace();\r
+ }\r
+\r
+ @Override\r
+ public void execute(final List<Pair<String, Object>> items) {\r
+ if(isDisposed()) return;\r
+ display.asyncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ if(isDisposed()) return;\r
+ itemList = items;\r
+ for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);\r
+ combo.setData(items);\r
+ combo.clearSelection();\r
+ try {\r
+ combo.removeAll();\r
+ } catch (Throwable t) {\r
+ t.printStackTrace();\r
+ }\r
+ if (items != null) {\r
+ int index = 0;\r
+ for(Pair<String, Object> key : items) {\r
+ combo.add(key.first);\r
+ combo.setData(key.first, index++);\r
+ }\r
+ String selectionKey = (String)combo.getData("_SelectionKey");\r
+ if(selectionKey != null) {\r
+ Integer selectionIndex = (Integer)combo.getData(selectionKey);\r
+ if(selectionIndex != null) combo.select(selectionIndex);\r
+ }\r
+ }\r
+ for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);\r
+ }\r
+\r
+ });\r
+ }\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return combo.isDisposed();\r
+ }\r
+\r
+ });\r
+ \r
+ }\r
+\r
+ public <T> void setSelection(final ParametrizedRead<T, String> read) {\r
+ \r
+ Simantics.getSession().async(new UniqueRead<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ T input = support.getInput(graph);\r
+ System.err.println("read: " + read);\r
+ return graph.syncRequest(read.get(input));\r
+ }\r
+ \r
+ }, new Listener<String>() {\r
+\r
+ @Override\r
+ public void exception(Throwable t) {\r
+ t.printStackTrace();\r
+ }\r
+\r
+ @Override\r
+ public void execute(final String selectionKey) {\r
+ if(isDisposed()) return;\r
+ display.asyncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ if(isDisposed()) return;\r
+ if(selectionKey == null) return;\r
+ for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);\r
+ combo.setData("_SelectionKey", selectionKey);\r
+ Integer selectionIndex = (Integer)combo.getData(selectionKey);\r
+ if(selectionIndex != null) combo.select(selectionIndex);\r
+ for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);\r
+ }\r
+\r
+ });\r
+ }\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return combo.isDisposed();\r
+ }\r
+\r
+ });\r
+ \r
+ }\r
+\r
+ public org.eclipse.swt.custom.CCombo getWidget() {\r
+ return combo;\r
+ }\r
+\r
+ @Override\r
+ public Control getControl() {\r
+ return combo;\r
+ }\r
+\r
+ @Override\r
+ public void setInput(ISessionContext context, Object input) {\r
+\r
+ if (selectionListeners != null) {\r
+ for (SelectionListener listener : selectionListeners) {\r
+ if(listener instanceof Widget) {\r
+ ((Widget) listener).setInput(context, input);\r
+ }\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ public synchronized void addSelectionListener(SelectionListener listener) {\r
+ selectionListeners.add(listener);\r
+ combo.addSelectionListener(listener);\r
+ }\r
+\r
+ /**\r
+ * @param index\r
+ * @return\r
+ * @throws IndexOutOfBoundsException if index is outside widget bounds\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ public <T> Pair<String, T> getData(int index) {\r
+ if (index == -1)\r
+ return null;\r
+ return (Pair<String, T>) itemList.get(index);\r
+ }\r
+\r
+ /**\r
+ * @return selected combo list item or <code>null</code> if no item is\r
+ * selected\r
+ */\r
+ public <T> Pair<String, T> getSelectedData() {\r
+ return getData(combo.getSelectionIndex());\r
+ }\r
+\r
+}\r