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.DocumentResource;
25 import org.simantics.document.ui.dialogs.UrlDetailDialog;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.utils.datastructures.Callback;
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);
69 },new Callback<DatabaseException>() {
71 public void run(DatabaseException parameter) {
72 dialog.getAnnotationConfigurator().dispose();
73 if (parameter != null) {
74 ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
83 private Resource doAddUrl(WriteGraph graph, String name, String uriString) throws ManyObjectsForFunctionalRelationException, ServiceException {
84 Layer0 l0 = Layer0.getInstance(graph);
85 DocumentResource doc = DocumentResource.getInstance(graph);
86 Resource urlDocument = graph.newResource();
87 graph.claim(urlDocument, l0.InstanceOf, doc.UrlDocument);
88 graph.claimLiteral(urlDocument, l0.HasName, name);
89 graph.claimLiteral(urlDocument, doc.HasUrl, uriString);
93 public static Resource addUrlDocumentWithDetailSCL(WriteGraph graph, Resource target, String name, String uriString) throws DatabaseException {
94 AddUrlDocumentWithDetail urlDocument = new AddUrlDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
95 Resource urlResource = urlDocument.doAddUrl(graph, name, uriString);
96 urlDocument.linkDocument(graph, target, urlResource);
102 // public Runnable create(Object target) {
104 // if(!(target instanceof Resource))
107 // final Resource resource = (Resource)target;
109 // return new Runnable() {
111 // public void run() {
112 // InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Add URL", "Input URL", "", new URLValidator());
113 // if (dialog.open() != InputDialog.OK)
115 // final String uriString = dialog.getValue();
116 // Simantics.getSession().asyncRequest(new WriteRequest() {
119 // public void perform(WriteGraph graph) throws DatabaseException {
120 // Layer0 l0 = Layer0.getInstance(graph);
121 // DocumentResource doc = DocumentResource.getInstance(graph);
122 // Resource urlDocument = graph.newResource();
123 // graph.claim(urlDocument, l0.InstanceOf, doc.UrlDocument);
124 // graph.claimLiteral(urlDocument, l0.HasName, uriString);
125 // graph.claimLiteral(urlDocument, doc.HasUrl, uriString);
127 // if (graph.isInstanceOf(resource, doc.UrlDocument)) {
128 // if (graph.hasStatement(resource, doc.HasNewerVersion))
129 // throw new DatabaseException("Document " + NameUtils.getSafeName(graph, resource) +" has already new version");
130 // // adding a new revision
131 // Resource inverse = graph.getInverse(relation);
132 // Resource lib = graph.getSingleObject(resource, inverse);
133 // graph.claim(lib, relation, urlDocument);
134 // graph.claim(resource, doc.HasNewerVersion, urlDocument);
136 // // adding to a library
137 // graph.claim(resource, relation, urlDocument);
140 // },new Callback<DatabaseException>() {
143 // public void run(DatabaseException parameter) {
144 // if (parameter != null) {
145 // ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);