/
SplitButton.java
384 lines (352 loc) · 12.5 KB
/
SplitButton.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*******************************************************************************
* Copyright (c) 2018 Akuiteo (http://www.akuiteo.com).
*
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - Initial
* implementation and API
*******************************************************************************/
package org.eclipse.nebula.widgets.splitbutton;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Widget;
/**
* Instances of this class represent a selectable user interface object that
* issues notification when pressed and released.
* A menu can be attached to this button
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>PUSH, TOGGLE</dd>
* <dt><b>Events:</b></dt>
* <dd>Selection</dd>
* </dl>
* <p>
* Note: Only one of the styles PUSH or TOGGLE may be specified.
* </p>
* <p>
* Note: Only one of the styles LEFT, RIGHT, and CENTER may be specified.
*
*/
public class SplitButton extends Button {
private static final int EXTRA_WIDTH = 60;
private final List<Listener> selectionListeners = new LinkedList<>();
private final List<SelectionListener> typedSelectionListeners = new LinkedList<>();
private int splitButtonAreaLeft = -1;
private int splitButtonAreaTop = -1;
private int splitButtonAreaRight = -1;
private int splitButtonAreaBottom = -1;
private Menu menu;
/**
* Constructs a new instance of this class given its parent
* and a style value describing its behavior and appearance.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </p>
*
* @param parent a composite control which will be the parent of the new instance (cannot be null)
* @param style the style of control to construct
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see SWT#PUSH
* @see SWT#TOGGLE
* @see Widget#getStyle
*/
public SplitButton(final Composite parent, final int style) {
super(parent, checkStyle(style));
setText(""); //$NON-NLS-1$
addPaintListener();
addMouseDownListener();
menu = new Menu(getShell(), SWT.POP_UP);
}
private static int checkStyle(int style) {
return checkBits(style, SWT.NONE, SWT.PUSH, SWT.TOGGLE);
}
private static int checkBits(int style, final int int0, final int int1, final int int2) {
final int mask = int0 | int1 | int2;
if ((style & mask) == 0) {
style |= int0;
}
if ((style & int0) != 0) {
style = style & ~mask | int0;
}
if ((style & int1) != 0) {
style = style & ~mask | int1;
}
if ((style & int2) != 0) {
style = style & ~mask | int2;
}
return style;
}
private void addPaintListener() {
super.addPaintListener(new PaintListener() {
private PaintEvent e;
private int deltaX;
private int deltaY;
@Override
public void paintControl(final PaintEvent e) {
this.e = e;
final Rectangle rect = getBounds();
final Color oldForeground = e.gc.getForeground();
final Color oldBackground = e.gc.getBackground();
splitButtonAreaLeft = e.x + rect.width - 20;
splitButtonAreaTop = e.y;
splitButtonAreaRight = e.x + rect.width;
splitButtonAreaBottom = e.y + rect.height;
final String osName = System.getProperty("os.name"); //$NON-NLS-1$
final boolean isMac = osName.toLowerCase().indexOf("mac os") > -1; //$NON-NLS-1$
final int macDelta = isMac ? 2 : 0;
deltaX = -e.gc.getClipping().x - macDelta;
deltaY = -e.gc.getClipping().y - macDelta;
drawLine();
drawTriangle();
e.gc.setForeground(oldForeground);
e.gc.setBackground(oldBackground);
}
private void drawLine() {
final Rectangle rect = getBounds();
e.gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
e.gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
e.gc.setLineWidth(1);
e.gc.drawLine(e.x + rect.width - 20 + deltaX, e.y + 6 + deltaY, //
e.x + rect.width - 20 + deltaX, e.y + rect.height - 6 + deltaY);
}
private void drawTriangle() {
final Rectangle rect = getBounds();
e.gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
e.gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
e.gc.fillPolygon(new int[] { e.x + rect.width - 15 + deltaX, e.y + rect.height / 2 - 1 + deltaY, e.x + rect.width - 8 + deltaX, e.y + rect.height / 2 - 1 + deltaY, e.x + rect.width - 12 + deltaX, e.y + rect.height / 2 + 3 + deltaY });
}
});
}
private void addMouseDownListener() {
super.addListener(SWT.MouseDown, event -> {
boolean shouldShowMenu = event.x >= splitButtonAreaLeft && event.y >= splitButtonAreaTop
&& event.x <= splitButtonAreaRight && event.y <= splitButtonAreaBottom;
if (shouldShowMenu) {
final Button button = (Button) event.widget;
final Rectangle rect = button.getBounds();
final Point p = button.toDisplay(rect.x, rect.y + rect.height);
getMenu().setLocation(p.x - rect.x, p.y - rect.y);
getMenu().setVisible(true);
} else {
Event evt = new Event();
evt.widget = SplitButton.this;
evt.display = getDisplay();
evt.type = SWT.Selection;
for (Listener listener : selectionListeners) {
listener.handleEvent(evt);
}
for (SelectionListener listener : typedSelectionListeners) {
listener.widgetSelected(new SelectionEvent(evt));
}
}
});
}
/**
* Adds the listener to the collection of listeners who will be notified when
* the control is selected by the user, by sending it one of the messages
* defined in the <code>SelectionListener</code> interface.
* <p>
* <code>widgetDefaultSelected</code> is not called.
* </p>
*
* @param listener the listener which should be notified when the control is
* selected by the user,
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*
* @see SelectionListener
* @see #removeSelectionListener
* @see SelectionEvent
*/
@Override
public void addSelectionListener(SelectionListener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
typedSelectionListeners.add(listener);
}
/**
* Removes the listener from the collection of listeners who will be notified
* when the control is selected by the user.
*
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
* disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
* thread that created the receiver</li>
* </ul>
*
* @see SelectionListener
* @see #addSelectionListener
*/
@Override
public void removeSelectionListener(SelectionListener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
typedSelectionListeners.removeIf(listener::equals);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when an event of the given type occurs. When the
* event does occur in the widget, the listener is notified by
* sending it the <code>handleEvent()</code> message. The event
* type is one of the event constants defined in class <code>SWT</code>.
*
* @param eventType the type of event to listen for
* @param listener the listener which should be notified when the event occurs
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see Listener
* @see SWT
* @see #getListeners(int)
* @see #removeListener(int, Listener)
* @see #notifyListeners
*/
@Override
public void addListener(int eventType, Listener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (eventType == SWT.Selection) {
selectionListeners.add(listener);
return;
}
super.addListener(eventType, listener);
}
/**
* Removes the listener from the collection of listeners who will
* be notified when an event of the given type occurs. The event
* type is one of the event constants defined in class <code>SWT</code>.
*
* @param eventType the type of event to listen for
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException
* <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see Listener
* @see SWT
* @see #addListener
* @see #getListeners(int)
* @see #notifyListeners
*/
@Override
public void removeListener(int eventType, Listener listener) {
checkWidget();
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (eventType == SWT.Selection) {
selectionListeners.remove(listener);
return;
}
super.removeListener(eventType, listener);
}
/**
* @see org.eclipse.swt.widgets.Control#getMenu()
*/
@Override
public Menu getMenu() {
checkWidget();
return menu;
}
/**
* @see org.eclipse.swt.widgets.Control#setMenu(org.eclipse.swt.widgets.Menu)
*/
@Override
public void setMenu(final Menu menu) {
checkWidget();
this.menu = menu;
}
/**
* @see org.eclipse.swt.widgets.Widget#checkSubclass()
*/
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
/**
* @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
*/
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
final Point point = super.computeSize(wHint, hHint, changed);
point.x += EXTRA_WIDTH;
return point;
}
}