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