forked from eclipse/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTree.java
1775 lines (1578 loc) · 46.1 KB
/
CTree.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
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
/****************************************************************************
* Copyright (c) 2005-2006 Jeremy Dowdall
*
* 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:
* Jeremy Dowdall <jeremyd@aspencloud.com> - initial API and implementation
*****************************************************************************/
package org.eclipse.nebula.widgets.ctree;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.TreeListener;
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.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Widget;
/**
* <p>
* NOTE: THIS WIDGET AND ITS API ARE STILL UNDER DEVELOPMENT. THIS IS A PRE-RELEASE ALPHA
* VERSION. USERS SHOULD EXPECT API CHANGES IN FUTURE VERSIONS.
* </p>
*/
public class CTree extends Composite implements Listener {
/**
* true if the platform is detected as being "carbon"
*/
public static final boolean carbon = "carbon".equals(SWT.getPlatform());
/**
* true if the platform is detected as being "gtk"
*/
public static final boolean gtk = "gtk".equals(SWT.getPlatform());
/**
* true if the platform is detected as being "win32"
*/
public static final boolean win32 = "win32".equals(SWT.getPlatform());
private static int checkStyle(int style) {
int mask = SWT.BORDER | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT
| SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE | SWT.MULTI
| SWT.NO_FOCUS | SWT.CHECK;
return (style & mask);
}
GC internalGC = new GC(Display.getDefault());
/**
* A list of items that will actually be painted to the screen.<br>
* Subclasses may override the order as the order of paintedItems represents
* the final drawing order of the items - the last item in this list is
* drawn to the screen last and is, therefore, drawn on top of any
* overlapping items.
*/
protected List paintedItems = new ArrayList();
/**
* A list of the selected items. Only items which are in the visibleItems
* list can be in this list.
*/
protected List selection = new ArrayList();
/**
* The first item selected with the SHIFT or CTRL key pressed. Used to
* create the selection array when selecting multiple items.
*/
protected CTreeItem shiftSel;
/**
* Signifies whether or not the items of this container can be selected.
*/
protected boolean selectable = true;
/**
* Signifies whether or not the items of this container are selected when
* their toggle is clicked on, as opposed to their body.
*/
protected boolean selectOnToggle = true;
Class[] cellClasses = null;
// private Point mmPoint = null; // mouse move point
// private Point mdPoint = null; // mouse down point
// private Point muPoint = null; // mouse up point
// private Point mmDelta = null; // mouse move delta
protected int style = 0;
protected Canvas body;
protected Composite header;
protected Table internalTable;
CTreeColumn[] columns = new CTreeColumn[0];
int[] columnOrder = new int[0];
boolean fillerColumnSet = false;
boolean nativeHeader = true;
CTreeColumn sortColumn;
int sortDirection = -1;
protected ScrollBar hBar;
protected ScrollBar vBar;
protected boolean hasFocus = false;
protected String emptyMessage = "";
protected boolean linesVisible = false;
protected boolean lastLine = false;
protected boolean vLines = true;
protected boolean hLines = true;
protected SColors colors;
// public int marginWidth = gtk ? 0 : 1;
// public int marginHeight = gtk ? 0 : 0;
private Listener filter;
private List paintedItemListeners;
protected boolean nativeGrid = true;
protected CTreeLayout layout;
public boolean drawViewportNorth = false;
public boolean drawViewportEast = false;
public boolean drawViewportSouth = false;
public boolean drawViewportWest = false;
// public void addListener(int eventType, Listener listener) {
// switch(eventType) {
// case SWT.MouseDoubleClick:
// case SWT.MouseDown:
// case SWT.MouseEnter:
// case SWT.MouseExit:
// case SWT.MouseHover:
// case SWT.MouseMove:
// case SWT.MouseUp:
// case SWT.MouseWheel:
// case SWT.KeyDown:
// case SWT.KeyUp:
// container.addListener(eventType, listener);
// break;
// default:
// super.addListener(eventType, listener);
// break;
// }
// }
//
// public void addMouseListener(MouseListener listener) {
// container.addMouseListener(listener);
// }
//
// public void addMouseMoveListener(MouseMoveListener listener) {
// container.addMouseMoveListener(listener);
// }
//
// public void addMouseTrackListener(MouseTrackListener listener) {
// container.addMouseTrackListener(listener);
// }
public boolean paintGridAsBackground = false;
List addedItems = new ArrayList();
List removedItems = new ArrayList();
int topOld = -1;
int heightOld = -1;
boolean updatePaintedList = false;
private int checkColumn = -1;
private boolean checkRoots = true;
private int treeColumn = 0;
private int treeIndent = 16;
private boolean selectOnTreeToggle = false;
List itemList = new ArrayList();
private List visibleItems = null;
public CTree(Composite parent, int style) {
super(parent, checkStyle(style));
this.style = style;
colors = new SColors(getDisplay());
updateColors();
// setBackground(getColors().getTableBackground());
setBackground(getDisplay().getSystemColor(SWT.COLOR_RED));
hBar = getHorizontalBar();
if (hBar != null) {
hBar.setVisible(false);
hBar.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (internalTable != null) {
if (win32) { // pogramatic scrolling doesn't work on
// win32
int sel = hBar.getSelection();
Rectangle hBounds = internalTable.getBounds();
hBounds.x = -sel;
hBounds.width += sel;
internalTable.setBounds(hBounds);
} else {
internalTable.getHorizontalBar().setSelection(
hBar.getSelection());
}
}
if(gtk) {
body.setLocation(-hBar.getSelection(), body.getLocation().y);
}
body.redraw();
}
});
}
vBar = getVerticalBar();
if (vBar != null) {
vBar.setVisible(false);
vBar.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(gtk) {
body.setLocation(body.getLocation().x, layout.headerSize.y-vBar.getSelection());
}
body.redraw();
}
});
}
body = new Canvas(this, SWT.BORDER | SWT.NO_BACKGROUND);
body.setBackground(colors.getTableBackground());
nativeHeader = false;
new CTreeColumn(this, 0);
fillerColumnSet = true;
nativeHeader = true;
filter = new Listener() {
public void handleEvent(Event event) {
if(CTree.this.getShell() == ((Control) event.widget).getShell()) {
handleFocus(SWT.FocusOut);
}
}
};
body.addKeyListener(new KeyAdapter() {}); // traverse does not work without this...
body.addListener(SWT.FocusIn, this);
body.addListener(SWT.MouseDown, this);
body.addListener(SWT.MouseDoubleClick, this);
body.addListener(SWT.MouseMove, this);
body.addListener(SWT.MouseUp, this);
body.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event e) {
if (SWT.Paint == e.type) {
paintBody(e);
}
}
});
body.addListener(SWT.Traverse, this);
addListener(SWT.Dispose, new Listener() {
public void handleEvent(Event event) {
getDisplay().removeFilter(SWT.FocusIn, filter);
removeAll();
if (internalTable != null && !internalTable.isDisposed()) {
internalTable.dispose();
}
if(internalGC != null && internalGC.isDisposed()) {
internalGC.dispose();
}
}
});
if((style & SWT.CHECK) != 0) checkColumn = 0;
setLayout(layout = new CTreeLayout(this));
}
void addColumn(CTreeColumn column, int style) {
if(fillerColumnSet) {
fillerColumnSet = false;
columns[0].dispose();
}
CTreeColumn[] newColumns = new CTreeColumn[columns.length+1];
int[] newColumnOrder = new int[columnOrder.length+1];
System.arraycopy(columns, 0, newColumns, 0, columns.length);
System.arraycopy(columnOrder, 0, newColumnOrder, 0, columnOrder.length);
columns = newColumns;
columnOrder = newColumnOrder;
columns[columns.length-1] = column;
columnOrder[columnOrder.length-1] = columnOrder.length-1;
}
void addItem(CTreeItem item) {
addItem(-1, item);
}
void addItem(int index, CTreeItem item) {
if(index < 0 || index > itemList.size()-1) {
itemList.add(item);
} else {
itemList.add(index, item);
}
addedItems.add(item);
visibleItems = null;
body.redraw();
}
void addItems() {
addedItems = new ArrayList();
layout(true, true);
updatePaintedList = true;
}
/**
* Adds the listener to the collection of listeners who will be notified
* when the Paint Status of an item changes.
* <p>
* An item may be considered visible, and will be returned with
* {@link CTree#getVisibleItems()}, even though it will not be
* painted on the screen. Paint status, on the other hand, refers to whether
* or not an item will actually be painted when the
* {@link CTree#paintBody(Event)} method is called.
* </p>
* <p>
* The Event that is passed to this listener will have the item, whose Paint
* Status has changed, set as Event.item. The actual Paint Status is
* dertermined through the value of the Event's detail field: values > 0
* mean the item will be painted, while values < 0 mean the item will not be
* painted.
* </p>
*
* @param listener
* the listener which should be notified
* @see #removePaintedItemListener
* @see #getPaintedItems()
*/
public void addPaintedItemListener(Listener listener) {
if (paintedItemListeners == null) {
paintedItemListeners = new ArrayList();
}
if (!paintedItemListeners.contains(listener)) {
paintedItemListeners.add(listener);
}
}
/**
* An item may be considered visible, and will be returned with
* {@link CTree#getVisibleItems()}, even though it will not be
* painted on the screen. Paint status, on the other hand, refers to whether
* or not an item will actually be painted when the
* {@link CTree#paintBody(Event)} method is called.
*
* @return an array of items that will be painted to the screen during paint
* events
* @see #getVisibleItems()
*/
// public CTreeItem[] getPaintedItems() {
// return (CTreeItem[]) paintedItems.toArray(new
// CTreeItem[paintedItems.size()]);
// }
public void addSelectionListener(SelectionListener listener) {
checkWidget();
if (listener != null) {
addTypedListener(listener, SWT.Selection, SWT.DefaultSelection);
}
}
public void addTreeListener(TreeListener listener) {
checkWidget ();
if(listener != null) {
addTypedListener(listener, SWT.Collapse, SWT.Expand);
}
}
public void clear(int index, boolean all) {
// TODO Auto-generated method stub
}
public void clearAll(boolean all) {
// TODO Auto-generated method stub
}
public void deselectAll() {
selection = new ArrayList();
finishSelection();
}
private void finishSelection() {
if(!isDisposed()) {
for(Iterator i = selection.iterator(); i.hasNext(); ) {
((CTreeItem) i.next()).setSelected(true);
}
showSelection();
body.redraw();
fireSelectionEvent(false);
}
}
void firePaintedItemEvent(CTreeItem item, boolean isPainted) {
if (paintedItemListeners != null) {
Event event = new Event();
event.detail = isPainted ? 1 : -1;
event.item = item;
Listener[] la = (Listener[]) paintedItemListeners
.toArray(new Listener[paintedItemListeners.size()]);
for (int i = 0; i < la.length; i++) {
la[i].handleEvent(event);
}
}
}
protected void fireSelectionEvent(boolean defaultSelection) {
Event event = new Event();
event.type = defaultSelection ? SWT.DefaultSelection : SWT.Selection;
if (selection.size() == 1)
event.item = (CTreeItem) selection.get(0);
notifyListeners(event.type, event);
}
private void fireTreeEvent(Widget item, boolean collapse) {
Event event = new Event();
event.type = collapse ? SWT.Collapse : SWT.Expand;
event.item = item;
notifyListeners(event.type, event);
}
Composite getBody() {
return body;
}
public int getCheckColumn() {
return checkColumn;
}
public boolean getCheckRoots() {
return checkRoots;
}
public SColors getColors() {
return colors;
}
public int getColumnCount() {
return columns.length;
}
public int[] getColumnOrder() {
int[] order = new int[columnOrder.length];
System.arraycopy(columnOrder, 0, order, 0, columnOrder.length);
return order;
}
int[] getColumnWidths() {
int[] widths = new int[columns.length];
for(int i = 0; i < widths.length; i++) {
widths[i] = columns[i].getWidth();
}
return widths;
}
protected Rectangle getContentArea() {
Rectangle area = getClientArea();
area.y = layout.headerSize.y;
area.height -= layout.headerSize.y;
return area;
}
/**
* The Empty Message is the text message that will be displayed when there
* are no Items to be displayed (the CTable is empty).
*
* @return a String representing the Empty Message. Guaranteed to NOT be
* null.
* @see org.aspencloud.widgets.ccontainer#setEmptyMessage(java.lang.String)
*/
public String getEmptyMessage() {
return emptyMessage;
}
public int getGridLineWidth() {
return (internalTable != null) ? internalTable.getGridLineWidth() : 0;
}
Composite getHeader() {
if (header == null) {
header = new Composite(this, SWT.NONE);
body.moveBelow(header);
}
return header;
}
// public int indexOf(CTreeItem item) {
// return items.indexOf(item);
// }
// public int indexOf(CTreeColumn column) {
// return (internalTable != null) ? internalTable.indexOf(column) : -1;
// }
// public boolean isDirty(int opcode) {
// return ((dirtyFlags & opcode) != 0);
// }
public int getHeaderHeight() {
if(nativeHeader) {
return internalTable.getHeaderHeight();
}
// TODO: getHeaderHeight... get from the first non-native column?
return 0;
}
// public boolean isOperation(int opcode) {
// return operation == opcode;
// }
// public boolean isSelected(int index) {
// return getItem(index).isSelected();
// }
public boolean getHeaderVisible() {
if(nativeHeader) {
return internalTable.getHeaderVisible();
}
return false;
}
Table getInternalTable() {
if(internalTable == null) {
internalTable = new Table(getHeader(), SWT.NONE);
internalTable.setLinesVisible(linesVisible);
}
return internalTable;
}
public CTreeItem getItem(int index) {
if(isEmpty() || index < 0 || index > itemList.size()-1) return null;
return (CTreeItem) itemList.get(index);
}
public int getItemCount() {
return itemList.size();
}
public int getItemHeight() {
return ((CTreeLayout) layout).getItemHeight();
}
/**
* Returns a (possibly empty) array of items contained in the
* receiver that are direct item children of the receiver. These
* are the roots of the tree.
* <p>
* Note: This is not the actual structure used by the receiver
* to maintain its list of items, so modifying the array will
* not affect the receiver.
* </p>
*
* @return the items
*
* @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 CTreeItem[] getItems() {
if(isEmpty()) return new CTreeItem[0];
return (CTreeItem[]) itemList.toArray(new CTreeItem[itemList.size()]);
}
/**
* returns a deep list of items belonging to the given item
* {@inheritDoc}
*/
// List getItems(CTreeItem item) {
// return getItems(item, true);
// }
List getItems(CTreeItem item, boolean all) {
List l = new ArrayList();
CTreeItem[] items = item.getItems();
for(int i = 0; i < items.length; i++) {
l.add(items[i]);
if(all || items[i].getExpanded()) {
l.addAll(getItems(items[i], all));
}
}
return l;
}
/**
* Get a list of items within, or touching, the given rectangle.
*
* @param rect
* @return
*/
protected CTreeItem[] getItems(Rectangle rect) {
if(isEmpty()) {
return new CTreeItem[0];
} else {
List il = new ArrayList();
for(Iterator i = paintedItems.iterator(); i.hasNext(); ) {
CTreeItem item = (CTreeItem) i.next();
Rectangle[] ra = item.getCellBounds();
for (int j = 0; j < ra.length; j++) {
if (ra[j].intersects(rect)) {
il.add(item);
break;
}
}
}
return (il.isEmpty()) ? new CTreeItem[0] : (CTreeItem[]) il.toArray(new CTreeItem[il.size()]);
}
}
public boolean getLastLineVisible() {
return lastLine;
}
public boolean getLinesVisible() {
return linesVisible;
}
public boolean getNativeHeader() {
return nativeHeader;
}
protected List getPaintedItems() {
int top = getScrollPosition().y;
int bot = top + body.getClientArea().height;
int itop = 0;
int ibot = 0;
List list = new ArrayList();
boolean painting = false;
for(Iterator i = items(false).iterator(); i.hasNext(); ) {
CTreeItem item = (CTreeItem) i.next();
Rectangle r = item.getBounds();
ibot = r.y+r.height;
if(itop <= top && top < ibot) {
painting = true;
}
if(painting) {
list.add(item);
}
if(itop < bot && bot <= ibot) {
break;
}
itop = r.y+r.height;
}
return list;
}
public CTreeItem getParentItem() {
// TODO Auto-generated method stub
return null;
}
Point getScrollPosition() {
return new Point(
(hBar == null || hBar.isDisposed()) ? 0 : hBar.getSelection(),
(vBar == null || vBar.isDisposed()) ? 0 : vBar.getSelection());
}
public CTreeItem[] getSelection() {
return selection.isEmpty() ?
new CTreeItem[0] :
(CTreeItem[]) selection.toArray(new CTreeItem[selection.size()]);
}
public int getSelectionCount() {
return selection.size();
}
/**
* @see CTree#setSelectOnToggle(boolean)
* @return
*/
public boolean getSelectOnToggle() {
return selectOnToggle;
}
/**
* @see CTree#setSelectOnTreeToggle(boolean)
* @return true if selection state is to changed when a toggle is clicked, false otherwise
*/
public boolean getSelectOnTreeToggle() {
return selectOnTreeToggle;
}
// public void redraw(CTreeItem item) {
// // TODO: paint / redraw individual item
// // Rectangle r = item.getBounds();
// // body.redraw(r.x-1, r.y-1, r.width+2, r.height+2, true);
// redraw();
// }
public int getSortDirection() {
return sortDirection;
}
public int getStyle() {
return style;
}
public CTreeItem getTopItem() {
int top = getScrollPosition().y;
int itop = 0;
int ibot = 0;
for(Iterator i = items(false).iterator(); i.hasNext(); ) {
CTreeItem item = (CTreeItem) i.next();
Rectangle r = item.getBounds();
ibot = r.y+r.height;
if(itop <= top && top < ibot) {
return item;
}
itop = r.y+r.height;
}
return null;
}
// public void removeListener(int eventType, Listener listener) {
// switch(eventType) {
// case SWT.MouseDoubleClick:
// case SWT.MouseDown:
// case SWT.MouseEnter:
// case SWT.MouseExit:
// case SWT.MouseHover:
// case SWT.MouseMove:
// case SWT.MouseUp:
// case SWT.MouseWheel:
// case SWT.KeyDown:
// case SWT.KeyUp:
// container.removeListener(eventType, listener);
// break;
// default:
// super.removeListener(eventType, listener);
// break;
// }
// }
//
// public void removeMouseListener(MouseListener listener) {
// container.removeMouseListener(listener);
// }
//
// public void removeMouseMoveListener(MouseMoveListener listener) {
// container.removeMouseMoveListener(listener);
// }
//
// public void removeMouseTrackListener(MouseTrackListener listener) {
// container.removeMouseTrackListener(listener);
// }
public int getTreeColumn() {
return treeColumn;
}
public int getTreeIndent() {
return treeIndent;
}
public void handleEvent(Event event) {
switch (event.type) {
case SWT.FocusIn:
case SWT.FocusOut:
handleFocus(event.type);
break;
case SWT.MouseDoubleClick:
case SWT.MouseDown:
case SWT.MouseMove:
case SWT.MouseUp:
handleMouseEvents(event);
break;
case SWT.Traverse:
handleTraverse(event);
break;
}
}
private void handleFocus(int type) {
if (isDisposed())
return;
switch (type) {
case SWT.FocusIn: {
if (hasFocus)
return;
hasFocus = true;
updateFocus();
Display display = getDisplay();
display.removeFilter(SWT.FocusIn, filter);
display.addFilter(SWT.FocusIn, filter);
Event e = new Event();
notifyListeners(SWT.FocusIn, e);
break;
}
case SWT.FocusOut: {
if (!hasFocus)
return;
Control focusControl = getDisplay().getFocusControl();
if (focusControl == this)
return;
for(Iterator i = items(false).iterator(); i.hasNext(); ) {
if(((CTreeItem) i.next()).contains(focusControl)) return;
}
hasFocus = false;
updateFocus();
Display display = getDisplay();
display.removeFilter(SWT.FocusIn, filter);
Event e = new Event();
notifyListeners(SWT.FocusOut, e);
}
}
}
protected void handleMouseEvents(Event event) {
CTreeItem item = null;
if (event.widget == body) {
item = getItem(event.x, event.y);
} else if (event.item instanceof CTreeItem) {
item = (CTreeItem) event.item;
}
switch (event.type) {
case SWT.MouseDoubleClick:
if(item != null && (selectOnToggle || !item.isToggle(event.x, event.y))) {
fireSelectionEvent(true);
}
break;
case SWT.MouseDown:
if(!hasFocus) setFocus();
if(item == null) {
if(event.widget == body) {
item = getItem(event.x, event.y);
} else if(event.item instanceof CTreeItem) {
item = (CTreeItem) event.item;
}
}
switch(event.button) {
// TODO - popup menu: not just for mouse down events!
case 3:
Menu menu = getMenu();
if ((menu != null) && ((menu.getStyle() & SWT.POP_UP) != 0)) {
menu.setVisible(true);
}
case 1:
if(selectOnToggle || !item.isToggle(event.x, event.y)) {
if(selectOnTreeToggle || !item.isTreeToggle(event.x,event.y)) {
if((event.stateMask & SWT.SHIFT) != 0) {
if(shiftSel == null) {
if(selection.isEmpty()) selection.add(item);
shiftSel = (CTreeItem) selection.get(selection.size() - 1);
}
setSelection(shiftSel, item);
} else if((event.stateMask & SWT.CONTROL) != 0) {
toggleSelection(item);
shiftSel = null;
} else {
setSelection(item);
shiftSel = null;
}
}
}
break;
}
break;
case SWT.MouseMove:
// TODO: make toggles more dynamic
break;
case SWT.MouseUp:
if(item.isToggle(event.x,event.y)) {
boolean open = item.isOpen(event.x,event.y);
if(item.isTreeToggle(event.x,event.y)) {
// visibleItems = null;
item.setExpanded(!open);
fireTreeEvent(item, !open);
} else {
item.getCell(event.x, event.y).setOpen(!open);
}
}
break;
}
}
private void handleTraverse(Event event) {
switch (event.detail) {
case SWT.TRAVERSE_RETURN:
if (event.data instanceof CTreeCell) {
fireSelectionEvent(true);
}
break;
case SWT.TRAVERSE_ARROW_NEXT:
if(!selection.isEmpty()) {
setSelection(((CTreeItem) selection.get(selection.size()-1)).nextVisible());
}
break;
case SWT.TRAVERSE_ARROW_PREVIOUS:
if(!selection.isEmpty()) {
setSelection(((CTreeItem) selection.get(0)).previousVisible());
}
break;
}
}
/**
* Convenience method indicating whether or not the treeColumn is set to an
* actual column, and thus the tree hierarchy will be displayed.
* <p>Note that if the hierarchy is not displayed, then certain methods are
* able to be optimized and will take advantage of this fact</p>
* @return true if treeColumn is set to an existing column, false otherwise
*/
public boolean hasTreeColumn() {
return treeColumn >= 0 && treeColumn < getColumnCount();
}
public int indexOf(CTreeColumn column) {
return indexOf(column);
}
public int indexOf(CTreeItem item) {
return itemList.indexOf(item);
}
CTreeColumn getColumn(int index) {
if(index >= 0 && index < columns.length) {
return columns[index];
}
return null;
}
CTreeColumn[] getColumns() {
CTreeColumn[] ca = new CTreeColumn[columns.length];
System.arraycopy(columns, 0, ca, 0, columns.length);
return ca;
}
CTreeItem getItem(int x, int y) {
// must iterate in reverse drawing order in case items overlap each other
for(ListIterator i = paintedItems.listIterator(paintedItems.size()); i.hasPrevious();) {
CTreeItem item = (CTreeItem) i.previous();
if(item.contains(x,y)) return item;
}
return null;
}
/**
* returns the sort column of this container as an CTreeColumn.
* subclasses should override to provide an appropriate cast.
* @return the sort column
*/
protected CTreeColumn getSortColumn() {
return sortColumn;
}
public boolean isEmpty() {
return itemList.isEmpty();
}
/**
* Use this method to find out if an item is visible, and thus has the potential to be
* painted.
* <p>An Item will be visible when every parent between it and the root of the tree
* is expanded</p>
* @param item the item in question
* @return true if the item can be painted to the screen, false if it is hidden due to a
* parent item being collapsed
*/
public boolean isVisible(CTreeItem item) {
if(item.getVisible()) {
CTreeItem parentItem = item.getParentItem();
if(parentItem == null) return true;
if(parentItem.getExpanded()) return isVisible(parentItem);
}
return false;
}
List items(boolean all) {
if(visibleItems == null) {
visibleItems = new ArrayList();