]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/CCombo2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / CCombo2.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.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;
31
32 public class CCombo2 extends WidgetImpl {
33
34     private final CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();
35     private List<Pair<String, Object>> itemList;
36
37     final private Display display;
38     final private org.eclipse.swt.custom.CCombo combo;
39
40     public CCombo2(Composite parent, WidgetSupport support, int style) {
41         super(support);
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);
46     }
47
48     public <T> void setAvailable(final ParametrizedRead<T, List<Pair<String, Object>>> read) {
49         
50         Simantics.getSession().async(new UniqueRead<List<Pair<String, Object>>>() {
51
52                         @Override
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));
57                         }
58                 
59         }, new Listener<List<Pair<String, Object>>>() {
60
61             @Override
62             public void exception(Throwable t) {
63                 t.printStackTrace();
64             }
65
66             @Override
67             public void execute(final List<Pair<String, Object>> items) {
68                 if(isDisposed()) return;
69                 display.asyncExec(new Runnable() {
70
71                     @Override
72                     public void run() {
73                         if(isDisposed()) return;
74                         itemList = items;
75                         for(SelectionListener listener : selectionListeners) combo.removeSelectionListener(listener);
76                         combo.setData(items);
77                         combo.clearSelection();
78                         try {
79                             combo.removeAll();
80                         } catch (Throwable t) {
81                             t.printStackTrace();
82                         }
83                         if (items != null) {
84                             int index = 0;
85                             for(Pair<String, Object> key : items) {
86                                 combo.add(key.first);
87                                 combo.setData(key.first, index++);
88                             }
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);
93                             }
94                         }
95                         for(SelectionListener listener : selectionListeners) combo.addSelectionListener(listener);
96                     }
97
98                 });
99             }
100
101             @Override
102             public boolean isDisposed() {
103                 return combo.isDisposed();
104             }
105
106         });
107         
108     }
109
110     public <T> void setSelection(final ParametrizedRead<T, String> read) {
111         
112         Simantics.getSession().async(new UniqueRead<String>() {
113
114                         @Override
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));
119                         }
120                 
121         }, new Listener<String>() {
122
123             @Override
124             public void exception(Throwable t) {
125                 t.printStackTrace();
126             }
127
128             @Override
129             public void execute(final String selectionKey) {
130                 if(isDisposed()) return;
131                 display.asyncExec(new Runnable() {
132
133                     @Override
134                     public void run() {
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);
142                     }
143
144                 });
145             }
146
147             @Override
148             public boolean isDisposed() {
149                 return combo.isDisposed();
150             }
151
152         });
153         
154     }
155
156     public org.eclipse.swt.custom.CCombo getWidget() {
157         return combo;
158     }
159
160     @Override
161     public Control getControl() {
162         return combo;
163     }
164
165     @Override
166     public void setInput(ISessionContext context, Object input) {
167
168         if (selectionListeners != null) {
169             for (SelectionListener listener : selectionListeners) {
170                 if(listener instanceof Widget) {
171                     ((Widget) listener).setInput(context, input);
172                 }
173             }
174         }
175
176     }
177
178     public synchronized void addSelectionListener(SelectionListener listener) {
179         selectionListeners.add(listener);
180         combo.addSelectionListener(listener);
181     }
182
183     /**
184      * @param index
185      * @return
186      * @throws IndexOutOfBoundsException if index is outside widget bounds
187      */
188     @SuppressWarnings("unchecked")
189     public <T> Pair<String, T> getData(int index) {
190         if (index == -1)
191             return null;
192         return (Pair<String, T>) itemList.get(index);
193     }
194
195     /**
196      * @return selected combo list item or <code>null</code> if no item is
197      *         selected
198      */
199     public <T> Pair<String, T> getSelectedData() {
200         return getData(combo.getSelectionIndex());
201     }
202
203 }