--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.modelBrowser.handlers;\r
+\r
+\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.action.IStatusLineManager;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.CopyHandler;\r
+import org.simantics.db.layer0.util.SimanticsClipboardImpl;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+public class StandardCutHandler extends AbstractHandler {\r
+\r
+ private static IStatusLineManager status;\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );\r
+ ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();\r
+ final Resource[] rs = ResourceAdaptionUtils.toResources( selection );\r
+ if (rs == null) {\r
+ setStatus("Nothing to cut.");\r
+ return null;\r
+ }\r
+\r
+ try {\r
+ final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();\r
+\r
+ Simantics.getSession().syncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Set<Resource> unique = new HashSet<Resource>();\r
+ for (Resource r : rs) {\r
+ if (!unique.add(r))\r
+ continue;\r
+ CopyHandler handler = graph.adapt(r, CopyHandler.class);\r
+ handler.cutToClipboard(graph, builder);\r
+ }\r
+ }\r
+ });\r
+\r
+ Simantics.setClipboard(builder);\r
+ setCutMessage(builder.getContents().size(), "resource");\r
+\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+ private static void setCutMessage(int count, String elementName) {\r
+ if (count > 1)\r
+ setStatus("Cut " + count + " " + elementName + "s to clipboard");\r
+ else if (count == 1)\r
+ setStatus("Cut " + elementName + " to clipboard");\r
+ else\r
+ setStatus("Nothing to cut.");\r
+ }\r
+\r
+ private static void setStatus(String message) {\r
+ if (status != null)\r
+ status.setMessage(message);\r
+ }\r
+\r
+ public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) {\r
+\r
+ try {\r
+ final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();\r
+ Simantics.getSession().syncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ for (Resource r : rs) {\r
+ CopyHandler handler = graph.adapt(r, CopyHandler.class);\r
+ handler.cutToClipboard(graph, builder);\r
+ }\r
+ }\r
+ });\r
+ Simantics.setClipboard(builder);\r
+ setCutMessage(builder.getContents().size(), "resource");\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+\r
+ return null;\r
+ }\r
+ \r
+}\r