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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.modeling.ui.modelBrowser.handlers;
14 import java.lang.reflect.InvocationTargetException;
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.common.utils.Logger;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.adapter.PasteHandler;
34 import org.simantics.ui.SimanticsUI;
35 import org.simantics.utils.ui.ErrorLogger;
36 import org.simantics.utils.ui.ExceptionUtils;
38 public class StandardPasteHandler extends AbstractHandler implements IHandler {
41 public Object execute(ExecutionEvent event) throws ExecutionException {
43 final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
44 final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class);
45 if (handler != null) {
49 Simantics.getSession().markUndoPoint();
50 IRunnableWithProgress op = pasteResourceFromClipboard(handler);
52 Shell shell = HandlerUtil.getActiveShell(event);
53 new ProgressMonitorDialog(shell).run(true, true, op);
55 } catch (InvocationTargetException e) {
56 Throwable t = e.getCause();
58 ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);
60 ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);
62 } catch (InterruptedException e) {
63 ErrorLogger.defaultLogError(e);
70 public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {
72 IRunnableWithProgress op = new IRunnableWithProgress() {
75 public void run(IProgressMonitor monitor) throws InvocationTargetException,
76 InterruptedException {
77 SubMonitor progress = SubMonitor.convert(monitor, 100);
79 progress.beginTask("Copying", 100);
81 progress.subTask("Please wait..");
82 handler.pasteFromClipboard(Simantics.getClipboard());
83 } catch (Exception e) {
84 throw new InvocationTargetException(e);
94 public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {
96 handler.pasteFromClipboard(Simantics.getClipboard());
97 } catch (DatabaseException e) {
99 throw new InvocationTargetException(e);
100 } catch (InvocationTargetException e1) {
101 e1.getCause().printStackTrace();
107 public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {
110 return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));
111 } catch (DatabaseException e) {
112 Logger.defaultLogError(e);