Skip to content

Commit

Permalink
Fix: Error when trying to delete selected tab item
Browse files Browse the repository at this point in the history
The selection is not cleared when a tab item has been deleted. When
updating the widget, we then run into an unhandled exception because the
item we try to select no longer exists.
  • Loading branch information
ptziegler committed Sep 5, 2023
1 parent 96d5cf1 commit d3c010d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2023 Google, Inc.
* 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
Expand Down Expand Up @@ -75,6 +75,7 @@ public void selecting(ObjectInfo object, boolean[] refreshFlag) throws Exception
@Override
public void before(ObjectInfo parent, ObjectInfo child) throws Exception {
if (child == m_this) {
getFolder().m_selectedItem = null;
ControlInfo ourControl = getControl();
if (ourControl != null) {
ourControl.delete();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2023 Google, Inc.
* 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
Expand Down Expand Up @@ -252,6 +252,35 @@ public void test_selecting() throws Exception {
}
}

/**
* Test whether the selection is removed when the tab item is deleted.
*
* @see <a href=
* "https://github.com/eclipse-windowbuilder/windowbuilder/issues/556">here</a>
*/
@Test
public void test_deleteSelectedTabItem() throws Exception {
CompositeInfo shell = parseComposite(
"public class Test extends Shell {",
" public Test() {",
" setLayout(new FillLayout());",
" CTabFolder tabFolder = new CTabFolder(this, SWT.NONE);",
" CTabItem item_1 = new CTabItem(tabFolder, SWT.NONE);",
" CTabItem item_2 = new CTabItem(tabFolder, SWT.NONE);",
" }",
"}");
shell.refresh();
CTabFolderInfo tabFolder = (CTabFolderInfo) shell.getChildrenControls().get(0);
CTabItemInfo item_1 = tabFolder.getItems2().get(0);
CTabItemInfo item_2 = tabFolder.getItems2().get(1);

item_2.doSelect();
assertEquals(tabFolder.getSelectedItem(), item_2);

item_2.delete();
assertEquals(tabFolder.getSelectedItem(), item_1);
}

/**
* We should show on design canvas only {@link ControlInfo}'s of expanded {@link CTabItemInfo}'s.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2023 Google, Inc.
* 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
Expand Down Expand Up @@ -256,6 +256,35 @@ public void test_selecting() throws Exception {
}
}

/**
* Test whether the selection is removed when the tab item is deleted.
*
* @see <a href=
* "https://github.com/eclipse-windowbuilder/windowbuilder/issues/556">here</a>
*/
@Test
public void test_deleteSelectedTabItem() throws Exception {
CompositeInfo shell = parseComposite(
"public class Test extends Shell {",
" public Test() {",
" setLayout(new FillLayout());",
" TabFolder tabFolder = new TabFolder(this, SWT.NONE);",
" TabItem item_1 = new TabItem(tabFolder, SWT.NONE);",
" TabItem item_2 = new TabItem(tabFolder, SWT.NONE);",
" }",
"}");
shell.refresh();
TabFolderInfo tabFolder = (TabFolderInfo) shell.getChildrenControls().get(0);
TabItemInfo item_1 = tabFolder.getItems2().get(0);
TabItemInfo item_2 = tabFolder.getItems2().get(1);

item_2.doSelect();
assertEquals(tabFolder.getSelectedItem(), item_2);

item_2.delete();
assertEquals(tabFolder.getSelectedItem(), item_1);
}

/**
* We should show on design canvas only {@link ControlInfo}'s of expanded {@link TabItemInfo}'s.
*/
Expand Down

0 comments on commit d3c010d

Please sign in to comment.