]> gerrit.simantics Code Review - simantics/platform.git/blob
5ed7a1a2bcf0926fb20a5fc417bafc86e877da49
[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.datastructures.Callback;
29 import org.simantics.utils.ui.ExceptionUtils;
30
31 /**
32  * Action for adding URI-links as documents.
33  * 
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class AddUrlDocumentWithDetail extends AddDocumentAction {
38
39         public AddUrlDocumentWithDetail(ReadGraph graph, String relationUri)
40                         throws DatabaseException {
41                 super(graph, relationUri);
42         }
43
44
45         @Override
46         public Runnable create(Object target) {
47                 if(!(target instanceof Resource))
48                         return null;
49                 final Resource resource = (Resource)target;
50                 return new Runnable() {
51                         
52                         @Override
53                         public void run() {
54                                 final UrlDetailDialog dialog = new UrlDetailDialog(Display.getCurrent().getActiveShell(),resource);
55                                 if (dialog.open() != InputDialog.OK) {
56                                         dialog.getAnnotationConfigurator().dispose();
57                                         return;
58                                 }
59                                 final String uriString = dialog.getUrl();
60                                 final String name = dialog.getName();
61                                 Simantics.getSession().asyncRequest(new WriteRequest() {
62                                         @Override
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);
69                                         }
70                                 },new Callback<DatabaseException>() {
71                                         @Override
72                                         public void run(DatabaseException parameter) {
73                                                 dialog.getAnnotationConfigurator().dispose();
74                                                 if (parameter != null) {
75                                                         ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
76                                                 }
77                                                 
78                                         }
79                                 });
80                         }
81                 };
82         }
83
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);
91         return urlDocument;
92         }
93         
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);
98             return urlResource;
99         }
100
101         
102 //      @Override
103 //      public Runnable create(Object target) {
104 //
105 //              if(!(target instanceof Resource))
106 //                      return null;
107 //
108 //              final Resource resource = (Resource)target;
109 //
110 //              return new Runnable() {
111 //                      @Override
112 //                      public void run() {
113 //                              InputDialog dialog = new InputDialog(Display.getCurrent().getActiveShell(), "Add URL", "Input URL", "", new URLValidator());
114 //                              if (dialog.open() != InputDialog.OK)
115 //                                      return;
116 //                              final String uriString = dialog.getValue();
117 //                              Simantics.getSession().asyncRequest(new WriteRequest() {
118 //                                      
119 //                                      @Override
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);
127 //                                              
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);
136 //                                              } else {
137 //                                                      // adding to a library
138 //                                                      graph.claim(resource, relation, urlDocument);
139 //                                              }
140 //                                      }
141 //                              },new Callback<DatabaseException>() {
142 //                                      
143 //                                      @Override
144 //                                      public void run(DatabaseException parameter) {
145 //                                              if (parameter != null) {
146 //                                                      ExceptionUtils.logAndShowError("Cannot add URL link.", parameter);
147 //                                              }
148 //                                              
149 //                                      }
150 //                              });
151 //                              
152 //                      }
153 //              };
154 //      }
155 }