]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/GetNodeSize.java
remove typo in comments
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / GetNodeSize.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr.ui.explorer.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.eclipse.ui.ErrorFeedback;
25 import org.argeo.jcr.JcrUtils;
26 import org.argeo.jcr.ui.explorer.JcrExplorerPlugin;
27 import org.argeo.jcr.ui.explorer.model.SingleJcrNode;
28 import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
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 /** Opens the generic node editor. */
39 public class GetNodeSize extends AbstractHandler {
40 // private final static Log log = LogFactory.getLog(GetNodeSize.class);
41
42 public final static String ID = JcrExplorerPlugin.ID + ".getNodeSize";
43
44 // public final static String DEFAULT_ICON_REL_PATH = "icons/getSize.gif";
45 // public final static String DEFAULT_LABEL = JcrExplorerPlugin
46 // .getMessage("getNodeSizeCmdLbl");
47
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49 // JcrUtils.getRepositoryByAlias(repositoryRegister,
50 // ArgeoJcrConstants.ALIAS_NODE);
51
52 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
53 .getActivePage().getSelection();
54
55 if (selection != null && !selection.isEmpty()
56 && selection instanceof IStructuredSelection) {
57
58 // IStructuredSelection iss = (IStructuredSelection) selection;
59 // if (iss.size() > 1)
60 // ErrorFeedback.show(JcrExplorerPlugin
61 // .getMessage("warningInvalidMultipleSelection"), null);
62
63 long size = 0;
64
65 Iterator<?> it = ((IStructuredSelection) selection).iterator();
66
67 // as the size method is recursive, we keep track of nodes for which
68 // we already have computed size so that we don't count them twice.
69 // In a first approximation, we assume that the structure selection
70 // keep the nodes ordered.
71 // TODO : enhance that.
72 List<String> importedPathes = new ArrayList<String>();
73 try {
74 nodesIt: while (it.hasNext()) {
75 Object obj = it.next();
76 String curPath;
77 Node node;
78 if (obj instanceof SingleJcrNode) {
79 node = ((SingleJcrNode) obj).getNode();
80 curPath = node.getSession().getWorkspace().getName();
81 curPath += "/" + node.getPath();
82 } else if (obj instanceof WorkspaceNode) {
83 node = ((WorkspaceNode) obj).getRootNode();
84 curPath = node.getSession().getWorkspace().getName();
85 } else
86 // unvalid object type
87 continue nodesIt;
88
89 Iterator<String> itPath = importedPathes.iterator();
90 while (itPath.hasNext()) {
91 String refPath = itPath.next();
92 if (curPath.startsWith(refPath))
93 // Already done : skip node
94 continue nodesIt;
95 }
96 size += JcrUtils.getNodeApproxSize(node);
97 importedPathes.add(curPath);
98 }
99 } catch (Exception e) {
100 ErrorFeedback.show("Cannot Get size of selected node ", e);
101 }
102
103 String[] labels = { "OK" };
104 Shell shell = HandlerUtil.getActiveWorkbenchWindow(event)
105 .getShell();
106 MessageDialog md = new MessageDialog(shell, "Node size", null,
107 "Node size is: " + size / 1024 + " KB",
108 MessageDialog.INFORMATION, labels, 0);
109 md.open();
110 }
111 return null;
112 }
113 }