]> gerrit.simantics Code Review - simantics/platform.git/blob
32e3ef60ff13afa83a43e6061d6f1ae6b1c284b9
[simantics/platform.git] /
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.modeling.ui.modelBrowser.handlers;
12
13
14 import java.lang.reflect.InvocationTargetException;
15
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;
37
38 public class StandardPasteHandler extends AbstractHandler implements IHandler {
39
40     @Override
41     public Object execute(ExecutionEvent event) throws ExecutionException {
42
43         final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
44         final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class);
45         if (handler != null) {
46                 
47                 try {
48                         
49                     Simantics.getSession().markUndoPoint();
50                         IRunnableWithProgress op = pasteResourceFromClipboard(handler);
51                         
52                         Shell shell = HandlerUtil.getActiveShell(event);
53                         new ProgressMonitorDialog(shell).run(true, true, op);
54                         
55                 } catch (InvocationTargetException e) {
56                         Throwable t = e.getCause();
57                         if (t != null) {
58                                 ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e);
59                         } else {
60                                 ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e);
61                         }
62                 } catch (InterruptedException e) {
63                         ErrorLogger.defaultLogError(e);
64                 }
65                 
66         }
67         return null;
68     }
69     
70     public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) {
71         
72         IRunnableWithProgress op = new IRunnableWithProgress() {
73
74                         @Override
75                         public void run(IProgressMonitor monitor) throws InvocationTargetException,
76                         InterruptedException {
77                                 SubMonitor progress = SubMonitor.convert(monitor, 100);
78                                 try {
79                                         progress.beginTask("Copying", 100);
80                                         progress.worked(50);
81                                         progress.subTask("Please wait..");
82                             handler.pasteFromClipboard(Simantics.getClipboard());
83                                 } catch (Exception e) {
84                                         throw new InvocationTargetException(e);
85                                 } finally {
86                                         monitor.done();
87                                 }
88                         }
89                 };
90                 return op;
91         
92     }
93     
94     public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) {
95         try {
96                 handler.pasteFromClipboard(Simantics.getClipboard());
97                 } catch (DatabaseException e) {
98                         try {
99                                 throw new InvocationTargetException(e);
100                         } catch (InvocationTargetException e1) {
101                                 e1.getCause().printStackTrace();
102                         }
103                         e.printStackTrace();
104                 }
105     }
106     
107     public static <T> T getPasteHandlerFromResource (Resource resource, Class<T> assignableFrom) {
108     
109     try {
110         return Simantics.getSession().syncRequest(new Adapter<T>(resource, assignableFrom));
111     } catch (DatabaseException e) {
112         Logger.defaultLogError(e);
113         return null;
114     }
115     }
116 }
117