-
Notifications
You must be signed in to change notification settings - Fork 4
/
GamaToolbar2.java
616 lines (565 loc) · 15.7 KB
/
GamaToolbar2.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*******************************************************************************************************
*
* GamaToolbar2.java, in ummisco.gama.ui.shared, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.ui.views.toolbar;
import static msi.gama.application.workbench.ThemeHelper.isDark;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolItem;
import msi.gama.runtime.PlatformHelper;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.ui.controls.FlatButton;
import ummisco.gama.ui.resources.GamaColors;
import ummisco.gama.ui.resources.GamaColors.GamaUIColor;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaColors;
import ummisco.gama.ui.views.toolbar.GamaToolbarFactory.ToggleAction;
/**
* Class GamaToolbar. A declarative wrapper around 2 toolbars (left, right).
*
* @author drogoul
* @since 3 déc. 2014
*
*/
public class GamaToolbar2 extends Composite {
{
DEBUG.ON();
}
/** The right. */
private GamaToolbarSimple left, right;
/** The has tooltip. */
private boolean hasTooltip;
/** The height. */
final int height;
/** The is visible. */
boolean isVisible = true;
/**
* Instantiates a new gama toolbar 2.
*
* @param parent
* the parent
* @param style
* the style
* @param height
* the height
*/
public GamaToolbar2(final Composite parent, final int style, final int height) {
super(parent, SWT.NONE);
this.height = height;
createLayout();
createToolbars();
}
/**
* Empty to prevent the CSS engine from changing the color now and then (apparently randomly)
*/
@Override
public void setBackground(final Color c) {
// DEBUG.OUT("setBackground() Called by " + DEBUG.CALLER() + "." + DEBUG.METHOD());
}
/**
* Sets the background color.
*
* @param c
* the new background color
*/
// Necessary to have the background color "stick"
public void setBackgroundColor(final Color c) {
// DEBUG.OUT("setBackgroundColor() called by = " + DEBUG.METHOD() + " of " + DEBUG.CALLER());
GamaColors.setBackground(this, c);
// Calls super explicitly
super.setBackground(c);
if (left != null) { GamaColors.setBackground(left, c); }
if (right != null) { GamaColors.setBackground(right, c); }
}
@Override
public void setVisible(final boolean visible) { isVisible = visible; }
@Override
public boolean isVisible() { return isVisible; }
/**
* Creates the layout.
*/
public void createLayout() {
final var layout = new GridLayout(2, false);
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
layout.marginWidth = 5;
layout.marginHeight = 0;
setLayout(layout);
}
/**
* Creates the toolbars.
*/
public void createToolbars() {
left = new GamaToolbarSimple(this, SWT.FLAT | SWT.HORIZONTAL | SWT.WRAP | SWT.NO_FOCUS | SWT.INHERIT_FORCE);
var data = new GridData(SWT.FILL, SWT.CENTER, true, true);
data.verticalIndent = 0;
data.horizontalAlignment = SWT.LEFT;
data.minimumWidth = height * 2;
left.setLayoutData(data);
right = new GamaToolbarSimple(this, SWT.FLAT | SWT.HORIZONTAL | SWT.NO_FOCUS);
data = new GridData(SWT.FILL, SWT.FILL, true, false);
data.verticalIndent = 0;
data.horizontalAlignment = SWT.RIGHT;
data.minimumWidth = height * 2;
right.setLayoutData(data);
setBackgroundColor(isDark() ? getShell().getBackground() : IGamaColors.WHITE.color());
}
@Override
protected void checkSubclass() {}
/**
* Sep. Width is not used anymore
*
* @param width
* the n
* @param side
* the side
* @return the tool item
*/
public ToolItem sep(final int width, final int side /* SWT.LEFT or SWT.RIGHT */) {
return sep(side);
}
/**
* Sep.
*
* @param side
* the side
* @return the tool item
*/
public ToolItem sep(final int side /* SWT.LEFT or SWT.RIGHT */) {
return new ToolItem(side == SWT.LEFT ? left : right, SWT.SEPARATOR);
}
/**
* Sep.
*
* @param width
* the n
* @param side
* the side
* @param height
* the height
* @return the tool item
*/
public ToolItem sep(final int width, final int height, final int side /* SWT.LEFT or SWT.RIGHT */) {
final var icon = GamaIcons.createSizer(getBackground(), width, height);
final var item = create(icon.getCode(), null, null, null, SWT.NONE, false, null, side);
item.setDisabledImage(icon.image());
if (!PlatformHelper.isLinux()) { item.setEnabled(false); }
return item;
}
/**
* Status.
*
* @param image
* the image
* @param s
* the s
* @param color
* the color
* @param side
* the side
* @return the tool item
*/
public ToolItem status(final Image image, final String s, final GamaUIColor color,
final int side /* SWT.LEFT or SWT.RIGHT */) {
wipe(side, true);
final var item = button(color, s, image, side);
refresh(true);
return item;
}
/**
* Status.
*
* @param image
* the image
* @param s
* the s
* @param l
* the l
* @param color
* the color
* @param side
* the side
* @return the tool item
*/
public ToolItem status(final Image image, final String s, final Selector l, final GamaUIColor color,
final int side /* SWT.LEFT or SWT.RIGHT */) {
wipe(side, true);
final var item = button(color, s, image, side);
((FlatButton) item.getControl()).addSelectionListener(l);
refresh(true);
return item;
}
/**
* Tooltip.
*
* @param s
* the s
* @param rgb
* the rgb
* @param side
* the side
* @return the tool item
*/
public ToolItem tooltip(final String s, final GamaUIColor rgb, final int side /* SWT.LEFT or SWT.RIGHT */) {
Color color = rgb == null ? getBackground() : rgb.color();
if (s == null) return null;
hasTooltip = true;
final var tb = getToolbar(side);
wipe(side, false);
final var other = tb == right ? left : right;
final var mySize = getSize().x;
final var remainingLeftSize = tb.getSize().x;
final var rightSize = other.getSize().x;
final var width = mySize - remainingLeftSize - rightSize - 50;
// wipe(side, false);
tb.setLayout(new GridLayout(1, false));
final var label = new Label(tb, SWT.WRAP | SWT.LEFT);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
// data.verticalIndent = 0;
label.setLayoutData(data);
label.setForeground(GamaColors.getTextColorForBackground(color).color());
StringBuilder newString = new StringBuilder();
// java.util.List<String> result = new ArrayList<>();
try {
final var reader = new BufferedReader(new StringReader(s));
var line = reader.readLine();
while (line != null) {
if (!line.trim().isEmpty()) { newString.append(line).append(System.lineSeparator()); }
line = reader.readLine();
}
} catch (final IOException exc) {}
label.setText(newString.toString());
// label.setFont(GamaFonts.getSmallFont());
label.setBackground(color/* .inactive() */);
final var t = control(label, /* c.computeSize(SWT.DEFAULT, SWT.DEFAULT).x + 10 */width, side);
refresh(true);
return t;
}
/**
* Check.
*
* @param image
* the image
* @param text
* the text
* @param tip
* the tip
* @param listener
* the listener
* @param side
* the side
* @return the tool item
*/
public ToolItem check(final String image, final String text, final String tip, final Selector listener,
final int side /* SWT.LEFT or SWT.RIGHT */) {
return create(image, text, tip, listener, SWT.CHECK, false, null, side);
}
/**
* Check.
*
* @param command
* the command
* @param side
* the side
* @return the tool item
*/
public ToolItem check(final GamaCommand command, final int side) {
return check(command.getImage(), command.getText(), command.getTooltip(), command.getListener(), side);
}
/**
* Button.
*
* @param image
* the image
* @param text
* the text
* @param tip
* the tip
* @param listener
* the listener
* @param side
* the side
* @return the tool item
*/
public ToolItem button(final String image, final String text, final String tip, final Selector listener,
final int side /* SWT.LEFT or SWT.RIGHT */) {
return create(image, text, tip, listener, SWT.PUSH, false, null, side);
}
/**
* Button.
*
* @param command
* the command
* @param side
* the side
* @return the tool item
*/
public ToolItem button(final GamaCommand command, final int side) {
return button(command.getImage(), command.getText(), command.getTooltip(), command.getListener(), side);
}
/**
* Button.
*
* @param color
* the color
* @param text
* the text
* @param listener
* the listener
* @param side
* the side
* @return the tool item
*/
public ToolItem button(final GamaUIColor color, final String text, final Selector listener, final int side) {
final var button = FlatButton.button(side == SWT.LEFT ? left : right, color, text, null);
button.addSelectionListener(listener);
return control(button, button.computeSize(SWT.DEFAULT, button.getHeight(), false).x + 4, side);
}
/**
* Button.
*
* @param color
* the color
* @param text
* the text
* @param image
* the image
* @param side
* the side
* @return the tool item
*/
public ToolItem button(final GamaUIColor color, final String text, final Image image, final int side) {
final var button = FlatButton.button(side == SWT.LEFT ? left : right, color, text, image);
return control(button, button.computeSize(SWT.DEFAULT, button.getHeight(), false).x + 4, side);
}
/**
* Button.
*
* @param color
* the color
* @param text
* the text
* @param image
* the image
* @param listener
* the listener
* @param side
* the side
* @return the tool item
*/
public ToolItem button(final GamaUIColor color, final String text, final Image image, final Selector listener,
final int side) {
final var button = FlatButton.button(side == SWT.LEFT ? left : right, color, text, image);
button.addSelectionListener(listener);
return control(button, button.computeSize(SWT.DEFAULT, button.getHeight(), false).x + 4, side);
}
/**
* Menu.
*
* @param color
* the color
* @param text
* the text
* @param side
* the side
* @return the tool item
*/
public ToolItem menu(final GamaUIColor color, final String text, final int side) {
final var button = FlatButton.menu(side == SWT.LEFT ? left : right, color, text);
return control(button, button.computeSize(SWT.DEFAULT, button.getHeight(), false).x + 4, side);
}
/**
* Menu.
*
* @param image
* the image
* @param text
* the text
* @param tip
* the tip
* @param listener
* the listener
* @param side
* the side
* @return the tool item
*/
public ToolItem menu(final String image, final String text, final String tip, final Selector listener,
final int side /* SWT.LEFT or SWT.RIGHT */) {
return create(image, text, tip, listener, SWT.DROP_DOWN, false, null, side);
}
/**
* Control.
*
* @param c
* the c
* @param width
* the width
* @param side
* the side
* @return the tool item
*/
public ToolItem control(final Control c, final int width, final int side /* SWT.LEFT or SWT.RIGHT */) {
final var control = create(null, null, null, null, SWT.SEPARATOR, false, c, side);
if (width == SWT.DEFAULT) {
control.setWidth(c.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
} else {
control.setWidth(width);
}
return control;
}
/**
* Refresh.
*
* @param layout
* the layout
*/
public void refresh(final boolean layout) {
left.layout(true, true);
left.update();
right.layout(true, true);
right.update();
layout(true, true);
update();
}
/**
* Visually update.
*/
// public void visuallyUpdate() {
// GamaToolbarFactory.visuallyUpdate(left);
// GamaToolbarFactory.visuallyUpdate(right);
// }
/**
* Wipes the toolbar (left or right), including or not the simple tool items. Retuns the width of the toolbar once
* wiped.
*
* @param side
* @param includingToolItems
* @return
*/
public void wipe(final int side /* SWT.LEFT or SWT.RIGHT */, final boolean includingToolItems) {
final var items = getToolbar(side).getItems();
for (final ToolItem t : items) {
final var c = t.getControl();
if (c == null && includingToolItems || c != null) {
if (c != null) { c.dispose(); }
t.dispose();
}
}
normalizeToolbars();
refresh(true);
}
/**
* Item.
*
* @param item
* the item
* @param side
* the side
*/
public void item(final IContributionItem item, final int side) {
item.fill(getToolbar(side), getToolbar(side).getItemCount());
}
/**
* Creates the.
*
* @param image
* the image
* @param text
* the text
* @param tip
* the tip
* @param listener
* the listener
* @param style
* the style
* @param forceText
* the force text
* @param control
* the control
* @param side
* the side
* @return the tool item
*/
private ToolItem create(final String image, final String text, final String tip, final SelectionListener listener,
final int style, final boolean forceText, final Control control,
final int side /* SWT.LEFT or SWT.RIGHT */) {
final var tb = getToolbar(side);
final var button = new ToolItem(tb, style);
if (text != null && forceText) { button.setText(text); }
if (tip != null) { button.setToolTipText(tip); }
if (image != null) {
final var im = GamaIcons.create(image).image();
button.setImage(im);
}
if (listener != null) { button.addSelectionListener(listener); }
if (control != null) {
// GamaColors.setBackground(control, getBackground());
button.setControl(control);
}
normalizeToolbars();
return button;
}
/**
* Normalize toolbars.
*/
private void normalizeToolbars() {
// final int n = right.getItemCount();
var size = 0;
for (final ToolItem t : right.getItems()) { size += t.getWidth(); }
((GridData) right.getLayoutData()).minimumWidth = size;
}
/**
* @param right2
* @return
*/
public GamaToolbarSimple getToolbar(final int side) {
return side == SWT.LEFT ? left : right;
}
/**
* @return
*/
public boolean hasTooltip() {
return hasTooltip;
}
/** The toggle. */
private ToggleAction toggle;
/**
* Sets the toogle action.
*
* @param toggle
* the new toogle action
*/
public void setToogleAction(final ToggleAction toggle) { this.toggle = toggle; }
/**
* Hide.
*/
public void hide() {
isVisible = true; // force to true
toggle.run(); // will make it false
}
/**
* Show.
*/
public void show() {
isVisible = false; // force to false
toggle.run(); // will make it true
}
}