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