]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/DiagramFlagPreferences.java
Added `possibleInvertibleExpressionReferencedTransformedProperty`
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / DiagramFlagPreferences.java
1 package org.simantics.diagram.flag;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.RequestProcessor;
6 import org.simantics.db.Resource;
7 import org.simantics.db.Session;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.primitiverequest.PossibleObject;
10 import org.simantics.db.common.request.ResourceRead;
11 import org.simantics.db.common.request.WriteRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.diagram.stubs.DiagramResource;
14 import org.simantics.utils.ui.ErrorLogger;
15
16 /**
17  * @author Tuukka Lehtonen
18  */
19 public class DiagramFlagPreferences {
20
21     /**
22      * Return the resource describing the currently active flag labeling scheme
23      * from the specified resource. The default intended resource for checking
24      * the scheme from is the active project.
25      */
26     public static class ReadFlagLabelingSchemeResource extends ResourceRead<Resource> {
27
28         public ReadFlagLabelingSchemeResource(Resource resource) {
29             super(resource);
30         }
31
32         @Override
33         public Resource perform(ReadGraph graph) throws DatabaseException {
34             DiagramResource DIA = DiagramResource.getInstance(graph);
35             Resource scheme = graph.syncRequest(new PossibleObject(resource,
36                     DIA.UsesLocalFlagLabelingScheme));
37             return scheme;
38         }
39
40     }
41
42     /**
43      * Adapt {@link FlagLabelingScheme} instance from the resource describing
44      * the currently active flag labeling scheme according to
45      * {@link ReadFlagLabelingSchemeResource}.
46      */
47     public static class ReadFlagLabelingScheme extends ResourceRead<FlagLabelingScheme> {
48
49         public ReadFlagLabelingScheme(Resource resource) {
50             super(resource);
51         }
52
53         @Override
54         public FlagLabelingScheme perform(ReadGraph graph) throws DatabaseException {
55             DiagramResource DIA = DiagramResource.getInstance(graph);
56             Resource scheme = graph.syncRequest(new ReadFlagLabelingSchemeResource(resource));
57             if (scheme == null)
58                 return graph.adapt(DIA.FlagLabelingScheme_Alphabetical, FlagLabelingScheme.class);
59             return graph.adapt(scheme, FlagLabelingScheme.class);
60         }
61
62     }
63
64     public static Resource getActiveFlagLabelingSchemeResource(RequestProcessor processor) throws DatabaseException {
65         Resource project = Simantics.getProjectResource();
66         if (project == null)
67             throw new IllegalStateException("no project is active");
68
69         return processor.syncRequest(new ReadFlagLabelingSchemeResource(project));
70     }
71
72     public static FlagLabelingScheme getActiveFlagLabelingScheme(RequestProcessor processor) throws DatabaseException {
73         Resource project = Simantics.getProjectResource();
74         if (project == null)
75             throw new IllegalStateException("no project is active");
76
77         return processor.syncRequest(new ReadFlagLabelingScheme(project));
78     }
79
80     public static void setProjectFlagLabelingScheme(Resource scheme) {
81         Resource project = Simantics.getProjectResource();
82         if (project == null)
83             throw new IllegalStateException("no project is active");
84         setFlagLabelingScheme(Simantics.getSession(), project, scheme);
85     }
86
87     public static void setFlagLabelingScheme(Session session, final Resource forTarget, final Resource scheme) {
88         session.asyncRequest(new WriteRequest() {
89             @Override
90             public void perform(WriteGraph graph) throws DatabaseException {
91                 setFlagLabelingScheme(graph, forTarget, scheme);
92             }
93         }, parameter -> {
94             if (parameter != null)
95                 ErrorLogger.defaultLogError(parameter);
96         });
97     }
98
99     public static void setFlagLabelingScheme(WriteGraph graph, Resource forTarget, Resource scheme) throws DatabaseException {
100         DiagramResource DIA = DiagramResource.getInstance(graph);
101         graph.deny(forTarget, DIA.UsesLocalFlagLabelingScheme);
102         graph.claim(forTarget, DIA.UsesLocalFlagLabelingScheme, scheme);
103     }
104
105 }