001: package jimm.datavision.gui.cmd;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.Group;
005: import jimm.datavision.gui.Designer;
006: import jimm.datavision.gui.GroupWinListItem;
007: import jimm.util.I18N;
008: import java.util.Collection;
009: import java.util.ArrayList;
010: import java.util.Iterator;
011:
012: /**
013: * A command for changing a report's {@link Group}s.
014: *
015: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
016: */
017: public class GroupEditCommand extends CommandAdapter {
018:
019: protected static final int NO_CHANGE = 0;
020: protected static final int ONLY_SORTING_CHANGE = 1;
021: protected static final int DRASTIC_CHANGE = 2;
022:
023: protected Report report;
024: protected Designer designer;
025: protected Collection newGroupItems;
026: protected Collection oldGroupItems;
027:
028: public GroupEditCommand(Report r, Designer designer,
029: Collection groupItems) {
030: super (I18N.get("GroupEditCommand.name"));
031: report = r;
032: this .designer = designer;
033: this .newGroupItems = groupItems;
034:
035: // Create list of current groups
036: oldGroupItems = new ArrayList();
037: for (Iterator iter = report.groups(); iter.hasNext();) {
038: Group group = (Group) iter.next();
039: oldGroupItems.add(new GroupWinListItem(group
040: .getSelectable(), group));
041: }
042:
043: }
044:
045: public void perform() {
046: setGroups(oldGroupItems, newGroupItems);
047: }
048:
049: public void undo() {
050: setGroups(newGroupItems, oldGroupItems);
051: }
052:
053: protected void setGroups(Collection fromList, Collection toList) {
054: switch (whatChanged(fromList, toList)) {
055: case ONLY_SORTING_CHANGE:
056: for (Iterator iter = toList.iterator(); iter.hasNext();) {
057: GroupWinListItem item = (GroupWinListItem) iter.next();
058: item.getGroup().setSortOrder(item.getSortOrder());
059: }
060: break;
061: case DRASTIC_CHANGE:
062: report.removeAllGroups();
063: for (Iterator iter = toList.iterator(); iter.hasNext();) {
064: GroupWinListItem item = (GroupWinListItem) iter.next();
065: Group g = item.getGroup();
066: if (g == null)
067: g = Group.create(report, item.getSelectable());
068: g.setSortOrder(item.getSortOrder());
069: report.addGroup(g);
070: }
071: designer.rebuildGroups();
072: break;
073: case NO_CHANGE:
074: break;
075: }
076: }
077:
078: /**
079: * Determines the severity of the difference between the report and what we
080: * have now: <code>NO_CHANGE</code>, <code>ONLY_SORTING_CHANGE</code>, or
081: * <code>DRASTIC_CHANGE</code>.
082: *
083: * @param fromList the list of groups before the pending change
084: * @param toList the list of groups after the pending change
085: * @return one of <code>NO_CHANGE</code>, <code>ONLY_SORTING_CHANGE</code>,
086: * or <code>DRASTIC_CHANGE</code>
087: */
088: protected int whatChanged(Collection fromList, Collection toList) {
089: if (fromList.size() != toList.size())
090: return DRASTIC_CHANGE;
091:
092: int change = NO_CHANGE;
093: Iterator fromIter = fromList.iterator();
094: Iterator toIter = toList.iterator();
095: while (fromIter.hasNext()) {
096: GroupWinListItem fromItem = (GroupWinListItem) fromIter
097: .next();
098: GroupWinListItem toItem = (GroupWinListItem) toIter.next();
099: if (fromItem.getGroup() != toItem.getGroup())
100: return DRASTIC_CHANGE;
101: if (fromItem.sortsAscending() != toItem.sortsAscending())
102: change = ONLY_SORTING_CHANGE;
103: }
104: return change; // Either NO_CHANGE or ONLY_SORTING_CHANGE
105: }
106:
107: }
|