1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.workbench.internal.jcr.commands;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import javax.jcr.Node;
23
24 import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
25 import org.argeo.cms.ui.jcr.model.WorkspaceElem;
26 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
27 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
28 import org.argeo.jcr.JcrUtils;
29 import org.eclipse.core.commands.AbstractHandler;
30 import org.eclipse.core.commands.ExecutionEvent;
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.handlers.HandlerUtil;
37
38
39 public class GetNodeSize extends AbstractHandler {
40
41
42 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".getNodeSize";
43
44 public Object execute(ExecutionEvent event) throws ExecutionException {
45
46 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
47 .getActivePage().getSelection();
48
49 if (selection != null && !selection.isEmpty()
50 && selection instanceof IStructuredSelection) {
51
52 long size = 0;
53
54 Iterator<?> it = ((IStructuredSelection) selection).iterator();
55
56
57
58
59
60 List<String> importedPathes = new ArrayList<String>();
61 try {
62 nodesIt: while (it.hasNext()) {
63 Object obj = it.next();
64 String curPath;
65 Node node;
66 if (obj instanceof SingleJcrNodeElem) {
67 node = ((SingleJcrNodeElem) obj).getNode();
68 curPath = node.getSession().getWorkspace().getName();
69 curPath += "/" + node.getPath();
70 } else if (obj instanceof WorkspaceElem) {
71 node = ((WorkspaceElem) obj).getRootNode();
72 curPath = node.getSession().getWorkspace().getName();
73 } else
74
75 continue nodesIt;
76
77 Iterator<String> itPath = importedPathes.iterator();
78 while (itPath.hasNext()) {
79 String refPath = itPath.next();
80 if (curPath.startsWith(refPath))
81
82 continue nodesIt;
83 }
84 size += JcrUtils.getNodeApproxSize(node);
85 importedPathes.add(curPath);
86 }
87 } catch (Exception e) {
88 ErrorFeedback.show("Cannot Get size of selected node ", e);
89 }
90
91 String[] labels = { "OK" };
92 Shell shell = HandlerUtil.getActiveWorkbenchWindow(event)
93 .getShell();
94 MessageDialog md = new MessageDialog(shell, "Node size", null,
95 "Node size is: " + size / 1024 + " KB",
96 MessageDialog.INFORMATION, labels, 0);
97 md.open();
98 }
99 return null;
100 }
101 }