]> git.argeo.org Git - gpl/argeo-slc.git/blob - commands/GetNodeSize.java
Prepare next development cycle
[gpl/argeo-slc.git] / commands / GetNodeSize.java
1 package org.argeo.cms.ui.workbench.internal.jcr.commands;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import javax.jcr.Node;
8
9 import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
10 import org.argeo.cms.ui.jcr.model.WorkspaceElem;
11 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
12 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
13 import org.argeo.jcr.JcrUtils;
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 /** Compute an approximative size for the selected node(s) */
24 public class GetNodeSize extends AbstractHandler {
25 // private final static Log log = LogFactory.getLog(GetNodeSize.class);
26
27 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".getNodeSize";
28
29 public Object execute(ExecutionEvent event) throws ExecutionException {
30
31 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
32 .getActivePage().getSelection();
33
34 if (selection != null && !selection.isEmpty()
35 && selection instanceof IStructuredSelection) {
36
37 long size = 0;
38
39 Iterator<?> it = ((IStructuredSelection) selection).iterator();
40
41 // TODO enhance this: as the size method is recursive, we keep track
42 // of nodes for which we already have computed size so that we don't
43 // count them twice. In a first approximation, we assume that the
44 // structure selection keep the nodes ordered.
45 List<String> importedPathes = new ArrayList<String>();
46 try {
47 nodesIt: while (it.hasNext()) {
48 Object obj = it.next();
49 String curPath;
50 Node node;
51 if (obj instanceof SingleJcrNodeElem) {
52 node = ((SingleJcrNodeElem) obj).getNode();
53 curPath = node.getSession().getWorkspace().getName();
54 curPath += "/" + node.getPath();
55 } else if (obj instanceof WorkspaceElem) {
56 node = ((WorkspaceElem) obj).getRootNode();
57 curPath = node.getSession().getWorkspace().getName();
58 } else
59 // non valid object type
60 continue nodesIt;
61
62 Iterator<String> itPath = importedPathes.iterator();
63 while (itPath.hasNext()) {
64 String refPath = itPath.next();
65 if (curPath.startsWith(refPath))
66 // Already done : skip node
67 continue nodesIt;
68 }
69 size += JcrUtils.getNodeApproxSize(node);
70 importedPathes.add(curPath);
71 }
72 } catch (Exception e) {
73 ErrorFeedback.show("Cannot Get size of selected node ", e);
74 }
75
76 String[] labels = { "OK" };
77 Shell shell = HandlerUtil.getActiveWorkbenchWindow(event)
78 .getShell();
79 MessageDialog md = new MessageDialog(shell, "Node size", null,
80 "Node size is: " + size / 1024 + " KB",
81 MessageDialog.INFORMATION, labels, 0);
82 md.open();
83 }
84 return null;
85 }
86 }