]>
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
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
.jcr
.ui
.explorer
.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
.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
.SingleJcrNodeElem
;
28 import org
.argeo
.jcr
.ui
.explorer
.model
.WorkspaceElem
;
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 /** Opens the generic node editor. */
39 public class GetNodeSize
extends AbstractHandler
{
40 // private final static Log log = LogFactory.getLog(GetNodeSize.class);
42 public final static String ID
= JcrExplorerPlugin
.ID
+ ".getNodeSize";
44 // public final static String DEFAULT_ICON_REL_PATH = "icons/getSize.gif";
45 // public final static String DEFAULT_LABEL = JcrExplorerPlugin
46 // .getMessage("getNodeSizeCmdLbl");
48 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
49 // JcrUtils.getRepositoryByAlias(repositoryRegister,
50 // ArgeoJcrConstants.ALIAS_NODE);
52 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
53 .getActivePage().getSelection();
55 if (selection
!= null && !selection
.isEmpty()
56 && selection
instanceof IStructuredSelection
) {
58 // IStructuredSelection iss = (IStructuredSelection) selection;
59 // if (iss.size() > 1)
60 // ErrorFeedback.show(JcrExplorerPlugin
61 // .getMessage("warningInvalidMultipleSelection"), null);
65 Iterator
<?
> it
= ((IStructuredSelection
) selection
).iterator();
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
>();
74 nodesIt
: while (it
.hasNext()) {
75 Object obj
= it
.next();
78 if (obj
instanceof SingleJcrNodeElem
) {
79 node
= ((SingleJcrNodeElem
) obj
).getNode();
80 curPath
= node
.getSession().getWorkspace().getName();
81 curPath
+= "/" + node
.getPath();
82 } else if (obj
instanceof WorkspaceElem
) {
83 node
= ((WorkspaceElem
) obj
).getRootNode();
84 curPath
= node
.getSession().getWorkspace().getName();
86 // unvalid object type
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
96 size
+= JcrUtils
.getNodeApproxSize(node
);
97 importedPathes
.add(curPath
);
99 } catch (Exception e
) {
100 ErrorFeedback
.show("Cannot Get size of selected node ", e
);
103 String
[] labels
= { "OK" };
104 Shell shell
= HandlerUtil
.getActiveWorkbenchWindow(event
)
106 MessageDialog md
= new MessageDialog(shell
, "Node size", null,
107 "Node size is: " + size
/ 1024 + " KB",
108 MessageDialog
.INFORMATION
, labels
, 0);