]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCutHandler.java
Fixed StandardCutHandler IllegalThreadAccess problem
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardCutHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *     Semantum Oy - gitlab simantics/platform#133
11  *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.handlers;
13
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.SubMonitor;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.jface.action.IStatusLineManager;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.handlers.HandlerUtil;
30 import org.simantics.Simantics;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.common.request.ReadRequest;
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.modeling.ui.Activator;
38 import org.simantics.ui.utils.ResourceAdaptionUtils;
39 import org.simantics.utils.ui.SWTUtils;
40 import org.simantics.utils.ui.workbench.WorkbenchUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class StandardCutHandler extends AbstractHandler {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(StandardCutHandler.class);
47
48     private static IStatusLineManager status;
49
50     @Override
51     public Object execute(ExecutionEvent event) throws ExecutionException {
52         status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
53         ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
54         final Resource[] rs = ResourceAdaptionUtils.toResources( selection );
55         if (rs == null) {
56             setStatus("Nothing to cut.");
57            return null;
58         }
59         Job job = new Job("Cut resources") {
60
61             @Override
62             protected IStatus run(IProgressMonitor monitor) {
63                 monitor.beginTask("Cut resources", rs.length);
64                 try {
65                     final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
66
67                     Simantics.getSession().syncRequest(new ReadRequest() {
68                         @Override
69                         public void run(ReadGraph graph) throws DatabaseException {
70                             Set<Resource> unique = new HashSet<>();
71                             for (Resource r : rs) {
72                                 if (!unique.add(r))
73                                     continue;
74                                 CopyHandler handler = graph.adapt(r, CopyHandler.class);
75                                 handler.cutToClipboard(graph, builder, SubMonitor.convert(monitor, 1));
76                             }
77                         }
78                     });
79
80                     Simantics.setClipboard(builder);
81                     setCutMessage(builder.getContents().size(), "resource");
82                     return Status.OK_STATUS;
83                 } catch (DatabaseException e) {
84                     LOGGER.error("Cut operation failed", e); //$NON-NLS-1$
85                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cut operation failed", e);
86                 }
87             }
88         };
89         job.setUser(true);
90         job.schedule();
91
92         return null;
93     }
94
95     private static void setCutMessage(int count, String elementName) {
96         if (count > 1)
97             setStatus("Cut " + count + " " + elementName + "s to clipboard");
98         else if (count == 1)
99             setStatus("Cut " + elementName + " to clipboard");
100         else
101             setStatus("Nothing to cut.");
102     }
103
104     private static void setStatus(String message) {
105         if (status != null) {
106             SWTUtils.asyncExec(
107                     PlatformUI.getWorkbench().getDisplay(),
108                     () -> status.setMessage(message));
109         }
110     }
111
112     public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) {
113         try {
114             final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
115             Simantics.getSession().syncRequest(new ReadRequest() {
116                 @Override
117                 public void run(ReadGraph graph) throws DatabaseException {
118                     for (Resource r : rs) {
119                         CopyHandler handler = graph.adapt(r, CopyHandler.class);
120                         handler.cutToClipboard(graph, builder, null);
121                     }
122                 }
123             });
124             Simantics.setClipboard(builder);
125             setCutMessage(builder.getContents().size(), "resource");
126         } catch (DatabaseException e) {
127             LOGGER.error("Failed to cut {} resources to clipboard: {}", rs.length, Arrays.toString(rs), e); //$NON-NLS-1$
128         }
129
130         return null;
131     }
132
133 }