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.datastructures.Callback;
29 import org.simantics.utils.ui.ExceptionUtils;
32 * Action for adding URI-links as documents.
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class AddUrlDocumentWithDetail extends AddDocumentAction {
39 public AddUrlDocumentWithDetail(ReadGraph graph, String relationUri)
40 throws DatabaseException {
41 super(graph, relationUri);
46 public Runnable create(Object target) {
47 if(!(target instanceof Resource))
49 final Resource resource = (Resource)target;
50 return new Runnable() {
54 final UrlDetailDialog dialog = new UrlDetailDialog(Display.getCurrent().getActiveShell(),resource);
55 if (dialog.open() != InputDialog.OK) {
56 dialog.getAnnotationConfigurator().dispose();
59 final String uriString = dialog.getUrl();
60 final String name = dialog.getName();
61 Simantics.getSession().asyncRequest(new WriteRequest() {
63 public void perform(WriteGraph graph)
64 throws DatabaseException {
65 graph.markUndoPoint();
66 Resource urlDocument = doAddUrl(graph, name, uriString);
67 linkDocument(graph, resource, urlDocument);
68 dialog.getAnnotationConfigurator().apply(graph,urlDocument);
70 },new Callback<DatabaseException>() {
72 public void run(DatabaseException parameter) {
73 dialog.getAnnotationConfigurator().dispose();
74 if (parameter != null) {
75 ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
84 private Resource doAddUrl(WriteGraph graph, String name, String uriString) throws ManyObjectsForFunctionalRelationException, ServiceException {
85 Layer0 l0 = Layer0.getInstance(graph);
86 DocumentResource doc = DocumentResource.getInstance(graph);
87 Resource urlDocument = graph.newResource();
88 graph.claim(urlDocument, l0.InstanceOf, doc.UrlDocument);
89 graph.claimLiteral(urlDocument, l0.HasName, name);
90 graph.claimLiteral(urlDocument, doc.HasUrl, uriString);
94 public static Resource addUrlDocumentWithDetailSCL(WriteGraph graph, Resource target, String name, String uriString) throws DatabaseException {
95 AddUrlDocumentWithDetail urlDocument = new AddUrlDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
96 Resource urlResource = urlDocument.doAddUrl(graph, name, uriString);
97 urlDocument.linkDocument(graph, target, urlResource);
103 // public Runnable create(Object target) {
105 // if(!(target instanceof Resource))
108 // final Resource resource = (Resource)target;
110 // return new Runnable() {
112 // public void run() {
113 // InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Add URL", "Input URL", "", new URLValidator());
114 // if (dialog.open() != InputDialog.OK)
116 // final String uriString = dialog.getValue();
117 // Simantics.getSession().asyncRequest(new WriteRequest() {
120 // public void perform(WriteGraph graph) throws DatabaseException {
121 // Layer0 l0 = Layer0.getInstance(graph);
122 // DocumentResource doc = DocumentResource.getInstance(graph);
123 // Resource urlDocument = graph.newResource();
124 // graph.claim(urlDocument, l0.InstanceOf, doc.UrlDocument);
125 // graph.claimLiteral(urlDocument, l0.HasName, uriString);
126 // graph.claimLiteral(urlDocument, doc.HasUrl, uriString);
128 // if (graph.isInstanceOf(resource, doc.UrlDocument)) {
129 // if (graph.hasStatement(resource, doc.HasNewerVersion))
130 // throw new DatabaseException("Document " + NameUtils.getSafeName(graph, resource) +" has already new version");
131 // // adding a new revision
132 // Resource inverse = graph.getInverse(relation);
133 // Resource lib = graph.getSingleObject(resource, inverse);
134 // graph.claim(lib, relation, urlDocument);
135 // graph.claim(resource, doc.HasNewerVersion, urlDocument);
137 // // adding to a library
138 // graph.claim(resource, relation, urlDocument);
141 // },new Callback<DatabaseException>() {
144 // public void run(DatabaseException parameter) {
145 // if (parameter != null) {
146 // ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);