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.SWT;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.TableItem;
22 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;
23 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
24 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
25 import org.simantics.db.management.ISessionContext;
26 import org.simantics.db.procedure.Listener;
27 import org.simantics.utils.datastructures.Pair;
28 import org.simantics.utils.ui.SWTUtils;
30 public class Table extends WidgetImpl {
32 private ReadFactory<?, List<Pair<String, Object>>> itemFactory;
33 private ReadFactory<?, String> selectionFactory;
34 private CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
36 final private org.eclipse.swt.widgets.Table table;
38 public Table(Composite parent, WidgetSupport support, int style) {
40 table = new org.eclipse.swt.widgets.Table(parent, style);
41 table.setData("org.simantics.browsing.ui.widgets.Combo", this);
42 support.register(this);
45 public void setItemFactory(ReadFactory<?, List<Pair<String, Object>>> itemFactory) {
46 this.itemFactory = itemFactory;
49 public void setSelectionFactory(ReadFactory<?, String> selectionFactory) {
50 this.selectionFactory = selectionFactory;
53 public org.eclipse.swt.widgets.Table getWidget() {
58 public Control getControl() {
63 public void setInput(ISessionContext context, Object input) {
65 if (selectionListeners != null) {
66 for (SelectionListener listener : selectionListeners) {
67 if(listener instanceof Widget) {
68 ((Widget) listener).setInput(context, input);
73 if(itemFactory != null) {
74 itemFactory.listen(context, input, new Listener<List<Pair<String, Object>>>() {
77 public void exception(Throwable t) {
82 public void execute(final List<Pair<String, Object>> items) {
83 SWTUtils.asyncExec(table, new Runnable() {
87 if(isDisposed()) return;
88 // System.out.println("Combo received new items: " + items.size());
89 for(SelectionListener listener : selectionListeners) table.removeSelectionListener(listener);
93 } catch (Throwable t) {
96 //table.setItemCount(items.size());
97 for(Pair<String, Object> key : items) {
98 TableItem item = new TableItem (table, SWT.NONE);
99 item.setText(key.first);
100 item.setData(key.second);
102 String selectionKey = (String)table.getData("_SelectionKey");
103 if(selectionKey != null) {
104 Integer selectionIndex = (Integer)table.getData(selectionKey);
105 if(selectionIndex != null) table.select(selectionIndex);
107 for(SelectionListener listener : selectionListeners) table.addSelectionListener(listener);
109 //label.setSize(200, 20);
110 // label.getParent().layout();
111 // label.getParent().getParent().layout();
118 public boolean isDisposed() {
119 return table.isDisposed();
125 if(selectionFactory != null) {
126 selectionFactory.listen(context, input, new Listener<String>() {
129 public void exception(Throwable t) {
134 public void execute(final String selectionKey) {
135 SWTUtils.asyncExec(table, new Runnable() {
139 if(isDisposed()) return;
140 if(selectionKey == null) return;
141 // System.out.println("Combo received new selection key: " + selectionKey);
143 table.setData("_SelectionKey", selectionKey);
144 Integer selectionIndex = (Integer)table.getData(selectionKey);
145 if(selectionIndex != null) table.select(selectionIndex);
153 public boolean isDisposed() {
154 return table.isDisposed();
162 public synchronized void addSelectionListener(SelectionListener listener) {
163 selectionListeners.add(listener);
164 table.addSelectionListener(listener);