001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.explorer;
019:
020: import java.awt.event.ActionEvent;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.Enumeration;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Vector;
027:
028: import javax.swing.AbstractAction;
029: import javax.swing.tree.DefaultMutableTreeNode;
030: import javax.swing.tree.DefaultTreeModel;
031: import javax.swing.tree.TreePath;
032:
033: import de.finix.contelligent.client.base.ComponentReference;
034: import de.finix.contelligent.client.gui.ContelligentAction;
035: import de.finix.contelligent.client.i18n.Resources;
036: import de.finix.contelligent.client.util.ExceptionDialog;
037:
038: public class SortAscendAction extends AbstractAction implements
039: ContelligentAction {
040: /**
041: *
042: */
043: private final ExplorerEditor editor;
044:
045: public SortAscendAction(ExplorerEditor editor) {
046: super ("sort_ascending_action", Resources.sortAscendIcon);
047: this .editor = editor;
048: putValue(ROLLOVER_ICON, Resources.sortAscendIconRollOver);
049: putValue(TYPE, PUSH_ACTION);
050: putValue(ACTION_TYPE, VIEW_ACTION);
051: putValue(ACTION_GROUP, VIEW_SORT_GROUP);
052: putValue(ACTION_POS, VIEW_SORT_ASCEND);
053: putValue(MENU_TARGET, MENU);
054: putValue(BUTTON_TARGET, TOOLBAR);
055: }
056:
057: public void actionPerformed(ActionEvent e) {
058: DefaultMutableTreeNode node = (DefaultMutableTreeNode) this .editor.tree
059: .getLastSelectedPathComponent();
060: if (node != null
061: && node.getUserObject() instanceof ComponentReference) {
062: DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node
063: .getParent();
064: if (parent != null) {
065: try {
066: TreePath selectionPath = this .editor.tree
067: .getSelectionPath();
068: List children = new Vector();
069: for (Enumeration i = parent.children(); i
070: .hasMoreElements();) {
071: children.add(i.nextElement());
072: }
073: Collections.sort(children, new Comparator() {
074: public int compare(Object o1, Object o2) {
075: int nav = SortAscendAction.this .editor
076: .compareNavigation(o1, o2);
077: if (nav == 0) {
078: String s1 = o1.toString();
079: String s2 = o2.toString();
080: return (-s1.compareTo(s2));
081: } else {
082: return nav;
083: }
084: }
085:
086: public boolean equals(Object obj) {
087: if (obj == this )
088: return true;
089: return false;
090: }
091:
092: public int hashCode() {
093: return -1;
094: }
095:
096: });
097: // remove all children from this node
098: parent.removeAllChildren();
099: // add children in given order
100: for (Iterator i = children.iterator(); i.hasNext();) {
101: parent.add((DefaultMutableTreeNode) i.next());
102: }
103: ((DefaultTreeModel) this .editor.tree.getModel())
104: .reload(parent);
105: this .editor.tree.setSelectionPath(selectionPath);
106: this .editor.tree.validate();
107: } catch (RuntimeException re) {
108: ExceptionDialog.show(re);
109: }
110: }
111: }
112: }
113:
114: }
|