From: Antti Villberg Date: Sat, 11 Apr 2020 17:08:19 +0000 (+0300) Subject: Move some GraphFile UI from Simupedia to platform X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=d505a72b5e6499a3eba646765575d28b7409ecdb Move some GraphFile UI from Simupedia to platform gitlab #516 Change-Id: I01793863c377c7a44ca4cf5938e850f7d91e154c --- diff --git a/bundles/org.simantics.graphfile.ui.ontology/.classpath b/bundles/org.simantics.graphfile.ui.ontology/.classpath new file mode 100644 index 000000000..eca7bdba8 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui.ontology/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/bundles/org.simantics.graphfile.ui.ontology/.project b/bundles/org.simantics.graphfile.ui.ontology/.project new file mode 100644 index 000000000..740f3f884 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui.ontology/.project @@ -0,0 +1,34 @@ + + + org.simantics.graphfile.ui.ontology + + + + + + org.simantics.graph.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + org.simantics.graph.nature + + diff --git a/bundles/org.simantics.graphfile.ui.ontology/META-INF/MANIFEST.MF b/bundles/org.simantics.graphfile.ui.ontology/META-INF/MANIFEST.MF new file mode 100644 index 000000000..05c34057d --- /dev/null +++ b/bundles/org.simantics.graphfile.ui.ontology/META-INF/MANIFEST.MF @@ -0,0 +1,13 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: http://www.simantics.org/GraphFileUI +Bundle-SymbolicName: org.simantics.graphfile.ui.ontology +Bundle-Version: 1.0.0.qualifier +Automatic-Module-Name: org.simantics.graphfile.ui.ontology +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Require-Bundle: org.simantics.modeling.ontology;bundle-version="1.2.0", + org.simantics.silk.ontology;bundle-version="1.1.0", + org.simantics.layer0;bundle-version="1.1.0", + org.simantics.viewpoint.ontology;bundle-version="1.2.0", + org.simantics.action.ontology;bundle-version="1.1.0", + org.simantics.graphfile.ontology;bundle-version="0.1.0" diff --git a/bundles/org.simantics.graphfile.ui.ontology/build.properties b/bundles/org.simantics.graphfile.ui.ontology/build.properties new file mode 100644 index 000000000..e85b630a0 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui.ontology/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + graph.tg diff --git a/bundles/org.simantics.graphfile.ui.ontology/graph/GraphFileUI.pgraph b/bundles/org.simantics.graphfile.ui.ontology/graph/GraphFileUI.pgraph new file mode 100644 index 000000000..196296ec4 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui.ontology/graph/GraphFileUI.pgraph @@ -0,0 +1,48 @@ +L0 = +MOD = +SILK = +VP = +ACT = +GF = + +GFUI = : L0.Ontology + @L0.new + L0.HasResourceClass "org.simantics.graphfile.ui.ontology.GraphFileUIResource" : L0.String + +GFUI.BrowseContext : VP.BrowseContext + +ACTIONS = GFUI.BrowseContext.Actions : L0.Library + +ACTIONS.ImportFileDocument : ACT.Action +ACTIONS.ImportDocumentFolder : ACT.Action +ACTIONS.NewFileDocument : ACT.Action +ACTIONS.NewDocumentFolder : ACT.Action +ACTIONS.ExportFileDocument : ACT.Action +ACTIONS.ExportDocumentFolder : ACT.Action + +GFUI.ModelingBrowseContext : VP.BrowseContext + VP.BrowseContext.IsIncludedIn MOD.ModelingBrowseContext + @VP.relationChildRule L0.Library L0.ConsistsOf GF.Folder + @VP.relationChildRule L0.Library L0.ConsistsOf GF.File + @VP.relationChildRule GF.Folder L0.ConsistsOf GF.File + @VP.relationChildRule GF.Folder L0.ConsistsOf GF.Folder + + @VP.constantImageRule GF.Folder SILK.folder_page + @VP.constantImageRule GF.File SILK.page_white + @VP.customLabelRule GF.File VP.ResourceNameLabelRule + +GFUI.ModelingActionContext : VP.BrowseContext + VP.BrowseContext.IsIncludedIn MOD.ModelingActionContext + @VP.actionContribution "Folder" L0.Library SILK.folder_page VP.NewActionCategory ACTIONS.NewDocumentFolder + @VP.actionContribution "Folder" GF.Folder SILK.folder_page VP.NewActionCategory ACTIONS.NewDocumentFolder + @VP.actionContribution "File" L0.Library SILK.page_white VP.NewActionCategory ACTIONS.NewFileDocument + @VP.actionContribution "File" GF.Folder SILK.page_white VP.NewActionCategory ACTIONS.NewFileDocument + + @VP.actionContribution "Files" L0.Library SILK.page_white VP.ImportActionCategory ACTIONS.ImportFileDocument + @VP.actionContribution "Files" GF.Folder SILK.page_white VP.ImportActionCategory ACTIONS.ImportFileDocument + + @VP.actionContribution "Folder" L0.Library SILK.folder_page VP.ImportActionCategory ACTIONS.ImportDocumentFolder + @VP.actionContribution "Folder" GF.Folder SILK.folder_page VP.ImportActionCategory ACTIONS.ImportDocumentFolder + + @VP.actionContribution "File" GF.File SILK.page_white VP.ExportActionCategory ACTIONS.ExportFileDocument + @VP.actionContribution "Folder" GF.Folder SILK.folder_page VP.ExportActionCategory ACTIONS.ExportDocumentFolder \ No newline at end of file diff --git a/bundles/org.simantics.graphfile.ui/.classpath b/bundles/org.simantics.graphfile.ui/.classpath new file mode 100644 index 000000000..eca7bdba8 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/bundles/org.simantics.graphfile.ui/.project b/bundles/org.simantics.graphfile.ui/.project new file mode 100644 index 000000000..7533101c2 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/.project @@ -0,0 +1,28 @@ + + + org.simantics.graphfile.ui + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/bundles/org.simantics.graphfile.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.graphfile.ui/META-INF/MANIFEST.MF new file mode 100644 index 000000000..6c0a3817f --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/META-INF/MANIFEST.MF @@ -0,0 +1,13 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: GraphfileUI +Bundle-SymbolicName: org.simantics.graphfile.ui;singleton:=true +Bundle-Version: 1.0.0.qualifier +Automatic-Module-Name: org.simantics.graphfile.ui +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Require-Bundle: org.eclipse.ui.editors, + org.simantics.ui, + org.eclipse.jface.text;bundle-version="3.12.0", + org.simantics.graphfile.ontology, + org.slf4j.api;bundle-version="1.7.25", + org.simantics.document.ui;bundle-version="1.0.0" diff --git a/bundles/org.simantics.graphfile.ui/adapters.xml b/bundles/org.simantics.graphfile.ui/adapters.xml new file mode 100644 index 000000000..6eceb37e4 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/adapters.xml @@ -0,0 +1,50 @@ + + + + + + + + http://www.simantics.org/Layer0-0.0/ConsistsOf + + + + http://www.simantics.org/GraphFile-0.0/Folder + http://www.simantics.org/Layer0-0.0/ConsistsOf + + + + http://www.simantics.org/Layer0-0.0/ConsistsOf + File + + + + http://www.simantics.org/GraphFile-0.0/Folder + http://www.simantics.org/Layer0-0.0/ConsistsOf + + + + + http://www.simantics.org/Layer0-0.0/ConsistsOf + false + + + + \ No newline at end of file diff --git a/bundles/org.simantics.graphfile.ui/build.properties b/bundles/org.simantics.graphfile.ui/build.properties new file mode 100644 index 000000000..2df8c94f4 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/build.properties @@ -0,0 +1,6 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + plugin.xml,\ + adapters.xml diff --git a/bundles/org.simantics.graphfile.ui/plugin.xml b/bundles/org.simantics.graphfile.ui/plugin.xml new file mode 100644 index 000000000..925a7f3f8 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/plugin.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + diff --git a/bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditor.java b/bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditor.java new file mode 100644 index 000000000..d30210f30 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditor.java @@ -0,0 +1,92 @@ +package org.simantics.graphfile.ui.editor; + +import java.util.function.Supplier; + +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorSite; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.editors.text.TextEditor; +import org.simantics.Simantics; +import org.simantics.db.Session; +import org.simantics.db.common.request.ParametrizedRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.request.combinations.Combinators; +import org.simantics.ui.workbench.IResourceEditorInput; +import org.simantics.ui.workbench.ResourceEditorSupport; +import org.simantics.ui.workbench.TitleRequest; +import org.simantics.ui.workbench.TitleUpdater; +import org.simantics.ui.workbench.ToolTipRequest; +import org.simantics.ui.workbench.editor.input.InputValidationCombinators; + +/** + * @author Antti Villberg + */ +public class PlainTextEditor extends TextEditor { + + boolean isDisposed = false; + + ParametrizedRead INPUT_VALIDATOR = + Combinators.compose( + InputValidationCombinators.hasURI(), + InputValidationCombinators.extractInputResource() + ); + + ResourceEditorSupport support; + + public PlainTextEditor() { + super(); + setDocumentProvider(new PlainTextEditorDocumentProvider()); + //setSourceViewerConfiguration(new QueueTextEditorViewerConfiguration(new QueueTextEditorColorManager())); + } + + @Override + public void init(IEditorSite site, IEditorInput input) throws PartInitException { + super.init(site, input); + + support = new ResourceEditorSupport(this, INPUT_VALIDATOR); + support.activateValidation(); + try { + getResourceEditorInput().init(null); + } catch (DatabaseException e) { + throw new PartInitException("Failed to initialize " + input, e); + } + } + + protected IResourceEditorInput getResourceEditorInput() { + return (IResourceEditorInput) getEditorInput(); + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + updatePartName(); + } + + private void updatePartName() { + setPartName(getEditorInput().getName()); + + Session session = Simantics.peekSession(); + if (session != null) { + Supplier disposedCallback = () -> isDisposed; + session.asyncRequest( + new TitleRequest(null, getResourceEditorInput()), + new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback)); + session.asyncRequest( + new ToolTipRequest(getSite().getId(), getResourceEditorInput()), + new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback)); + } + + } + + @Override + public void dispose() { + isDisposed = true; + if (support != null) { + support.dispose(); + support = null; + } + super.dispose(); + } + +} diff --git a/bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditorDocumentProvider.java b/bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditorDocumentProvider.java new file mode 100644 index 000000000..3eb40ead5 --- /dev/null +++ b/bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditorDocumentProvider.java @@ -0,0 +1,108 @@ +package org.simantics.graphfile.ui.editor; + +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.operation.IRunnableContext; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.AnnotationModel; +import org.eclipse.jface.text.source.IAnnotationModel; +import org.eclipse.ui.texteditor.AbstractDocumentProvider; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.document.DocumentResource; +import org.simantics.graphfile.ontology.GraphFileResource; +import org.simantics.ui.workbench.ResourceEditorInput; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PlainTextEditorDocumentProvider extends AbstractDocumentProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(PlainTextEditor.class); + + protected String currentText; + + @Override + protected IDocument createDocument(Object element) throws CoreException { + ResourceEditorInput input = (ResourceEditorInput)element; + final Resource resource = input.getResource(); + try { + return Simantics.getSession().syncRequest(new UniqueRead() { + @Override + public Document perform(ReadGraph graph) throws DatabaseException { + + GraphFileResource GF = GraphFileResource.getInstance(graph); + DocumentResource DOC = DocumentResource.getInstance(graph); + if (!graph.isInstanceOf(resource, DOC.FileDocument)) + throw new DatabaseException("Invalid input resource for PlainTextEditor: " + NameUtils.getSafeName(graph, resource)); + + byte[] bytes = graph.getPossibleRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY); + + currentText = bytes != null ? new String(bytes, StandardCharsets.UTF_8) : ""; + + return new Document(currentText); + + } + }); + } catch (DatabaseException e) { + StringWriter sw = new StringWriter(); + LOGGER.error("Failed to create document", e); + return new Document(sw.toString()); + } + } + + @Override + protected IAnnotationModel createAnnotationModel(Object element) + throws CoreException { + return new AnnotationModel(); + } + + @Override + protected void doSaveDocument(IProgressMonitor monitor, Object element, + IDocument document, boolean overwrite) throws CoreException { + ResourceEditorInput input = (ResourceEditorInput)element; + final Resource resource = input.getResource(); + final String text = document.get(); + currentText = text; + Simantics.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + GraphFileResource GF = GraphFileResource.getInstance(graph); + graph.claimLiteral(resource, GF.HasFiledata, text.getBytes(StandardCharsets.UTF_8)); + graph.claimLiteral(resource, GF.LastModified, System.currentTimeMillis()); + } + }); + } + + @Override + protected IRunnableContext getOperationRunner(IProgressMonitor monitor) { + return null; + } + + @Override + public boolean isModifiable(Object element) { + return true; + } + + @Override + public boolean isReadOnly(Object element) { + return false; + } + + @Override + public boolean canSaveDocument(Object element) { + return !getDocument(element).get().equals(currentText); + } + +} diff --git a/bundles/pom.xml b/bundles/pom.xml index 59e88c12b..e1abeaf48 100644 --- a/bundles/pom.xml +++ b/bundles/pom.xml @@ -133,6 +133,8 @@ org.simantics.graph.db org.simantics.graphfile org.simantics.graphfile.ontology + org.simantics.graphfile.ui + org.simantics.graphfile.ui.ontology org.simantics.graphviz org.simantics.graphviz.ui org.simantics.gnuplot