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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.modeling.ui.modelBrowser.handlers;
\r
14 import java.lang.reflect.InvocationTargetException;
\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
38 public class StandardPasteHandler extends AbstractHandler implements IHandler {
\r
41 public Object execute(ExecutionEvent event) throws ExecutionException {
\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
49 Simantics.getSession().markUndoPoint();
\r
50 IRunnableWithProgress op = pasteResourceFromClipboard(handler);
\r
52 Shell shell = HandlerUtil.getActiveShell(event);
\r
53 new ProgressMonitorDialog(shell).run(true, true, op);
\r
55 } catch (InvocationTargetException e) {
\r
56 Throwable t = e.getCause();
\r
58 ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);
\r
60 ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);
\r
62 } catch (InterruptedException e) {
\r
63 ErrorLogger.defaultLogError(e);
\r
70 public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {
\r
72 IRunnableWithProgress op = new IRunnableWithProgress() {
\r
75 public void run(IProgressMonitor monitor) throws InvocationTargetException,
\r
76 InterruptedException {
\r
77 SubMonitor progress = SubMonitor.convert(monitor, 100);
\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
94 public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {
\r
96 handler.pasteFromClipboard(Simantics.getClipboard());
\r
97 } catch (DatabaseException e) {
\r
99 throw new InvocationTargetException(e);
\r
100 } catch (InvocationTargetException e1) {
\r
101 // TODO Auto-generated catch block
\r
102 e1.printStackTrace();
\r
104 e.printStackTrace();
\r
108 public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {
\r
111 return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));
\r
112 } catch (DatabaseException e) {
\r
113 Logger.defaultLogError(e);
\r