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