]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Table.java
Optimized EntityInstances and ModelingUtils.search*Shallow queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / Table.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
13
14 import java.util.List;
15 import java.util.concurrent.CopyOnWriteArrayList;
16
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;
29
30 public class Table extends WidgetImpl {
31
32         private ReadFactory<?, List<Pair<String, Object>>> itemFactory;
33         private ReadFactory<?, String> selectionFactory;
34     private CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
35
36         final private org.eclipse.swt.widgets.Table table;
37         
38         public Table(Composite parent, WidgetSupport support, int style) {
39                 super(support);
40                 table = new org.eclipse.swt.widgets.Table(parent, style);
41                 table.setData("org.simantics.browsing.ui.widgets.Combo", this);
42                 support.register(this);
43         }
44         
45         public void setItemFactory(ReadFactory<?, List<Pair<String, Object>>> itemFactory) {
46                 this.itemFactory = itemFactory;
47         }
48
49         public void setSelectionFactory(ReadFactory<?, String> selectionFactory) {
50                 this.selectionFactory = selectionFactory;
51         }
52
53         public org.eclipse.swt.widgets.Table getWidget() {
54                 return table;
55         }
56         
57         @Override
58         public Control getControl() {
59                 return table;
60         }
61
62         @Override
63         public void setInput(ISessionContext context, Object input) {
64
65         if (selectionListeners != null) {
66             for (SelectionListener listener : selectionListeners) {
67                 if(listener instanceof Widget) {
68                     ((Widget) listener).setInput(context, input);
69                 }
70             }
71         }
72                 
73                 if(itemFactory != null) {
74                         itemFactory.listen(context, input, new Listener<List<Pair<String, Object>>>() {
75
76                                 @Override
77                                 public void exception(Throwable t) {
78                                         t.printStackTrace();
79                                 }
80
81                                 @Override
82                                 public void execute(final List<Pair<String, Object>> items) {
83                                         SWTUtils.asyncExec(table, new Runnable() {
84
85                                                 @Override
86                                                 public void run() {
87                                                         if(isDisposed()) return;
88 //                                                      System.out.println("Combo received new items: " + items.size());
89                                                         for(SelectionListener listener : selectionListeners) table.removeSelectionListener(listener);
90                                                         table.setData(items);
91                                                         try {
92                                                                 table.removeAll();
93                                                         } catch (Throwable t) {
94                                                                 t.printStackTrace();
95                                                         }
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);
101                                                         }
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);
106                                                         }
107                                                         for(SelectionListener listener : selectionListeners) table.addSelectionListener(listener);
108                                                         
109                                                         //label.setSize(200, 20);
110 //                                                      label.getParent().layout();
111 //                                                      label.getParent().getParent().layout();
112                                                 }
113
114                                         });
115                                 }
116
117                                 @Override
118                                 public boolean isDisposed() {
119                                         return table.isDisposed();
120                                 }
121
122                         });
123                 }
124
125                 if(selectionFactory != null) {
126                         selectionFactory.listen(context, input, new Listener<String>() {
127
128                                 @Override
129                                 public void exception(Throwable t) {
130                                         t.printStackTrace();
131                                 }
132
133                                 @Override
134                                 public void execute(final String selectionKey) {
135                                         SWTUtils.asyncExec(table, new Runnable() {
136
137                                                 @Override
138                                                 public void run() {
139                                                         if(isDisposed()) return;
140                                                         if(selectionKey == null) return;
141 //                                                      System.out.println("Combo received new selection key: " + selectionKey);
142                                                         
143                                                         table.setData("_SelectionKey", selectionKey);
144                                                         Integer selectionIndex = (Integer)table.getData(selectionKey);
145                                                         if(selectionIndex != null) table.select(selectionIndex);
146                                                         
147                                                 }
148
149                                         });
150                                 }
151
152                                 @Override
153                                 public boolean isDisposed() {
154                                         return table.isDisposed();
155                                 }
156
157                         });
158                 }
159                 
160         }
161
162     public synchronized void addSelectionListener(SelectionListener listener) {
163         selectionListeners.add(listener);
164                 table.addSelectionListener(listener);
165     }
166         
167 }