1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.ui.actions;
14 import org.eclipse.jface.dialogs.InputDialog;
15 import org.eclipse.swt.widgets.Display;
16 import org.simantics.Simantics;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
23 import org.simantics.db.exception.ServiceException;
24 import org.simantics.document.AddDocumentAction;
25 import org.simantics.document.DocumentResource;
26 import org.simantics.document.ui.dialogs.UrlDetailDialog;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.ui.ExceptionUtils;
31 * Action for adding URI-links as documents.
33 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
36 public class AddUrlDocumentWithDetail extends AddDocumentAction {
38 public AddUrlDocumentWithDetail(ReadGraph graph, String relationUri)
39 throws DatabaseException {
40 super(graph, relationUri);
45 public Runnable create(Object target) {
46 if(!(target instanceof Resource))
48 final Resource resource = (Resource)target;
49 return new Runnable() {
53 final UrlDetailDialog dialog = new UrlDetailDialog(Display.getCurrent().getActiveShell(),resource);
54 if (dialog.open() != InputDialog.OK) {
55 dialog.getAnnotationConfigurator().dispose();
58 final String uriString = dialog.getUrl();
59 final String name = dialog.getName();
60 Simantics.getSession().asyncRequest(new WriteRequest() {
62 public void perform(WriteGraph graph)
63 throws DatabaseException {
64 graph.markUndoPoint();
65 Resource urlDocument = doAddUrl(graph, name, uriString);
66 linkDocument(graph, resource, urlDocument);
67 dialog.getAnnotationConfigurator().apply(graph,urlDocument);
70 dialog.getAnnotationConfigurator().dispose();
72 ExceptionUtils.logAndShowError("Cannot add URL link.", e);
78 private Resource doAddUrl(WriteGraph graph, String name, String uriString) throws ManyObjectsForFunctionalRelationException, ServiceException {
79 Layer0 l0 = Layer0.getInstance(graph);
80 DocumentResource doc = DocumentResource.getInstance(graph);
81 Resource urlDocument = graph.newResource();
82 graph.claim(urlDocument, l0.InstanceOf, doc.UrlDocument);
83 graph.claimLiteral(urlDocument, l0.HasName, name);
84 graph.claimLiteral(urlDocument, doc.HasUrl, uriString);
88 public static Resource addUrlDocumentWithDetailSCL(WriteGraph graph, Resource target, String name, String uriString) throws DatabaseException {
89 AddUrlDocumentWithDetail urlDocument = new AddUrlDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
90 Resource urlResource = urlDocument.doAddUrl(graph, name, uriString);
91 urlDocument.linkDocument(graph, target, urlResource);
97 // public Runnable create(Object target) {
99 // if(!(target instanceof Resource))
102 // final Resource resource = (Resource)target;
104 // return new Runnable() {
106 // public void run() {
107 // InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Add URL", "Input URL", "", new URLValidator());
108 // if (dialog.open() != InputDialog.OK)
110 // final String uriString = dialog.getValue();
111 // Simantics.getSession().asyncRequest(new WriteRequest() {
114 // public void perform(WriteGraph graph) throws DatabaseException {
115 // Layer0 l0 = Layer0.getInstance(graph);
116 // DocumentResource doc = DocumentResource.getInstance(graph);
117 // Resource urlDocument = graph.newResource();
118 // graph.claim(urlDocument, l0.InstanceOf, doc.UrlDocument);
119 // graph.claimLiteral(urlDocument, l0.HasName, uriString);
120 // graph.claimLiteral(urlDocument, doc.HasUrl, uriString);
122 // if (graph.isInstanceOf(resource, doc.UrlDocument)) {
123 // if (graph.hasStatement(resource, doc.HasNewerVersion))
124 // throw new DatabaseException("Document " + NameUtils.getSafeName(graph, resource) +" has already new version");
125 // // adding a new revision
126 // Resource inverse = graph.getInverse(relation);
127 // Resource lib = graph.getSingleObject(resource, inverse);
128 // graph.claim(lib, relation, urlDocument);
129 // graph.claim(resource, doc.HasNewerVersion, urlDocument);
131 // // adding to a library
132 // graph.claim(resource, relation, urlDocument);
135 // },new Callback<DatabaseException>() {
138 // public void run(DatabaseException parameter) {
139 // if (parameter != null) {
140 // ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);