]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardPasteHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardPasteHandler.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardPasteHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardPasteHandler.java
new file mode 100644 (file)
index 0000000..f86c330
--- /dev/null
@@ -0,0 +1,118 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.modelBrowser.handlers;\r
+\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.IHandler;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.Adapter;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.PasteHandler;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+public class StandardPasteHandler extends AbstractHandler implements IHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+        final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();\r
+        final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class);\r
+       if (handler != null) {\r
+               \r
+               try {\r
+                       \r
+                   Simantics.getSession().markUndoPoint();\r
+                       IRunnableWithProgress op = pasteResourceFromClipboard(handler);\r
+                       \r
+                       Shell shell = HandlerUtil.getActiveShell(event);\r
+                       new ProgressMonitorDialog(shell).run(true, true, op);\r
+                       \r
+               } catch (InvocationTargetException e) {\r
+                       Throwable t = e.getCause();\r
+                       if (t != null) {\r
+                               ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);\r
+                       } else {\r
+                               ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);\r
+                       }\r
+               } catch (InterruptedException e) {\r
+                       ErrorLogger.defaultLogError(e);\r
+               }\r
+               \r
+        }\r
+       return null;\r
+    }\r
+    \r
+    public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {\r
+       \r
+       IRunnableWithProgress op = new IRunnableWithProgress() {\r
+\r
+                       @Override\r
+                       public void run(IProgressMonitor monitor) throws InvocationTargetException,\r
+                       InterruptedException {\r
+                               SubMonitor progress = SubMonitor.convert(monitor, 100);\r
+                               try {\r
+                                       progress.beginTask("Copying", 100);\r
+                                       progress.worked(50);\r
+                                       progress.subTask("Please wait..");\r
+                           handler.pasteFromClipboard(Simantics.getClipboard());\r
+                               } catch (Exception e) {\r
+                                       throw new InvocationTargetException(e);\r
+                               } finally {\r
+                                       monitor.done();\r
+                               }\r
+                       }\r
+               };\r
+               return op;\r
+       \r
+    }\r
+    \r
+    public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {\r
+       try {\r
+               handler.pasteFromClipboard(Simantics.getClipboard());\r
+               } catch (DatabaseException e) {\r
+                       try {\r
+                               throw new InvocationTargetException(e);\r
+                       } catch (InvocationTargetException e1) {\r
+                               // TODO Auto-generated catch block\r
+                               e1.printStackTrace();\r
+                       }\r
+                       e.printStackTrace();\r
+               }\r
+    }\r
+    \r
+    public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {\r
+    \r
+    try {\r
+        return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));\r
+    } catch (DatabaseException e) {\r
+        Logger.defaultLogError(e);\r
+        return null;\r
+    }\r
+    }\r
+}\r
+\r