1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt.widgets;
\r
14 import java.util.List;
\r
15 import java.util.concurrent.CopyOnWriteArrayList;
\r
17 import org.eclipse.swt.events.SelectionListener;
\r
18 import org.eclipse.swt.widgets.Composite;
\r
19 import org.eclipse.swt.widgets.Control;
\r
20 import org.eclipse.swt.widgets.Display;
\r
21 import org.simantics.Simantics;
\r
22 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
23 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
24 import org.simantics.db.ReadGraph;
\r
25 import org.simantics.db.common.request.ParametrizedRead;
\r
26 import org.simantics.db.common.request.UniqueRead;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.management.ISessionContext;
\r
29 import org.simantics.db.procedure.Listener;
\r
30 import org.simantics.utils.datastructures.Pair;
\r
32 public class CCombo2 extends WidgetImpl {
\r
34 private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
\r
35 private List<Pair<String, Object>> itemList;
\r
37 final private Display display;
\r
38 final private org.eclipse.swt.custom.CCombo combo;
\r
40 public CCombo2(Composite parent, WidgetSupport support, int style) {
\r
42 this.display = parent.getDisplay();
\r
43 combo = new org.eclipse.swt.custom.CCombo(parent, style);
\r
44 combo.setData("org.simantics.browsing.ui.widgets.Combo", this);
\r
45 support.register(this);
\r
48 public <T> void setAvailable(final ParametrizedRead<T, List<Pair<String, Object>>> read) {
\r
50 Simantics.getSession().async(new UniqueRead<List<Pair<String, Object>>>() {
\r
53 public List<Pair<String, Object>> perform(ReadGraph graph) throws DatabaseException {
\r
54 T input = support.getInput(graph);
\r
55 System.err.println("read: " + read);
\r
56 return graph.syncRequest(read.get(input));
\r
59 }, new Listener<List<Pair<String, Object>>>() {
\r
62 public void exception(Throwable t) {
\r
63 t.printStackTrace();
\r
67 public void execute(final List<Pair<String, Object>> items) {
\r
68 if(isDisposed()) return;
\r
69 display.asyncExec(new Runnable() {
\r
73 if(isDisposed()) return;
\r
75 for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);
\r
76 combo.setData(items);
\r
77 combo.clearSelection();
\r
80 } catch (Throwable t) {
\r
81 t.printStackTrace();
\r
83 if (items != null) {
\r
85 for(Pair<String, Object> key : items) {
\r
86 combo.add(key.first);
\r
87 combo.setData(key.first, index++);
\r
89 String selectionKey = (String)combo.getData("_SelectionKey");
\r
90 if(selectionKey != null) {
\r
91 Integer selectionIndex = (Integer)combo.getData(selectionKey);
\r
92 if(selectionIndex != null) combo.select(selectionIndex);
\r
95 for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);
\r
102 public boolean isDisposed() {
\r
103 return combo.isDisposed();
\r
110 public <T> void setSelection(final ParametrizedRead<T, String> read) {
\r
112 Simantics.getSession().async(new UniqueRead<String>() {
\r
115 public String perform(ReadGraph graph) throws DatabaseException {
\r
116 T input = support.getInput(graph);
\r
117 System.err.println("read: " + read);
\r
118 return graph.syncRequest(read.get(input));
\r
121 }, new Listener<String>() {
\r
124 public void exception(Throwable t) {
\r
125 t.printStackTrace();
\r
129 public void execute(final String selectionKey) {
\r
130 if(isDisposed()) return;
\r
131 display.asyncExec(new Runnable() {
\r
134 public void run() {
\r
135 if(isDisposed()) return;
\r
136 if(selectionKey == null) return;
\r
137 for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);
\r
138 combo.setData("_SelectionKey", selectionKey);
\r
139 Integer selectionIndex = (Integer)combo.getData(selectionKey);
\r
140 if(selectionIndex != null) combo.select(selectionIndex);
\r
141 for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);
\r
148 public boolean isDisposed() {
\r
149 return combo.isDisposed();
\r
156 public org.eclipse.swt.custom.CCombo getWidget() {
\r
161 public Control getControl() {
\r
166 public void setInput(ISessionContext context, Object input) {
\r
168 if (selectionListeners != null) {
\r
169 for (SelectionListener listener : selectionListeners) {
\r
170 if(listener instanceof Widget) {
\r
171 ((Widget) listener).setInput(context, input);
\r
178 public synchronized void addSelectionListener(SelectionListener listener) {
\r
179 selectionListeners.add(listener);
\r
180 combo.addSelectionListener(listener);
\r
186 * @throws IndexOutOfBoundsException if index is outside widget bounds
\r
188 @SuppressWarnings("unchecked")
\r
189 public <T> Pair<String, T> getData(int index) {
\r
192 return (Pair<String, T>) itemList.get(index);
\r
196 * @return selected combo list item or <code>null</code> if no item is
\r
199 public <T> Pair<String, T> getSelectedData() {
\r
200 return getData(combo.getSelectionIndex());
\r