X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FmodelBrowser%2Fhandlers%2FStandardCutHandler.java;h=0c6d8ba3dcb27c736fc4fb1a5b83ffbcb2df7683;hp=3e89f544ecf7755c7c4cd6b1eca296c0ffdc3522;hb=60509f4629d5ca644dd9533dd2abc64349aad328;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCutHandler.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCutHandler.java index 3e89f544e..0c6d8ba3d 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCutHandler.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCutHandler.java @@ -1,111 +1,125 @@ -/******************************************************************************* - * 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.util.HashSet; -import java.util.Set; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.action.IStatusLineManager; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.Simantics; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.common.utils.Logger; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.CopyHandler; -import org.simantics.db.layer0.util.SimanticsClipboardImpl; -import org.simantics.ui.utils.ResourceAdaptionUtils; -import org.simantics.utils.ui.workbench.WorkbenchUtils; - -public class StandardCutHandler extends AbstractHandler { - - private static IStatusLineManager status; - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) ); - ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); - final Resource[] rs = ResourceAdaptionUtils.toResources( selection ); - if (rs == null) { - setStatus("Nothing to cut."); - return null; - } - - try { - final SimanticsClipboardImpl builder = new SimanticsClipboardImpl(); - - Simantics.getSession().syncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - Set unique = new HashSet(); - for (Resource r : rs) { - if (!unique.add(r)) - continue; - CopyHandler handler = graph.adapt(r, CopyHandler.class); - handler.cutToClipboard(graph, builder); - } - } - }); - - Simantics.setClipboard(builder); - setCutMessage(builder.getContents().size(), "resource"); - - } catch (DatabaseException e) { - Logger.defaultLogError(e); - } - - return null; - } - - private static void setCutMessage(int count, String elementName) { - if (count > 1) - setStatus("Cut " + count + " " + elementName + "s to clipboard"); - else if (count == 1) - setStatus("Cut " + elementName + " to clipboard"); - else - setStatus("Nothing to cut."); - } - - private static void setStatus(String message) { - if (status != null) - status.setMessage(message); - } - - public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) { - - try { - final SimanticsClipboardImpl builder = new SimanticsClipboardImpl(); - Simantics.getSession().syncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - for (Resource r : rs) { - CopyHandler handler = graph.adapt(r, CopyHandler.class); - handler.cutToClipboard(graph, builder); - } - } - }); - Simantics.setClipboard(builder); - setCutMessage(builder.getContents().size(), "resource"); - } 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.util.HashSet; +import java.util.Set; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.jface.action.IStatusLineManager; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.CopyHandler; +import org.simantics.db.layer0.util.SimanticsClipboardImpl; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +public class StandardCutHandler extends AbstractHandler { + + private static IStatusLineManager status; + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) ); + ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); + final Resource[] rs = ResourceAdaptionUtils.toResources( selection ); + if (rs == null) { + setStatus("Nothing to cut."); + return null; + } + Job job = new Job("Cut resources") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + monitor.beginTask("Cut resources", rs.length); + try { + final SimanticsClipboardImpl builder = new SimanticsClipboardImpl(); + + Simantics.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + Set unique = new HashSet(); + for (Resource r : rs) { + if (!unique.add(r)) + continue; + CopyHandler handler = graph.adapt(r, CopyHandler.class); + handler.cutToClipboard(graph, builder, SubMonitor.convert(monitor, 1)); + } + } + }); + + Simantics.setClipboard(builder); + setCutMessage(builder.getContents().size(), "resource"); + + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); + + return null; + } + + private static void setCutMessage(int count, String elementName) { + if (count > 1) + setStatus("Cut " + count + " " + elementName + "s to clipboard"); + else if (count == 1) + setStatus("Cut " + elementName + " to clipboard"); + else + setStatus("Nothing to cut."); + } + + private static void setStatus(String message) { + if (status != null) + status.setMessage(message); + } + + public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) { + + try { + final SimanticsClipboardImpl builder = new SimanticsClipboardImpl(); + Simantics.getSession().syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + for (Resource r : rs) { + CopyHandler handler = graph.adapt(r, CopyHandler.class); + handler.cutToClipboard(graph, builder, null); + } + } + }); + Simantics.setClipboard(builder); + setCutMessage(builder.getContents().size(), "resource"); + } catch (DatabaseException e) { + Logger.defaultLogError(e); + } + + return null; + } + +}