/
ToolItem.java
318 lines (277 loc) · 9.03 KB
/
ToolItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*******************************************************************************
* Copyright (c) 2014 IBM Corporation, BestSolution.at and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v.2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API
* Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.widgets;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.Control;
import javafx.scene.control.MenuButton;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Separator;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
public class ToolItem extends Item {
private Region nativeControl;
private ToolBar parent;
private Tooltip tooltip;
private String tooltipText;
private InvalidationListener disabledListener;
private EventHandler<MouseEvent> mouseHandler;
private Image disabledImage;
private Image hotImage;
private boolean hover;
private int width;
private org.eclipse.swt.widgets.Control control;
public ToolItem(ToolBar parent, int style) {
super(parent, style);
this.parent = parent;
parent.internal_itemAdded(this);
}
public ToolItem (ToolBar parent, int style, int index) {
super(parent, style);
this.parent = parent;
parent.internal_itemAdded(this, index);
}
@Override
protected Node createWidget() {
if( (style & SWT.CHECK) == SWT.CHECK ) {
nativeControl = new ToggleButton();
} else if( (style & SWT.RADIO) == SWT.RADIO ) {
nativeControl = new RadioButton();
} else if( (style & SWT.SEPARATOR) == SWT.SEPARATOR ) {
//TODO Why stackpane wrapper??
Separator separator = new Separator();
separator.setOrientation(Orientation.VERTICAL);
nativeControl = new StackPane(separator);
} else if( (style & SWT.DROP_DOWN) == SWT.DROP_DOWN ) {
MenuButton m = new MenuButton();
m.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Node n = (Node) event.getTarget();
Event evt = new Event();
if( n.getStyleClass().contains("arrow") ) {
evt.detail = SWT.ARROW;
}
internal_sendEvent(SWT.Selection, evt, true);
}
});
nativeControl = m;
} else {
nativeControl = new Button();
nativeControl.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Event evt = new Event();
internal_sendEvent(SWT.Selection, evt, true);
}
});
// nativeControl.setMouseTransparent(true);
}
return nativeControl;
}
@Override
public Node internal_getNativeObject() {
return nativeControl;
}
@Override
public void setImage(Image image) {
super.setImage(image);
if( hotImage != null || disabledImage != null ) {
updateStateImage();
} else {
if( nativeControl instanceof ButtonBase ) {
((ButtonBase) nativeControl).setGraphic(image == null ? null : new ImageView(image.internal_getImage()));
}
}
}
@Override
public void setText(String string) {
super.setText(string);
if( nativeControl instanceof ButtonBase ) {
((ButtonBase) nativeControl).setText(string);
}
}
public void addSelectionListener(SelectionListener listener) {
checkWidget();
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
TypedListener typedListener = new TypedListener(listener);
addListener(SWT.Selection,typedListener);
addListener(SWT.DefaultSelection,typedListener);
}
@Override
public void dispose() {
parent.internal_itemRemoved(this);
super.dispose();
}
public Rectangle getBounds () {
return new Rectangle((int)nativeControl.getLayoutX(), (int)nativeControl.getLayoutY(), (int)nativeControl.getWidth(), (int)nativeControl.getHeight());
}
public org.eclipse.swt.widgets.Control getControl () {
return control;
}
public Image getDisabledImage () {
return disabledImage;
}
public boolean getEnabled () {
return ! nativeControl.isDisable();
}
public Image getHotImage () {
return hotImage;
}
public ToolBar getParent () {
return parent;
}
public boolean getSelection () {
if( nativeControl instanceof ToggleButton ) {
return ((ToggleButton) nativeControl).isSelected();
}
return false;
}
public String getToolTipText () {
return tooltipText;
}
public int getWidth () {
return width;
}
public boolean isEnabled () {
return ! nativeControl.isDisabled();
}
public void removeSelectionListener(SelectionListener listener) {
removeListener(SWT.Selection, listener);
removeListener(SWT.DefaultSelection, listener);
}
public void setControl (org.eclipse.swt.widgets.Control control) {
this.control = control;
if( (style & SWT.SEPARATOR) == SWT.SEPARATOR ) {
if( control == null ) {
((StackPane)nativeControl).getChildren().setAll(new Separator());
} else {
((StackPane)nativeControl).getChildren().setAll(control.internal_getNativeObject());
}
}
}
public void setEnabled (boolean enabled) {
nativeControl.setDisable(!enabled);
}
public void setDisabledImage (Image image) {
this.disabledImage = image;
if( image != null ) {
if( disabledListener == null ) {
nativeControl.disabledProperty().addListener(getDisabledListener());
}
} else {
if( disabledListener != null ) {
nativeControl.disabledProperty().removeListener(getDisabledListener());
disabledListener = null;
}
}
}
public void setHotImage (Image image) {
this.hotImage = image;
if( image != null ) {
if( mouseHandler == null ) {
nativeControl.addEventHandler(MouseEvent.MOUSE_ENTERED, getMouseHandler());
nativeControl.addEventHandler(MouseEvent.MOUSE_EXITED, getMouseHandler());
}
} else {
if( mouseHandler != null ) {
nativeControl.removeEventHandler(MouseEvent.MOUSE_ENTERED, getMouseHandler());
nativeControl.removeEventHandler(MouseEvent.MOUSE_EXITED, getMouseHandler());
hover = false;
mouseHandler = null;
}
}
}
public void setSelection (boolean selected) {
if( nativeControl instanceof ToggleButton ) {
((ToggleButton) nativeControl).setSelected(selected);
}
}
public void setToolTipText (String string) {
this.tooltipText = string;
if( nativeControl instanceof Control ) {
Control control = (Control) nativeControl;
if( string == null ) {
tooltip = null;
} else {
if( tooltip == null ) {
tooltip = new Tooltip();
}
tooltip.setText(string);
}
control.setTooltip(tooltip);
}
}
public void setWidth (int width) {
this.width = width;
nativeControl.setPrefWidth(width);
}
private void updateStateImage() {
//TODO We should probably reuse ImageView
if( nativeControl instanceof ButtonBase ) {
if( nativeControl.isDisabled() ) {
if( disabledImage != null ) {
((ButtonBase) nativeControl).setGraphic(new ImageView(disabledImage.internal_getImage()));
} else {
((ButtonBase) nativeControl).setGraphic(getImage() == null ? null : new ImageView(getImage().internal_getImage()));
}
} else if( hover ) {
if( hotImage != null ) {
((ButtonBase) nativeControl).setGraphic(new ImageView(hotImage.internal_getImage()));
} else {
((ButtonBase) nativeControl).setGraphic(getImage() == null ? null : new ImageView(getImage().internal_getImage()));
}
} else {
((ButtonBase) nativeControl).setGraphic(getImage() == null ? null : new ImageView(getImage().internal_getImage()));
}
}
}
private InvalidationListener getDisabledListener() {
if( disabledListener == null ) {
disabledListener = new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
updateStateImage();
}
};
}
return disabledListener;
}
private EventHandler<MouseEvent> getMouseHandler() {
if( mouseHandler == null ) {
mouseHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
hover = event.getEventType() == MouseEvent.MOUSE_ENTERED;
updateStateImage();
}
};
}
return mouseHandler;
}
}