]> gerrit.simantics Code Review - simantics/platform.git/blob
824a0b5fbb8de26ebe6d6a138049809053dfa96a
[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.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;
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                                 },new Callback<DatabaseException>() {
70                                         @Override
71                                         public void run(DatabaseException parameter) {
72                                                 dialog.getAnnotationConfigurator().dispose();
73                                                 if (parameter != null) {
74                                                         ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
75                                                 }
76                                                 
77                                         }
78                                 });
79                         }
80                 };
81         }
82
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);
90         return urlDocument;
91         }
92         
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);
97             return urlResource;
98         }
99
100         
101 //      @Override
102 //      public Runnable create(Object target) {
103 //
104 //              if(!(target instanceof Resource))
105 //                      return null;
106 //
107 //              final Resource resource = (Resource)target;
108 //
109 //              return new Runnable() {
110 //                      @Override
111 //                      public void run() {
112 //                              InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Add URL", "Input URL", "", new URLValidator());
113 //                              if (dialog.open() != InputDialog.OK)
114 //                                      return;
115 //                              final String uriString = dialog.getValue();
116 //                              Simantics.getSession().asyncRequest(new WriteRequest() {
117 //                                      
118 //                                      @Override
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);
126 //                                              
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);
135 //                                              } else {
136 //                                                      // adding to a library
137 //                                                      graph.claim(resource, relation, urlDocument);
138 //                                              }
139 //                                      }
140 //                              },new Callback<DatabaseException>() {
141 //                                      
142 //                                      @Override
143 //                                      public void run(DatabaseException parameter) {
144 //                                              if (parameter != null) {
145 //                                                      ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
146 //                                              }
147 //                                              
148 //                                      }
149 //                              });
150 //                              
151 //                      }
152 //              };
153 //      }
154 }