1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document.ui.function;
\r
14 import java.util.TreeMap;
\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
75 private static boolean createDocument(WriteGraph graph, Resource resource, Resource model) throws DatabaseException {
\r
77 Layer0 L0 = Layer0.getInstance(graph);
\r
78 DocumentResource DOC = DocumentResource.getInstance(graph);
\r
80 if(graph.hasStatement(resource, DOC.HasDocumentation)) return true;
\r
82 Instances query = graph.adapt(DOC.DocumentTypeBinding, Instances.class);
\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
93 if(bindings.isEmpty()) return false;
\r
95 Resource binding = bindings.lastEntry().getValue();
\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
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
114 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
115 public static Object onCreateDocumentButton(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
117 return new FunctionImpl1<Object, Object>() {
\r
120 public Object apply(Object _o) {
\r
122 Simantics.async(new ReadRequest() {
\r
125 public void run(ReadGraph graph) throws DatabaseException {
\r
127 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
\r
128 if(selection == null) return;
\r
130 final Resource input = selection.getRepresents(graph);
\r
131 if(input == null) return;
\r
133 graph.async(new WriteRequest() {
\r
136 public void perform(WriteGraph graph) throws DatabaseException {
\r
138 Resource model = graph.sync(new PossibleModel(input));
\r
139 if(model == null) return;
\r
140 createDocument(graph, input, model);
\r
158 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
159 public static Object onDeleteDocumentButton(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
161 return new FunctionImpl1<Object, Object>() {
\r
164 public Object apply(Object _o) {
\r
166 Simantics.async(new ReadRequest() {
\r
169 public void run(ReadGraph graph) throws DatabaseException {
\r
171 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
\r
172 if(selection == null) return;
\r
174 final Resource input = selection.getRepresents(graph);
\r
175 if(input == null) return;
\r
177 DocumentResource DOC = DocumentResource.getInstance(graph);
\r
178 if(!graph.hasStatement(input, DOC.HasDocumentation)) return;
\r
180 graph.async(new WriteRequest() {
\r
183 public void perform(WriteGraph graph) throws DatabaseException {
\r
185 DocumentResource DOC = DocumentResource.getInstance(graph);
\r
186 Resource document = graph.getPossibleObject(input, DOC.HasDocumentation);
\r
187 if(document == null) return;
\r
189 RemoverUtil.remove(graph, document);
\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
210 return new FunctionImpl3<WriteGraph, Variable, Variable, Boolean>() {
\r
213 public Boolean apply(WriteGraph graph, Variable editor, Variable input) {
\r
216 // return createDocument(graph, input);
\r
217 // } catch (DatabaseException e) {
\r
218 // Logger.defaultLogError(e);
\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
232 DocumentResource DOC = DocumentResource.getInstance(graph);
\r
234 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
\r
235 if(selection == null) return false;
\r
237 Resource input = selection.getRepresents(graph);
\r
238 if(input == null) return null;
\r
240 return graph.hasStatement(input, DOC.HasDocumentation);
\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
247 return new FunctionImpl1<Variable, Boolean>() {
\r
250 public Boolean apply(final Variable viewVariable) {
\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
265 return Boolean.FALSE;
\r
266 Resource range = graph.getPossibleObject(p, L0.HasRange);
\r
267 return DOC.WikiDocument_WikiText.equals(range);
\r
270 private static Variable getDefaultSelection(ReadGraph graph, Variable context) throws DatabaseException {
\r
272 DocumentResource DOC = DocumentResource.getInstance(graph);
\r
273 Variable sel = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\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
279 Variable runtime = ScenegraphLoaderUtils.getRuntimeVariable(graph, context);
\r
280 INode root = runtime.adapt(graph, INode.class);
\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
292 private static Variable resolveEditSelection(ReadGraph graph, Variable context, String path) throws DatabaseException {
\r
294 final ScenegraphPropertyReference<Variable> selectionReference =
\r
295 ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, path);
\r
297 Variable editSelection = selectionReference.getExternalValue(graph);
\r
298 if(editSelection == null) editSelection = getDefaultSelection(graph, context);
\r
300 return editSelection;
\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
307 final ScenegraphPropertyReference<String> textReference = ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, ".../TextContainer/Text#text");
\r
309 return new FunctionImpl1<Object, Object>() {
\r
312 public Object apply(final Object event) {
\r
314 final String value = textReference.getValue();
\r
317 Simantics.getSession().sync(new WriteRequest() {
\r
320 public void perform(WriteGraph graph) throws DatabaseException {
\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
326 System.err.println("no selection for resource : " + resource + ", Variable context : " + context + ", value : " + value);
\r
331 } catch (DatabaseException e) {
\r
332 e.printStackTrace();
\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
346 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
\r
347 public static String selectedDocumentPart(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
\r
349 Variable selection = resolveEditSelection(graph, context, ".../Scroll/Browser#edited");
\r
350 if(selection == null) return null;
\r
352 return selection.getValue(graph);
\r
356 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
357 public static Object documentStructureSelected(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
\r
359 return new FunctionImpl1<Object, Boolean>() {
\r
362 public Boolean apply(Object _event) {
\r
364 Event event = (Event)_event;
\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
370 ISelectionProvider provider = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getSelectionProvider();
\r
371 provider.setSelection(new StructuredSelection(entry));
\r
381 abstract static class WikiButtonModifier extends FunctionImpl1<Object, Object> {
\r
383 final ScenegraphPropertyReference<String> textReference;
\r
384 final ScenegraphPropertyReference<Point> selectionReference;
\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
391 abstract void perform(String before, String selected, String after, Point selection);
\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
406 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
407 public static Object boldModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
409 return new WikiButtonModifier(graph, context) {
\r
412 void perform(String before, String selected, String after, Point selection) {
\r
414 if(selected.isEmpty()) {
\r
415 textReference.setValue(before + "'''bold text'''" + after);
\r
417 textReference.setValue(before + "'''" + selected + "'''" + after);
\r
426 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
427 public static Object italicModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
429 return new WikiButtonModifier(graph, context) {
\r
432 void perform(String before, String selected, String after, Point selection) {
\r
434 if(selected.isEmpty()) {
\r
435 textReference.setValue(before + "''italic text''" + after);
\r
437 textReference.setValue(before + "''" + selected + "''" + after);
\r
446 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
447 public static Object strikethroughModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
449 return new WikiButtonModifier(graph, context) {
\r
452 void perform(String before, String selected, String after, Point selection) {
\r
454 if(selected.isEmpty()) {
\r
455 textReference.setValue(before + "<span style=\"text-decoration:line-through;\">strikethrough text</span>" + after);
\r
457 textReference.setValue(before + "<span style=\"text-decoration:line-through;\">" + selected + "</span>" + after);
\r
466 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
467 public static Object underlineModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
469 return new WikiButtonModifier(graph, context) {
\r
472 void perform(String before, String selected, String after, Point selection) {
\r
474 if(selected.isEmpty()) {
\r
475 textReference.setValue(before + "<span style=\"text-decoration:underline;\">strikethrough text</span>" + after);
\r
477 textReference.setValue(before + "<span style=\"text-decoration:underline;\">" + selected + "</span>" + after);
\r
486 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
487 public static Object horizontalRulerModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
489 return new WikiButtonModifier(graph, context) {
\r
492 void perform(String before, String selected, String after, Point selection) {
\r
494 textReference.setValue(before + "\r\n<hr/>\r\n" + selected + after);
\r
502 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
503 public static Object indentModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
505 return new WikiButtonModifier(graph, context) {
\r
508 void perform(String before, String selected, String after, Point selection) {
\r
510 textReference.setValue(before + ":" + selected + after);
\r
518 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
519 public static Object fontModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
521 return new WikiButtonModifier(graph, context) {
\r
523 private String hex2(int value) {
\r
524 String result = Integer.toHexString(value);
\r
525 if(result.length() == 1) result = "0" + result;
\r
530 void perform(String before, String selected, String after, Point selection) {
\r
532 FontDialog dialog = new FontDialog(Display.getCurrent().getActiveShell());
\r
533 FontData data = dialog.open();
\r
534 if(data == null) return;
\r
536 String family = data.getName();
\r
537 int size = data.getHeight();
\r
539 RGB rgb = dialog.getRGB();
\r
540 String hex = hex2(rgb.red) + hex2(rgb.green) + hex2(rgb.blue);
\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
545 textReference.setValue(before + "<font style=\"font-size:" + size + ";color: #" + hex + ";font-family:" + family + ";\" >" + selected + "</font>" + after);
\r
554 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
555 public static Object imageModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
557 return new WikiButtonModifier(graph, context) {
\r
560 void perform(String before, String selected, String after, Point selection) {
\r
562 textReference.setValue(before + "[[Image:root://Library/image.png|100px]]" + "\r\n" + selected + after);
\r
570 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
571 public static Object header1Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
573 return new WikiButtonModifier(graph, context) {
\r
576 void perform(String before, String selected, String after, Point selection) {
\r
578 textReference.setValue(before + "\r\n= Header 1 =\r\n" + selected + after);
\r
586 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
587 public static Object header2Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
589 return new WikiButtonModifier(graph, context) {
\r
592 void perform(String before, String selected, String after, Point selection) {
\r
594 textReference.setValue(before + "\r\n== Header 2 ==\r\n" + selected + after);
\r
602 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
603 public static Object header3Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
605 return new WikiButtonModifier(graph, context) {
\r
608 void perform(String before, String selected, String after, Point selection) {
\r
610 textReference.setValue(before + "\r\n=== Header 3 ===\r\n" + selected + after);
\r
618 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
619 public static Object header4Modifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
621 return new WikiButtonModifier(graph, context) {
\r
624 void perform(String before, String selected, String after, Point selection) {
\r
626 textReference.setValue(before + "\r\n==== Header 4 ====\r\n" + selected + after);
\r
634 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
635 public static Object numberedListModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
637 return new WikiButtonModifier(graph, context) {
\r
640 void perform(String before, String selected, String after, Point selection) {
\r
642 textReference.setValue(before + "\r\n" +
\r
645 "## Item2.1\r\n" +
\r
646 "# Item3\r\n" + selected + after);
\r
654 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
655 public static Object bulletListModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
657 return new WikiButtonModifier(graph, context) {
\r
660 void perform(String before, String selected, String after, Point selection) {
\r
662 textReference.setValue(before + "\r\n" +
\r
665 "** Item2.1\r\n" +
\r
666 "* Item3\r\n" + selected + after);
\r
674 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
675 public static Object tableModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
677 return new WikiButtonModifier(graph, context) {
\r
680 void perform(String before, String selected, String after, Point selection) {
\r
682 textReference.setValue(before + "\r\n" +
\r
683 "{| border=\"1\"\r\n" +
\r
687 "| cell || cell2\r\n" +
\r
691 "|}\r\n" + selected + after);
\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
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
711 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
712 public static Object linkModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
714 return new WikiButtonModifier(graph, context) {
\r
717 void perform(String before, String selected, String after, Point selection) {
\r
719 textReference.setValue(before +
\r
720 "[http://www.simantics.org External Website Link]\r\n" + selected + after);
\r
728 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
\r
729 public static Object styleModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
731 return new WikiButtonModifier(graph, context) {
\r
734 void perform(String before, String selected, String after, Point selection) {
\r
736 Simantics.getSession().asyncRequest(new ReadRequest() {
\r
739 public void run(ReadGraph graph) throws DatabaseException {
\r
741 Variable sel = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\r
742 Resource root = sel.getIndexRoot(graph);
\r
743 String editorId = CSSEditor.EDITOR_ID;
\r
746 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
749 public void run() {
\r
752 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, root, root, rvi));
\r
753 } catch (PartInitException e) {
\r
754 Logger.defaultLogError(e);
\r
772 // @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
\r
773 // public static Object navigate(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
\r
775 // return new FunctionImpl1<String, Boolean>() {
\r
778 // public Boolean apply(final String path) {
\r
780 // Simantics.getSession().asyncRequest(new ReadRequest() {
\r
783 // public void run(ReadGraph graph) throws DatabaseException {
\r
785 // DocumentResource DOC = DocumentResource.getInstance(graph);
\r
786 // Variable sel = ScenegraphLoaderUtils.getVariableSelection(graph, context);
\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
792 // Variable runtime = ScenegraphLoaderUtils.getRuntimeVariable(graph, context);
\r
793 // INode root = runtime.adapt(graph, INode.class);
\r
795 // Variable result = ScenegraphLoaderProcess.getVariable(graph, null, scenegraph, ScenegraphLoaderUtils.getRuntime(graph, context), root);
\r
796 // Variable location = result.browse(graph, path);
\r
798 // ScenegraphPropertyReference<Variable> editReference = ScenegraphLoaderUtils.getRelativePropertyReference(SWTThread.getThreadAccess(), graph, context, ".#edited");
\r
799 // editReference.setValue(location);
\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
816 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
\r
817 if(selection == null) return "<no input>";
\r
819 Resource input = selection.getRepresents(graph);
\r
820 if(input == null) return "<no input>";
\r
822 String path = DocumentUtils.indexRootPath(graph, selection);
\r
823 if(!path.isEmpty()) {
\r
824 return "for " + path + selection.getName(graph);
\r
827 return "for " + NameUtils.getSafeLabel(graph, input);
\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
834 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
\r
835 if(selection == null) return false;
\r
837 Resource input = selection.getRepresents(graph);
\r
838 if(input == null) return false;
\r
844 private static class ResolveURI extends UnaryRead<String, Object> {
\r
845 public ResolveURI(String uri) {
\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
857 private static final Function1<Object, Boolean> PERFORM_DEFAULT_ACTION_FOR_URI_RESOURCE = new Function1<Object, Boolean>() {
\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
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
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
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
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