View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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.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  /** 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);
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  			// 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>();
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  						// non valid object type
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  							// Already done : skip node
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 }