[Japi-cvs] SF.net SVN: japi: [557] libs/swing-list/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2007-07-29 20:51:34
|
Revision: 557 http://japi.svn.sourceforge.net/japi/?rev=557&view=rev Author: christianhujer Date: 2007-07-29 13:51:31 -0700 (Sun, 29 Jul 2007) Log Message: ----------- Fixed bug in ListControlPanel behaviour. Added more unit tests for ListControlPanel. Modified Paths: -------------- libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java Modified: libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java =================================================================== --- libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java 2007-07-29 20:50:47 UTC (rev 556) +++ libs/swing-list/trunk/src/net/sf/japi/swing/list/ListControlPanel.java 2007-07-29 20:51:31 UTC (rev 557) @@ -143,14 +143,44 @@ } } - private void updateActionStates() { + /** Updates the states of the actions. */ + protected void updateActionStates() { assert list == null || list.getModel() == model; - final boolean top = list == null || list.getSelectedIndex() == 0; - final boolean bottom = list == null || list.getSelectedIndex() == model.getSize() - 1; + final boolean noSelection = list == null || model.getSize() == 0 || list.getSelectedIndex() == -1; + final boolean top = noSelection || list.getSelectedIndex() == 0; + final boolean bottom = noSelection || list.getSelectedIndex() == model.getSize() - 1; moveToTop.setEnabled(!top); moveUp.setEnabled(!top); moveDown.setEnabled(!bottom); moveToBottom.setEnabled(!bottom); } + /** Returns the action for moving an element to top. + * @return The action for moving an element to top. + */ + @NotNull public Action getMoveToTop() { + return moveToTop; + } + + /** Returns the action for moving an element up. + * @return The action for moving an element up. + */ + @NotNull public Action getMoveUp() { + return moveUp; + } + + /** Returns the action for moving an element down. + * @return The action for moving an element down. + */ + @NotNull public Action getMoveDown() { + return moveDown; + } + + /** Returns the action for moving an element to bottom. + * @return The action for moving an element to bottom. + */ + @NotNull public Action getMoveToBottom() { + return moveToBottom; + } + } // class ListControlPanel Modified: libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java =================================================================== --- libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java 2007-07-29 20:50:47 UTC (rev 556) +++ libs/swing-list/trunk/src/test/net/sf/japi/swing/list/ListControlPanelTest.java 2007-07-29 20:51:31 UTC (rev 557) @@ -19,12 +19,13 @@ package test.net.sf.japi.swing.list; -import java.awt.BorderLayout; -import javax.swing.JFrame; import javax.swing.JList; -import javax.swing.JScrollPane; +import javax.swing.ListSelectionModel; import net.sf.japi.swing.list.ArrayListModel; import net.sf.japi.swing.list.ListControlPanel; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; import org.junit.Test; /** Test for {@link ListControlPanel}. @@ -32,19 +33,104 @@ */ public class ListControlPanelTest { - /** Tests that a ListControlPanel works. */ + /** The model for testing. */ + private ArrayListModel<String> model; + + /** The JList for testing. */ + private JList list; + + /** The ListControlPanel to test. */ + private ListControlPanel testling; + + /** Initializes the test data. */ + @Before + public void setUp() { + model = new ArrayListModel<String>(); + list = new JList(model); + testling = new ListControlPanel(list, model); + } + + /** Removes the test data. */ + @After + public void tearDown() { + } + + /** Tests that the actions of a ListControlPanel for an empty model are disabled. */ @Test - public void testListControlPanel() { - final ArrayListModel<String> model = new ArrayListModel<String>(); + public void testEmptyListDisabled() { + Assert.assertFalse("For an empty model, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For an empty model, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For an empty model, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For an empty model, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); + } + + /** Tests that the actions of a ListControlPanel for a singleton model are disabled. */ + @Test + public void testSingletonListDisabled() { model.add("foo"); + final ListSelectionModel selectionModel = list.getSelectionModel(); + selectionModel.setSelectionInterval(-1, -1); + Assert.assertFalse("For a 1-element model, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For a 1-element model, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For a 1-element model, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For a 1-element model, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); + selectionModel.setSelectionInterval(0, 0); + Assert.assertFalse("For a 1-element model, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For a 1-element model, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For a 1-element model, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For a 1-element model, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); + } + + /** Tests that the actions of a ListControlPanel for a two element model are disabled / enabled correctly. */ + @Test + public void testTwoElementListEnabled() { + model.add("foo"); model.add("bar"); + final ListSelectionModel selectionModel = list.getSelectionModel(); + selectionModel.setSelectionInterval(-1, -1); + Assert.assertFalse("For a 2-element model without selection, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For a 2-element model without selection, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For a 2-element model without selection, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For a 2-element model without selection, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); + selectionModel.setSelectionInterval(0, 0); + Assert.assertFalse("For a 2-element model with top selection, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For a 2-element model with top selection, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertTrue("For a 2-element model with top selection, down must be enabled.", testling.getMoveDown().isEnabled()); + Assert.assertTrue("For a 2-element model with top selection, bottom must be enabled.", testling.getMoveToBottom().isEnabled()); + selectionModel.setSelectionInterval(1, 1); + Assert.assertTrue("For a 2-element model with bottom selection, top must be enabled.", testling.getMoveToTop().isEnabled()); + Assert.assertTrue("For a 2-element model with bottom selection, up must be enabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For a 2-element model with bottom selection, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For a 2-element model with bottom selection, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); + } + + /** Tests that the actions of a ListControlPanel for a three element model are disabled / enabled correctly. */ + @Test + public void testThreeElementListEnabled() { + model.add("foo"); + model.add("bar"); model.add("buzz"); - final JList list = new JList(model); - final JFrame f = new JFrame("Test"); - final ListControlPanel testling = new ListControlPanel(list, model); - f.add(new JScrollPane(list)); - f.add(testling, BorderLayout.LINE_END); - f.pack(); + final ListSelectionModel selectionModel = list.getSelectionModel(); + selectionModel.setSelectionInterval(-1, -1); + Assert.assertFalse("For a 3-element model without selection, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For a 3-element model without selection, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For a 3-element model without selection, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For a 3-element model without selection, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); + selectionModel.setSelectionInterval(0, 0); + Assert.assertFalse("For a 3-element model with top selection, top must be disabled.", testling.getMoveToTop().isEnabled()); + Assert.assertFalse("For a 3-element model with top selection, up must be disabled.", testling.getMoveUp().isEnabled()); + Assert.assertTrue("For a 3-element model with top selection, down must be enabled.", testling.getMoveDown().isEnabled()); + Assert.assertTrue("For a 3-element model with top selection, bottom must be enabled.", testling.getMoveToBottom().isEnabled()); + selectionModel.setSelectionInterval(1, 1); + Assert.assertTrue("For a 3-element model with middle selection, top must be enabled.", testling.getMoveToTop().isEnabled()); + Assert.assertTrue("For a 3-element model with middle selection, up must be enabled.", testling.getMoveUp().isEnabled()); + Assert.assertTrue("For a 3-element model with middle selection, down must be enabled.", testling.getMoveDown().isEnabled()); + Assert.assertTrue("For a 3-element model with middle selection, bottom must be enabled.", testling.getMoveToBottom().isEnabled()); + selectionModel.setSelectionInterval(2, 2); + Assert.assertTrue("For a 3-element model with bottom selection, top must be enabled.", testling.getMoveToTop().isEnabled()); + Assert.assertTrue("For a 3-element model with bottom selection, up must be enabled.", testling.getMoveUp().isEnabled()); + Assert.assertFalse("For a 3-element model with bottom selection, down must be disabled.", testling.getMoveDown().isEnabled()); + Assert.assertFalse("For a 3-element model with bottom selection, bottom must be disabled.", testling.getMoveToBottom().isEnabled()); } } // class ListControlPanelTest This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |