Mercurial > hg > release > thermostat-0.11
changeset 139:09f0c7f7d73a
Use view action support in main view/controller.
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Fri, 23 Mar 2012 22:00:50 +0100 |
parents | 90264e5705e3 |
children | 84e1f8e3f8a6 |
files | client/src/main/java/com/redhat/thermostat/client/MainView.java client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java |
diffstat | 5 files changed, 60 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/MainView.java Fri Mar 23 22:00:26 2012 +0100 +++ b/client/src/main/java/com/redhat/thermostat/client/MainView.java Fri Mar 23 22:00:50 2012 +0100 @@ -36,19 +36,20 @@ package com.redhat.thermostat.client; -import java.beans.PropertyChangeListener; public interface MainView { - enum ViewProperty { + enum Action { HOST_VM_TREE_FILTER, SHUTDOWN } - void addViewPropertyListener(PropertyChangeListener capture); + void addViewActionListener(ViewActionListener<Action> capture); void updateTree(String eq, HostsVMsLoader any); void showMainWindow(); + String getHostVmTreeFilter(); + }
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java Fri Mar 23 22:00:26 2012 +0100 +++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java Fri Mar 23 22:00:50 2012 +0100 @@ -151,16 +151,21 @@ private void initView(MainView mainView) { this.view = mainView; - mainView.addViewPropertyListener(new PropertyChangeListener() { + mainView.addViewActionListener(new ViewActionListener<MainView.Action>() { @Override - public void propertyChange(PropertyChangeEvent evt) { - String propertyName = evt.getPropertyName(); - if (propertyName.equals(MainView.ViewProperty.HOST_VM_TREE_FILTER.toString())) { - String filter = (String) evt.getNewValue(); + public void viewActionPerformed(ViewActionEvent<MainView.Action> evt) { + MainView.Action action = evt.getActionId(); + switch (action) { + case HOST_VM_TREE_FILTER: + String filter = view.getHostVmTreeFilter(); setHostVmTreeFilter(filter); - } else if (propertyName.equals(MainView.ViewProperty.SHUTDOWN.toString())) { + break; + case SHUTDOWN: stop(); + break; + default: + assert false; } }
--- a/client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java Fri Mar 23 22:00:26 2012 +0100 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java Fri Mar 23 22:00:50 2012 +0100 @@ -92,6 +92,8 @@ import com.redhat.thermostat.client.HostsVMsLoader; import com.redhat.thermostat.client.MainView; import com.redhat.thermostat.client.UiFacadeFactory; +import com.redhat.thermostat.client.ViewActionListener; +import com.redhat.thermostat.client.ViewActionSupport; import com.redhat.thermostat.client.locale.LocaleResources; import com.redhat.thermostat.common.dao.HostRef; import com.redhat.thermostat.common.dao.Ref; @@ -227,7 +229,7 @@ private ApplicationInfo appInfo; - private PropertyChangeSupport viewPropertySupport = new PropertyChangeSupport(this); + private ViewActionSupport<Action> actionSupport = new ViewActionSupport<>(this); private final DefaultMutableTreeNode publishedRoot = new DefaultMutableTreeNode(localize(LocaleResources.MAIN_WINDOW_TREE_ROOT_NAME)); @@ -349,8 +351,7 @@ } catch (BadLocationException ble) { // ignore } - // We don't have information about the old value, and are not interested either, so we send null. - fireViewPropertyChange(MainView.ViewProperty.HOST_VM_TREE_FILTER.toString(), null, searchField.getText()); + fireViewAction(MainView.Action.HOST_VM_TREE_FILTER); } }); searchPanel.add(searchField); @@ -416,7 +417,7 @@ private void shutdown() { dispose(); - fireViewPropertyChange(ViewProperty.SHUTDOWN.toString(), false, true); + fireViewAction(Action.SHUTDOWN); } } @@ -502,16 +503,16 @@ } } - public void addViewPropertyListener(PropertyChangeListener l) { - viewPropertySupport.addPropertyChangeListener(l); + public void addViewActionListener(ViewActionListener<Action> l) { + actionSupport.addViewActionListener(l); } - public void removeViewPropertyListener(PropertyChangeListener l) { - viewPropertySupport.removePropertyChangeListener(l); + public void removeViewActionListener(ViewActionListener<Action> l) { + actionSupport.removeViewActionListener(l); } - private void fireViewPropertyChange(String propertyName, Object oldValue, Object newValue) { - viewPropertySupport.firePropertyChange(propertyName, oldValue, newValue); + private void fireViewAction(Action action) { + actionSupport.fireViewAction(action); } public void updateTree(String filter, HostsVMsLoader hostsVMsLoader) { @@ -535,4 +536,9 @@ setVisible(true); } + @Override + public String getHostVmTreeFilter() { + return searchField.getText(); + } + }
--- a/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java Fri Mar 23 22:00:26 2012 +0100 +++ b/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java Fri Mar 23 22:00:50 2012 +0100 @@ -59,7 +59,7 @@ public class MainWindowControllerImplTest { - private PropertyChangeListener l; + private ViewActionListener<MainView.Action> l; private MainWindowControllerImpl controller; @@ -77,8 +77,8 @@ DB db = mock(DB.class); view = mock(MainView.class); - ArgumentCaptor<PropertyChangeListener> grabListener = ArgumentCaptor.forClass(PropertyChangeListener.class); - doNothing().when(view).addViewPropertyListener(grabListener.capture()); + ArgumentCaptor<ViewActionListener> grabListener = ArgumentCaptor.forClass(ViewActionListener.class); + doNothing().when(view).addViewActionListener(grabListener.capture()); controller = new MainWindowControllerImpl(db, view); l = grabListener.getValue(); } @@ -94,7 +94,7 @@ @Test public void verifyThatShutdownEventStopsController() { - l.propertyChange(new PropertyChangeEvent(view, MainView.ViewProperty.SHUTDOWN.toString(), false, true)); + l.viewActionPerformed(new ViewActionEvent<MainView.Action>(view, MainView.Action.SHUTDOWN)); verify(mainWindowTimer).stop(); @@ -103,7 +103,9 @@ @Test public void verifyThatHostsVmsFilterChangeUpdatesTree() { - l.propertyChange(new PropertyChangeEvent(view, MainView.ViewProperty.HOST_VM_TREE_FILTER.toString(), null, "test")); + when(view.getHostVmTreeFilter()).thenReturn("test"); + + l.viewActionPerformed(new ViewActionEvent<MainView.Action>(view, MainView.Action.HOST_VM_TREE_FILTER)); verify(view).updateTree(eq("test"), any(HostsVMsLoader.class));
--- a/client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java Fri Mar 23 22:00:26 2012 +0100 +++ b/client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java Fri Mar 23 22:00:50 2012 +0100 @@ -36,14 +36,13 @@ package com.redhat.thermostat.client.ui; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.inOrder; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; import org.apache.commons.lang3.ObjectUtils; import org.fest.swing.edt.GuiActionRunner; @@ -56,11 +55,13 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.mockito.ArgumentMatcher; -import org.mockito.InOrder; import com.redhat.thermostat.client.ChangeableText; +import com.redhat.thermostat.client.MainView; import com.redhat.thermostat.client.SummaryPanelFacade; import com.redhat.thermostat.client.UiFacadeFactory; +import com.redhat.thermostat.client.ViewActionEvent; +import com.redhat.thermostat.client.ViewActionListener; import com.redhat.thermostat.test.GUITest; public class MainWindowTest { @@ -91,7 +92,7 @@ private FrameFixture frameFixture; private MainWindow window; - private PropertyChangeListener l; + private ViewActionListener<MainView.Action> l; @Before public void setUp() { @@ -104,8 +105,8 @@ when(uiFacadeFactory.getSummaryPanel()).thenReturn(summaryPanelFacade); window = new MainWindow(uiFacadeFactory); - l = mock(PropertyChangeListener.class); - window.addViewPropertyListener(l); + l = mock(ViewActionListener.class); + window.addViewActionListener(l); frameFixture = new FrameFixture(window); } @@ -125,11 +126,7 @@ JTextComponentFixture hostVMTreeFilterField = frameFixture.textBox("hostVMTreeFilter"); hostVMTreeFilterField.enterText("test"); - InOrder inOrder = inOrder(l); - inOrder.verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.ViewProperty.HOST_VM_TREE_FILTER.toString(), null, "t")))); - inOrder.verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.ViewProperty.HOST_VM_TREE_FILTER.toString(), null, "te")))); - inOrder.verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.ViewProperty.HOST_VM_TREE_FILTER.toString(), null, "tes")))); - inOrder.verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.ViewProperty.HOST_VM_TREE_FILTER.toString(), null, "test")))); + verify(l, times(4)).viewActionPerformed(new ViewActionEvent<MainView.Action>(window, MainView.Action.HOST_VM_TREE_FILTER)); } @Category(GUITest.class) @@ -140,7 +137,7 @@ frameFixture.close(); frameFixture.requireNotVisible(); - verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.ViewProperty.SHUTDOWN.toString(), false, true)))); + verify(l).viewActionPerformed(new ViewActionEvent<MainView.Action>(window, MainView.Action.SHUTDOWN)); } @Category(GUITest.class) @@ -155,4 +152,16 @@ }); frameFixture.requireVisible(); } + + @Category(GUITest.class) + @Test + public void testGetHostVMTreeFilter() { + frameFixture.show(); + JTextComponentFixture hostVMTreeFilterField = frameFixture.textBox("hostVMTreeFilter"); + hostVMTreeFilterField.enterText("test"); + + assertEquals("test", window.getHostVmTreeFilter()); + } + + }