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