]> gerrit.simantics Code Review - simantics/platform.git/blob
61210a222b4f64dce3b2ea9dc3091b5d9efb0e2b
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.ui.actions;
13
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;
29
30 /**
31  * Action for adding URI-links as documents.
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  *
35  */
36 public class AddUrlDocumentWithDetail extends AddDocumentAction {
37
38         public AddUrlDocumentWithDetail(ReadGraph graph, String relationUri)
39                         throws DatabaseException {
40                 super(graph, relationUri);
41         }
42
43
44         @Override
45         public Runnable create(Object target) {
46                 if(!(target instanceof Resource))
47                         return null;
48                 final Resource resource = (Resource)target;
49                 return new Runnable() {
50                         
51                         @Override
52                         public void run() {
53                                 final UrlDetailDialog dialog = new UrlDetailDialog(Display.getCurrent().getActiveShell(),resource);
54                                 if (dialog.open() != InputDialog.OK) {
55                                         dialog.getAnnotationConfigurator().dispose();
56                                         return;
57                                 }
58                                 final String uriString = dialog.getUrl();
59                                 final String name = dialog.getName();
60                                 Simantics.getSession().asyncRequest(new WriteRequest() {
61                                         @Override
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);
68                                         }
69                                 }, e -> {
70                                         dialog.getAnnotationConfigurator().dispose();
71                                         if (e != null)
72                                                 ExceptionUtils.logAndShowError("Cannot add URL link.", e);
73                                 });
74                         }
75                 };
76         }
77
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);
85         return urlDocument;
86         }
87         
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);
92             return urlResource;
93         }
94
95         
96 //      @Override
97 //      public Runnable create(Object target) {
98 //
99 //              if(!(target instanceof Resource))
100 //                      return null;
101 //
102 //              final Resource resource = (Resource)target;
103 //
104 //              return new Runnable() {
105 //                      @Override
106 //                      public void run() {
107 //                              InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Add URL", "Input URL", "", new URLValidator());
108 //                              if (dialog.open() != InputDialog.OK)
109 //                                      return;
110 //                              final String uriString = dialog.getValue();
111 //                              Simantics.getSession().asyncRequest(new WriteRequest() {
112 //                                      
113 //                                      @Override
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);
121 //                                              
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);
130 //                                              } else {
131 //                                                      // adding to a library
132 //                                                      graph.claim(resource, relation, urlDocument);
133 //                                              }
134 //                                      }
135 //                              },new Callback<DatabaseException>() {
136 //                                      
137 //                                      @Override
138 //                                      public void run(DatabaseException parameter) {
139 //                                              if (parameter != null) {
140 //                                                      ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
141 //                                              }
142 //                                              
143 //                                      }
144 //                              });
145 //                              
146 //                      }
147 //              };
148 //      }
149 }