]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/actions/CopyURI.java
Add utility class org.simantics.modeling.help.HelpContexts
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / actions / CopyURI.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.actions;
13
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;
27
28 /**
29  * @author Jani Simomaa
30  * @since 1.30.0
31  */
32 public class CopyURI implements ActionFactory {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(CopyURI.class);
35
36     @Override
37     public Runnable create(Object target) {
38         return () -> {
39             try {
40                 String uri = getPossibleURI(target);
41                 if (uri != null) {
42                     Clipboard cb = new Clipboard(Display.getCurrent());
43                     cb.setContents(new Object[] { uri }, new Transfer[] { TextTransfer.getInstance() });
44                 }
45             } catch (Exception e) {
46                 LOGGER.error("Could not get URI for input {} to copy to clipboard", target, e);
47             }
48         };
49     }
50
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));
56         }
57         return null;
58     }
59
60 }