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