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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.modeling.ui.modelBrowser.handlers;
13 import gnu.trove.set.hash.THashSet;
15 import java.util.Collections;
16 import java.util.List;
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.jface.action.IStatusLineManager;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.swt.dnd.Clipboard;
26 import org.eclipse.swt.dnd.TextTransfer;
27 import org.eclipse.swt.dnd.Transfer;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Tree;
32 import org.eclipse.swt.widgets.TreeItem;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.handlers.HandlerUtil;
35 import org.simantics.Simantics;
36 import org.simantics.browsing.ui.BuiltinKeys;
37 import org.simantics.browsing.ui.GraphExplorer;
38 import org.simantics.browsing.ui.NodeContext;
39 import org.simantics.db.ReadGraph;
40 import org.simantics.db.Resource;
41 import org.simantics.db.common.request.ReadRequest;
42 import org.simantics.db.common.utils.Logger;
43 import org.simantics.db.exception.DatabaseException;
44 import org.simantics.db.layer0.SelectionHints;
45 import org.simantics.db.layer0.adapter.CopyHandler;
46 import org.simantics.db.layer0.util.ClipboardUtils;
47 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
48 import org.simantics.db.layer0.variable.Variable;
49 import org.simantics.ui.utils.ResourceAdaptionUtils;
50 import org.simantics.utils.datastructures.hints.IHintContext;
51 import org.simantics.utils.ui.ISelectionUtils;
52 import org.simantics.utils.ui.SWTUtils;
53 import org.simantics.utils.ui.SWTUtils.ControlFilter;
54 import org.simantics.utils.ui.workbench.WorkbenchUtils;
56 public class StandardCopyHandler extends AbstractHandler {
58 private static IStatusLineManager status;
60 private static List<Variable> getVariables(ISelection selection) {
61 NodeContext context = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
62 if(context == null) return Collections.emptyList();
63 Object input = context.getConstant(BuiltinKeys.INPUT);
64 IHintContext hints = input instanceof IHintContext ? (IHintContext) input : null;
65 if(hints == null) return Collections.emptyList();
66 Variable var = hints.getHint(SelectionHints.KEY_SELECTION_PROPERTY);
67 if(var == null) return Collections.emptyList();
68 else return Collections.singletonList(var);
71 private boolean copyText(ISelection selection) {
72 if(selection instanceof StructuredSelection) {
73 StructuredSelection sel = (StructuredSelection)selection;
75 Object element = sel.getFirstElement();
76 if(element instanceof String) {
77 setSystemClipboardText((String) element);
85 public Object execute(ExecutionEvent event) throws ExecutionException {
87 status = WorkbenchUtils.getStatusLine( HandlerUtil.getActiveSite(event) );
88 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
90 // If the selection is plain text copy it into system clipboard and be happy
91 if(copyText(selection)) return null;
93 formatSelectionToClipboardText(event);
95 final Resource[] rs = ResourceAdaptionUtils.toResources(selection);
96 copyResourcesToClipboard(rs, selection);
100 public static String copyResourcesToClipboard(final Resource[] rs, ISelection selection) {
102 if(rs == null || rs.length == 0) {
103 // This support was added for copying of properties (variables)
104 final List<Variable> variables = getVariables(selection);
105 if(!variables.isEmpty()) {
106 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
107 for(Variable var : variables) {
108 builder.addContent(Collections.singleton(ClipboardUtils.createVariable(Simantics.getSession(), var)));
110 Simantics.setClipboard(builder);
111 setCopyMessage(builder.getContents().size(), "variable");
114 setCopyMessage(0, "");
119 final SimanticsClipboardImpl builder = new SimanticsClipboardImpl();
120 Simantics.getSession().syncRequest(new ReadRequest() {
122 public void run(ReadGraph graph) throws DatabaseException {
123 for (Resource r : rs) {
124 CopyHandler handler = graph.adapt(r, CopyHandler.class);
125 handler.copyToClipboard(graph, builder);
129 Simantics.setClipboard(builder);
130 setCopyMessage(builder.getContents().size(), "resource");
131 } catch (DatabaseException e) {
132 Logger.defaultLogError(e);
138 private static void setCopyMessage(int count, String elementName) {
140 setStatus("Copied " + count + " " + elementName + "s to clipboard");
142 setStatus("Copied " + elementName + " to clipboard");
144 setStatus("Nothing to copy.");
147 private static void setStatus(String message) {
149 status.setMessage(message);
152 private boolean formatSelectionToClipboardText(ExecutionEvent event) {
153 Shell shell = HandlerUtil.getActiveShell(event);
154 Tree tree = shell == null ? null : tryGetExplorer(shell.getDisplay().getFocusControl());
158 TreeItem[] selection = tree.getSelection();
159 if (selection.length == 0)
162 StringBuilder sb = format(selection, new StringBuilder());
163 if (sb.length() > 0) {
164 setSystemClipboardText(sb.toString());
170 private static StringBuilder format(TreeItem[] selection, StringBuilder sb) {
171 Set<TreeItem> items = new THashSet<TreeItem>(selection.length);
172 for (TreeItem item : selection)
174 for (TreeItem item : selection) {
175 int cc = item.getParent().getColumnCount();
176 int indent = indentLevel(item, items);
177 for (int i = 0; i < indent; ++i)
179 boolean first = true;
180 for (int c = 0; c < cc; ++c) {
181 String ct = item.getText(c);
193 private static int indentLevel(TreeItem item, Set<TreeItem> items) {
194 TreeItem p = item.getParentItem();
195 for (int i = 1; ; p = p.getParentItem()) {
198 if (items.contains(p))
203 private static Tree tryGetExplorer(Control control) {
204 return SWTUtils.tryGetObject(control, new ControlFilter<Tree>() {
206 public Tree accept(Control control) {
207 if (!control.isDisposed()
208 && control instanceof Tree
209 && control.getData(GraphExplorer.KEY_GRAPH_EXPLORER) != null)
210 return (Tree) control;
216 private static void setSystemClipboardText(String text) {
217 Clipboard clipboard = new Clipboard(Display.getCurrent());
218 clipboard.setContents(new Object[]{ text }, new Transfer[] { TextTransfer.getInstance() });