X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FmodelBrowser%2Fhandlers%2FStandardPasteHandler.java;h=32e3ef60ff13afa83a43e6061d6f1ae6b1c284b9;hb=refs%2Fchanges%2F38%2F238%2F2;hp=51140988bcd505220271becb41c3f7082c86f1f4;hpb=b35573372259ace60d8827766fe41443f4c57629;p=simantics%2Fplatform.git 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 index 51140988b..32e3ef60f 100644 --- 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 @@ -1,117 +1,117 @@ -/******************************************************************************* - * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.modeling.ui.modelBrowser.handlers; - - -import java.lang.reflect.InvocationTargetException; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.IHandler; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.jface.dialogs.ProgressMonitorDialog; -import org.eclipse.jface.operation.IRunnableWithProgress; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.Simantics; -import org.simantics.db.Resource; -import org.simantics.db.common.primitiverequest.Adapter; -import org.simantics.db.common.utils.Logger; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.PasteHandler; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.ErrorLogger; -import org.simantics.utils.ui.ExceptionUtils; - -public class StandardPasteHandler extends AbstractHandler implements IHandler { - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - - final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); - final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class); - if (handler != null) { - - try { - - Simantics.getSession().markUndoPoint(); - IRunnableWithProgress op = pasteResourceFromClipboard(handler); - - Shell shell = HandlerUtil.getActiveShell(event); - new ProgressMonitorDialog(shell).run(true, true, op); - - } catch (InvocationTargetException e) { - Throwable t = e.getCause(); - if (t != null) { - ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e); - } else { - ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e); - } - } catch (InterruptedException e) { - ErrorLogger.defaultLogError(e); - } - - } - return null; - } - - public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) { - - IRunnableWithProgress op = new IRunnableWithProgress() { - - @Override - public void run(IProgressMonitor monitor) throws InvocationTargetException, - InterruptedException { - SubMonitor progress = SubMonitor.convert(monitor, 100); - try { - progress.beginTask("Copying", 100); - progress.worked(50); - progress.subTask("Please wait.."); - handler.pasteFromClipboard(Simantics.getClipboard()); - } catch (Exception e) { - throw new InvocationTargetException(e); - } finally { - monitor.done(); - } - } - }; - return op; - - } - - public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) { - try { - handler.pasteFromClipboard(Simantics.getClipboard()); - } catch (DatabaseException e) { - try { - throw new InvocationTargetException(e); - } catch (InvocationTargetException e1) { - e1.getCause().printStackTrace(); - } - e.printStackTrace(); - } - } - - public static T getPasteHandlerFromResource (Resource resource, Class assignableFrom) { - - try { - return Simantics.getSession().syncRequest(new Adapter(resource, assignableFrom)); - } catch (DatabaseException e) { - Logger.defaultLogError(e); - return null; - } - } -} - +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.modelBrowser.handlers; + + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.jface.dialogs.ProgressMonitorDialog; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.common.primitiverequest.Adapter; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.PasteHandler; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.ExceptionUtils; + +public class StandardPasteHandler extends AbstractHandler implements IHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); + final PasteHandler handler = SimanticsUI.filterSingleSelection(selection, PasteHandler.class); + if (handler != null) { + + try { + + Simantics.getSession().markUndoPoint(); + IRunnableWithProgress op = pasteResourceFromClipboard(handler); + + Shell shell = HandlerUtil.getActiveShell(event); + new ProgressMonitorDialog(shell).run(true, true, op); + + } catch (InvocationTargetException e) { + Throwable t = e.getCause(); + if (t != null) { + ExceptionUtils.logAndShowError("Paste Failed", t.getMessage(), e); + } else { + ExceptionUtils.logAndShowError("Paste Failed", "Paste failed for unknown reason.", e); + } + } catch (InterruptedException e) { + ErrorLogger.defaultLogError(e); + } + + } + return null; + } + + public static IRunnableWithProgress pasteResourceFromClipboard(final PasteHandler handler) { + + IRunnableWithProgress op = new IRunnableWithProgress() { + + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException { + SubMonitor progress = SubMonitor.convert(monitor, 100); + try { + progress.beginTask("Copying", 100); + progress.worked(50); + progress.subTask("Please wait.."); + handler.pasteFromClipboard(Simantics.getClipboard()); + } catch (Exception e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); + } + } + }; + return op; + + } + + public static void pasteResourceFromClipboardWithoutMonitor (final PasteHandler handler) { + try { + handler.pasteFromClipboard(Simantics.getClipboard()); + } catch (DatabaseException e) { + try { + throw new InvocationTargetException(e); + } catch (InvocationTargetException e1) { + e1.getCause().printStackTrace(); + } + e.printStackTrace(); + } + } + + public static T getPasteHandlerFromResource (Resource resource, Class assignableFrom) { + + try { + return Simantics.getSession().syncRequest(new Adapter(resource, assignableFrom)); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + return null; + } + } +} +