1 package org.simantics.views.swt.client.impl;
3 import java.io.ByteArrayInputStream;
4 import java.util.Arrays;
6 import org.eclipse.jface.resource.ImageDescriptor;
7 import org.eclipse.jface.resource.ResourceManager;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.events.SelectionListener;
10 import org.eclipse.swt.graphics.ImageData;
11 import org.eclipse.swt.graphics.ImageLoader;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Composite;
14 import org.simantics.scl.runtime.function.Function1;
15 import org.simantics.views.swt.client.base.SingleSWTViewNode;
17 public class SWTButton extends SingleSWTViewNode<Button> {
19 private static final long serialVersionUID = 7932335224632082902L;
21 public Function1<Object, Object> modifier;
23 public String tooltip;
25 private transient ImageDescriptor imageDesc;
28 public void createControls(Composite parent) {
30 control = new Button(parent, style);
34 control.addSelectionListener(new SelectionListener() {
37 public void widgetSelected(SelectionEvent e) {
39 modifier.apply(SWTButton.this);
43 public void widgetDefaultSelected(SelectionEvent e) {
52 public void synchronizeText(String text) {
54 control.setText(text);
55 // This seems to be necessary for correct size computations
56 control.getParent().layout(true);
60 public void synchronizeImage(byte[] image) {
61 ResourceManager rm = getResourceManager();
62 ImageDescriptor oldDesc = imageDesc;
64 control.setImage(rm.createImage(imageDesc = new Descriptor(image)));
66 rm.destroyImage(oldDesc);
69 public void synchronizeModifier(Function1<Object, Object> modifier) {
73 public void synchronizeTooltip(String tooltip) {
74 if(tooltip != null) control.setToolTipText(tooltip);
77 static class Descriptor extends ImageDescriptor {
82 public Descriptor(byte[] data) {
84 this.hash = Arrays.hashCode(data);
88 public ImageData getImageData() {
89 return new ImageLoader().load(new ByteArrayInputStream(data))[0];
93 public int hashCode() {
98 public boolean equals(Object obj) {
103 if (getClass() != obj.getClass())
105 Descriptor other = (Descriptor) obj;
106 if (!Arrays.equals(data, other.data))