Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b4c56dd4bec12312a5533d1ea48ac43dd09aa72 (plain) (blame)
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
/*******************************************************************************
 * Copyright (c) 2012 Laurent CARON. All rights reserved. 
 * This program and the accompanying materials are made available under the terms 
 * of the Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *     Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
 *******************************************************************************/
package org.mihalis.opal.breadcrumb;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Widget;
import org.mihalis.opal.utils.SWTGraphicUtil;

/**
 * Instances of this class represent a selectable user interface object that represents an item of a breadcrumb.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>TOGGLE,PUSH,NONE</dd>
 * <dt><b>Events:</b></dt>
 * <dd>Selection</dd>
 * </dl>
 * 
 */
public class BreadcrumbItem extends Item {

	/** The Constant MIN_WIDTH. */
	private static final int MIN_WIDTH = 40;
	
	/** The Constant MARGIN. */
	private static final int MARGIN = 4;
	
	/** The selected color. */
	private static Color SELECTED_COLOR = SWTGraphicUtil.getColorSafely(223, 220, 213);

	/** The parent breadcrumb. */
	private final Breadcrumb parentBreadcrumb;
	
	/** The selection listeners. */
	private final List<SelectionListener> selectionListeners;
	
	/** The bounds. */
	private Rectangle bounds;
	
	/** The enabled. */
	private boolean enabled;
	
	/** The selection. */
	private boolean selection;
	
	/** The width. */
	private int width;
	
	/** The height. */
	private int height;
	
	/** The disabled image. */
	private Image disabledImage;
	
	/** The selection image. */
	private Image selectionImage;
	
	/** The alignment. */
	private int alignment;
	
	/** The text color selected. */
	private Color textColorSelected;
	
	/** The text color. */
	private Color textColor;
	
	/** The tooltip text. */
	private String tooltipText;
	
	/** The gc. */
	private GC gc;
	
	/** The toolbar height. */
	private int toolbarHeight;
	
	/** The is last item of the bread crumb. */
	private boolean isLastItemOfTheBreadCrumb;

	/**
	 * Constructs a new instance of this class given its parent (which must be a
	 * <code>Breadcrumb</code>) and a style value describing its behavior and
	 * appearance. The item is added to the end of the items maintained by its
	 * parent.
	 * <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)
	 * @see Widget#getStyle
	 * @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>
	 */
	public BreadcrumbItem(final Breadcrumb parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Constructs a new instance of this class given its parent (which must be a
	 * <code>Breadcrumb</code>) and a style value describing its behavior and
	 * appearance. The item is added to the end of the items maintained by its
	 * parent.
	 * <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
	 * @see Widget#getStyle
	 * @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>
	 */
	public BreadcrumbItem(final Breadcrumb parent, final int style) {
		super(parent, checkStyle(style));
		parent.addItem(this);
		this.parentBreadcrumb = parent;
		this.textColor = parent.getDisplay().getSystemColor(SWT.COLOR_BLACK);
		this.textColorSelected = parent.getDisplay().getSystemColor(SWT.COLOR_BLACK);
		this.enabled = true;

		if ((style & SWT.LEFT) != 0) {
			this.alignment = SWT.LEFT;
		}
		if ((style & SWT.CENTER) != 0) {
			this.alignment = SWT.CENTER;
		}
		if ((style & SWT.RIGHT) != 0) {
			this.alignment = SWT.RIGHT;
		}

		this.selectionListeners = new ArrayList<SelectionListener>();
		this.width = this.height = -1;
	}

	/**
	 * Check style.
	 *
	 * @param style the style
	 * @return the int
	 */
	private static int checkStyle(int style) {
		style = checkBits(style, SWT.NONE, SWT.PUSH, SWT.TOGGLE);
		if ((style & (SWT.PUSH | SWT.TOGGLE)) != 0) {
			return checkBits(style, SWT.CENTER, SWT.LEFT, SWT.RIGHT);
		}
		return style;
	}

	/**
	 * Check bits.
	 *
	 * @param style the style
	 * @param int0 the int0
	 * @param int1 the int1
	 * @param int2 the int2
	 * @return the int
	 */
	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;
	}

	/**
	 * 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,
	 * @see SelectionListener
	 * @see #removeSelectionListener
	 * @see SelectionEvent
	 * @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>
	 */
	public void addSelectionListener(final SelectionListener listener) {
		checkWidget();
		if (listener == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		this.selectionListeners.add(listener);
	}

	/**
	 * Dispose.
	 *
	 * @see org.eclipse.swt.widgets.Widget#dispose()
	 */
	@Override
	public void dispose() {
		getParent().removeItem(this);
		this.bounds = null;
		this.disabledImage = null;
		this.selectionImage = null;
		this.textColor = null;
		this.textColorSelected = null;
		super.dispose();
	}

	/**
	 * Returns a value which describes the position of the text in the receiver. 
	 * The value will be one of <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>.
	 * 
	 * @return the alignment
	 * 
	 * @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>
	 */
	public int getAlignment() {
		checkWidget();
		return this.alignment;
	}

	/**
	 * Returns a rectangle describing the receiver's size and location relative to its parent 
	 * (or its display if its parent is null), unless the receiver is a shell. 
	 * In this case, the location is relative to the display.
	 * 
	 * @return the receiver's bounding rectangle
	 * 
	 * @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>
	 */
	public Rectangle getBounds() {
		checkWidget();
		return this.bounds;
	}

	/**
	 * Gets the disabled image.
	 *
	 * @return the image displayed when the button is disabled
	 * @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>
	 */
	public Image getDisabledImage() {
		checkWidget();
		return this.disabledImage;
	}

	/**
	 * Returns <code>true</code> if the receiver is enabled, and
	 * <code>false</code> otherwise. A disabled control is typically not
	 * selectable from the user interface and draws with an inactive or "grayed"
	 * look.
	 *
	 * @return the receiver's enabled state
	 * @see #isEnabled
	 * @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>
	 */
	public boolean getEnabled() {
		checkWidget();
		return this.enabled;
	}

	/**
	 * Returns the whole height of the item.
	 * 
	 * @return the receiver's height
	 * 
	 * @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>
	 */
	public int getHeight() {
		checkWidget();
		if (this.height == -1) {
			return computeDefaultSize().y;
		}
		return this.height;
	}

	/**
	 * Compute default size.
	 *
	 * @return the default size of the item
	 */
	private Point computeDefaultSize() {
		final Point sizeOfTextAndImages = computeSizeOfTextAndImages();
		return new Point(2 * MARGIN + sizeOfTextAndImages.x, 2 * MARGIN + sizeOfTextAndImages.y);
	}

	/**
	 * Returns the receiver's parent, which must be a <code>Breadcrumb</code>.
	 * 
	 * @return the receiver's parent
	 * 
	 * @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>
	 */
	public Breadcrumb getParent() {
		checkWidget();
		return this.parentBreadcrumb;
	}

	/**
	 * Returns <code>true</code> if the receiver is selected, and false otherwise.
	 * 
	 * @return the selection state
	 * 
	 * @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>
	 */
	public boolean getSelection() {
		checkWidget();
		return this.selection;
	}

	/**
	 * Gets the selection image.
	 *
	 * @return the image displayed when the button is selected
	 * @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>
	 */
	public Image getSelectionImage() {
		checkWidget();
		return this.selectionImage;
	}

	/**
	 * Returns the color of the text when the button is enabled and not selected.
	 * 
	 * @return the receiver's text color
	 * 
	 * @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>
	 */
	public Color getTextColor() {
		checkWidget();
		return this.textColor;
	}

	/**
	 * Returns the color of the text when the button is not selected.
	 * 
	 * @return the receiver's text color
	 * 
	 * @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>
	 */

	public Color getTextColorSelected() {
		checkWidget();
		return this.textColorSelected;
	}

	/**
	 * Returns the receiver's tool tip text, or null if it has not been set.
	 * 
	 * @return the receiver's tool tip text
	 * 
	 * @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>
	 */
	public String getTooltipText() {
		checkWidget();
		return this.tooltipText;
	}

	/**
	 * Returns the whole width of the item.
	 * 
	 * @return the receiver's height
	 * 
	 * @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>
	 */
	public int getWidth() {
		checkWidget();
		if (this.width == -1) {
			return Math.max(computeDefaultSize().x, MIN_WIDTH);
		}
		return Math.max(this.width, MIN_WIDTH);
	}

	/**
	 * Returns <code>true</code> if the receiver is enabled, and
	 * <code>false</code> otherwise. A disabled control is typically not
	 * selectable from the user interface and draws with an inactive or "grayed"
	 * look.
	 *
	 * @return the receiver's enabled state
	 * @see #getEnabled
	 * @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>
	 */
	public boolean isEnabled() {
		checkWidget();
		return this.enabled;
	}

	/**
	 * 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
	 * @see SelectionListener
	 * @see #addSelectionListener
	 * @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>
	 */
	public void removeSelectionListener(final SelectionListener listener) {
		checkWidget();
		if (listener == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		this.selectionListeners.remove(listener);
	}

	/**
	 * Controls how text will be displayed in the receiver. The argument should 
	 * be one of <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>.
	 * 
	 * @param alignment the new alignment
	 * 
	 * @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>
	 */
	public void setAlignment(final int alignment) {
		checkWidget();
		this.alignment = alignment;
	}

	/**
	 * Sets the receiver's size and location to the rectangular area specified
	 * by the argument. The <code>x</code> and <code>y</code> fields of the
	 * rectangle are relative to the receiver's parent (or its display if its
	 * parent is null).
	 * <p>
	 * Note: Attempting to set the width or height of the receiver to a negative
	 * number will cause that value to be set to zero instead.
	 * </p>
	 *
	 * @param rectangle the new bounds
	 * @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>
	 */
	public void setBounds(final Rectangle rectangle) {
		checkWidget();
		if (this.bounds == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		this.bounds = new Rectangle(Math.max(0, rectangle.x), //
				Math.max(0, rectangle.y), //
				Math.max(0, rectangle.width), //
				Math.max(0, rectangle.height));
	}

	/**
	 * Sets the receiver's image to the argument when this is one is disabled, which may be null indicating that no image should be displayed.
	 * 
	 * @param image the image to display on the receiver (may be null)
	 * 
	 * @exception IllegalArgumentException <ul>
	 *     <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</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>
	 */
	public void setDisabledImage(final Image image) {
		checkWidget();
		this.disabledImage = image;
	}

	/**
	 * Enables the receiver if the argument is <code>true</code>, and disables it otherwise.
	 * <p>
	 * A disabled control is typically not selectable from the user interface and draws with an inactive or "grayed" look.
	 * </p>
	 * 
	 * @param enabled the new enabled state
	 * 
	 * @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>
	 */
	public void setEnabled(final boolean enabled) {
		checkWidget();
		this.enabled = enabled;
	}

	/**
	 * Sets the height of the receiver.
	 * <p>
	 * Note: Attempting to set the width or height of the receiver to a negative number will cause that value to be set to zero 
	 * instead.
	 * </p>
	 * @param height the new width
	 * 
	 * @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>
	 */
	public void setHeight(final int height) {
		checkWidget();
		this.height = Math.max(height, 0);
	}

	/**
	 * Sets the selection state of the receiver.
	 * 
	 * @param selected the new selection state
	 * 
	 * @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>
	 */
	public void setSelection(final boolean selected) {
		checkWidget();
		this.selection = selected;
	}

	/**
	 * Sets the receiver's image to the argument when this one is selected, which may be 
	 * null indicating that no image should be displayed.
	 * 
	 * @param image the image to display on the receiver (may be null)
	 * 
	 * @exception IllegalArgumentException <ul>
	 *     <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</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>
	 */
	public void setSelectionImage(final Image image) {
		checkWidget();
		this.selectionImage = image;
	}

	/**
	 * Sets the receiver's text color to the argument, which may be null indicating that no image should be displayed.
	 * 
	 * @param textColor the text color to display on the receiver (may be null)
	 * 
	 * @exception IllegalArgumentException <ul>
	 *     <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</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>
	 */
	public void setTextColor(final Color textColor) {
		checkWidget();
		this.textColor = textColor;
	}

	/**
	 * Sets the receiver's text color to the argument when this one is selected, which may be null indicating that no image should be displayed.
	 * 
	 * @param textColor the text color to display on the receiver (may be null)
	 * 
	 * @exception IllegalArgumentException <ul>
	 *     <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</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>
	 */
	public void setTextColorSelected(final Color textColor) {
		checkWidget();
		this.textColorSelected = textColor;
	}

	/**
	 * Sets the receiver's tool tip text to the argument, which may be null indicating 
	 * that the default tool tip for the control will be shown. For a control that has 
	 * a default tool tip, such as the Tree control on Windows, setting the tool tip text to an
	 * empty string replaces the default, causing no tool tip text to be shown.
	 * <p>
	 * The mnemonic indicator (character '&amp;') is not displayed in a tool tip. To display 
	 * a single '&amp;' in the tool tip, the character '&amp;' can be escaped by doubling it in the string.
	 * </p>
	 * 
	 * @param string the new tool tip text (or null)
	 * 
	 * @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>
	 */
	public void setTooltipText(final String string) {
		checkWidget();
		this.tooltipText = (string == null ? "" : string);
	}

	/**
	 * Sets the width of the receiver.
	 * <p>
	 * Note: Attempting to set the width or height of the receiver to a negative number will cause that value to be set to zero 
	 * instead.
	 * </p>
	 * @param width the new width
	 * 
	 * @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>
	 */
	public void setWidth(final int width) {
		checkWidget();
		this.width = Math.max(0, width);
	}

	/**
	 * Fire selection event.
	 */
	// --------------------------------------------- Package visibility section
	void fireSelectionEvent() {
		final Event event = new Event();
		event.widget = this.parentBreadcrumb;
		event.display = getDisplay();
		event.item = this;
		event.type = SWT.Selection;
		for (final SelectionListener selectionListener : this.selectionListeners) {
			selectionListener.widgetSelected(new SelectionEvent(event));
		}
	}

	/**
	 * Draw button at position.
	 *
	 * @param x the x
	 */
	void drawButtonAtPosition(final int x) {

		if (this.selection) {
			drawBackgroundAtPosition(x);
		}
		if (!this.isLastItemOfTheBreadCrumb) {
			drawTrianglesAtPosition(x);
		}

		int xPosition = computeGap();
		final Image drawnedImage = drawImageAtPosition(x + xPosition);
		if (drawnedImage != null) {
			xPosition += drawnedImage.getBounds().width + 2 * MARGIN;
		}
		drawTextAtPosition(x + xPosition);

		this.bounds = new Rectangle(x, 0, getWidth(), this.toolbarHeight);
	}

	/**
	 * Draw background at position.
	 *
	 * @param x the x
	 */
	private void drawBackgroundAtPosition(final int x) {
		this.gc.setAdvanced(true);
		this.gc.setAntialias(SWT.ON);

		this.gc.setForeground(SELECTED_COLOR);
		this.gc.setBackground(SELECTED_COLOR);

		final boolean hasBorder = this.parentBreadcrumb.hasBorder;
		final boolean isFirst = this.parentBreadcrumb.indexOf(this) == 0;
		final int borderWidth = hasBorder ? 1 : 0;

		int leftSide;
		if (isFirst) {
			leftSide = 0;
		} else {
			leftSide = 5 + (hasBorder ? 0 : 1);
		}

		final int xUpperLeft = x + borderWidth + leftSide;
		final int yUpperLeft = borderWidth;
		final int rectWidth = getWidth() - borderWidth - leftSide - (this.isLastItemOfTheBreadCrumb && hasBorder ? 1 : 0);
		final int rectHeight = getHeight() - 2 * borderWidth;
		this.gc.fillRectangle(xUpperLeft, yUpperLeft, rectWidth, rectHeight);

		if (!isFirst) {
			this.gc.fillPolygon(new int[] { xUpperLeft - 5, yUpperLeft, //
					xUpperLeft, yUpperLeft, //
					xUpperLeft, yUpperLeft + this.toolbarHeight / 2 //
			});
			this.gc.fillPolygon(new int[] { xUpperLeft - 5, yUpperLeft + rectHeight, //
					xUpperLeft, yUpperLeft + rectHeight, //
					xUpperLeft, yUpperLeft + this.toolbarHeight / 2 //
			});

		}

		if (!this.isLastItemOfTheBreadCrumb) {
			this.gc.fillPolygon(new int[] { xUpperLeft + rectWidth, yUpperLeft + 1, //
					xUpperLeft + rectWidth, yUpperLeft + getHeight(), //
					xUpperLeft + rectWidth + 5, yUpperLeft + this.toolbarHeight / 2 //
			});
		}

		this.gc.setClipping((Rectangle) null);
	}

	/**
	 * Draw triangles at position.
	 *
	 * @param x the x
	 */
	private void drawTrianglesAtPosition(final int x) {
		this.gc.setForeground(Breadcrumb.BORDER_COLOR);
		drawTriangleAtPosition(x + getWidth());

		this.gc.setAlpha(127);
		this.gc.setForeground(Breadcrumb.BORDER_COLOR_1);
		drawTriangleAtPosition(x + getWidth() + 1);

		this.gc.setForeground(Breadcrumb.BORDER_COLOR_2);
		drawTriangleAtPosition(x + getWidth() + 2);

		this.gc.setForeground(Breadcrumb.BORDER_COLOR_3);
		drawTriangleAtPosition(x + getWidth() + 3);

		this.gc.setAlpha(255);
		if (this.parentBreadcrumb.hasBorder) {
			this.gc.setForeground(Breadcrumb.BORDER_COLOR);
			this.gc.drawLine(x + getWidth(), 0, x + getWidth() + 3, 0);
			this.gc.drawLine(x + getWidth(), this.toolbarHeight - 1, x + getWidth() + 3, this.toolbarHeight - 1);
		}
	}

	/**
	 * Draw triangle at position.
	 *
	 * @param x the x
	 */
	private void drawTriangleAtPosition(final int x) {
		this.gc.drawLine(x, 0, x + 5, this.toolbarHeight / 2);
		this.gc.drawLine(x + 5, this.toolbarHeight / 2, x, this.toolbarHeight);

	}

	/**
	 * Compute gap.
	 *
	 * @return the int
	 */
	private int computeGap() {
		final int widthOfTextAndImage = computeSizeOfTextAndImages().x;
		switch (this.alignment) {
			case SWT.CENTER:
				return (getWidth() - widthOfTextAndImage) / 2;
			case SWT.RIGHT:
				return getWidth() - widthOfTextAndImage - MARGIN;
			default:
				return MARGIN;
		}
	}

	/**
	 * Compute size of text and images.
	 *
	 * @return the point
	 */
	private Point computeSizeOfTextAndImages() {
		int width = 0, height = 0;
		final boolean textISNotEmpty = getText() != null && !getText().equals("");

		if (textISNotEmpty) {
			final GC gc = new GC(this.parentBreadcrumb);
			gc.setFont(this.parentBreadcrumb.getFont());
			final Point extent = gc.stringExtent(getText());
			gc.dispose();
			width += extent.x;
			height = extent.y;
		}

		final Point imageSize = computeMaxWidthAndHeightForImages(getImage(), this.selectionImage, this.disabledImage);

		if (imageSize.x != -1) {
			width += imageSize.x;
			height = Math.max(imageSize.y, height);
			if (textISNotEmpty) {
				width += MARGIN * 2;
			}
		}
		width += MARGIN;
		return new Point(width, height);
	}

	/**
	 * Compute max width and height for images.
	 *
	 * @param images the images
	 * @return the point
	 */
	private Point computeMaxWidthAndHeightForImages(final Image... images) {
		final Point imageSize = new Point(-1, -1);
		for (final Image image : images) {
			if (image == null) {
				continue;
			}
			final Rectangle imageBounds = image.getBounds();
			imageSize.x = Math.max(imageBounds.width, imageSize.x);
			imageSize.y = Math.max(imageBounds.height, imageSize.y);
		}
		return imageSize;
	}

	/**
	 * Draw image at position.
	 *
	 * @param xPosition the x position
	 * @return the image
	 */
	private Image drawImageAtPosition(final int xPosition) {
		Image image;
		if (!isEnabled()) {
			image = this.disabledImage;
		} else if (this.selection) {
			image = this.selectionImage;
		} else {
			image = getImage();
		}

		if (image == null) {
			return null;
		}

		final int yPosition = (this.toolbarHeight - image.getBounds().height) / 2;
		this.gc.drawImage(image, (int) (xPosition + MARGIN * 1.5), yPosition);
		return image;
	}

	/**
	 * Draw text at position.
	 *
	 * @param xPosition the x position
	 */
	private void drawTextAtPosition(final int xPosition) {
		this.gc.setFont(this.parentBreadcrumb.getFont());
		if (this.selection) {
			this.gc.setForeground(this.textColorSelected);
		} else {
			this.gc.setForeground(this.textColor);
		}

		final Point textSize = this.gc.stringExtent(getText());
		final int yPosition = (this.toolbarHeight - textSize.y) / 2;

		int padding;
		if (this.parentBreadcrumb.indexOf(this) == 0 || this.isLastItemOfTheBreadCrumb) {
			padding = 0;
		} else {
			padding = 5;
		}
		this.gc.drawText(getText(), xPosition + padding, yPosition, true);
	}

	/**
	 * Sets the gc.
	 *
	 * @param gc the gc
	 * @return the breadcrumb item
	 */
	BreadcrumbItem setGc(final GC gc) {
		this.gc = gc;
		return this;
	}

	/**
	 * Sets the toolbar height.
	 *
	 * @param toolbarHeight the toolbar height
	 * @return the breadcrumb item
	 */
	BreadcrumbItem setToolbarHeight(final int toolbarHeight) {
		this.toolbarHeight = toolbarHeight;
		return this;
	}

	/**
	 * Sets the is last item of the bread crumb.
	 *
	 * @param isLastItemOfTheBreadCrumb the is last item of the bread crumb
	 * @return the breadcrumb item
	 */
	BreadcrumbItem setIsLastItemOfTheBreadCrumb(final boolean isLastItemOfTheBreadCrumb) {
		this.isLastItemOfTheBreadCrumb = isLastItemOfTheBreadCrumb;
		return this;
	}

}

Back to the top