1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.modeling.ui.modelBrowser.handlers;
14 import java.util.HashSet;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.handlers.HandlerUtil;
24 import org.simantics.Simantics;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.common.request.ReadRequest;
28 import org.simantics.db.common.utils.Logger;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.adapter.CopyHandler;
31 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
32 import org.simantics.ui.utils.ResourceAdaptionUtils;
33 import org.simantics.utils.ui.workbench.WorkbenchUtils;
35 public class StandardCutHandler extends AbstractHandler {
37 private static IStatusLineManager status;
40 public Object execute(ExecutionEvent event) throws ExecutionException {
41 status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
42 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
43 final Resource[] rs = ResourceAdaptionUtils.toResources( selection );
45 setStatus("Nothing to cut.");
50 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
52 Simantics.getSession().syncRequest(new ReadRequest() {
54 public void run(ReadGraph graph) throws DatabaseException {
55 Set<Resource> unique = new HashSet<Resource>();
56 for (Resource r : rs) {
59 CopyHandler handler = graph.adapt(r, CopyHandler.class);
60 handler.cutToClipboard(graph, builder);
65 Simantics.setClipboard(builder);
66 setCutMessage(builder.getContents().size(), "resource");
68 } catch (DatabaseException e) {
69 Logger.defaultLogError(e);
75 private static void setCutMessage(int count, String elementName) {
77 setStatus("Cut " + count + " " + elementName + "s to clipboard");
79 setStatus("Cut " + elementName + " to clipboard");
81 setStatus("Nothing to cut.");
84 private static void setStatus(String message) {
86 status.setMessage(message);
89 public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) {
92 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
93 Simantics.getSession().syncRequest(new ReadRequest() {
95 public void run(ReadGraph graph) throws DatabaseException {
96 for (Resource r : rs) {
97 CopyHandler handler = graph.adapt(r, CopyHandler.class);
98 handler.cutToClipboard(graph, builder);
102 Simantics.setClipboard(builder);
103 setCutMessage(builder.getContents().size(), "resource");
104 } catch (DatabaseException e) {
105 Logger.defaultLogError(e);