01: package jimm.datavision.gui.cmd;
02:
03: import jimm.datavision.Selectable;
04: import jimm.datavision.source.Query;
05: import jimm.datavision.gui.SortWinListItem;
06: import jimm.util.I18N;
07: import java.util.Collection;
08: import java.util.ArrayList;
09: import java.util.Iterator;
10:
11: /**
12: * A command for changing the sort orders in a {@link Query}.
13: *
14: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
15: */
16: public class SortEditCommand extends CommandAdapter {
17:
18: protected static final int NO_CHANGE = 0;
19: protected static final int ONLY_SORTING_CHANGE = 1;
20: protected static final int DRASTIC_CHANGE = 2;
21:
22: protected Query query;
23: protected Collection newSortItems;
24: protected Collection oldSortItems;
25:
26: public SortEditCommand(Query query, Collection sortItems) {
27: super (I18N.get("SortEditCommand.name"));
28: this .query = query;
29: this .newSortItems = sortItems;
30:
31: // Create list of current sorts
32: oldSortItems = new ArrayList();
33: for (Iterator iter = query.sortedSelectables(); iter.hasNext();) {
34: Selectable g = (Selectable) iter.next();
35: oldSortItems.add(new SortWinListItem(g, query
36: .sortOrderOf(g)));
37: }
38: }
39:
40: public void perform() {
41: setSorts(newSortItems);
42: }
43:
44: public void undo() {
45: setSorts(oldSortItems);
46: }
47:
48: protected void setSorts(Collection itemList) {
49: query.clearSorts();
50: for (Iterator iter = itemList.iterator(); iter.hasNext();) {
51: SortWinListItem item = (SortWinListItem) iter.next();
52: query.addSort(item.getSelectable(),
53: item.sortsAscending() ? Query.SORT_ASCENDING
54: : Query.SORT_DESCENDING);
55: }
56: }
57:
58: }
|