1 package org.simantics.diagram.flag;
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.datastructures.Callback;
15 import org.simantics.utils.ui.ErrorLogger;
18 * @author Tuukka Lehtonen
20 public class DiagramFlagPreferences {
23 * Return the resource describing the currently active flag labeling scheme
24 * from the specified resource. The default intended resource for checking
25 * the scheme from is the active project.
27 public static class ReadFlagLabelingSchemeResource extends ResourceRead<Resource> {
29 public ReadFlagLabelingSchemeResource(Resource resource) {
34 public Resource perform(ReadGraph graph) throws DatabaseException {
35 DiagramResource DIA = DiagramResource.getInstance(graph);
36 Resource scheme = graph.syncRequest(new PossibleObject(resource,
37 DIA.UsesLocalFlagLabelingScheme));
44 * Adapt {@link FlagLabelingScheme} instance from the resource describing
45 * the currently active flag labeling scheme according to
46 * {@link ReadFlagLabelingSchemeResource}.
48 public static class ReadFlagLabelingScheme extends ResourceRead<FlagLabelingScheme> {
50 public ReadFlagLabelingScheme(Resource resource) {
55 public FlagLabelingScheme perform(ReadGraph graph) throws DatabaseException {
56 DiagramResource DIA = DiagramResource.getInstance(graph);
57 Resource scheme = graph.syncRequest(new ReadFlagLabelingSchemeResource(resource));
59 return graph.adapt(DIA.FlagLabelingScheme_Alphabetical, FlagLabelingScheme.class);
60 return graph.adapt(scheme, FlagLabelingScheme.class);
65 public static Resource getActiveFlagLabelingSchemeResource(RequestProcessor processor) throws DatabaseException {
66 Resource project = Simantics.getProjectResource();
68 throw new IllegalStateException("no project is active");
70 return processor.syncRequest(new ReadFlagLabelingSchemeResource(project));
73 public static FlagLabelingScheme getActiveFlagLabelingScheme(RequestProcessor processor) throws DatabaseException {
74 Resource project = Simantics.getProjectResource();
76 throw new IllegalStateException("no project is active");
78 return processor.syncRequest(new ReadFlagLabelingScheme(project));
81 public static void setProjectFlagLabelingScheme(Resource scheme) {
82 Resource project = Simantics.getProjectResource();
84 throw new IllegalStateException("no project is active");
85 setFlagLabelingScheme(Simantics.getSession(), project, scheme);
88 public static void setFlagLabelingScheme(Session session, final Resource forTarget, final Resource scheme) {
89 session.asyncRequest(new WriteRequest() {
91 public void perform(WriteGraph graph) throws DatabaseException {
92 setFlagLabelingScheme(graph, forTarget, scheme);
94 }, new Callback<DatabaseException>() {
96 public void run(DatabaseException parameter) {
97 if (parameter != null)
98 ErrorLogger.defaultLogError(parameter);
103 public static void setFlagLabelingScheme(WriteGraph graph, Resource forTarget, Resource scheme) throws DatabaseException {
104 DiagramResource DIA = DiagramResource.getInstance(graph);
105 graph.deny(forTarget, DIA.UsesLocalFlagLabelingScheme);
106 graph.claim(forTarget, DIA.UsesLocalFlagLabelingScheme, scheme);