]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCutHandler.java
350366e6cc621c6fa3fee4c8728224d1d2421777
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardCutHandler.java
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.util.HashSet;
15 import java.util.Set;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.jface.action.IStatusLineManager;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.handlers.HandlerUtil;
24 import org.simantics.Simantics;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.common.request.ReadRequest;
28 import org.simantics.db.common.utils.Logger;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.adapter.CopyHandler;
31 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
32 import org.simantics.ui.utils.ResourceAdaptionUtils;
33 import org.simantics.utils.ui.workbench.WorkbenchUtils;
34
35 public class StandardCutHandler extends AbstractHandler {
36
37     private static IStatusLineManager status;
38
39     @Override
40     public Object execute(ExecutionEvent event) throws ExecutionException {
41         status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
42         ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
43         final Resource[] rs = ResourceAdaptionUtils.toResources( selection );
44         if (rs == null) {
45             setStatus("Nothing to cut.");
46            return null;
47         }
48
49         try {
50             final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
51
52             Simantics.getSession().syncRequest(new ReadRequest() {
53                 @Override
54                 public void run(ReadGraph graph) throws DatabaseException {
55                     Set<Resource> unique = new HashSet<Resource>();
56                     for (Resource r : rs) {
57                         if (!unique.add(r))
58                             continue;
59                         CopyHandler handler = graph.adapt(r, CopyHandler.class);
60                         handler.cutToClipboard(graph, builder);
61                     }
62                 }
63             });
64
65             Simantics.setClipboard(builder);
66             setCutMessage(builder.getContents().size(), "resource");
67
68         } catch (DatabaseException e) {
69             Logger.defaultLogError(e);
70         }
71
72         return null;
73     }
74
75     private static void setCutMessage(int count, String elementName) {
76         if (count > 1)
77             setStatus("Cut " + count + " " + elementName + "s to clipboard");
78         else if (count == 1)
79             setStatus("Cut " + elementName + " to clipboard");
80         else
81             setStatus("Nothing to cut.");
82     }
83
84     private static void setStatus(String message) {
85         if (status != null)
86             status.setMessage(message);
87     }
88
89     public static String cutResourcesToClipboard(final Resource[] rs, ISelection selection) {
90
91         try {
92             final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
93             Simantics.getSession().syncRequest(new ReadRequest() {
94                 @Override
95                 public void run(ReadGraph graph) throws DatabaseException {
96                     for (Resource r : rs) {
97                         CopyHandler handler = graph.adapt(r, CopyHandler.class);
98                         handler.cutToClipboard(graph, builder);
99                     }
100                 }
101             });
102             Simantics.setClipboard(builder);
103             setCutMessage(builder.getContents().size(), "resource");
104         } catch (DatabaseException e) {
105             Logger.defaultLogError(e);
106         }
107
108         return null;
109     }
110     
111 }