]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Button.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / Button.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt.widgets;\r
13 \r
14 import java.util.concurrent.CopyOnWriteArrayList;\r
15 \r
16 import org.eclipse.jface.resource.ImageDescriptor;\r
17 import org.eclipse.jface.resource.ResourceManager;\r
18 import org.eclipse.swt.events.SelectionListener;\r
19 import org.eclipse.swt.graphics.Image;\r
20 import org.eclipse.swt.widgets.Composite;\r
21 import org.eclipse.swt.widgets.Control;\r
22 import org.simantics.Simantics;\r
23 import org.simantics.browsing.ui.common.ErrorLogger;\r
24 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
25 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
26 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
27 import org.simantics.db.ReadGraph;\r
28 import org.simantics.db.common.request.ParametrizedRead;\r
29 import org.simantics.db.common.request.UniqueRead;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.management.ISessionContext;\r
32 import org.simantics.db.procedure.Listener;\r
33 import org.simantics.utils.ui.SWTUtils;\r
34 \r
35 public class Button extends WidgetImpl {\r
36 \r
37     private ReadFactory<?, String> textFactory;\r
38     private ReadFactory<?, String> tooltipFactory;\r
39     private ReadFactory<?, ImageDescriptor> imageFactory;\r
40     private ReadFactory<?, Boolean> selectionFactory;\r
41     private CopyOnWriteArrayList<SelectionListener> selectionListeners = new CopyOnWriteArrayList<SelectionListener>();\r
42 \r
43     private final org.eclipse.swt.widgets.Button button;\r
44 \r
45     public Button(Composite parent, WidgetSupport support, int style) {\r
46         super(support);\r
47         button = new org.eclipse.swt.widgets.Button(parent, style);\r
48         support.register(this);\r
49     }\r
50 \r
51     public void setTextFactory(ReadFactory<?, String> textFactory) {\r
52         this.textFactory = textFactory;\r
53     }\r
54 \r
55     public void setTooltipFactory(ReadFactory<?, String> tooltipFactory) {\r
56         this.tooltipFactory = tooltipFactory;\r
57     }\r
58 \r
59     public void setImageFactory(ReadFactory<?, ImageDescriptor> imageFactory) {\r
60         this.imageFactory = imageFactory;\r
61     }\r
62 \r
63     public void setSelectionFactory(ReadFactory<?, Boolean> selectionFactory) {\r
64         this.selectionFactory = selectionFactory;\r
65     }\r
66 \r
67     public org.eclipse.swt.widgets.Button getWidget() {\r
68         return button;\r
69     }\r
70 \r
71     @Override\r
72     public Control getControl() {\r
73         return button;\r
74     }\r
75 \r
76     @Override\r
77     public void setInput(ISessionContext context, Object input) {\r
78 \r
79         if (selectionListeners != null) {\r
80             for (SelectionListener listener : selectionListeners) {\r
81                 if(listener instanceof Widget) {\r
82                     ((Widget) listener).setInput(context, input);\r
83                 }\r
84             }\r
85         }\r
86 \r
87 \r
88         if(textFactory != null) {\r
89             textFactory.listen(context, input, new Listener<String>() {\r
90 \r
91                 @Override\r
92                 public void exception(final Throwable t) {\r
93                     SWTUtils.asyncExec(button, new Runnable() {\r
94 \r
95                         @Override\r
96                         public void run() {\r
97                             if(isDisposed()) return;\r
98 //                            System.out.println("Button received new text: " + text);\r
99                             button.setText(t.toString());\r
100                         }\r
101 \r
102                     });\r
103                 }\r
104 \r
105                 @Override\r
106                 public void execute(final String text) {\r
107                     SWTUtils.asyncExec(button, new Runnable() {\r
108 \r
109                         @Override\r
110                         public void run() {\r
111                             if(isDisposed()) return;\r
112 //                            System.out.println("Button received new text: " + text);\r
113                             button.setText(text);\r
114                         }\r
115 \r
116                     });\r
117                 }\r
118 \r
119                 @Override\r
120                 public boolean isDisposed() {\r
121                     return button.isDisposed();\r
122                 }\r
123 \r
124             });\r
125         }\r
126 \r
127         if(tooltipFactory != null) {\r
128             tooltipFactory.listen(context, input, new Listener<String>() {\r
129 \r
130                 @Override\r
131                 public void exception(Throwable t) {\r
132                     ErrorLogger.defaultLogError(t);\r
133                 }\r
134 \r
135                 @Override\r
136                 public void execute(final String text) {\r
137                     SWTUtils.asyncExec(button, new Runnable() {\r
138 \r
139                         @Override\r
140                         public void run() {\r
141                             if(isDisposed()) return;\r
142 //                            System.out.println("Button received new tooltip: " + text);\r
143                             button.setToolTipText(text);\r
144                         }\r
145 \r
146                     });\r
147                 }\r
148 \r
149                 @Override\r
150                 public boolean isDisposed() {\r
151                     return button.isDisposed();\r
152                 }\r
153 \r
154             });\r
155         }\r
156 \r
157         if(imageFactory != null) {\r
158             imageFactory.listen(context, input, new Listener<ImageDescriptor>() {\r
159 \r
160                 @Override\r
161                 public void exception(Throwable t) {\r
162                     ErrorLogger.defaultLogError(t);\r
163                 }\r
164 \r
165                 @Override\r
166                 public void execute(final ImageDescriptor imageDescriptor) {\r
167                     SWTUtils.asyncExec(button, new Runnable() {\r
168 \r
169                         @Override\r
170                         public void run() {\r
171 \r
172                             if(isDisposed()) return;\r
173 //                            System.out.println("Button received new image");\r
174                             ResourceManager rm = support.getParameter(WidgetSupport.RESOURCE_MANAGER);\r
175                             if (rm != null) {\r
176                                 Image image = (Image) rm.get(imageDescriptor);\r
177                                 button.setImage(image);\r
178                             }\r
179                             // TODO: how can we resize without this knife??\r
180                             button.getParent().layout();\r
181                             button.getParent().getParent().layout();\r
182 \r
183                         }\r
184 \r
185                     });\r
186                 }\r
187 \r
188                 @Override\r
189                 public boolean isDisposed() {\r
190                     return button.isDisposed();\r
191                 }\r
192 \r
193             });\r
194         }\r
195 \r
196         if (selectionFactory != null) {\r
197             selectionFactory.listen(context, input, new Listener<Boolean>() {\r
198                 @Override\r
199                 public void exception(Throwable t) {\r
200                     ErrorLogger.defaultLogError(t);\r
201                 }\r
202                 @Override\r
203                 public void execute(final Boolean selected) {\r
204                     SWTUtils.asyncExec(button, new Runnable() {\r
205                         @Override\r
206                         public void run() {\r
207                             if(isDisposed()) return;\r
208                             button.setSelection(selected);\r
209                         }\r
210                     });\r
211                 }\r
212                 @Override\r
213                 public boolean isDisposed() {\r
214                     return button.isDisposed();\r
215                 }\r
216             });\r
217         }\r
218     }\r
219 \r
220     public void setText(String text) {\r
221         button.setText(text);\r
222     }\r
223 \r
224     public <T> void setText(final ParametrizedRead<T, String> read) {\r
225                 \r
226         Simantics.getSession().async(new UniqueRead<String>() {\r
227 \r
228                 @Override\r
229                 public String perform(ReadGraph graph) throws DatabaseException {\r
230                         T input = support.getInput(graph);\r
231                         return graph.syncRequest(read.get(input));\r
232                 }\r
233 \r
234         }, new Listener<String>() {\r
235 \r
236                 @Override\r
237                 public void exception(Throwable t) {\r
238                         t.printStackTrace();\r
239                 }\r
240 \r
241                 @Override\r
242                 public void execute(final String text) {\r
243 \r
244                         if(isDisposed()) return;\r
245 \r
246                         button.getDisplay().asyncExec(new Runnable() {\r
247 \r
248                                 @Override\r
249                                 public void run() {\r
250                                         button.setText(text);\r
251                                 }\r
252 \r
253                         });\r
254                 }\r
255 \r
256                 @Override\r
257                 public boolean isDisposed() {\r
258                         return button.isDisposed();\r
259                 }\r
260 \r
261         });\r
262 \r
263     }\r
264     \r
265     public void setTooltipText(String text) {\r
266         button.setToolTipText(text);\r
267     }\r
268 \r
269     public void setImage(Image image) {\r
270         button.setImage(image);\r
271     }\r
272 \r
273     public void setSelection(boolean selected) {\r
274         button.setSelection(selected);\r
275     }\r
276 \r
277     public synchronized void addSelectionListener(SelectionListener listener) {\r
278         selectionListeners.add(listener);\r
279         button.addSelectionListener(listener);\r
280     }\r
281 \r
282 }\r