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.ui.ErrorLogger;
17 * @author Tuukka Lehtonen
19 public class DiagramFlagPreferences {
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.
26 public static class ReadFlagLabelingSchemeResource extends ResourceRead<Resource> {
28 public ReadFlagLabelingSchemeResource(Resource resource) {
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));
43 * Adapt {@link FlagLabelingScheme} instance from the resource describing
44 * the currently active flag labeling scheme according to
45 * {@link ReadFlagLabelingSchemeResource}.
47 public static class ReadFlagLabelingScheme extends ResourceRead<FlagLabelingScheme> {
49 public ReadFlagLabelingScheme(Resource resource) {
54 public FlagLabelingScheme perform(ReadGraph graph) throws DatabaseException {
55 DiagramResource DIA = DiagramResource.getInstance(graph);
56 Resource scheme = graph.syncRequest(new ReadFlagLabelingSchemeResource(resource));
58 return graph.adapt(DIA.FlagLabelingScheme_Alphabetical, FlagLabelingScheme.class);
59 return graph.adapt(scheme, FlagLabelingScheme.class);
64 public static Resource getActiveFlagLabelingSchemeResource(RequestProcessor processor) throws DatabaseException {
65 Resource project = Simantics.getProjectResource();
67 throw new IllegalStateException("no project is active");
69 return processor.syncRequest(new ReadFlagLabelingSchemeResource(project));
72 public static FlagLabelingScheme getActiveFlagLabelingScheme(RequestProcessor processor) throws DatabaseException {
73 Resource project = Simantics.getProjectResource();
75 throw new IllegalStateException("no project is active");
77 return processor.syncRequest(new ReadFlagLabelingScheme(project));
80 public static void setProjectFlagLabelingScheme(Resource scheme) {
81 Resource project = Simantics.getProjectResource();
83 throw new IllegalStateException("no project is active");
84 setFlagLabelingScheme(Simantics.getSession(), project, scheme);
87 public static void setFlagLabelingScheme(Session session, final Resource forTarget, final Resource scheme) {
88 session.asyncRequest(new WriteRequest() {
90 public void perform(WriteGraph graph) throws DatabaseException {
91 setFlagLabelingScheme(graph, forTarget, scheme);
94 if (parameter != null)
95 ErrorLogger.defaultLogError(parameter);
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);