]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GroupStyleProfileEntry.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / GroupStyleProfileEntry.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GroupStyleProfileEntry.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GroupStyleProfileEntry.java
new file mode 100644 (file)
index 0000000..32c2857
--- /dev/null
@@ -0,0 +1,124 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.adapter;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.scenegraph.profile.EvaluationContext;\r
+import org.simantics.scenegraph.profile.Group;\r
+import org.simantics.scenegraph.profile.ProfileEntry;\r
+import org.simantics.scenegraph.profile.Style;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+public class GroupStyleProfileEntry implements ProfileEntry {\r
+\r
+    private final Resource entry;\r
+    private boolean        active = false;\r
+\r
+    private Style          style;\r
+    private Group          group;\r
+    private double         priority;\r
+\r
+    public GroupStyleProfileEntry(ReadGraph graph, Resource entry) throws DatabaseException {\r
+        this.entry = entry;\r
+\r
+        DiagramResource dr = DiagramResource.getInstance(graph);\r
+        Resource style = graph.getPossibleObject(entry, dr.ProfileEntry_HasStyle);\r
+        Resource group = graph.getPossibleObject(entry, dr.ProfileEntry_HasGroup);\r
+\r
+        Double priority = graph.getPossibleRelatedValue(entry, dr.ProfileEntry_HasPriority);\r
+        if(priority != null) this.priority = priority;\r
+        else this.priority = 0;\r
+\r
+        GroupStyleProfileEntry.this.style = graph.adapt(style, Style.class);\r
+        GroupStyleProfileEntry.this.group = graph.adapt(group, Group.class);\r
+    }\r
+\r
+    @Override\r
+    public void activate(RequestProcessor processor, final Resource runtimeDiagram, final EvaluationContext observer) {\r
+\r
+        try {\r
+            processor.syncRequest(new ReadRequest() {\r
+\r
+                @Override\r
+                public void run(ReadGraph graph) throws DatabaseException {\r
+\r
+                    GroupStyleProfileEntry.this.style.activate(graph, runtimeDiagram, entry, GroupStyleProfileEntry.this.group, observer);\r
+\r
+                }\r
+\r
+            });\r
+\r
+            active = true;\r
+\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void deactivate(Resource diagram, EvaluationContext observer) {\r
+        if (style == null) {\r
+            System.out.println("GroupStyleProfileEntry has null style!");\r
+            return;\r
+        }\r
+\r
+        style.deactivate(diagram, entry, group, observer);\r
+        active = false;\r
+    }\r
+\r
+    @Override\r
+    public void apply(EvaluationContext observer) {\r
+        if (style == null) {\r
+            System.out.println("GroupStyleProfileEntry has null style!");\r
+            return;\r
+        }\r
+        style.apply(entry, group, observer);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return entry.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (GroupStyleProfileEntry.class != object.getClass())\r
+            return false;\r
+        GroupStyleProfileEntry other = (GroupStyleProfileEntry)object;\r
+        return entry.equals(other.entry);\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return "" + style + " applied to " + group;\r
+    }\r
+\r
+    @Override\r
+    public boolean isActive() {\r
+        return active;\r
+    }\r
+    \r
+    @Override\r
+    public double getPriority() {\r
+       return priority;\r
+    }\r
+\r
+}\r