]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardPasteHandler.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardPasteHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.modeling.ui.modelBrowser.handlers;\r
12 \r
13 \r
14 import java.lang.reflect.InvocationTargetException;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.core.commands.IHandler;\r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.eclipse.core.runtime.SubMonitor;\r
22 import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
23 import org.eclipse.jface.operation.IRunnableWithProgress;\r
24 import org.eclipse.jface.viewers.ISelection;\r
25 import org.eclipse.swt.widgets.Shell;\r
26 import org.eclipse.ui.PlatformUI;\r
27 import org.eclipse.ui.handlers.HandlerUtil;\r
28 import org.simantics.Simantics;\r
29 import org.simantics.db.Resource;\r
30 import org.simantics.db.common.primitiverequest.Adapter;\r
31 import org.simantics.db.common.utils.Logger;\r
32 import org.simantics.db.exception.DatabaseException;\r
33 import org.simantics.db.layer0.adapter.PasteHandler;\r
34 import org.simantics.ui.SimanticsUI;\r
35 import org.simantics.utils.ui.ErrorLogger;\r
36 import org.simantics.utils.ui.ExceptionUtils;\r
37 \r
38 public class StandardPasteHandler extends AbstractHandler implements IHandler {\r
39 \r
40     @Override\r
41     public Object execute(ExecutionEvent event) throws ExecutionException {\r
42 \r
43         final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();\r
44         final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class);\r
45         if (handler != null) {\r
46                 \r
47                 try {\r
48                         \r
49                     Simantics.getSession().markUndoPoint();\r
50                         IRunnableWithProgress op = pasteResourceFromClipboard(handler);\r
51                         \r
52                         Shell shell = HandlerUtil.getActiveShell(event);\r
53                         new ProgressMonitorDialog(shell).run(true, true, op);\r
54                         \r
55                 } catch (InvocationTargetException e) {\r
56                         Throwable t = e.getCause();\r
57                         if (t != null) {\r
58                                 ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);\r
59                         } else {\r
60                                 ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);\r
61                         }\r
62                 } catch (InterruptedException e) {\r
63                         ErrorLogger.defaultLogError(e);\r
64                 }\r
65                 \r
66         }\r
67         return null;\r
68     }\r
69     \r
70     public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {\r
71         \r
72         IRunnableWithProgress op = new IRunnableWithProgress() {\r
73 \r
74                         @Override\r
75                         public void run(IProgressMonitor monitor) throws InvocationTargetException,\r
76                         InterruptedException {\r
77                                 SubMonitor progress = SubMonitor.convert(monitor, 100);\r
78                                 try {\r
79                                         progress.beginTask("Copying", 100);\r
80                                         progress.worked(50);\r
81                                         progress.subTask("Please wait..");\r
82                             handler.pasteFromClipboard(Simantics.getClipboard());\r
83                                 } catch (Exception e) {\r
84                                         throw new InvocationTargetException(e);\r
85                                 } finally {\r
86                                         monitor.done();\r
87                                 }\r
88                         }\r
89                 };\r
90                 return op;\r
91         \r
92     }\r
93     \r
94     public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {\r
95         try {\r
96                 handler.pasteFromClipboard(Simantics.getClipboard());\r
97                 } catch (DatabaseException e) {\r
98                         try {\r
99                                 throw new InvocationTargetException(e);\r
100                         } catch (InvocationTargetException e1) {\r
101                                 e1.getCause().printStackTrace();\r
102                         }\r
103                         e.printStackTrace();\r
104                 }\r
105     }\r
106     \r
107     public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {\r
108     \r
109     try {\r
110         return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));\r
111     } catch (DatabaseException e) {\r
112         Logger.defaultLogError(e);\r
113         return null;\r
114     }\r
115     }\r
116 }\r
117 \r