changeset 241:f105046da8c3

Keep internal details about the configuration views internal Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000901.html
author Omair Majid <omajid@redhat.com>
date Tue, 17 Apr 2012 15:05:55 -0400
parents 2dca3a8b0c0d
children 1c37a952b712
files client/src/main/java/com/redhat/thermostat/client/ui/AgentConfigurationFrame.java client/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationFrame.java client/src/test/java/com/redhat/thermostat/client/ui/AgentConfigurationFrameTest.java client/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationFrameTest.java
diffstat 4 files changed, 234 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/ui/AgentConfigurationFrame.java	Tue Apr 17 14:14:06 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/AgentConfigurationFrame.java	Tue Apr 17 15:05:55 2012 -0400
@@ -68,7 +68,7 @@
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 
-public class AgentConfigurationFrame extends JFrame implements AgentConfigurationView, java.awt.event.ActionListener, ListSelectionListener {
+public class AgentConfigurationFrame extends JFrame implements AgentConfigurationView {
 
     private static final long serialVersionUID = -6049272471909474886L;
 
@@ -79,6 +79,10 @@
     private final JPanel availableBackendsPanel;
     private final GridBagConstraints availableBackendsPanelContstraints = new GridBagConstraints();
 
+    private final ConfigurationCompleteListener configurationComplete;
+    private final AgentChangedListener agentChanged;
+    private final WindowClosingListener windowListener;
+
     private final JButton okayButton;
     private final JButton cancelButton;
 
@@ -89,17 +93,22 @@
     public AgentConfigurationFrame() {
         assertInEDT();
 
+        configurationComplete = new ConfigurationCompleteListener();
+        agentChanged = new AgentChangedListener();
+        windowListener = new WindowClosingListener();
+
         setTitle(localize(LocaleResources.CONFIGURE_AGENT_WINDOW_TITLE));
+        addWindowListener(windowListener);
 
         JLabel lblEnabledisableBackends = new JLabel(localize(LocaleResources.CONFIGURE_ENABLE_BACKENDS));
 
         availableBackendsPanel = new JPanel();
 
         okayButton = new JButton(localize(LocaleResources.BUTTON_OK));
-        okayButton.addActionListener(this);
+        okayButton.addActionListener(configurationComplete);
 
         cancelButton = new JButton(localize(LocaleResources.BUTTON_CANCEL));
-        cancelButton.addActionListener(this);
+        cancelButton.addActionListener(configurationComplete);
 
         JScrollPane scrollPane = new JScrollPane();
 
@@ -150,18 +159,13 @@
 
         listModel = new DefaultListModel<String>();
         agentList = new JList<String>(listModel);
-        agentList.addListSelectionListener(this);
+        agentList.setName("agentList");
+        agentList.addListSelectionListener(agentChanged);
         scrollPane.setViewportView(agentList);
 
         availableBackendsPanel.setLayout(new GridBagLayout());
         getContentPane().setLayout(groupLayout);
 
-        addWindowListener(new WindowAdapter() {
-            @Override
-            public void windowClosing(WindowEvent e) {
-                fireAction(new ActionEvent<>(AgentConfigurationFrame.this, ConfigurationAction.CLOSE_CANCEL));
-            }
-        });
     }
 
     private void resetConstraints() {
@@ -227,7 +231,6 @@
                     JCheckBox checkBox = new JCheckBox(backendName);
                     checkBox.setSelected(checked);
                     checkBox.setActionCommand(backendName);
-                    checkBox.addActionListener(AgentConfigurationFrame.this);
                     backends.put(backendName, checkBox);
                     availableBackendsPanel.add(checkBox, availableBackendsPanelContstraints);
                     availableBackendsPanelContstraints.gridy++;
@@ -270,28 +273,6 @@
         dispose();
     }
 
-    @Override
-    public void actionPerformed(java.awt.event.ActionEvent e) {
-        Object source = e.getSource();
-        if (source == okayButton) {
-            fireAction(new ActionEvent<>(this, ConfigurationAction.CLOSE_ACCEPT));
-        } else if (source == cancelButton) {
-            fireAction(new ActionEvent<>(this, ConfigurationAction.CLOSE_CANCEL));
-        }
-    }
-
-    @Override
-    public void valueChanged(ListSelectionEvent e) {
-        if (e.getSource() == agentList) {
-            if (e.getValueIsAdjusting()) {
-                return;
-            }
-            fireAction(new ActionEvent<>(this, ConfigurationAction.SWITCH_AGENT));
-        } else {
-            throw new IllegalStateException("unknown trigger");
-        }
-    }
-
     private void fireAction(ActionEvent<ConfigurationAction> actionEvent) {
         for (ActionListener<ConfigurationAction> l: listeners) {
             l.actionPerformed(actionEvent);
@@ -304,5 +285,37 @@
         }
     }
 
+    class ConfigurationCompleteListener implements java.awt.event.ActionListener {
+        @Override
+        public void actionPerformed(java.awt.event.ActionEvent e) {
+            Object source = e.getSource();
+            if (source == okayButton) {
+                fireAction(new ActionEvent<>(AgentConfigurationFrame.this, ConfigurationAction.CLOSE_ACCEPT));
+            } else if (source == cancelButton) {
+                fireAction(new ActionEvent<>(AgentConfigurationFrame.this, ConfigurationAction.CLOSE_CANCEL));
+            }
+        }
+    }
+
+    class WindowClosingListener extends WindowAdapter {
+        @Override
+        public void windowClosing(WindowEvent e) {
+            fireAction(new ActionEvent<>(AgentConfigurationFrame.this, ConfigurationAction.CLOSE_CANCEL));
+        }
+    }
+
+    class AgentChangedListener implements ListSelectionListener {
+        @Override
+        public void valueChanged(ListSelectionEvent e) {
+            if (e.getSource() == agentList) {
+                if (e.getValueIsAdjusting()) {
+                    return;
+                }
+                fireAction(new ActionEvent<>(AgentConfigurationFrame.this, ConfigurationAction.SWITCH_AGENT));
+            } else {
+                throw new IllegalStateException("unknown trigger");
+            }
+        }
+    }
 
 }
--- a/client/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationFrame.java	Tue Apr 17 14:14:06 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationFrame.java	Tue Apr 17 15:05:55 2012 -0400
@@ -38,6 +38,8 @@
 
 import static com.redhat.thermostat.client.locale.Translate.localize;
 
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.swing.GroupLayout;
@@ -46,17 +48,20 @@
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JTextField;
+import javax.swing.LayoutStyle.ComponentPlacement;
 import javax.swing.SwingUtilities;
-import javax.swing.LayoutStyle.ComponentPlacement;
 
 import com.redhat.thermostat.client.locale.LocaleResources;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.ActionListener;
 
-public class ClientConfigurationFrame extends JFrame implements ClientConfigurationView, java.awt.event.ActionListener {
+public class ClientConfigurationFrame extends JFrame implements ClientConfigurationView {
 
     private static final long serialVersionUID = 6888957994092403516L;
 
+    private final ConfigurationCompleteListener configurationCompleteListener;
+    private final WindowClosingListener windowClosingListener;
+
     private final JTextField storageUrl;
     private final JButton btnOk;
     private final JButton btnCancel;
@@ -64,13 +69,17 @@
     private CopyOnWriteArrayList<ActionListener<Action>> listeners = new CopyOnWriteArrayList<>();
 
     public ClientConfigurationFrame() {
+        configurationCompleteListener = new ConfigurationCompleteListener();
+        windowClosingListener = new WindowClosingListener();
+
         setTitle(localize(LocaleResources.CLIENT_PREFS_WINDOW_TITLE));
+        addWindowListener(windowClosingListener);
 
         btnOk = new JButton(localize(LocaleResources.BUTTON_OK));
-        btnOk.addActionListener(this);
+        btnOk.addActionListener(configurationCompleteListener);
         btnOk.setName("ok");
         btnCancel = new JButton(localize(LocaleResources.BUTTON_CANCEL));
-        btnCancel.addActionListener(this);
+        btnCancel.addActionListener(configurationCompleteListener);
         btnCancel.setName("cancel");
         JLabel lblClientConfiguration = new JLabel(localize(LocaleResources.CLIENT_PREFS_GENERAL));
 
@@ -150,15 +159,6 @@
     }
 
     @Override
-    public void actionPerformed(java.awt.event.ActionEvent e) {
-        if (e.getSource() == btnOk) {
-            fireAction(new ActionEvent<>(this, Action.CLOSE_ACCEPT));
-        } else if (e.getSource() == btnCancel) {
-            fireAction(new ActionEvent<>(this, Action.CLOSE_CANCEL));
-        }
-    }
-
-    @Override
     public void addListener(ActionListener<Action> listener) {
         listeners.add(listener);
     }
@@ -179,4 +179,22 @@
             throw new IllegalStateException("must be invoked in the EDT");
         }
     }
+
+    class ConfigurationCompleteListener implements java.awt.event.ActionListener {
+        @Override
+        public void actionPerformed(java.awt.event.ActionEvent e) {
+            if (e.getSource() == btnOk) {
+                fireAction(new ActionEvent<>(ClientConfigurationFrame.this, Action.CLOSE_ACCEPT));
+            } else if (e.getSource() == btnCancel) {
+                fireAction(new ActionEvent<>(ClientConfigurationFrame.this, Action.CLOSE_CANCEL));
+            }
+        }
+    }
+
+    class WindowClosingListener extends WindowAdapter {
+        @Override
+        public void windowClosing(WindowEvent e) {
+            fireAction(new ActionEvent<>(ClientConfigurationFrame.this, Action.CLOSE_CANCEL));
+        }
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/test/java/com/redhat/thermostat/client/ui/AgentConfigurationFrameTest.java	Tue Apr 17 15:05:55 2012 -0400
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.client.ui;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import net.java.openjdk.cacio.ctc.junit.CacioFESTRunner;
+
+import org.fest.swing.edt.GuiActionRunner;
+import org.fest.swing.edt.GuiQuery;
+import org.fest.swing.edt.GuiTask;
+import org.fest.swing.fixture.FrameFixture;
+import org.fest.swing.fixture.JListFixture;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.redhat.thermostat.common.ActionEvent;
+import com.redhat.thermostat.common.ActionListener;
+
+@RunWith(CacioFESTRunner.class)
+public class AgentConfigurationFrameTest {
+
+    private AgentConfigurationFrame agentConfigFrame;
+    private FrameFixture fixture;
+    private ActionListener<AgentConfigurationView.ConfigurationAction> l;
+
+    @Before
+    public void setUp() {
+        agentConfigFrame = GuiActionRunner.execute(new GuiQuery<AgentConfigurationFrame>() {
+
+            @Override
+            protected AgentConfigurationFrame executeInEDT() throws Throwable {
+                 return new AgentConfigurationFrame();
+            }
+        });
+
+        @SuppressWarnings("unchecked")
+        ActionListener<AgentConfigurationView.ConfigurationAction> listener = mock(ActionListener.class);
+        l = listener;
+        agentConfigFrame.addActionListener(l);
+
+        fixture = new FrameFixture(agentConfigFrame);
+    }
+
+    @After
+    public void tearDown() {
+        GuiActionRunner.execute(new GuiTask() {
+            @Override
+            protected void executeInEDT() throws Throwable {
+                agentConfigFrame.hideDialog();
+            }
+        });
+
+        fixture.requireNotVisible();
+        agentConfigFrame.removeActionListener(l);
+
+        fixture.cleanUp();
+        fixture = null;
+    }
+
+    @Test
+    public void testAddingAgentWorks() {
+        fixture.show();
+        JListFixture list = fixture.list("agentList");
+        assertArrayEquals(new String[0], list.contents());
+
+        agentConfigFrame.addAgent("test-agent");
+
+        assertArrayEquals(new String[] {"test-agent"}, list.contents());
+    }
+
+    @Test
+    public void testSelectingAgentWorks() {
+        fixture.show();
+        agentConfigFrame.addAgent("testAgent");
+        JListFixture list = fixture.list("agentList");
+
+        list.selectItem("testAgent");
+
+        verify(l).actionPerformed(eq(new ActionEvent<>(agentConfigFrame, AgentConfigurationView.ConfigurationAction.SWITCH_AGENT)));
+    }
+
+    @Test
+    public void testRemovingAllAgentsWorks() {
+        fixture.show();
+        agentConfigFrame.addAgent("test-agent");
+        JListFixture list = fixture.list("agentList");
+
+        agentConfigFrame.clearAllAgents();
+
+        assertArrayEquals(new String[0], list.contents());
+    }
+
+    @Test
+    public void testWindowClose() {
+        fixture.show();
+
+        fixture.close();
+
+        verify(l).actionPerformed(eq(new ActionEvent<>(agentConfigFrame, AgentConfigurationView.ConfigurationAction.CLOSE_CANCEL)));
+    }
+
+}
--- a/client/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationFrameTest.java	Tue Apr 17 14:14:06 2012 -0400
+++ b/client/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationFrameTest.java	Tue Apr 17 15:05:55 2012 -0400
@@ -44,6 +44,7 @@
 import org.fest.swing.annotation.GUITest;
 import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
 import org.fest.swing.edt.GuiActionRunner;
+import org.fest.swing.edt.GuiQuery;
 import org.fest.swing.edt.GuiTask;
 import org.fest.swing.fixture.FrameFixture;
 import org.fest.swing.fixture.JButtonFixture;
@@ -72,11 +73,11 @@
     @SuppressWarnings("unchecked") // ActionListener
     @Before
     public void setUp() {
-        GuiActionRunner.execute(new GuiTask() {
-            
+        frame = GuiActionRunner.execute(new GuiQuery<ClientConfigurationFrame>() {
+
             @Override
-            protected void executeInEDT() throws Throwable {
-                frame = new ClientConfigurationFrame();
+            protected ClientConfigurationFrame executeInEDT() throws Throwable {
+                 return new ClientConfigurationFrame();
             }
         });
         l = mock(ActionListener.class);
@@ -124,4 +125,14 @@
         verify(l).actionPerformed(eq(new ActionEvent<>(frame, ClientConfigurationView.Action.CLOSE_CANCEL)));
 
     }
+
+    @Category(GUITest.class)
+    @Test
+    public void testCloseWindow() {
+        frameFixture.show();
+
+        frameFixture.close();
+
+        verify(l).actionPerformed(eq(new ActionEvent<>(frame, ClientConfigurationView.Action.CLOSE_CANCEL)));
+    }
 }