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:
022: import javax.swing.AbstractAction;
023: import javax.swing.JOptionPane;
024: import javax.swing.tree.DefaultMutableTreeNode;
025:
026: import org.w3c.dom.Element;
027: import org.w3c.dom.Node;
028: import org.w3c.dom.NodeList;
029:
030: import de.finix.contelligent.client.base.ComponentNotFoundException;
031: import de.finix.contelligent.client.base.ComponentReference;
032: import de.finix.contelligent.client.base.ContelligentComponent;
033: import de.finix.contelligent.client.gui.ContelligentAction;
034: import de.finix.contelligent.client.i18n.Resources;
035: import de.finix.contelligent.client.remote.ActionResult;
036: import de.finix.contelligent.client.remote.Actions;
037: import de.finix.contelligent.client.remote.RemoteActionException;
038: import de.finix.contelligent.client.util.ExceptionDialog;
039: import de.finix.contelligent.client.util.xml.XMLUtil;
040:
041: public class SubtreeSizeAction extends AbstractAction implements
042: ContelligentAction {
043: /**
044: *
045: */
046: private final ExplorerEditor editor;
047:
048: public SubtreeSizeAction(ExplorerEditor editor) {
049: super ("subtree_size_action", Resources.defaultViewIcon);
050: this .editor = editor;
051: putValue(ROLLOVER_ICON, Resources.defaultViewIcon);
052: putValue(TYPE, PUSH_ACTION);
053: putValue(ACTION_TYPE, FILE_ACTION);
054: putValue(ACTION_GROUP, FILE_COMPONENT_GROUP);
055: putValue(ACTION_POS, FILE_COMPONENT_INFO);
056: putValue(MENU_TARGET, MENU);
057: // putValue(BUTTON_TARGET, TOOLBAR);
058: putValue(POPUP_TARGET, POPUP_MENU);
059: }
060:
061: public void actionPerformed(ActionEvent e) {
062: /*
063: * componentToCopy = getSelectedComponent(); TransferHandler th =
064: * tree.getTransferHandler(); Clipboard clipboard = getClipboard(tree);
065: * if ((clipboard != null) && (th != null)) { th.exportToClipboard(tree,
066: * clipboard, DnDConstants.ACTION_COPY); }
067: */
068: DefaultMutableTreeNode node = (DefaultMutableTreeNode) this .editor.tree
069: .getLastSelectedPathComponent();
070: if (node != null
071: && node.getUserObject() instanceof ComponentReference) {
072: try {
073: ContelligentComponent component = ((ComponentReference) node
074: .getUserObject()).getComponent();
075: try {
076: ActionResult response = Actions
077: .subtreeSize(component.getPath());
078: if (response.hasError()) {
079: response.showErrors();
080: } else {
081: Element rootElement = response.getContent();
082: NodeList childNodes = rootElement
083: .getChildNodes();
084: long components = 0;
085: long size = 0;
086: for (int i = 0; i < childNodes.getLength(); i++) {
087: Node childElement = (Node) childNodes
088: .item(i);
089: if (childElement.getNodeName().equals(
090: "components")) {
091: String content = XMLUtil
092: .getContent(childElement);
093: components = Long.parseLong(content);
094: } else if (childElement.getNodeName()
095: .equals("size")) {
096: String content = XMLUtil
097: .getContent(childElement);
098: size = Long.parseLong(content);
099: }
100: }
101: String sizeText;
102: if (size > 1024 * 1024) {
103: double mb = Math
104: .floor(((double) size / (1024 * 1024)) * 100) / 100;
105: sizeText = String.valueOf(mb) + " MB";
106: } else if (size > 1024) {
107: double kb = Math
108: .floor(((double) size / 1024) * 100) / 100;
109: sizeText = String.valueOf(kb) + " kB";
110: } else {
111: sizeText = String.valueOf(size) + " B";
112: }
113: JOptionPane
114: .showOptionDialog(
115: this .editor.getParent(),
116: Resources
117: .getLocalString(
118: "subtree_size_dialog",
119: new String[] {
120: String
121: .valueOf(components),
122: sizeText }),
123: Resources
124: .getLocalString("subtree_size_dialog_title"),
125: JOptionPane.OK_OPTION,
126: JOptionPane.INFORMATION_MESSAGE,
127: null,
128: ExplorerEditor.OK_OPTIONS,
129: ExplorerEditor.OK_OPTIONS[0]);
130: }
131: } catch (RemoteActionException rae) {
132: ExceptionDialog.show(rae);
133: }
134: } catch (ComponentNotFoundException cnfe) {
135: ExceptionDialog.show(cnfe);
136: }
137: }
138: }
139: }
|