Mercurial > hg > release > thermostat-0.15
changeset 235:fdda2e68edf2
Move more logic out from the MainWindow into MainWindowControllerImpl
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000833.html
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Mon, 16 Apr 2012 12:58:48 -0400 |
parents | 001b59e74e8d |
children | e69fb1a3dbd9 |
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/UiFacadeFactoryImpl.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 | 6 files changed, 160 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/MainView.java Mon Apr 16 11:14:53 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/MainView.java Mon Apr 16 12:58:48 2012 -0400 @@ -36,16 +36,20 @@ package com.redhat.thermostat.client; +import java.awt.Component; + import com.redhat.thermostat.common.ActionListener; - +import com.redhat.thermostat.common.dao.Ref; public interface MainView { enum Action { HOST_VM_TREE_FILTER, + HOST_VM_SELECTION_CHANGED, SHOW_AGENT_CONFIG, SHOW_CLIENT_CONFIG, SWITCH_HISTORY_MODE, + SHOW_ABOUT_DIALOG, SHUTDOWN, } @@ -53,8 +57,16 @@ void updateTree(String eq, HostsVMsLoader any); + void setWindowTitle(String title); + void showMainWindow(); + void hideMainWindow(); + String getHostVmTreeFilter(); + Ref getSelectedHostOrVm(); + + void setSubView(Component view); + }
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java Mon Apr 16 11:14:53 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java Mon Apr 16 12:58:48 2012 -0400 @@ -40,11 +40,15 @@ import java.util.concurrent.TimeUnit; import com.redhat.thermostat.client.config.ClientPreferences; +import com.redhat.thermostat.client.ui.AboutDialog; import com.redhat.thermostat.client.ui.AgentConfigurationController; import com.redhat.thermostat.client.ui.AgentConfigurationModel; import com.redhat.thermostat.client.ui.AgentConfigurationView; import com.redhat.thermostat.client.ui.ClientConfigurationController; import com.redhat.thermostat.client.ui.ClientConfigurationView; +import com.redhat.thermostat.client.ui.HostPanel; +import com.redhat.thermostat.client.ui.SummaryPanel; +import com.redhat.thermostat.client.ui.VmPanel; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.Timer; @@ -52,6 +56,7 @@ import com.redhat.thermostat.common.dao.DAOFactory; import com.redhat.thermostat.common.dao.HostInfoDAO; import com.redhat.thermostat.common.dao.HostRef; +import com.redhat.thermostat.common.dao.Ref; import com.redhat.thermostat.common.dao.VmInfoDAO; import com.redhat.thermostat.common.dao.VmRef; @@ -66,9 +71,14 @@ private final HostInfoDAO hostsDAO; private final VmInfoDAO vmsDAO; + private ApplicationInfo appInfo; + + private UiFacadeFactory facadeFactory; + private boolean showHistory; - public MainWindowControllerImpl(MainView view) { + public MainWindowControllerImpl(UiFacadeFactory facadeFactory, MainView view) { + this.facadeFactory = facadeFactory; ApplicationContext ctx = ApplicationContext.getInstance(); DAOFactory daoFactory = ctx.getDAOFactory(); @@ -76,6 +86,8 @@ vmsDAO = daoFactory.getVmInfoDAO(); initView(view); + appInfo = new ApplicationInfo(); + view.setWindowTitle(appInfo.getName()); initializeTimer(); start(); } @@ -141,6 +153,9 @@ public void actionPerformed(ActionEvent<MainView.Action> evt) { MainView.Action action = evt.getActionId(); switch (action) { + case HOST_VM_SELECTION_CHANGED: + updateView(); + break; case HOST_VM_TREE_FILTER: String filter = view.getHostVmTreeFilter(); setHostVmTreeFilter(filter); @@ -154,11 +169,15 @@ case SWITCH_HISTORY_MODE: switchHistoryMode(); break; + case SHOW_ABOUT_DIALOG: + showAboutDialog(); + break; case SHUTDOWN: + view.hideMainWindow(); stop(); break; default: - assert false; + throw new IllegalStateException("unhandled action"); } } }); @@ -169,6 +188,13 @@ view.showMainWindow(); } + private void showAboutDialog() { + AboutDialog aboutDialog = new AboutDialog(appInfo); + aboutDialog.setModal(true); + aboutDialog.pack(); + aboutDialog.setVisible(true); + } + private void showAgentConfiguration() { AgentConfigurationSource agentPrefs = new AgentConfigurationSource(); AgentConfigurationModel model = new AgentConfigurationModel(agentPrefs); @@ -188,4 +214,21 @@ showHistory = !showHistory; doUpdateTreeAsync(); } + + private void updateView() { + // this is quite an ugly method. there must be a cleaner way to do this + Ref ref = view.getSelectedHostOrVm(); + if (ref == null) { + view.setSubView(new SummaryPanel(facadeFactory.getSummaryPanel())); + } else if (ref instanceof HostRef) { + HostRef hostRef = (HostRef) ref; + view.setSubView(new HostPanel(facadeFactory.getHostPanel(hostRef))); + } else if (ref instanceof VmRef) { + VmRef vmRef = (VmRef) ref; + view.setSubView(new VmPanel(facadeFactory.getVmPanel(vmRef))); + } else { + throw new IllegalArgumentException("unknown type of ref"); + } + } + }
--- a/client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java Mon Apr 16 11:14:53 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/UiFacadeFactoryImpl.java Mon Apr 16 12:58:48 2012 -0400 @@ -44,8 +44,8 @@ @Override public MainWindowController getMainWindow() { - MainView mainView = new MainWindow(this); - return new MainWindowControllerImpl(mainView); + MainView mainView = new MainWindow(); + return new MainWindowControllerImpl(this, mainView); } @Override
--- a/client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java Mon Apr 16 11:14:53 2012 -0400 +++ b/client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java Mon Apr 16 12:58:48 2012 -0400 @@ -81,15 +81,12 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; -import com.redhat.thermostat.client.ApplicationInfo; import com.redhat.thermostat.client.HostsVMsLoader; import com.redhat.thermostat.client.MainView; -import com.redhat.thermostat.client.UiFacadeFactory; import com.redhat.thermostat.client.locale.LocaleResources; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.ActionNotifier; @@ -216,8 +213,6 @@ private static final long serialVersionUID = 5608972421496808177L; - private final UiFacadeFactory facadeFactory; - private JPanel contentArea = null; private JTextField searchField = null; @@ -225,28 +220,22 @@ private final ShutdownClient shutdownAction; - private ApplicationInfo appInfo; - private ActionNotifier<Action> actionNotifier = new ActionNotifier<>(this); private final DefaultMutableTreeNode publishedRoot = new DefaultMutableTreeNode(localize(LocaleResources.MAIN_WINDOW_TREE_ROOT_NAME)); private final DefaultTreeModel publishedTreeModel = new DefaultTreeModel(publishedRoot); - public MainWindow(UiFacadeFactory facadeFactory) { + public MainWindow() { super(); - appInfo = new ApplicationInfo(); - setTitle(appInfo.getName()); - - this.facadeFactory = facadeFactory; shutdownAction = new ShutdownClient(); searchField = new JTextField(); searchField.setName("hostVMTreeFilter"); - TreeModel model = publishedTreeModel; - agentVmTree = new JTree(model); - model.addTreeModelListener(new KeepRootExpandedListener(agentVmTree)); + agentVmTree = new JTree(publishedTreeModel); + agentVmTree.setName("agentVmTree"); + publishedTreeModel.addTreeModelListener(new KeepRootExpandedListener(agentVmTree)); agentVmTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); agentVmTree.setCellRenderer(new AgentVmTreeCellRenderer()); ToolTipManager.sharedInstance().registerComponent(agentVmTree); @@ -257,7 +246,7 @@ this.setPreferredSize(new Dimension(800, 600)); - agentVmTree.setSelectionPath(new TreePath(((DefaultMutableTreeNode) model.getRoot()).getPath())); + agentVmTree.setSelectionPath(new TreePath(((DefaultMutableTreeNode) publishedTreeModel.getRoot()).getPath())); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(shutdownAction); @@ -298,6 +287,7 @@ mainMenuBar.add(editMenu); JMenuItem configureAgentMenuItem = new JMenuItem(localize(LocaleResources.MENU_EDIT_CONFIGURE_AGENT)); + configureAgentMenuItem.setName("showAgentConfig"); configureAgentMenuItem.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -336,10 +326,7 @@ helpAboutMenu.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent e) { - AboutDialog aboutDialog = new AboutDialog(appInfo); - aboutDialog.setModal(true); - aboutDialog.pack(); - aboutDialog.setVisible(true); + fireViewAction(Action.SHOW_ABOUT_DIALOG); } }); helpMenu.add(helpAboutMenu); @@ -396,20 +383,7 @@ @Override public void valueChanged(TreeSelectionEvent e) { if (e.isAddedPath()) { - contentArea.removeAll(); - TreePath path = e.getPath(); - if (path.getPathCount() == 1) {/* root */ - contentArea.add(new SummaryPanel(facadeFactory.getSummaryPanel())); - } else if (path.getPathCount() == 2) { /* agent */ - HostRef hostRef = (HostRef) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject(); - HostPanel panel = new HostPanel(facadeFactory.getHostPanel(hostRef)); - contentArea.add(panel); - } else { /* vm */ - VmRef vmRef = (VmRef) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject(); - VmPanel panel = new VmPanel(facadeFactory.getVmPanel(vmRef)); - contentArea.add(panel); - } - contentArea.revalidate(); + fireViewAction(Action.HOST_VM_SELECTION_CHANGED); } } }); @@ -565,12 +539,43 @@ } @Override + public void setWindowTitle(String title) { + setTitle(title); + } + + @Override public void showMainWindow() { pack(); setVisible(true); } @Override + public void hideMainWindow() { + setVisible(false); + dispose(); + } + + @Override + public void setSubView(Component view) { + contentArea.removeAll(); + Component toAdd = view; + contentArea.add(toAdd); + contentArea.revalidate(); + } + + /** + * Returns null to indicate no Ref is selected + */ + @Override + public Ref getSelectedHostOrVm() { + TreePath path = agentVmTree.getSelectionPath(); + if (path.getPathCount() == 1) { + return null; + } + return (Ref) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject(); + } + + @Override public String getHostVmTreeFilter() { return searchField.getText(); }
--- a/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java Mon Apr 16 11:14:53 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java Mon Apr 16 12:58:48 2012 -0400 @@ -88,12 +88,19 @@ when(timerFactory.createTimer()).thenReturn(mainWindowTimer); ApplicationContext.getInstance().setTimerFactory(timerFactory); + SummaryPanelFacade summaryPanelFacade = mock(SummaryPanelFacade.class); + when(summaryPanelFacade.getTotalMonitoredHosts()).thenReturn(new ChangeableText("totalConnectedAgents")); + when(summaryPanelFacade.getTotalMonitoredVms()).thenReturn(new ChangeableText("connectedVms")); + + UiFacadeFactory uiFacadeFactory = mock(UiFacadeFactory.class); + when(uiFacadeFactory.getSummaryPanel()).thenReturn(summaryPanelFacade); + setupDAOs(); view = mock(MainView.class); ArgumentCaptor<ActionListener> grabListener = ArgumentCaptor.forClass(ActionListener.class); doNothing().when(view).addActionListener(grabListener.capture()); - controller = new MainWindowControllerImpl(view); + controller = new MainWindowControllerImpl(uiFacadeFactory, view); l = grabListener.getValue(); } @@ -159,7 +166,7 @@ expectedHosts.add(new HostRef("456", "fluffhost2")); when(mockHostsDAO.getAliveHosts()).thenReturn(expectedHosts); - + controller.doUpdateTreeAsync(); ArgumentCaptor<HostsVMsLoader> arg = ArgumentCaptor.forClass(HostsVMsLoader.class);
--- a/client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java Mon Apr 16 11:14:53 2012 -0400 +++ b/client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java Mon Apr 16 12:58:48 2012 -0400 @@ -40,7 +40,6 @@ 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 org.fest.swing.annotation.GUITest; import org.fest.swing.edt.GuiActionRunner; @@ -48,6 +47,7 @@ import org.fest.swing.fixture.FrameFixture; import org.fest.swing.fixture.JMenuItemFixture; import org.fest.swing.fixture.JTextComponentFixture; +import org.fest.swing.fixture.JTreeFixture; import org.fest.swing.junit.v4_5.runner.GUITestRunner; import org.junit.After; import org.junit.Before; @@ -55,10 +55,7 @@ import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; -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.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; @@ -73,14 +70,7 @@ @Before public void setUp() { - SummaryPanelFacade summaryPanelFacade = mock(SummaryPanelFacade.class); - when(summaryPanelFacade.getTotalMonitoredHosts()).thenReturn(new ChangeableText("totalConnectedAgents")); - when(summaryPanelFacade.getTotalMonitoredVms()).thenReturn(new ChangeableText("connectedVms")); - - UiFacadeFactory uiFacadeFactory = mock(UiFacadeFactory.class); - when(uiFacadeFactory.getSummaryPanel()).thenReturn(summaryPanelFacade); - - window = new MainWindow(uiFacadeFactory); + window = new MainWindow(); l = mock(ActionListener.class); window.addActionListener(l); @@ -97,6 +87,16 @@ @Category(GUITest.class) @Test + public void testHostVmSelectionChangedSupport() { + frameFixture.show(); + JTreeFixture hostVMTree = frameFixture.tree("agentVmTree"); + hostVMTree.selectRows(0); + + verify(l).actionPerformed(new ActionEvent<MainView.Action>(window, MainView.Action.HOST_VM_SELECTION_CHANGED)); + } + + @Category(GUITest.class) + @Test public void testHostVMTreeFilterPropertySupport() { frameFixture.show(); JTextComponentFixture hostVMTreeFilterField = frameFixture.textBox("hostVMTreeFilter"); @@ -131,6 +131,25 @@ @Category(GUITest.class) @Test + public void verifyHideMainWindowHidesWindow() { + GuiActionRunner.execute(new GuiTask() { + @Override + protected void executeInEDT() throws Throwable { + window.showMainWindow(); + } + }); + frameFixture.requireVisible(); + GuiActionRunner.execute(new GuiTask() { + @Override + protected void executeInEDT() throws Throwable { + window.hideMainWindow(); + } + }); + frameFixture.requireNotVisible(); + } + + @Category(GUITest.class) + @Test public void verifyThatClientPreferencesMenuItemTriggersEvent() { frameFixture.show(); JMenuItemFixture menuItem = frameFixture.menuItem("showClientConfig"); @@ -139,9 +158,21 @@ frameFixture.requireNotVisible(); verify(l).actionPerformed(new ActionEvent<MainView.Action>(window, MainView.Action.SHOW_CLIENT_CONFIG)); + } + @Category(GUITest.class) + @Test + public void verifyThatAgentPreferencesMenuItemTriggersEvent() { + frameFixture.show(); + JMenuItemFixture menuItem = frameFixture.menuItem("showAgentConfig"); + menuItem.click(); + frameFixture.close(); + frameFixture.requireNotVisible(); + + verify(l).actionPerformed(new ActionEvent<MainView.Action>(window, MainView.Action.SHOW_AGENT_CONFIG)); } + @Category(GUITest.class) @Test public void verifyThatHistorySwitchTriggersEvent() { @@ -164,5 +195,14 @@ assertEquals("test", window.getHostVmTreeFilter()); } + @Category(GUITest.class) + @Test + public void testGetSelectedHostOrVm() { + frameFixture.show(); + JTreeFixture hostVMTree = frameFixture.tree("agentVmTree"); + hostVMTree.selectRow(0); + + assertEquals(null, window.getSelectedHostOrVm()); + } }