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.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;
39 public class StandardPasteHandler extends AbstractHandler implements IHandler {
41 private static final Logger LOGGER = LoggerFactory.getLogger(StandardPasteHandler.class);
44 public Object execute(ExecutionEvent event) throws ExecutionException {
46 final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
47 final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class);
48 if (handler != null) {
52 Simantics.getSession().markUndoPoint();
53 IRunnableWithProgress op = pasteResourceFromClipboard(handler);
55 Shell shell = HandlerUtil.getActiveShell(event);
56 new ProgressMonitorDialog(shell).run(true, true, op);
58 } catch (InvocationTargetException e) {
59 Throwable t = e.getCause();
61 ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);
63 ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);
65 } catch (InterruptedException e) {
66 ErrorLogger.defaultLogError(e);
73 public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {
75 IRunnableWithProgress op = new IRunnableWithProgress() {
78 public void run(IProgressMonitor monitor) throws InvocationTargetException,
79 InterruptedException {
80 SubMonitor progress = SubMonitor.convert(monitor, 100);
82 progress.beginTask("Copying", 100);
84 progress.subTask("Please wait..");
85 handler.pasteFromClipboard(Simantics.getClipboard());
86 } catch (Exception e) {
87 throw new InvocationTargetException(e);
97 public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {
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$
105 public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {
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$