]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - workbench/internal/jcr/commands/GetNodeSize.java
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.commands
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
22 import javax
.jcr
.Node
;
24 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
25 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.SingleJcrNodeElem
;
26 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.WorkspaceElem
;
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
;
38 /** Compute an approximative size for the selected node(s) */
39 public class GetNodeSize
extends AbstractHandler
{
40 // private final static Log log = LogFactory.getLog(GetNodeSize.class);
42 public final static String ID
= WorkbenchUiPlugin
.PLUGIN_ID
+ ".getNodeSize";
44 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
46 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
47 .getActivePage().getSelection();
49 if (selection
!= null && !selection
.isEmpty()
50 && selection
instanceof IStructuredSelection
) {
54 Iterator
<?
> it
= ((IStructuredSelection
) selection
).iterator();
56 // TODO enhance this: as the size method is recursive, we keep track
57 // of nodes for which we already have computed size so that we don't
58 // count them twice. In a first approximation, we assume that the
59 // structure selection keep the nodes ordered.
60 List
<String
> importedPathes
= new ArrayList
<String
>();
62 nodesIt
: while (it
.hasNext()) {
63 Object obj
= it
.next();
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();
74 // non valid object type
77 Iterator
<String
> itPath
= importedPathes
.iterator();
78 while (itPath
.hasNext()) {
79 String refPath
= itPath
.next();
80 if (curPath
.startsWith(refPath
))
81 // Already done : skip node
84 size
+= JcrUtils
.getNodeApproxSize(node
);
85 importedPathes
.add(curPath
);
87 } catch (Exception e
) {
88 ErrorFeedback
.show("Cannot Get size of selected node ", e
);
91 String
[] labels
= { "OK" };
92 Shell shell
= HandlerUtil
.getActiveWorkbenchWindow(event
)
94 MessageDialog md
= new MessageDialog(shell
, "Node size", null,
95 "Node size is: " + size
/ 1024 + " KB",
96 MessageDialog
.INFORMATION
, labels
, 0);