]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTButton.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTButton.java
1 package org.simantics.views.swt.client.impl;
2
3 import java.io.ByteArrayInputStream;
4 import java.util.Arrays;
5
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;
16
17 public class SWTButton extends SingleSWTViewNode<Button> {
18         
19         private static final long serialVersionUID = 7932335224632082902L;
20
21         public Function1<Object, Object> modifier;
22         public byte[] image;
23         public String tooltip;
24
25         private transient ImageDescriptor imageDesc;
26
27         @Override
28         public void createControls(Composite parent) {
29                 
30                 control = new Button(parent, style);
31
32                 setProperties();
33
34                 control.addSelectionListener(new SelectionListener() {
35
36                         @Override
37                         public void widgetSelected(SelectionEvent e) {
38                                 if(modifier != null)
39                                         modifier.apply(SWTButton.this);
40                         }
41
42                         @Override
43                         public void widgetDefaultSelected(SelectionEvent e) {
44                                 widgetSelected(e);
45                         }
46                         
47                 });
48                 
49         }
50         
51         @Override
52         public void synchronizeText(String text) {
53                 if(text != null) {
54                         control.setText(text);
55                         // This seems to be necessary for correct size computations
56                         control.getParent().layout(true);
57                 }
58         }
59
60         public void synchronizeImage(byte[] image) {
61                 ResourceManager rm =  getResourceManager();
62                 ImageDescriptor oldDesc = imageDesc;
63                 if (image != null)
64                         control.setImage(rm.createImage(imageDesc = new Descriptor(image)));
65                 if (oldDesc != null)
66                         rm.destroyImage(oldDesc);
67         }
68         
69         public void synchronizeModifier(Function1<Object, Object> modifier) {
70                 
71         }
72         
73         public void synchronizeTooltip(String tooltip) {
74                 if(tooltip != null) control.setToolTipText(tooltip);
75         }
76
77         static class Descriptor extends ImageDescriptor {
78             
79             final byte[] data;
80             final int hash;
81             
82             public Descriptor(byte[] data) {
83                 this.data = data;
84                 this.hash = Arrays.hashCode(data);
85             }
86
87             @Override
88             public ImageData getImageData() {
89                 return new ImageLoader().load(new ByteArrayInputStream(data))[0];
90             }
91
92             @Override
93             public int hashCode() {
94                 return hash;
95             }
96
97             @Override
98             public boolean equals(Object obj) {
99                 if (this == obj)
100                     return true;
101                 if (obj == null)
102                     return false;
103                 if (getClass() != obj.getClass())
104                     return false;
105                 Descriptor other = (Descriptor) obj;
106                 if (!Arrays.equals(data, other.data))
107                     return false;
108                 return true;
109             }
110
111         }
112
113 }