]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/Button.java
Merge "(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, () -> {
94                         if(isDisposed()) return;
95 //                        System.out.println("Button received new text: " + text);
96                         button.setText(t.toString());
97                     });
98                 }
99
100                 @Override
101                 public void execute(final String text) {
102                     SWTUtils.asyncExec(button, () -> {
103                         if(isDisposed()) return;
104 //                        System.out.println("Button received new text: " + text);
105                         button.setText(text);
106                     });
107                 }
108
109                 @Override
110                 public boolean isDisposed() {
111                     return button.isDisposed();
112                 }
113
114             });
115         }
116
117         if(tooltipFactory != null) {
118             tooltipFactory.listen(context, input, new Listener<String>() {
119
120                 @Override
121                 public void exception(Throwable t) {
122                     ErrorLogger.defaultLogError(t);
123                 }
124
125                 @Override
126                 public void execute(final String text) {
127                     SWTUtils.asyncExec(button, () -> {
128                         if(isDisposed()) return;
129 //                        System.out.println("Button received new tooltip: " + text);
130                         button.setToolTipText(text);
131                     });
132                 }
133
134                 @Override
135                 public boolean isDisposed() {
136                     return button.isDisposed();
137                 }
138
139             });
140         }
141
142         if(imageFactory != null) {
143             imageFactory.listen(context, input, new Listener<ImageDescriptor>() {
144
145                 @Override
146                 public void exception(Throwable t) {
147                     ErrorLogger.defaultLogError(t);
148                 }
149
150                 @Override
151                 public void execute(final ImageDescriptor imageDescriptor) {
152                     SWTUtils.asyncExec(button, () -> {
153                         if(isDisposed()) return;
154 //                        System.out.println("Button received new image");
155                         ResourceManager rm = support.getParameter(WidgetSupport.RESOURCE_MANAGER);
156                         if (rm != null) {
157                             Image image = (Image) rm.get(imageDescriptor);
158                             button.setImage(image);
159                         }
160                         // TODO: how can we resize without this knife??
161                         button.getParent().layout();
162                         button.getParent().getParent().layout();
163                     });
164                 }
165
166                 @Override
167                 public boolean isDisposed() {
168                     return button.isDisposed();
169                 }
170
171             });
172         }
173
174         if (selectionFactory != null) {
175             selectionFactory.listen(context, input, new Listener<Boolean>() {
176                 @Override
177                 public void exception(Throwable t) {
178                     ErrorLogger.defaultLogError(t);
179                 }
180                 @Override
181                 public void execute(final Boolean selected) {
182                     SWTUtils.asyncExec(button, () -> {
183                         if(isDisposed()) return;
184                         button.setSelection(Boolean.TRUE.equals(selected));
185                     });
186                 }
187                 @Override
188                 public boolean isDisposed() {
189                     return button.isDisposed();
190                 }
191             });
192         }
193     }
194
195     public void setText(String text) {
196         button.setText(text);
197     }
198
199     public <T> void setText(final ParametrizedRead<T, String> read) {
200         Simantics.getSession().async(new UniqueRead<String>() {
201             @Override
202             public String perform(ReadGraph graph) throws DatabaseException {
203                 T input = support.getInput(graph);
204                 return graph.syncRequest(read.get(input));
205             }
206         }, new Listener<String>() {
207             @Override
208             public void exception(Throwable t) {
209                 ErrorLogger.defaultLogError(t);
210             }
211
212             @Override
213             public void execute(final String text) {
214                 if(isDisposed()) return;
215                 button.getDisplay().asyncExec(() -> {
216                     if(isDisposed()) return;
217                     button.setText(text);
218                 });
219             }
220
221             @Override
222             public boolean isDisposed() {
223                 return button.isDisposed();
224             }
225         });
226     }
227
228     public void setTooltipText(String text) {
229         button.setToolTipText(text);
230     }
231
232     public void setImage(Image image) {
233         button.setImage(image);
234     }
235
236     public void setSelection(boolean selected) {
237         button.setSelection(selected);
238     }
239
240     public synchronized void addSelectionListener(SelectionListener listener) {
241         selectionListeners.add(listener);
242         button.addSelectionListener(listener);
243     }
244
245 }