Skip to content

Commit f1881af

Browse files
estepperniraj-modi
authored andcommitted
Bug 553598 - Fire a new EmptinessChanged event from Tree after first addition or last removal of a TreeItem
Change-Id: I8c215c59864ed3b985e1c4b81d7f81b5efb671e0 Signed-off-by: Eike Stepper <[email protected]> Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/185581 Tested-by: Platform Bot <[email protected]> Tested-by: Niraj Modi <[email protected]> Reviewed-by: Sravan Kumar Lakkimsetti <[email protected]> Reviewed-by: Niraj Modi <[email protected]>
1 parent f405e70 commit f1881af

File tree

5 files changed

+114
-3
lines changed

5 files changed

+114
-3
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Tree.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
* <dt><b>Styles:</b></dt>
6363
* <dd>SINGLE, MULTI, CHECK, FULL_SELECTION, VIRTUAL, NO_SCROLL</dd>
6464
* <dt><b>Events:</b></dt>
65-
* <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem</dd>
65+
* <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem, EmptinessChanged</dd>
6666
* </dl>
6767
* <p>
6868
* Note: Only one of the styles SINGLE and MULTI may be specified.
@@ -757,6 +757,11 @@ void createItem (TreeItem item, TreeItem parentItem, int index) {
757757
widget.expandItem (parentItem.handle);
758758
}
759759
ignoreExpand = false;
760+
if (parentItem == null && this.itemCount == 1) {
761+
Event event = new Event ();
762+
event.detail = 0;
763+
sendEvent (SWT.EmptinessChanged, event);
764+
}
760765
}
761766

762767
@Override
@@ -988,6 +993,11 @@ void destroyItem (TreeItem item) {
988993
setScrollWidth ();
989994
if (this.itemCount == 0) imageBounds = null;
990995
if (insertItem == item) insertItem = null;
996+
if (parentItem == null && this.itemCount == 0) {
997+
Event event = new Event ();
998+
event.detail = 1;
999+
sendEvent (SWT.EmptinessChanged, event);
1000+
}
9911001
}
9921002

9931003
@Override

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,20 @@ public class SWT {
10201020
*/
10211021
public static final int ZoomChanged = 55;
10221022

1023+
/**
1024+
* The SWT emptiness change event type (value is 56).
1025+
*
1026+
* <p>
1027+
* This event is sent on <code>Tree</code> when the first <code>TreeItem</code> was
1028+
* added to it (with the <code>detail</code> field set to 0) or the last
1029+
* <code>TreeItem</code> was removed from it (with the <code>detail</code> field
1030+
* set to 1).
1031+
* </p>
1032+
*
1033+
* @since 4.22
1034+
*/
1035+
public static final int EmptinessChanged = 56;
1036+
10231037
/* Event Details */
10241038

10251039
/**

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Tree.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
* <dt><b>Styles:</b></dt>
6868
* <dd>SINGLE, MULTI, CHECK, FULL_SELECTION, VIRTUAL, NO_SCROLL</dd>
6969
* <dt><b>Events:</b></dt>
70-
* <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem</dd>
70+
* <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem, EmptinessChanged</dd>
7171
* </dl>
7272
* <p>
7373
* Note: Only one of the styles SINGLE and MULTI may be specified.
@@ -1024,6 +1024,18 @@ void createItem (TreeItem item, long parentIter, int index) {
10241024
int id = getId (item.handle, false);
10251025
items [id] = item;
10261026
modelChanged = true;
1027+
1028+
if (parentIter == 0 ) {
1029+
/*
1030+
If this was the first root item fire an EmptinessChanged event.
1031+
*/
1032+
int roots = GTK.gtk_tree_model_iter_n_children (modelHandle, 0);
1033+
if (roots == 1) {
1034+
Event event = new Event ();
1035+
event.detail = 0;
1036+
sendEvent (SWT.EmptinessChanged, event);
1037+
}
1038+
}
10271039
}
10281040

10291041
void createRenderers (long columnHandle, int modelIndex, boolean check, int columnStyle) {
@@ -1277,6 +1289,16 @@ void destroyItem (TreeItem item) {
12771289
GTK.gtk_tree_store_remove (modelHandle, item.handle);
12781290
OS.g_signal_handlers_unblock_matched (selection, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, CHANGED);
12791291
modelChanged = true;
1292+
1293+
/*
1294+
If this was the last root item fire an EmptinessChanged event.
1295+
*/
1296+
int roots = GTK.gtk_tree_model_iter_n_children (modelHandle, 0);
1297+
if (roots == 0) {
1298+
Event event = new Event ();
1299+
event.detail = 1;
1300+
sendEvent (SWT.EmptinessChanged, event);
1301+
}
12801302
}
12811303

12821304
@Override

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* <dt><b>Styles:</b></dt>
6565
* <dd>SINGLE, MULTI, CHECK, FULL_SELECTION, VIRTUAL, NO_SCROLL</dd>
6666
* <dt><b>Events:</b></dt>
67-
* <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem</dd>
67+
* <dd>Selection, DefaultSelection, Collapse, Expand, SetData, MeasureItem, EraseItem, PaintItem, EmptinessChanged</dd>
6868
* </dl>
6969
* <p>
7070
* Note: Only one of the styles SINGLE and MULTI may be specified.
@@ -2158,6 +2158,14 @@ void createItem (TreeItem item, long hParent, long hInsertAfter, long hItem) {
21582158
Later, setRedraw(true) will update scrollbars once.
21592159
*/
21602160
if (getDrawing ()) updateScrollBar ();
2161+
/*
2162+
If this is the first item added fire an EmptinessChanged event.
2163+
*/
2164+
if (item != null && id == 0) {
2165+
Event event = new Event ();
2166+
event.detail = 0;
2167+
sendEvent (SWT.EmptinessChanged, event);
2168+
}
21612169
}
21622170
}
21632171

@@ -2641,6 +2649,15 @@ void destroyItem (TreeItem item, long hItem) {
26412649
Later, setRedraw(true) will update scrollbars once.
26422650
*/
26432651
if (getDrawing ()) updateScrollBar ();
2652+
2653+
/*
2654+
If this is the last item removed fire an EmptinessChanged event.
2655+
*/
2656+
if (count == 0) {
2657+
Event event = new Event ();
2658+
event.detail = 1;
2659+
sendEvent (SWT.EmptinessChanged, event);
2660+
}
26442661
}
26452662

26462663
@Override

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Tree.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,4 +974,52 @@ public void test_Virtual() {
974974
assertTrue("SetData callback count not in range: " + dataCounter[0],
975975
dataCounter[0] > visibleCount / 2 && dataCounter[0] <= visibleCount * 3);
976976
}
977+
978+
@Test
979+
public void test_emptinessChanged() {
980+
int NOT_EMPTY = 0;
981+
int EMPTY = 1;
982+
int[] count = { 0, 0 };
983+
tree.addListener(SWT.EmptinessChanged, e -> ++count[e.detail] );
984+
985+
// Create first item. Expected one NOT_EMPTY event.
986+
TreeItem item1 = new TreeItem(tree, SWT.NONE);
987+
assertEquals(1, count[NOT_EMPTY]);
988+
assertEquals(0, count[EMPTY]);
989+
990+
// Create second item. Expected no further event.
991+
TreeItem item2 = new TreeItem(tree, SWT.NONE);
992+
assertEquals(1, count[NOT_EMPTY]);
993+
assertEquals(0, count[EMPTY]);
994+
995+
// Remove one item. Expected no further event.
996+
item1.dispose();
997+
assertEquals(1, count[NOT_EMPTY]);
998+
assertEquals(0, count[EMPTY]);
999+
1000+
// Remove last item. Expected one EMPTY event.
1001+
item2.dispose();
1002+
assertEquals(1, count[NOT_EMPTY]);
1003+
assertEquals(1, count[EMPTY]);
1004+
1005+
// Create first item. Expected one more NOT_EMPTY event.
1006+
item1 = new TreeItem(tree, SWT.NONE);
1007+
assertEquals(2, count[NOT_EMPTY]);
1008+
assertEquals(1, count[EMPTY]);
1009+
1010+
// Create second item as child of the first item. Expected no further event.
1011+
item2 = new TreeItem(item1, SWT.NONE);
1012+
assertEquals(2, count[NOT_EMPTY]);
1013+
assertEquals(1, count[EMPTY]);
1014+
1015+
// Remove both items. Expected one more EMPTY event.
1016+
item1.dispose();
1017+
assertEquals(2, count[NOT_EMPTY]);
1018+
assertEquals(2, count[EMPTY]);
1019+
1020+
// Noop. Expected no further event.
1021+
item2.dispose();
1022+
assertEquals(2, count[NOT_EMPTY]);
1023+
assertEquals(2, count[EMPTY]);
1024+
}
9771025
}

0 commit comments

Comments
 (0)