]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardPasteHandler.java
Fixed StandardCutHandler IllegalThreadAccess problem
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardPasteHandler.java
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.modeling.ui.modelBrowser.handlers;
12
13
14 import java.lang.reflect.InvocationTargetException;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.IHandler;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.simantics.Simantics;
29 import org.simantics.db.Resource;
30 import org.simantics.db.common.primitiverequest.Adapter;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.adapter.PasteHandler;
33 import org.simantics.ui.SimanticsUI;
34 import org.simantics.utils.ui.ErrorLogger;
35 import org.simantics.utils.ui.ExceptionUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class StandardPasteHandler extends AbstractHandler implements IHandler {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(StandardPasteHandler.class);
42
43     @Override
44     public Object execute(ExecutionEvent event) throws ExecutionException {
45
46         final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
47         final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class);
48         if (handler != null) {
49                 
50                 try {
51                         
52                     Simantics.getSession().markUndoPoint();
53                         IRunnableWithProgress op = pasteResourceFromClipboard(handler);
54                         
55                         Shell shell = HandlerUtil.getActiveShell(event);
56                         new ProgressMonitorDialog(shell).run(true, true, op);
57                         
58                 } catch (InvocationTargetException e) {
59                         Throwable t = e.getCause();
60                         if (t != null) {
61                                 ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);
62                         } else {
63                                 ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);
64                         }
65                 } catch (InterruptedException e) {
66                         ErrorLogger.defaultLogError(e);
67                 }
68                 
69         }
70         return null;
71     }
72     
73     public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {
74         
75         IRunnableWithProgress op = new IRunnableWithProgress() {
76
77                         @Override
78                         public void run(IProgressMonitor monitor) throws InvocationTargetException,
79                         InterruptedException {
80                                 SubMonitor progress = SubMonitor.convert(monitor, 100);
81                                 try {
82                                         progress.beginTask("Copying", 100);
83                                         progress.worked(50);
84                                         progress.subTask("Please wait..");
85                             handler.pasteFromClipboard(Simantics.getClipboard());
86                                 } catch (Exception e) {
87                                         throw new InvocationTargetException(e);
88                                 } finally {
89                                         monitor.done();
90                                 }
91                         }
92                 };
93                 return op;
94         
95     }
96
97     public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {
98         try {
99             handler.pasteFromClipboard(Simantics.getClipboard());
100         } catch (DatabaseException e) {
101             LOGGER.error("Failed to paste resource from clipboard with handler {}", handler, e); //$NON-NLS-1$
102         }
103     }
104
105     public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {
106         try {
107             return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));
108         } catch (DatabaseException e) {
109             LOGGER.error("Failed to get paste handler from resource {}", resource, e); //$NON-NLS-1$
110             return null;
111         }
112     }
113
114 }