]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/function/All.java
Several Wiki documentation view improvements.
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / function / All.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.document.ui.function;\r
13 \r
14 import java.util.TreeMap;\r
15 \r
16 import org.eclipse.core.runtime.IStatus;\r
17 import org.eclipse.core.runtime.Status;\r
18 import org.eclipse.jface.viewers.ISelectionProvider;\r
19 import org.eclipse.jface.viewers.StructuredSelection;\r
20 import org.eclipse.swt.browser.LocationEvent;\r
21 import org.eclipse.swt.graphics.FontData;\r
22 import org.eclipse.swt.graphics.Point;\r
23 import org.eclipse.swt.graphics.RGB;\r
24 import org.eclipse.swt.widgets.Display;\r
25 import org.eclipse.swt.widgets.Event;\r
26 import org.eclipse.swt.widgets.FontDialog;\r
27 import org.eclipse.swt.widgets.TreeItem;\r
28 import org.eclipse.ui.PartInitException;\r
29 import org.eclipse.ui.PlatformUI;\r
30 import org.simantics.Simantics;\r
31 import org.simantics.browsing.ui.BuiltinKeys;\r
32 import org.simantics.browsing.ui.NodeContext;\r
33 import org.simantics.databoard.Bindings;\r
34 import org.simantics.db.ReadGraph;\r
35 import org.simantics.db.Resource;\r
36 import org.simantics.db.Session;\r
37 import org.simantics.db.WriteGraph;\r
38 import org.simantics.db.common.request.ReadRequest;\r
39 import org.simantics.db.common.request.UnaryRead;\r
40 import org.simantics.db.common.request.WriteRequest;\r
41 import org.simantics.db.common.utils.Logger;\r
42 import org.simantics.db.common.utils.NameUtils;\r
43 import org.simantics.db.common.utils.RequestUtil;\r
44 import org.simantics.db.exception.DatabaseException;\r
45 import org.simantics.db.layer0.adapter.Instances;\r
46 import org.simantics.db.layer0.request.PossibleModel;\r
47 import org.simantics.db.layer0.request.PossibleResource;\r
48 import org.simantics.db.layer0.util.RemoverUtil;\r
49 import org.simantics.db.layer0.variable.RVI;\r
50 import org.simantics.db.layer0.variable.Variable;\r
51 import org.simantics.db.layer0.variable.Variables;\r
52 import org.simantics.document.DocumentDialect;\r
53 import org.simantics.document.DocumentResource;\r
54 import org.simantics.document.DocumentUtils;\r
55 import org.simantics.document.ui.Activator;\r
56 import org.simantics.document.ui.CSSEditor;\r
57 import org.simantics.layer0.Layer0;\r
58 import org.simantics.scenegraph.INode;\r
59 import org.simantics.scenegraph.loader.ScenegraphLoaderProcess;\r
60 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;\r
61 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils.ScenegraphPropertyReference;\r
62 import org.simantics.scl.reflection.annotations.SCLValue;\r
63 import org.simantics.scl.runtime.function.Function1;\r
64 import org.simantics.scl.runtime.function.FunctionImpl1;\r
65 import org.simantics.scl.runtime.function.FunctionImpl3;\r
66 import org.simantics.ui.SimanticsUI;\r
67 import org.simantics.ui.workbench.ResourceEditorInput2;\r
68 import org.simantics.ui.workbench.action.DefaultActions;\r
69 import org.simantics.utils.threads.SWTThread;\r
70 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
71 \r
72 \r
73 public class All {\r
74 \r
75         private static boolean createDocument(WriteGraph graph, Resource resource, Resource model) throws DatabaseException {\r
76 \r
77                 Layer0 L0 = Layer0.getInstance(graph);\r
78                 DocumentResource DOC = DocumentResource.getInstance(graph);\r
79 \r
80                 if(graph.hasStatement(resource, DOC.HasDocumentation)) return true;\r
81                 \r
82                 Instances query = graph.adapt(DOC.DocumentTypeBinding, Instances.class);\r
83                 \r
84                 TreeMap<Double, Resource> bindings = new TreeMap<Double, Resource>();\r
85                 for(Resource binding : query.find(graph, model)) {\r
86                         double priority = graph.getRelatedValue2(binding, DOC.DocumentTypeBinding_priority, Bindings.DOUBLE);\r
87                         Resource type = graph.getSingleObject(binding, DOC.DocumentTypeBinding_HasType);\r
88                         if(graph.isInstanceOf(resource, type)) {\r
89                                 bindings.put(priority, binding);\r
90                         }\r
91                 }\r
92                 \r
93                 if(bindings.isEmpty()) return false;\r
94                 \r
95                 Resource binding = bindings.lastEntry().getValue();\r
96                                 \r
97                 Resource documentType = graph.getSingleObject(binding, DOC.DocumentTypeBinding_HasDocumentType);\r
98                 Resource document = graph.newResource();\r
99                 graph.claim(document, L0.InstanceOf, null, DOC.ScenegraphDocument);\r
100                 graph.claimLiteral(document, L0.HasName, "Documentation");\r
101                 graph.claim(resource, DOC.HasDocumentation, document);\r
102                 graph.claim(document, L0.PartOf, resource);\r
103 \r
104                 Resource scenegraph = graph.newResource();\r
105                 graph.claim(scenegraph, L0.InstanceOf, null, documentType);\r
106                 graph.claimLiteral(scenegraph, L0.HasName, "Scenegraph");\r
107                 graph.claim(scenegraph, L0.PartOf, document);\r
108                 graph.claim(document, DOC.ScenegraphDocument_scenegraph, scenegraph);\r
109 \r
110                 return true;\r
111                 \r
112         }\r
113 \r
114     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
115     public static Object onCreateDocumentButton(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
116 \r
117         return new FunctionImpl1<Object, Object>() {\r
118 \r
119                         @Override\r
120                         public Object apply(Object _o) {\r
121                                 \r
122                                 Simantics.async(new ReadRequest() {\r
123 \r
124                                         @Override\r
125                                         public void run(ReadGraph graph) throws DatabaseException {\r
126                                                 \r
127                                         Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
128                                         if(selection == null) return;\r
129                                         \r
130                                                 final Resource input = selection.getRepresents(graph);\r
131                                         if(input == null) return;\r
132                                                 \r
133                                         graph.async(new WriteRequest() {\r
134 \r
135                                                         @Override\r
136                                                         public void perform(WriteGraph graph) throws DatabaseException {\r
137                                                                 \r
138                                                                 Resource model = graph.sync(new PossibleModel(input));\r
139                                                                 if(model == null) return;\r
140                                                                 createDocument(graph, input, model);\r
141                                                                 \r
142                                                         }\r
143                                                 \r
144                                         });\r
145                                         \r
146                                         }\r
147                                         \r
148                                 });\r
149                                 \r
150                                 return true;\r
151                                 \r
152                         }\r
153                         \r
154                 };\r
155         \r
156     }\r
157 \r
158     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
159     public static Object onDeleteDocumentButton(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
160 \r
161         return new FunctionImpl1<Object, Object>() {\r
162 \r
163                         @Override\r
164                         public Object apply(Object _o) {\r
165                                 \r
166                                 Simantics.async(new ReadRequest() {\r
167 \r
168                                         @Override\r
169                                         public void run(ReadGraph graph) throws DatabaseException {\r
170                                                 \r
171                                         Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
172                                         if(selection == null) return;\r
173                                         \r
174                                                 final Resource input = selection.getRepresents(graph);\r
175                                         if(input == null) return;\r
176                                         \r
177                                                 DocumentResource DOC = DocumentResource.getInstance(graph);\r
178                                                 if(!graph.hasStatement(input, DOC.HasDocumentation)) return;\r
179                                                 \r
180                                         graph.async(new WriteRequest() {\r
181 \r
182                                                         @Override\r
183                                                         public void perform(WriteGraph graph) throws DatabaseException {\r
184                                                                 \r
185                                                                 DocumentResource DOC = DocumentResource.getInstance(graph);\r
186                                                                 Resource document = graph.getPossibleObject(input, DOC.HasDocumentation);\r
187                                                                 if(document == null) return;\r
188                                                                 \r
189                                                                 RemoverUtil.remove(graph, document);\r
190                                                                 \r
191                                                         }\r
192                                                 \r
193                                         });\r
194                                         \r
195                                         }\r
196                                         \r
197                                 });\r
198                                 \r
199                                 return true;\r
200                                 \r
201                         }\r
202                         \r
203                 };\r
204         \r
205     }\r
206     \r
207     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
208     public static Object editorLoaded(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
209         \r
210         return new FunctionImpl3<WriteGraph, Variable, Variable, Boolean>() {\r
211 \r
212                         @Override\r
213                         public Boolean apply(WriteGraph graph, Variable editor, Variable input) {\r
214 \r
215 //                              try {\r
216 //                                      return createDocument(graph, input);\r
217 //                              } catch (DatabaseException e) {\r
218 //                                      Logger.defaultLogError(e);\r
219 //                              }\r
220 \r
221                                 return true;\r
222                                 \r
223                         }\r
224                 \r
225         };\r
226         \r
227     }\r
228 \r
229     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")\r
230     public static Boolean hasDocument(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
231         \r
232                 DocumentResource DOC = DocumentResource.getInstance(graph);\r
233                 \r
234         Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
235         if(selection == null) return false;\r
236         \r
237                 Resource input = selection.getRepresents(graph);\r
238         if(input == null) return null;\r
239         \r
240                 return graph.hasStatement(input, DOC.HasDocumentation);\r
241         \r
242     }\r
243 \r
244     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
245     public static Object viewInputChanged(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
246         \r
247         return new FunctionImpl1<Variable, Boolean>() {\r
248 \r
249                         @Override\r
250                         public Boolean apply(final Variable viewVariable) {\r
251                                 return true;\r
252                         }\r
253                 \r
254         };\r
255         \r
256     }\r
257     \r
258         \r
259     @SCLValue(type = "ReadGraph -> Variable -> Boolean")\r
260     public static Boolean isWikitext(ReadGraph graph, Variable context) throws DatabaseException {\r
261         Layer0 L0 = Layer0.getInstance(graph);\r
262         DocumentResource DOC = DocumentResource.getInstance(graph);\r
263         Resource p = context.getPossiblePredicateResource(graph);\r
264         if (p == null)\r
265             return Boolean.FALSE;\r
266         Resource range = graph.getPossibleObject(p, L0.HasRange);\r
267         return DOC.WikiDocument_WikiText.equals(range);\r
268     }\r
269     \r
270     private static Variable getDefaultSelection(ReadGraph graph, Variable context) throws DatabaseException {\r
271         \r
272                 DocumentResource DOC = DocumentResource.getInstance(graph);\r
273         Variable sel = ScenegraphLoaderUtils.getVariableSelection(graph, context);\r
274         \r
275         Resource represents = sel.getRepresents(graph);\r
276         Resource doc = graph.getSingleObject(represents, DOC.HasDocument);\r
277         Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph);\r
278         \r
279         Variable runtime = ScenegraphLoaderUtils.getRuntimeVariable(graph, context);\r
280                 INode root = runtime.adapt(graph, INode.class);\r
281 \r
282         Variable result = ScenegraphLoaderProcess.getVariable(graph, null, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context), root);\r
283         if(result != null) {\r
284                 Variable userDoc = result.getPossibleProperty(graph, "UserDocumentation");\r
285                 if(userDoc != null) return userDoc;\r
286         }\r
287         \r
288         return null;\r
289         \r
290     }\r
291     \r
292     private static Variable resolveEditSelection(ReadGraph graph, Variable context, String path) throws DatabaseException {\r
293         \r
294                 final ScenegraphPropertyReference<Variable> selectionReference = \r
295                                 ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, path);\r
296                 \r
297                 Variable editSelection = selectionReference.getExternalValue(graph);\r
298                 if(editSelection == null) editSelection = getDefaultSelection(graph, context);\r
299                 \r
300                 return editSelection;\r
301         \r
302     }\r
303 \r
304     @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
305     public static Object wikitextModifier(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
306         \r
307                 final ScenegraphPropertyReference<String> textReference = ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, ".../TextContainer/Text#text");\r
308         \r
309         return new FunctionImpl1<Object, Object>() {\r
310 \r
311                         @Override\r
312                         public Object apply(final Object event) {\r
313             \r
314                                 final String value = textReference.getValue();\r
315 \r
316                                 try {\r
317                                         Simantics.getSession().sync(new WriteRequest() {\r
318 \r
319                                                 @Override\r
320                                                 public void perform(WriteGraph graph) throws DatabaseException {\r
321                                                         \r
322                                                         Variable selection = resolveEditSelection(graph, context, "..../Scroll/Browser#edited");\r
323                                                         if (selection != null)\r
324                                                             selection.setValue(graph, (String)value, Bindings.STRING);\r
325                                                         else {\r
326                                                                 System.err.println("no selection for resource : " + resource + ", Variable context : " + context + ", value : " + value);\r
327                                                         }\r
328                                                 }\r
329                                                 \r
330                                         });\r
331                                 } catch (DatabaseException e) {\r
332                                         e.printStackTrace();\r
333                                 }\r
334                                 return null;\r
335                         }\r
336                 \r
337         };\r
338     }\r
339     \r
340     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")\r
341     public static String wikitext(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
342         Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);\r
343         return selection.getValue(graph);\r
344     }\r
345 \r
346     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")\r
347     public static String selectedDocumentPart(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
348 \r
349                 Variable selection = resolveEditSelection(graph, context, ".../Scroll/Browser#edited");\r
350                 if(selection == null) return null;\r
351 \r
352         return selection.getValue(graph);\r
353         \r
354     }\r
355     \r
356     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
357     public static Object documentStructureSelected(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {\r
358         \r
359         return new FunctionImpl1<Object, Boolean>() {\r
360                 \r
361             @Override\r
362             public Boolean apply(Object _event) {\r
363                 \r
364                 Event event = (Event)_event;\r
365                 \r
366                 final TreeItem item = (TreeItem)event.item;\r
367                 NodeContext context = (NodeContext)item.getData();\r
368                 final Variable entry = (Variable)context.getConstant(BuiltinKeys.INPUT);\r
369                 \r
370                 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();\r
371                 provider.setSelection(new StructuredSelection(entry));\r
372                 \r
373                 return null;\r
374                 \r
375             }\r
376                 \r
377         };\r
378         \r
379     }\r
380 \r
381     abstract static class WikiButtonModifier extends FunctionImpl1<Object, Object> {\r
382 \r
383                 final ScenegraphPropertyReference<String> textReference;\r
384                 final ScenegraphPropertyReference<Point> selectionReference;\r
385 \r
386         public WikiButtonModifier(ReadGraph graph, Variable context) throws DatabaseException {\r
387                 textReference = ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, ".../TextContainer/Text#text");\r
388                 selectionReference = ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, ".../TextContainer/Text#selection");\r
389         }\r
390         \r
391         abstract void perform(String before, String selected, String after, Point selection);\r
392         \r
393             @Override\r
394             public Object apply(Object _event) {\r
395                 String text = textReference.getValue();\r
396                 Point selection = selectionReference.getValue(); \r
397                 String before = text.substring(0, selection.x);\r
398                 String selected = text.substring(selection.x, selection.y);\r
399                 String after = text.substring(selection.y);\r
400                 perform(before, selected, after, selection);\r
401                 return null;\r
402             }\r
403 \r
404     }\r
405     \r
406     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
407     public static Object boldModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
408 \r
409         return new WikiButtonModifier(graph, context) {\r
410                         \r
411                         @Override\r
412                         void perform(String before, String selected, String after, Point selection) {\r
413 \r
414                                 if(selected.isEmpty()) {\r
415                                         textReference.setValue(before + "'''bold text'''" + after);\r
416                                 } else {\r
417                                         textReference.setValue(before + "'''" + selected + "'''" + after);      \r
418                                 }\r
419                                 \r
420                         }\r
421                         \r
422                 };\r
423         \r
424     }\r
425 \r
426     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
427     public static Object italicModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
428 \r
429         return new WikiButtonModifier(graph, context) {\r
430                         \r
431                         @Override\r
432                         void perform(String before, String selected, String after, Point selection) {\r
433 \r
434                                 if(selected.isEmpty()) {\r
435                                         textReference.setValue(before + "''italic text''" + after);\r
436                                 } else {\r
437                                         textReference.setValue(before + "''" + selected + "''" + after);        \r
438                                 }\r
439                                 \r
440                         }\r
441                         \r
442                 };\r
443         \r
444     }\r
445     \r
446     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
447     public static Object strikethroughModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
448 \r
449         return new WikiButtonModifier(graph, context) {\r
450                         \r
451                         @Override\r
452                         void perform(String before, String selected, String after, Point selection) {\r
453 \r
454                                 if(selected.isEmpty()) {\r
455                                         textReference.setValue(before + "<span style=\"text-decoration:line-through;\">strikethrough text</span>" + after);\r
456                                 } else {\r
457                                         textReference.setValue(before + "<span style=\"text-decoration:line-through;\">" + selected + "</span>" + after);       \r
458                                 }\r
459                                 \r
460                         }\r
461                         \r
462                 };\r
463         \r
464     }\r
465 \r
466     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
467     public static Object underlineModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
468 \r
469         return new WikiButtonModifier(graph, context) {\r
470                         \r
471                         @Override\r
472                         void perform(String before, String selected, String after, Point selection) {\r
473 \r
474                                 if(selected.isEmpty()) {\r
475                                         textReference.setValue(before + "<span style=\"text-decoration:underline;\">strikethrough text</span>" + after);\r
476                                 } else {\r
477                                         textReference.setValue(before + "<span style=\"text-decoration:underline;\">" + selected + "</span>" + after);  \r
478                                 }\r
479                                 \r
480                         }\r
481                         \r
482                 };\r
483         \r
484     }\r
485 \r
486     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
487     public static Object horizontalRulerModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
488 \r
489         return new WikiButtonModifier(graph, context) {\r
490                         \r
491                         @Override\r
492                         void perform(String before, String selected, String after, Point selection) {\r
493 \r
494                                 textReference.setValue(before + "\r\n<hr/>\r\n" + selected + after);\r
495                                 \r
496                         }\r
497                         \r
498                 };\r
499         \r
500     }\r
501     \r
502     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
503     public static Object indentModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
504 \r
505         return new WikiButtonModifier(graph, context) {\r
506                         \r
507                         @Override\r
508                         void perform(String before, String selected, String after, Point selection) {\r
509 \r
510                                 textReference.setValue(before + ":" + selected + after);\r
511                                 \r
512                         }\r
513                         \r
514                 };\r
515         \r
516     }\r
517 \r
518     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
519     public static Object fontModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
520 \r
521         return new WikiButtonModifier(graph, context) {\r
522                         \r
523                 private String hex2(int value) {\r
524                         String result = Integer.toHexString(value);\r
525                         if(result.length() == 1) result = "0" + result;\r
526                         return result;\r
527                 }\r
528                 \r
529                         @Override\r
530                         void perform(String before, String selected, String after, Point selection) {\r
531 \r
532                                 FontDialog dialog = new FontDialog(Display.getCurrent().getActiveShell()); \r
533                                 FontData data = dialog.open();\r
534                                 if(data == null) return;\r
535 \r
536                                 String family = data.getName();\r
537                                 int size = data.getHeight();\r
538                                 \r
539                                 RGB rgb = dialog.getRGB();\r
540                                 String hex = hex2(rgb.red) + hex2(rgb.green) + hex2(rgb.blue);\r
541 \r
542                                 if(selected.isEmpty()) {\r
543                                         textReference.setValue(before + "<font style=\"font-size:" + size + ";color: #" + hex + ";font-family:" + family + ";\" >formatted text</font>" + selected + after);\r
544                                 } else {\r
545                                         textReference.setValue(before + "<font style=\"font-size:" + size + ";color: #" + hex + ";font-family:" + family + ";\" >" + selected + "</font>" + after);\r
546                                 }\r
547                                 \r
548                         }\r
549                         \r
550                 };\r
551         \r
552     }\r
553 \r
554     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
555     public static Object imageModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
556 \r
557         return new WikiButtonModifier(graph, context) {\r
558                 \r
559                         @Override\r
560                         void perform(String before, String selected, String after, Point selection) {\r
561 \r
562                                 textReference.setValue(before + "[[Image:root://Library/image.png|100px]]" + "\r\n" + selected + after);\r
563                                 \r
564                         }\r
565                         \r
566                 };\r
567         \r
568     }\r
569     \r
570     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
571     public static Object header1Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
572 \r
573         return new WikiButtonModifier(graph, context) {\r
574                         \r
575                         @Override\r
576                         void perform(String before, String selected, String after, Point selection) {\r
577 \r
578                                 textReference.setValue(before + "\r\n= Header 1 =\r\n" + selected + after);\r
579                                 \r
580                         }\r
581                         \r
582                 };\r
583         \r
584     }\r
585 \r
586     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
587     public static Object header2Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
588 \r
589         return new WikiButtonModifier(graph, context) {\r
590                         \r
591                         @Override\r
592                         void perform(String before, String selected, String after, Point selection) {\r
593 \r
594                                 textReference.setValue(before + "\r\n== Header 2 ==\r\n" + selected + after);\r
595                                 \r
596                         }\r
597                         \r
598                 };\r
599         \r
600     }\r
601 \r
602     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
603     public static Object header3Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
604 \r
605         return new WikiButtonModifier(graph, context) {\r
606                         \r
607                         @Override\r
608                         void perform(String before, String selected, String after, Point selection) {\r
609 \r
610                                 textReference.setValue(before + "\r\n=== Header 3 ===\r\n" + selected + after);\r
611                                 \r
612                         }\r
613                         \r
614                 };\r
615         \r
616     }\r
617     \r
618     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
619     public static Object header4Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
620 \r
621         return new WikiButtonModifier(graph, context) {\r
622                         \r
623                         @Override\r
624                         void perform(String before, String selected, String after, Point selection) {\r
625 \r
626                                 textReference.setValue(before + "\r\n==== Header 4 ====\r\n" + selected + after);\r
627                                 \r
628                         }\r
629                         \r
630                 };\r
631         \r
632     }\r
633     \r
634     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
635     public static Object numberedListModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
636 \r
637         return new WikiButtonModifier(graph, context) {\r
638                         \r
639                         @Override\r
640                         void perform(String before, String selected, String after, Point selection) {\r
641 \r
642                                 textReference.setValue(before + "\r\n" +\r
643                                                 "# Item1\r\n" + \r
644                                                 "# Item2\r\n" + \r
645                                                 "## Item2.1\r\n" + \r
646                                                 "# Item3\r\n" + selected + after);\r
647                                 \r
648                         }\r
649                         \r
650                 };\r
651         \r
652     }\r
653 \r
654     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
655     public static Object bulletListModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
656 \r
657         return new WikiButtonModifier(graph, context) {\r
658                         \r
659                         @Override\r
660                         void perform(String before, String selected, String after, Point selection) {\r
661 \r
662                                 textReference.setValue(before + "\r\n" +\r
663                                                 "* Item1\r\n" + \r
664                                                 "* Item2\r\n" + \r
665                                                 "** Item2.1\r\n" + \r
666                                                 "* Item3\r\n" + selected + after);\r
667                                 \r
668                         }\r
669                         \r
670                 };\r
671         \r
672     }\r
673     \r
674     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
675     public static Object tableModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
676 \r
677         return new WikiButtonModifier(graph, context) {\r
678                         \r
679                         @Override\r
680                         void perform(String before, String selected, String after, Point selection) {\r
681 \r
682                                 textReference.setValue(before + "\r\n" +\r
683                                                 "{| border=\"1\"\r\n" + \r
684                                                 "! header\r\n" +\r
685                                                 "! header2 \r\n" +\r
686                                                 "|-\r\n" +\r
687                                                 "| cell || cell2\r\n" + \r
688                                                 "|-\r\n" +\r
689                                                 "| cell3\r\n" + \r
690                                                 "| cell4\r\n" + \r
691                                                 "|}\r\n"  + selected + after);\r
692                                 \r
693                         }\r
694                         \r
695                 };\r
696         \r
697     }\r
698 \r
699     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
700     public static Object internalLinkModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
701         return new WikiButtonModifier(graph, context) {\r
702             @Override\r
703             void perform(String before, String selected, String after, Point selection) {\r
704                 textReference.setValue(before +\r
705                         "[[Media:root://Documents/Document.pdf|Link to a file within the model]]\r\n" + selected + after);\r
706                 \r
707             }\r
708         };\r
709     }\r
710     \r
711     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
712     public static Object linkModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
713 \r
714         return new WikiButtonModifier(graph, context) {\r
715                         \r
716                         @Override\r
717                         void perform(String before, String selected, String after, Point selection) {\r
718 \r
719                                 textReference.setValue(before +\r
720                                                 "[http://www.simantics.org External Website Link]\r\n" + selected + after);\r
721                                 \r
722                         }\r
723                         \r
724                 };\r
725         \r
726     }\r
727     \r
728     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
729     public static Object styleModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
730 \r
731         return new WikiButtonModifier(graph, context) {\r
732                         \r
733                         @Override\r
734                         void perform(String before, String selected, String after, Point selection) {\r
735 \r
736                                 Simantics.getSession().asyncRequest(new ReadRequest() {\r
737 \r
738                                         @Override\r
739                                         public void run(ReadGraph graph) throws DatabaseException {\r
740 \r
741                                         Variable sel = ScenegraphLoaderUtils.getVariableSelection(graph, context);\r
742                                         Resource root = sel.getIndexRoot(graph);\r
743                                         String editorId = CSSEditor.EDITOR_ID;\r
744                                 RVI rvi = null;\r
745 \r
746                                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {\r
747                                         \r
748                                     @Override\r
749                                     public void run() {\r
750 \r
751                                                     try {\r
752                                                                         WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, root, root, rvi));\r
753                                                                 } catch (PartInitException e) {\r
754                                                                         Logger.defaultLogError(e);\r
755                                                                 }\r
756                                         \r
757                                     }\r
758                                     \r
759                                 });\r
760 \r
761                                         }\r
762                                         \r
763                                 });\r
764                                 \r
765                         }\r
766                         \r
767                 };\r
768         \r
769     }\r
770     \r
771     \r
772 //    @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")\r
773 //    public static Object navigate(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {\r
774 //      \r
775 //      return new FunctionImpl1<String, Boolean>() {\r
776 //\r
777 //                      @Override\r
778 //                      public Boolean apply(final String path) {\r
779 //                              \r
780 //                              Simantics.getSession().asyncRequest(new ReadRequest() {\r
781 //\r
782 //                                      @Override\r
783 //                                      public void run(ReadGraph graph) throws DatabaseException {\r
784 //                                              \r
785 //                                              DocumentResource DOC = DocumentResource.getInstance(graph);\r
786 //                                      Variable sel = ScenegraphLoaderUtils.getVariableSelection(graph, context);\r
787 //                                      \r
788 //                                      Resource represents = sel.getRepresents(graph);\r
789 //                                      Resource doc = graph.getSingleObject(represents, DOC.HasDocument);\r
790 //                                      Resource scenegraph = graph.getSingleObject(doc, DOC.ScenegraphDocument_scenegraph);\r
791 //                                      \r
792 //                                      Variable runtime = ScenegraphLoaderUtils.getRuntimeVariable(graph, context);\r
793 //                                              INode root = runtime.adapt(graph, INode.class);\r
794 //                                      \r
795 //                                      Variable result = ScenegraphLoaderProcess.getVariable(graph, null, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context), root);\r
796 //                                      Variable location = result.browse(graph, path);\r
797 //                                              \r
798 //                                              ScenegraphPropertyReference<Variable> editReference = ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, ".#edited");\r
799 //                                              editReference.setValue(location);\r
800 //                                              \r
801 //                                      }\r
802 //                                      \r
803 //                              });\r
804 //                              \r
805 //                              return null;\r
806 //                              \r
807 //                      }\r
808 //              \r
809 //      };\r
810 //      \r
811 //    }\r
812 \r
813     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")\r
814     public static String noDocumentText(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
815                 \r
816         Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
817         if(selection == null) return "<no input>";\r
818         \r
819                 Resource input = selection.getRepresents(graph);\r
820         if(input == null) return "<no input>";\r
821         \r
822         String path = DocumentUtils.indexRootPath(graph, selection);\r
823         if(!path.isEmpty()) {\r
824                 return "for " + path + selection.getName(graph);\r
825         }\r
826         \r
827                 return "for " + NameUtils.getSafeLabel(graph, input);\r
828         \r
829     }\r
830 \r
831     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")\r
832     public static Boolean canCreateDocument(ReadGraph graph, final Resource resource, final Variable context) throws DatabaseException {\r
833                 \r
834         Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
835         if(selection == null) return false;\r
836         \r
837                 Resource input = selection.getRepresents(graph);\r
838         if(input == null) return false;\r
839         \r
840         return true;\r
841         \r
842     }\r
843 \r
844     private static class ResolveURI extends UnaryRead<String, Object> {\r
845         public ResolveURI(String uri) {\r
846             super(uri);\r
847         }\r
848         @Override\r
849         public Object perform(ReadGraph graph) throws DatabaseException {\r
850             Object result = graph.syncRequest(new PossibleResource(parameter));\r
851             if (result == null)\r
852                 result = Variables.getPossibleVariable(graph, parameter);\r
853             return result;\r
854         }\r
855     }\r
856 \r
857     private static final Function1<Object, Boolean> PERFORM_DEFAULT_ACTION_FOR_URI_RESOURCE = new Function1<Object, Boolean>() {\r
858         @Override\r
859         public Boolean apply(Object p0) {\r
860             LocationEvent le = (LocationEvent) p0;\r
861             if (le.location.startsWith(DocumentDialect.SIMANTICS_INTERNAL_URI_PREFIX)) {\r
862                 // This is not a valid URL anyway so deny relocation.\r
863                 le.doit = false;\r
864 \r
865                 // Try to execute default action for the resource or variable\r
866                 // that the URI represents.\r
867                 String uri = le.location.substring(DocumentDialect.SIMANTICS_INTERNAL_URI_PREFIX.length());\r
868                 try {\r
869                     Session s = Simantics.getSession();\r
870                     Object input = RequestUtil.trySyncRequest(s,\r
871                             SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
872                             SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
873                             new ResolveURI(uri));\r
874                     if (input != null) {\r
875                         DefaultActions.asyncPerformDefaultAction(s, input, false, false, false);\r
876                     }\r
877                 } catch (DatabaseException | InterruptedException e) {\r
878                     Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to resolve URI to a database resource or variable: " + uri, e));\r
879                 }\r
880             }\r
881             return true;\r
882         }\r
883     };\r
884 \r
885     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")\r
886     public static Function1<Object, Boolean> locationChanging(ReadGraph graph, Resource variable, Variable context) throws DatabaseException {\r
887         return PERFORM_DEFAULT_ACTION_FOR_URI_RESOURCE;\r
888     }\r
889 \r
890 }\r