Thursday, October 22, 2009

ADD A WAY TO SORT TO THE GUI









































Prev don't be afraid of buying books Next






























ADD A WAY TO SORT TO THE GUI






Add a View
menu to the GUI with an
option for each attribute that can be sorted on. Selecting one of
them results in the visible movie list being sorted by that
attribute.





The last task for the sorting story is to add
access to the new sorting capabilities to the Swing GUI.






Test 80.
Selecting "Sort by name" from the "View" menu causes the displayed
list to be sorted by name.










Test 81.
Selecting "Sort by rating" from the "View" menu causes the
displayed list to be sorted by rating.











Since this is conceptually different than our
previous GUI tests, and requires a slightly different fixture,
we'll create a new TestCase:




public class TestSwingMovieListEditorViewSorting extends TestCase {
private MovieList movieList;
private Movie starWars;
private Movie starTrek;
private Movie stargate;
private Movie theShining;
private Vector movies;
private JFrameOperator mainWindow;
private MovieListEditor editor;
private Vector sortedMovies;
privateJMenuBarOperator menubar;
private JMenuOperator viewMenu;

protected void setUp() throws Exception {
super.setUp();
SwingMovieListEditorView.start();
stargate = new Movie("Stargate", Category.SCIFI, -1);
theShining = new Movie("The Shining", Category.HORROR, 2);
starWars = new Movie("Star Wars", Category.SCIFI, 5);
starTrek = new Movie("Star Trek", Category.SCIFI, 3);








movies =new Vector();
movies.add(stargate);
movies.add(theShining);
movies.add(starWars);
movies.add(starTrek);

sortedMovies = new Vector();
sortedMovies.add(starTrek);
sortedMovies.add(starWars);
sortedMovies.add(stargate);
sortedMovies.add(theShining);

movieList = new MovieList();
movieList.add(stargate);
movieList.add(theShining);
movieList.add(starWars);
movieList.add(starTrek);

mainWindow = new JFrameOperator("Movie List");
editor = new MovieListEditor(movieList,
(SwingMovieListEditorView)mainWindow.getWindow());

menubar = new JMenuBarOperator(mainWindow);
viewMenu = new JMenuOperator(menubar, "View");
viewMenu.push();
}

protected void tearDown() throws Exception {
super.tearDown();
mainWindow.dispose();
}
}








Test 80: Selecting "Sort by name" from
the "View" menu causes the displayed list to be sorted by name




public void testSortingByName() {
JMenuItemOperator sortByNameItem =
new JMenuItemOperator(mainWindow,
new NameBasedChooser("sortByName"));
sortByNameItem.push();

JListOperator movieList =
new JListOperator(mainWindow,
new NameBasedChooser("movieList"));
ListModel listModel = movieList.getModel();
assertEquals("Movie list is the wrong size",sortedMovies.size(),
listModel.getSize());

for (int i = 0; i < sortedMovies.size(); i++) {
assertEquals("Movie list contains bad movie at index " +i,
sortedMovies.get(i),
listModel.getElementAt(i));
}
}







This fails with an error when looking for
the View menu. We need to add it
to the GUI:




private JMenuBar initMenuBar() {
//...
JMenu viewMenu = new JMenu("View");
menuBar.add(viewMenu);
viewMenu.add(initSortByNameItem());
return menuBar;
}

private JMenuItem initSortByNameItem() {
JMenuItem sortByNameItem = new JMenuItem("Sort by name");
sortByNameItem.setName("sortByName");
return sortByNameItem;
}






Now we see a failure due to the list not being
sorted after the Sort by name menu
item is selected. To deal with this, we need to add an action
listener which will forward the request to the associated
editor:




sortByNameItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myEditor.sortUsing(new MovieNameComparator());
}});






Green.





Test 81: Selecting "Sort by rating"
from the "View" menu causes the displayed list to be sorted by
rating



Now we do the same for Sort by rating. Here's the test:




protected void setUp() throws Exception {
//...
ratingSortedMovies = new Vector();
ratingSortedMovies.add(starWars);
ratingSortedMovies.add(starTrek);
ratingSortedMovies.add(theShining);
ratingSortedMovies.add(stargate);
//...
}

public void testSortingByRating() {
JMenuItemOperator sortByRatingItem =
new JMenuItemOperator(mainWindow,
new NameBasedChooser("sortByRating"));
sortByRatingItem.push();

JListOperator movieList =
new JListOperator(mainWindow,
new NameBasedChooser("movieList"));







ListModel listModel = movieList.getModel();
assertEquals("Movie list is the wrong size",
ratingSortedMovies.size(),
listModel.getSize());

for (int i = 0; i < ratingSortedMovies.size(); i++) {
assertEquals("Movie list contains bad movie at index " +i,
ratingSortedMovies.get(i),
listModel.getElementAt(i));
}
}






And here's the new menu item code to make it
pass (done in one bigger step):




private JMenuBar initMenuBar() {
//. . .
viewMenu.add(initSortByRatingItem());
//. . .
}

private JMenuItem initSortByRatingItem() {
JMenuItem sortByRatingItem = new JMenuItem("Sort by rating");
sortByRatingItem.setName("sortByRating");
sortByRatingItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myEditor.sortUsing(new MovieRatingComparator());
}});
return sortByRatingItem;
}


















































Amazon






No comments: