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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt.widgets;
\r
14 import java.util.concurrent.CopyOnWriteArrayList;
\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
35 public class Button extends WidgetImpl {
\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
43 private final org.eclipse.swt.widgets.Button button;
\r
45 public Button(Composite parent, WidgetSupport support, int style) {
\r
47 button = new org.eclipse.swt.widgets.Button(parent, style);
\r
48 support.register(this);
\r
51 public void setTextFactory(ReadFactory<?, String> textFactory) {
\r
52 this.textFactory = textFactory;
\r
55 public void setTooltipFactory(ReadFactory<?, String> tooltipFactory) {
\r
56 this.tooltipFactory = tooltipFactory;
\r
59 public void setImageFactory(ReadFactory<?, ImageDescriptor> imageFactory) {
\r
60 this.imageFactory = imageFactory;
\r
63 public void setSelectionFactory(ReadFactory<?, Boolean> selectionFactory) {
\r
64 this.selectionFactory = selectionFactory;
\r
67 public org.eclipse.swt.widgets.Button getWidget() {
\r
72 public Control getControl() {
\r
77 public void setInput(ISessionContext context, Object input) {
\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
88 if(textFactory != null) {
\r
89 textFactory.listen(context, input, new Listener<String>() {
\r
92 public void exception(final Throwable t) {
\r
93 SWTUtils.asyncExec(button, new Runnable() {
\r
97 if(isDisposed()) return;
\r
98 // System.out.println("Button received new text: " + text);
\r
99 button.setText(t.toString());
\r
106 public void execute(final String text) {
\r
107 SWTUtils.asyncExec(button, new Runnable() {
\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
120 public boolean isDisposed() {
\r
121 return button.isDisposed();
\r
127 if(tooltipFactory != null) {
\r
128 tooltipFactory.listen(context, input, new Listener<String>() {
\r
131 public void exception(Throwable t) {
\r
132 ErrorLogger.defaultLogError(t);
\r
136 public void execute(final String text) {
\r
137 SWTUtils.asyncExec(button, new Runnable() {
\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
150 public boolean isDisposed() {
\r
151 return button.isDisposed();
\r
157 if(imageFactory != null) {
\r
158 imageFactory.listen(context, input, new Listener<ImageDescriptor>() {
\r
161 public void exception(Throwable t) {
\r
162 ErrorLogger.defaultLogError(t);
\r
166 public void execute(final ImageDescriptor imageDescriptor) {
\r
167 SWTUtils.asyncExec(button, new Runnable() {
\r
170 public void run() {
\r
172 if(isDisposed()) return;
\r
173 // System.out.println("Button received new image");
\r
174 ResourceManager rm = support.getParameter(WidgetSupport.RESOURCE_MANAGER);
\r
176 Image image = (Image) rm.get(imageDescriptor);
\r
177 button.setImage(image);
\r
179 // TODO: how can we resize without this knife??
\r
180 button.getParent().layout();
\r
181 button.getParent().getParent().layout();
\r
189 public boolean isDisposed() {
\r
190 return button.isDisposed();
\r
196 if (selectionFactory != null) {
\r
197 selectionFactory.listen(context, input, new Listener<Boolean>() {
\r
199 public void exception(Throwable t) {
\r
200 ErrorLogger.defaultLogError(t);
\r
203 public void execute(final Boolean selected) {
\r
204 SWTUtils.asyncExec(button, new Runnable() {
\r
206 public void run() {
\r
207 if(isDisposed()) return;
\r
208 button.setSelection(selected);
\r
213 public boolean isDisposed() {
\r
214 return button.isDisposed();
\r
220 public void setText(String text) {
\r
221 button.setText(text);
\r
224 public <T> void setText(final ParametrizedRead<T, String> read) {
\r
226 Simantics.getSession().async(new UniqueRead<String>() {
\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
234 }, new Listener<String>() {
\r
237 public void exception(Throwable t) {
\r
238 t.printStackTrace();
\r
242 public void execute(final String text) {
\r
244 if(isDisposed()) return;
\r
246 button.getDisplay().asyncExec(new Runnable() {
\r
249 public void run() {
\r
250 button.setText(text);
\r
257 public boolean isDisposed() {
\r
258 return button.isDisposed();
\r
265 public void setTooltipText(String text) {
\r
266 button.setToolTipText(text);
\r
269 public void setImage(Image image) {
\r
270 button.setImage(image);
\r
273 public void setSelection(boolean selected) {
\r
274 button.setSelection(selected);
\r
277 public synchronized void addSelectionListener(SelectionListener listener) {
\r
278 selectionListeners.add(listener);
\r
279 button.addSelectionListener(listener);
\r