X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fflag%2FDiagramFlagPreferences.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fflag%2FDiagramFlagPreferences.java;h=9756606221504173214c986a7c8e05ea4c8270b3;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/DiagramFlagPreferences.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/DiagramFlagPreferences.java new file mode 100644 index 000000000..975660622 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/DiagramFlagPreferences.java @@ -0,0 +1,109 @@ +package org.simantics.diagram.flag; + +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.primitiverequest.PossibleObject; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.ui.ErrorLogger; + +/** + * @author Tuukka Lehtonen + */ +public class DiagramFlagPreferences { + + /** + * Return the resource describing the currently active flag labeling scheme + * from the specified resource. The default intended resource for checking + * the scheme from is the active project. + */ + public static class ReadFlagLabelingSchemeResource extends ResourceRead { + + public ReadFlagLabelingSchemeResource(Resource resource) { + super(resource); + } + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource scheme = graph.syncRequest(new PossibleObject(resource, + DIA.UsesLocalFlagLabelingScheme)); + return scheme; + } + + } + + /** + * Adapt {@link FlagLabelingScheme} instance from the resource describing + * the currently active flag labeling scheme according to + * {@link ReadFlagLabelingSchemeResource}. + */ + public static class ReadFlagLabelingScheme extends ResourceRead { + + public ReadFlagLabelingScheme(Resource resource) { + super(resource); + } + + @Override + public FlagLabelingScheme perform(ReadGraph graph) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource scheme = graph.syncRequest(new ReadFlagLabelingSchemeResource(resource)); + if (scheme == null) + return graph.adapt(DIA.FlagLabelingScheme_Alphabetical, FlagLabelingScheme.class); + return graph.adapt(scheme, FlagLabelingScheme.class); + } + + } + + public static Resource getActiveFlagLabelingSchemeResource(RequestProcessor processor) throws DatabaseException { + Resource project = Simantics.getProjectResource(); + if (project == null) + throw new IllegalStateException("no project is active"); + + return processor.syncRequest(new ReadFlagLabelingSchemeResource(project)); + } + + public static FlagLabelingScheme getActiveFlagLabelingScheme(RequestProcessor processor) throws DatabaseException { + Resource project = Simantics.getProjectResource(); + if (project == null) + throw new IllegalStateException("no project is active"); + + return processor.syncRequest(new ReadFlagLabelingScheme(project)); + } + + public static void setProjectFlagLabelingScheme(Resource scheme) { + Resource project = Simantics.getProjectResource(); + if (project == null) + throw new IllegalStateException("no project is active"); + setFlagLabelingScheme(Simantics.getSession(), project, scheme); + } + + public static void setFlagLabelingScheme(Session session, final Resource forTarget, final Resource scheme) { + session.asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + setFlagLabelingScheme(graph, forTarget, scheme); + } + }, new Callback() { + @Override + public void run(DatabaseException parameter) { + if (parameter != null) + ErrorLogger.defaultLogError(parameter); + } + }); + } + + public static void setFlagLabelingScheme(WriteGraph graph, Resource forTarget, Resource scheme) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + graph.deny(forTarget, DIA.UsesLocalFlagLabelingScheme); + graph.claim(forTarget, DIA.UsesLocalFlagLabelingScheme, scheme); + } + +}