]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/StandardCopyHandler.java
Fixed StandardCutHandler IllegalThreadAccess problem
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / StandardCopyHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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  *     Semantum Oy - gitlab simantics/platform#133
11  *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.handlers;
13
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.SubMonitor;
26 import org.eclipse.core.runtime.jobs.Job;
27 import org.eclipse.jface.action.IStatusLineManager;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.swt.dnd.Clipboard;
31 import org.eclipse.swt.dnd.TextTransfer;
32 import org.eclipse.swt.dnd.Transfer;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Tree;
37 import org.eclipse.swt.widgets.TreeItem;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.handlers.HandlerUtil;
40 import org.simantics.Simantics;
41 import org.simantics.browsing.ui.BuiltinKeys;
42 import org.simantics.browsing.ui.GraphExplorer;
43 import org.simantics.browsing.ui.NodeContext;
44 import org.simantics.db.ReadGraph;
45 import org.simantics.db.Resource;
46 import org.simantics.db.common.request.ReadRequest;
47 import org.simantics.db.exception.DatabaseException;
48 import org.simantics.db.layer0.SelectionHints;
49 import org.simantics.db.layer0.adapter.CopyHandler;
50 import org.simantics.db.layer0.util.ClipboardUtils;
51 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
52 import org.simantics.db.layer0.variable.Variable;
53 import org.simantics.ui.utils.ResourceAdaptionUtils;
54 import org.simantics.utils.datastructures.hints.IHintContext;
55 import org.simantics.utils.ui.ISelectionUtils;
56 import org.simantics.utils.ui.SWTUtils;
57 import org.simantics.utils.ui.SWTUtils.ControlFilter;
58 import org.simantics.utils.ui.workbench.WorkbenchUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 import gnu.trove.set.hash.THashSet;
63
64 public class StandardCopyHandler extends AbstractHandler {
65
66     private static final Logger LOGGER = LoggerFactory.getLogger(StandardCopyHandler.class);
67
68     private static IStatusLineManager status;
69
70     private static List<Variable> getVariables(ISelection selection) {
71         NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
72         if(context == null) return Collections.emptyList();
73         Object input = context.getConstant(BuiltinKeys.INPUT);
74         IHintContext hints = input instanceof IHintContext ? (IHintContext) input : null;
75         if(hints == null) return Collections.emptyList();
76         Variable var = hints.getHint(SelectionHints.KEY_SELECTION_PROPERTY);
77         if(var == null) return Collections.emptyList();
78         else return Collections.singletonList(var);
79     }
80
81     private boolean copyText(ISelection selection) {
82         if(selection instanceof StructuredSelection) {
83                 StructuredSelection sel = (StructuredSelection)selection;
84                 if(sel.size() == 1) {
85                         Object element = sel.getFirstElement();
86                         if(element instanceof String) {
87                                 setSystemClipboardText((String) element);
88                         }
89                 }
90         }
91         return false;
92     }
93     
94     @Override
95     public Object execute(ExecutionEvent event) throws ExecutionException {
96         
97         status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
98         ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
99         
100         // If the selection is plain text copy it into system clipboard and be happy
101         if(copyText(selection)) return null;
102
103         formatSelectionToClipboardText(event);
104         final Resource[] rs = ResourceAdaptionUtils.toResources(selection);
105         Job job = new Job("Copy resources") {
106             @Override
107             protected IStatus run(IProgressMonitor monitor) {
108                 monitor.beginTask("Copy resources to clipboard", 1);
109                 copyResourcesToClipboard(rs, selection, SubMonitor.convert(monitor, 1));
110                 return Status.OK_STATUS;
111             }
112         };
113         job.setUser(true);
114         job.schedule();
115         
116         return null;
117     }
118     
119     public static String copyResourcesToClipboard(final Resource[] rs, ISelection selection, IProgressMonitor monitor) {
120         
121         if(rs == null || rs.length == 0) {
122             // This support was added for copying of properties (variables)
123             final List<Variable> variables = getVariables(selection);
124             if(!variables.isEmpty()) {
125                 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
126                 for(Variable var : variables) {
127                     builder.addContent(Collections.singleton(ClipboardUtils.createVariable(Simantics.getSession(), var)), monitor);
128                 }
129                 Simantics.setClipboard(builder);
130                 setCopyMessage(builder.getContents().size(), "variable");
131                 return null;
132             }
133             setCopyMessage(0, ""); //$NON-NLS-1$
134             return null;
135         }
136
137         try {
138             final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
139             Simantics.getSession().syncRequest(new ReadRequest() {
140                 @Override
141                 public void run(ReadGraph graph) throws DatabaseException {
142                     for (Resource r : rs) {
143                         CopyHandler handler = graph.adapt(r, CopyHandler.class);
144                         handler.copyToClipboard(graph, builder, monitor);
145                     }
146                 }
147             });
148             Simantics.setClipboard(builder);
149             setCopyMessage(builder.getContents().size(), "resource");
150         } catch (DatabaseException e) {
151             LOGGER.error("Failed to copy {} resources to clipboard: {}", rs.length, Arrays.toString(rs), e); //$NON-NLS-1$
152         }
153
154         return null;
155     }
156
157     private static void setCopyMessage(int count, String elementName) {
158         if (count > 1)
159             setStatus("Copied " + count + " " + elementName + "s to clipboard");
160         else if (count == 1)
161             setStatus("Copied " + elementName + " to clipboard");
162         else
163             setStatus("Nothing to copy.");
164     }
165
166     private static void setStatus(String message) {
167         if (status != null) {
168             SWTUtils.asyncExec(
169                     PlatformUI.getWorkbench().getDisplay(),
170                     () -> status.setMessage(message));
171         }
172     }
173
174     private boolean formatSelectionToClipboardText(ExecutionEvent event) {
175         Shell shell = HandlerUtil.getActiveShell(event);
176         Tree tree = shell == null ? null : tryGetExplorer(shell.getDisplay().getFocusControl());
177         if (tree == null)
178             return false;
179
180         TreeItem[] selection = tree.getSelection();
181         if (selection.length == 0)
182             return false;
183
184         StringBuilder sb = format(selection, new StringBuilder());
185         if (sb.length() > 0) {
186             setSystemClipboardText(sb.toString());
187             return true;
188         }
189         return false;
190     }
191
192     private static StringBuilder format(TreeItem[] selection, StringBuilder sb) {
193         Set<TreeItem> items = new THashSet<TreeItem>(selection.length);
194         for (TreeItem item : selection)
195             items.add(item);
196         for (TreeItem item : selection) {
197             int cc = item.getParent().getColumnCount();
198             int indent = indentLevel(item, items);
199             for (int i = 0; i < indent; ++i)
200                 sb.append('\t');
201             boolean first = true;
202             for (int c = 0; c < cc; ++c) {
203                 String ct = item.getText(c);
204                 if (!first) {
205                     sb.append('\t');
206                 }
207                 first = false;
208                 sb.append(ct);
209             }
210             sb.append('\n');
211         }
212         return sb;
213     }
214
215     private static int indentLevel(TreeItem item, Set<TreeItem> items) {
216         TreeItem p = item.getParentItem();
217         for (int i = 1; ; p = p.getParentItem()) {
218             if (p == null)
219                 return 0;
220             if (items.contains(p))
221                 return i;
222         }
223     }
224
225     private static Tree tryGetExplorer(Control control) {
226         return SWTUtils.tryGetObject(control, new ControlFilter<Tree>() {
227             @Override
228             public Tree accept(Control control) {
229                 if (!control.isDisposed()
230                         && control instanceof Tree
231                         && control.getData(GraphExplorer.KEY_GRAPH_EXPLORER) != null)
232                     return (Tree) control;
233                 return null;
234             }
235         });
236     }
237
238     private static void setSystemClipboardText(String text) {
239         Clipboard clipboard = new Clipboard(Display.getCurrent());
240         clipboard.setContents(new Object[]{ text }, new Transfer[] { TextTransfer.getInstance() });
241         clipboard.dispose();
242     }
243
244 }