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.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.SubMonitor;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.jface.action.IStatusLineManager;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.handlers.HandlerUtil;
29 import org.simantics.Simantics;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.common.request.ReadRequest;
33 import org.simantics.db.common.utils.Logger;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.adapter.CopyHandler;
36 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
37 import org.simantics.ui.utils.ResourceAdaptionUtils;
38 import org.simantics.utils.ui.workbench.WorkbenchUtils;
40 public class StandardCutHandler extends AbstractHandler {
42 private static IStatusLineManager status;
45 public Object execute(ExecutionEvent event) throws ExecutionException {
46 status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
47 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
48 final Resource[] rs = ResourceAdaptionUtils.toResources( selection );
50 setStatus("Nothing to cut.");
53 Job job = new Job("Cut resources") {
56 protected IStatus run(IProgressMonitor monitor) {
57 monitor.beginTask("Cut resources", rs.length);
59 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
61 Simantics.getSession().syncRequest(new ReadRequest() {
63 public void run(ReadGraph graph) throws DatabaseException {
64 Set<Resource> unique = new HashSet<Resource>();
65 for (Resource r : rs) {
68 CopyHandler handler = graph.adapt(r, CopyHandler.class);
69 handler.cutToClipboard(graph, builder, SubMonitor.convert(monitor, 1));
74 Simantics.setClipboard(builder);
75 setCutMessage(builder.getContents().size(), "resource");
77 } catch (DatabaseException e) {
78 Logger.defaultLogError(e);
80 return Status.OK_STATUS;
89 private static void setCutMessage(int count, String elementName) {
91 setStatus("Cut " + count + " " + elementName + "s to clipboard");
93 setStatus("Cut " + elementName + " to clipboard");
95 setStatus("Nothing to cut.");
98 private static void setStatus(String message) {
100 status.setMessage(message);
103 public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) {
106 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
107 Simantics.getSession().syncRequest(new ReadRequest() {
109 public void run(ReadGraph graph) throws DatabaseException {
110 for (Resource r : rs) {
111 CopyHandler handler = graph.adapt(r, CopyHandler.class);
112 handler.cutToClipboard(graph, builder, null);
116 Simantics.setClipboard(builder);
117 setCutMessage(builder.getContents().size(), "resource");
118 } catch (DatabaseException e) {
119 Logger.defaultLogError(e);