changeset 1314:0a4e551bef62

Use ReferenceFilter for context actions too review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008694.html reviewed-by:omajid
author Mario Torre <neugens.limasoftware@gmail.com>
date Mon, 11 Nov 2013 11:06:04 +0100
parents 987d175c2d74
children 91c6d3747b46 f9acec89f53f
files client/core/src/main/java/com/redhat/thermostat/client/ui/ReferenceContextAction.java client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandler.java client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandlerTest.java killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java
diffstat 4 files changed, 92 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/ReferenceContextAction.java	Mon Nov 11 11:06:04 2013 +0100
+++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/ReferenceContextAction.java	Mon Nov 11 11:06:04 2013 +0100
@@ -59,5 +59,5 @@
      * The {@link Filter} returned by this method is used to select what
      * {@link Ref} this {@code ReferenceAction} is applicable to.
      */
-    Filter<Ref> getFilter();
+    ReferenceFilter getFilter();
 }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandler.java	Mon Nov 11 11:06:04 2013 +0100
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandler.java	Mon Nov 11 11:06:04 2013 +0100
@@ -44,6 +44,7 @@
 import com.redhat.thermostat.client.swing.components.ThermostatPopupMenu;
 import com.redhat.thermostat.client.swing.internal.osgi.ContextActionServiceTracker;
 import com.redhat.thermostat.client.ui.ReferenceContextAction;
+import com.redhat.thermostat.client.ui.ReferenceFilter;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.ActionListener;
 import com.redhat.thermostat.common.ActionNotifier;
@@ -93,31 +94,30 @@
                 
                 boolean showPopup = false;
                 for (final ReferenceContextAction action : actions) {
-                    
-                    if (!action.getFilter().matches(payload.ref))  {
-                        continue;
+                    ReferenceFilter filter = action.getFilter();
+                    if (filter.applies(payload.ref) && filter.matches(payload.ref))  {
+                     
+                        showPopup = true;
+                        
+                        JMenuItem contextAction = createContextMenuItem();
+                        contextAction.setText(action.getName().getContents());
+                        contextAction.setToolTipText(action.getDescription().getContents());
+                        contextAction.addActionListener(new java.awt.event.ActionListener() {
+                            @Override
+                            public void actionPerformed(java.awt.event.ActionEvent e) {
+                                Payload actionPayload = new Payload();
+                                actionPayload.reference = payload.ref;
+                                actionPayload.action = action;
+                                
+                                notifier.fireAction(ContextHandlerAction.ACTION_PERFORMED, actionPayload);
+                            }
+                        });
+                        
+                        // the component name is for unit tests only
+                        contextAction.setName(action.getName().getContents());
+                        
+                        contextMenu.add(contextAction);
                     }
-                    
-                    showPopup = true;
-                    
-                    JMenuItem contextAction = createContextMenuItem();
-                    contextAction.setText(action.getName().getContents());
-                    contextAction.setToolTipText(action.getDescription().getContents());
-                    contextAction.addActionListener(new java.awt.event.ActionListener() {
-                        @Override
-                        public void actionPerformed(java.awt.event.ActionEvent e) {
-                            Payload actionPayload = new Payload();
-                            actionPayload.reference = payload.ref;
-                            actionPayload.action = action;
-                            
-                            notifier.fireAction(ContextHandlerAction.ACTION_PERFORMED, actionPayload);
-                        }
-                    });
-
-                    // the component name is for unit tests only
-                    contextAction.setName(action.getName().getContents());
-
-                    contextMenu.add(contextAction);
                 }
                 
                 if (showPopup) {
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandlerTest.java	Mon Nov 11 11:06:04 2013 +0100
+++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/controller/ContextHandlerTest.java	Mon Nov 11 11:06:04 2013 +0100
@@ -68,6 +68,7 @@
 import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextHandler.ContextHandlerAction;
 import com.redhat.thermostat.client.swing.internal.vmlist.controller.ContextHandler.Payload;
 import com.redhat.thermostat.client.ui.ReferenceContextAction;
+import com.redhat.thermostat.client.ui.ReferenceFilter;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.Filter;
 import com.redhat.thermostat.shared.locale.LocalizedString;
@@ -119,6 +120,59 @@
     }
     
     @Test
+    public void testFilterEvaluatedOnlyIfApplies() {
+        
+        final ThermostatPopupMenu popup = mock(ThermostatPopupMenu.class);
+        
+        ContextActionController.Payload payload =
+                new ContextActionController.Payload();
+        
+        HostRef host0 = new HostRef("0", "0");
+        
+        payload.ref = host0;
+        
+        ReferenceFilter hostFilter0 = mock(ReferenceFilter.class);
+        when(hostFilter0.applies(host0)).thenReturn(false);
+        when(hostFilter0.matches(host0)).thenReturn(true);
+        
+        ReferenceFilter hostFilter1 = mock(ReferenceFilter.class);
+        when(hostFilter1.applies(host0)).thenReturn(true);
+        when(hostFilter1.matches(host0)).thenReturn(false);
+        
+        ReferenceContextAction hostAction0 = mock(ReferenceContextAction.class);
+        when(hostAction0.getFilter()).thenReturn(hostFilter0);
+        
+        ReferenceContextAction hostAction1 = mock(ReferenceContextAction.class);
+        when(hostAction1.getFilter()).thenReturn(hostFilter1);
+        
+        when(actionEvent.getPayload()).thenReturn(payload);
+        
+        List<ReferenceContextAction> hostActions = new ArrayList<>();
+        hostActions.add(hostAction0);
+        hostActions.add(hostAction1);
+        
+        when(contextActionTracker.getActions()).thenReturn(hostActions);
+        
+        ContextHandler handler = new ContextHandler(contextActionTracker) {
+            @Override
+            ThermostatPopupMenu createContextPopMenu() {
+                return popup;
+            }
+        };
+        
+        handler.actionPerformed(actionEvent);
+        waitForSwing();
+
+        verify(hostFilter0).applies(host0);
+        verify(hostFilter0, times(0)).matches(host0);
+
+        verifyNoMoreInteractions(hostFilter0);
+        
+        verify(hostFilter1).applies(host0);
+        verify(hostFilter1).matches(host0);
+    }
+    
+    @Test
     public void testActions() {
         
         ArgumentCaptor<ActionListener> captor0 = ArgumentCaptor.forClass(ActionListener.class);
@@ -184,9 +238,10 @@
         // *** test two actions, no filter
         
         // no reason to change the event, but add actions
-        Filter<Ref> hostFilter = mock(Filter.class);
+        ReferenceFilter hostFilter = mock(ReferenceFilter.class);
         when(hostFilter.matches(host0)).thenReturn(true).thenReturn(true);
-        
+        when(hostFilter.applies(host0)).thenReturn(true).thenReturn(true);
+
         LocalizedString name0 = new LocalizedString("actionName0");
         LocalizedString des0 = new LocalizedString("actionDesc0");
 
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java	Mon Nov 11 11:06:04 2013 +0100
+++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java	Mon Nov 11 11:06:04 2013 +0100
@@ -40,8 +40,8 @@
 import java.util.Objects;
 
 import com.redhat.thermostat.client.command.RequestQueue;
-import com.redhat.thermostat.common.Filter;
 import com.redhat.thermostat.client.ui.ReferenceContextAction;
+import com.redhat.thermostat.client.ui.ReferenceFilter;
 import com.redhat.thermostat.common.command.Request;
 import com.redhat.thermostat.common.command.Request.RequestType;
 import com.redhat.thermostat.common.command.RequestResponseListener;
@@ -115,16 +115,22 @@
     }
 
     @Override
-    public Filter<Ref> getFilter() {
+    public ReferenceFilter getFilter() {
         return new LocalAndAliveFilter();
     }
 
-    private class LocalAndAliveFilter extends Filter<Ref> {
+    private class LocalAndAliveFilter extends ReferenceFilter {
+        
+        @Override
+        public boolean applies(Ref reference) {
+            return (reference instanceof VmRef);
+        }
+        
         @Override
         public boolean matches(Ref ref) {
             boolean match = false;
             
-            if (ref instanceof VmRef) {
+            if (applies(ref)) {
                 VmRef reference = (VmRef) ref;
                 VmInfo vmInfo = vmDao.getVmInfo(reference);
                 match = vmInfo.isAlive();