[bug 400423] Clear selection after deleting a destination
diff --git a/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationConfiguratorsPresenter.java b/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationConfiguratorsPresenter.java
index 0d06d14..e574b47 100644
--- a/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationConfiguratorsPresenter.java
+++ b/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationConfiguratorsPresenter.java
@@ -68,21 +68,21 @@
 

 	@Override

 	public void selectionChanged(DestinationConfiguratorSelection selection) {

-		ContractChecker.nullCheckParam(selection.destProviderId);

-

-		destinationProviderId = selection.destProviderId;

-		destination = selection.destination;

-		

+	    destination = selection.destination;

+	    destinationProviderId = selection.destProviderId;

+	    if(destination != null) {

+	        ContractChecker.nullCheckField(destinationProviderId, "destinationProviderId");

+	    }

+	    

+		view.setAddEnabled(destinationProviderId != null);

 		if(destination==null) {

-			view.setAddEnabled(true);

 			view.setEditEnabled(false);

 			view.setRemoveEnabled(false);

 			view.setTestEnabled(false);

 		} else{

-			view.setAddEnabled(true);

 			view.setEditEnabled(true);

 			view.setRemoveEnabled(true);

-			view.setTestEnabled(getConfigDescription(destinationProviderId).createConfigurator().getSearchDestinationTester()!=null);

+		    view.setTestEnabled(getConfigDescription(destinationProviderId).createConfigurator().getSearchDestinationTester()!=null);

 		}

 	}

 

@@ -140,5 +140,4 @@
 		IStatus result = tester.test(view.getShell(), (T)destination);

 		view.setStatus(result);

 	}

-

 }

diff --git a/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationsPreferencePage.java b/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationsPreferencePage.java
index 264d061..76149a4 100644
--- a/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationsPreferencePage.java
+++ b/bundles/org.eclipse.platform.discovery.destprefs/src/org/eclipse/platform/discovery/destprefs/internal/prefpage/ui/DestinationsPreferencePage.java
@@ -112,6 +112,7 @@
             @Override
             public void selectionChanged(final SelectionChangedEvent event) {
                 if (event.getSelection().isEmpty()) {
+                    presenter.selectionChanged(new DestinationConfiguratorSelection(null, null));
                     return;
                 }
 
diff --git a/tests/org.eclipse.platform.discovery.destprefs.test.unit/META-INF/MANIFEST.MF b/tests/org.eclipse.platform.discovery.destprefs.test.unit/META-INF/MANIFEST.MF
index 901003b..d08a750 100644
--- a/tests/org.eclipse.platform.discovery.destprefs.test.unit/META-INF/MANIFEST.MF
+++ b/tests/org.eclipse.platform.discovery.destprefs.test.unit/META-INF/MANIFEST.MF
@@ -19,4 +19,5 @@
  org.eclipse.platform.discovery.util,
  org.eclipse.ui,
  org.eclipse.ui.forms,
- org.eclipse.swtbot.eclipse.finder
+ org.eclipse.swtbot.eclipse.finder,
+ org.apache.log4j
diff --git a/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/DestinationConfiguratorsPresenterTest.java b/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/DestinationConfiguratorsPresenterTest.java
index ac06d8a..746bff3 100644
--- a/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/DestinationConfiguratorsPresenterTest.java
+++ b/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/DestinationConfiguratorsPresenterTest.java
@@ -183,7 +183,15 @@
 		Mockito.verify(expectedConfigurator).deleteDestination(Mockito.same(viewShell), Mockito.same(fixture.destination1));

 		Mockito.verify(view).setStatus(Mockito.same(result));

 		Mockito.verify(view, Mockito.times(2)).setInput(Mockito.argThat(matchesFixture()));

-

+	}

+	

+	@Test

+	public void testClearSelection() {

+	    presenter.selectionChanged(new DestinationConfiguratorSelection(null, null));

+        Mockito.verify(view).setAddEnabled(Mockito.eq(false));

+        Mockito.verify(view).setEditEnabled(Mockito.eq(false));

+        Mockito.verify(view).setRemoveEnabled(Mockito.eq(false));

+        Mockito.verify(view).setTestEnabled(Mockito.eq(false));

 	}

 	

 	@Test

diff --git a/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/ui/DestinationsPreferencePageTest.java b/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/ui/DestinationsPreferencePageTest.java
index d9c83dc..67591b9 100644
--- a/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/ui/DestinationsPreferencePageTest.java
+++ b/tests/org.eclipse.platform.discovery.destprefs.test.unit/src/org/eclipse/platform/discovery/destprefs/test/unit/prefpage/ui/DestinationsPreferencePageTest.java
@@ -47,7 +47,7 @@
     private static final String DESTINATION_PROVIDER_ID = "my.dest.provider.id";
     private static final String DESTINATION_NAME = "My Destination";
     private String currentDestinationName;
-    
+
     private DestinationsPreferencePagePageObject pageObject;
     @Mock
     private IDestinationConfiguratorsPresenter presenter;
@@ -65,7 +65,9 @@
         MockitoAnnotations.initMocks(this);
         stubDestinations();
         pageObject = new DestinationsPreferencePagePageObject(presenter);
-        
+
+        pageObject.open();
+        pageObject.setInput(createPageInput());
         Mockito.doAnswer(new Answer<Void>() {
             @Override
             public Void answer(InvocationOnMock invocation) throws Throwable {
@@ -76,9 +78,7 @@
                 return null;
             }
         }).when(presenter).selectionChanged(Mockito.any(DestinationConfiguratorSelection.class));
-        
-        pageObject.open();
-        pageObject.setInput(createPageInput());
+
     }
 
     private void stubDestinations() {
@@ -89,7 +89,8 @@
                 return currentDestinationName;
             }
         });
-        Mockito.stub(destinationProvider.getSearchDestinations()).toReturn(new HashSet<ISearchDestination>(Arrays.asList(new ISearchDestination[]{destination})));
+        Mockito.stub(destinationProvider.getSearchDestinations()).toReturn(
+                new HashSet<ISearchDestination>(Arrays.asList(new ISearchDestination[] { destination })));
         Mockito.stub(destinationProviderDescr.createProvider()).toReturn(destinationProvider);
         Mockito.stub(destinationProviderDescr.getId()).toReturn(DESTINATION_PROVIDER_ID);
         Mockito.stub(category.getId()).toReturn(CATEGORY_ID);
@@ -158,7 +159,7 @@
     public void testSetOkStatus() throws InterruptedException {
         setStatusTest(createStatus(IStatus.OK, "Success"));
     }
-    
+
     @Test
     public void testSelectCategory() {
         pageObject.getCategory(CATEGORY_NAME).select();
@@ -170,24 +171,24 @@
         selectTheDestination();
         Mockito.verify(presenter, Mockito.atLeastOnce()).selectionChanged(Mockito.argThat(selectionMatcher(DESTINATION_PROVIDER_ID, destination)));
     }
-    
+
     @Test
     public void testAddDestination() {
         selectTheDestination();
         Mockito.doAnswer(new Answer<Void>() {
             @Override
             public Void answer(InvocationOnMock invocation) throws Throwable {
-                pageObject.setInput(createPageInput());                
+                pageObject.setInput(createPageInput());
                 return null;
             }
         }).when(presenter).addDestination();
-        
+
         pageObject.addDestination();
         Mockito.verify(presenter).addDestination();
         assertCategoryIsExpanded();
         assertDestinationIsSelected(DESTINATION_NAME);
     }
-    
+
     @Test
     public void testEditDestination() {
         selectTheDestination();
@@ -196,11 +197,11 @@
             @Override
             public Void answer(InvocationOnMock invocation) throws Throwable {
                 currentDestinationName = newDestinationName;
-                pageObject.setInput(createPageInput());                
+                pageObject.setInput(createPageInput());
                 return null;
             }
         }).when(presenter).editDestination();
-        
+
         pageObject.editDestination();
         Mockito.verify(presenter).editDestination();
 
@@ -212,65 +213,80 @@
         final CategoryPageObject category = pageObject.getCategory(CATEGORY_NAME);
         assertTrue("Category does not display destinations after edit", category.isDestinationsVisible());
     }
-    
+
     private void assertDestinationIsSelected(final String destinationName) {
         final DestinationPageObject destination = pageObject.getCategory(CATEGORY_NAME).getDestination(destinationName);
         assertTrue("Destination is not selected", destination.isSelected());
     }
-    
+
     @Test
     public void testRemoveDestination() {
         selectTheDestination();
         Mockito.doAnswer(new Answer<Void>() {
             @Override
             public Void answer(InvocationOnMock invocation) throws Throwable {
-                pageObject.setInput(createPageInput());                
+                pageObject.setInput(createPageInput());
                 return null;
             }
         }).when(presenter).removeDestination();
-        
+
         pageObject.removeDestination();
         Mockito.verify(presenter).removeDestination();
         assertCategoryIsExpanded();
+        Mockito.verify(presenter).selectionChanged(Mockito.argThat(emptySelectionMatcher()));
+        
     }
-    
+
+    private Matcher<DestinationConfiguratorSelection> emptySelectionMatcher() {
+        return new BaseMatcher<DestinationConfiguratorSelection>() {
+            @Override
+            public boolean matches(Object item) {
+                final DestinationConfiguratorSelection selection = (DestinationConfiguratorSelection) item;
+                return selection.destination == null && selection.destProviderId == null;
+            }
+
+            @Override
+            public void describeTo(Description description) {
+            }
+        };
+    }
+
     @Test
     public void testTestDestination() {
         selectTheDestination();
         pageObject.testDestination();
         Mockito.verify(presenter).testDestination();
     }
-    
+
     @Test
     public void testSelectionPreservedOnDestinationEdit() {
         selectTheDestination();
         final String newDestinationName = "ModifiedDestinationName";
-        
+
         Mockito.doAnswer(new Answer<Void>() {
             @Override
             public Void answer(InvocationOnMock invocation) throws Throwable {
                 currentDestinationName = newDestinationName;
-                pageObject.setInput(createPageInput());                
+                pageObject.setInput(createPageInput());
                 return null;
             }
         }).when(presenter).editDestination();
         pageObject.editDestination();
 
-        
         final DestinationPageObject selectedDestination = pageObject.getCategory(CATEGORY_NAME).getDestination(newDestinationName);
         assertTrue("Modified destination lost its selection", selectedDestination.isSelected());
     }
-    
+
     private void selectTheDestination() {
         pageObject.getCategory(CATEGORY_NAME).getDestination(DESTINATION_NAME).select();
     }
-    
+
     private Matcher<DestinationConfiguratorSelection> selectionMatcher(String destinationProviderId, final ISearchDestination destination) {
         return new BaseMatcher<DestinationConfiguratorSelection>() {
             @Override
             public boolean matches(Object item) {
-                final DestinationConfiguratorSelection selection = (DestinationConfiguratorSelection)item;
-                return selection.destProviderId.equals(DESTINATION_PROVIDER_ID) && (selection.destination == destination);
+                final DestinationConfiguratorSelection selection = (DestinationConfiguratorSelection) item;
+                return selection.destProviderId != null && selection.destProviderId.equals(DESTINATION_PROVIDER_ID) && (selection.destination == destination);
             }
 
             @Override