1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.actions;
14 import org.eclipse.swt.dnd.Clipboard;
15 import org.eclipse.swt.dnd.TextTransfer;
16 import org.eclipse.swt.dnd.Transfer;
17 import org.eclipse.swt.widgets.Display;
18 import org.simantics.Simantics;
19 import org.simantics.db.Resource;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.adapter.ActionFactory;
22 import org.simantics.db.layer0.request.PossibleURI;
23 import org.simantics.db.layer0.request.VariableURI;
24 import org.simantics.db.layer0.variable.Variable;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * @author Jani Simomaa
32 public class CopyURI implements ActionFactory {
34 private static final Logger LOGGER = LoggerFactory.getLogger(CopyURI.class);
37 public Runnable create(Object target) {
40 String uri = getPossibleURI(target);
42 Clipboard cb = new Clipboard(Display.getCurrent());
43 cb.setContents(new Object[] { uri }, new Transfer[] { TextTransfer.getInstance() });
45 } catch (Exception e) {
46 LOGGER.error("Could not get URI for input {} to copy to clipboard", target, e);
51 private String getPossibleURI(Object input) throws DatabaseException {
52 if (input instanceof Resource) {
53 return Simantics.getSession().syncRequest(new PossibleURI((Resource) input));
54 } else if (input instanceof Variable) {
55 return Simantics.getSession().syncRequest(new VariableURI((Variable) input));