]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.adapter;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.RequestProcessor;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.common.request.ReadRequest;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.diagram.stubs.DiagramResource;\r
20 import org.simantics.scenegraph.profile.EvaluationContext;\r
21 import org.simantics.scenegraph.profile.Group;\r
22 import org.simantics.scenegraph.profile.ProfileEntry;\r
23 import org.simantics.scenegraph.profile.Style;\r
24 import org.simantics.utils.ui.ErrorLogger;\r
25 \r
26 public class GroupStyleProfileEntry implements ProfileEntry {\r
27 \r
28     private final Resource entry;\r
29     private boolean        active = false;\r
30 \r
31     private Style          style;\r
32     private Group          group;\r
33     private double         priority;\r
34 \r
35     public GroupStyleProfileEntry(ReadGraph graph, Resource entry) throws DatabaseException {\r
36         this.entry = entry;\r
37 \r
38         DiagramResource dr = DiagramResource.getInstance(graph);\r
39         Resource style = graph.getPossibleObject(entry, dr.ProfileEntry_HasStyle);\r
40         Resource group = graph.getPossibleObject(entry, dr.ProfileEntry_HasGroup);\r
41 \r
42         Double priority = graph.getPossibleRelatedValue(entry, dr.ProfileEntry_HasPriority);\r
43         if(priority != null) this.priority = priority;\r
44         else this.priority = 0;\r
45 \r
46         GroupStyleProfileEntry.this.style = graph.adapt(style, Style.class);\r
47         GroupStyleProfileEntry.this.group = graph.adapt(group, Group.class);\r
48     }\r
49 \r
50     @Override\r
51     public void activate(RequestProcessor processor, final Resource runtimeDiagram, final EvaluationContext observer) {\r
52 \r
53         try {\r
54             processor.syncRequest(new ReadRequest() {\r
55 \r
56                 @Override\r
57                 public void run(ReadGraph graph) throws DatabaseException {\r
58 \r
59                     GroupStyleProfileEntry.this.style.activate(graph, runtimeDiagram, entry, GroupStyleProfileEntry.this.group, observer);\r
60 \r
61                 }\r
62 \r
63             });\r
64 \r
65             active = true;\r
66 \r
67         } catch (DatabaseException e) {\r
68             ErrorLogger.defaultLogError(e);\r
69         }\r
70     }\r
71 \r
72     @Override\r
73     public void deactivate(Resource diagram, EvaluationContext observer) {\r
74         if (style == null) {\r
75             System.out.println("GroupStyleProfileEntry has null style!");\r
76             return;\r
77         }\r
78 \r
79         style.deactivate(diagram, entry, group, observer);\r
80         active = false;\r
81     }\r
82 \r
83     @Override\r
84     public void apply(EvaluationContext observer) {\r
85         if (style == null) {\r
86             System.out.println("GroupStyleProfileEntry has null style!");\r
87             return;\r
88         }\r
89         style.apply(entry, group, observer);\r
90     }\r
91 \r
92     @Override\r
93     public int hashCode() {\r
94         return entry.hashCode();\r
95     }\r
96 \r
97     @Override\r
98     public boolean equals(Object object) {\r
99         if (this == object)\r
100             return true;\r
101         else if (object == null)\r
102             return false;\r
103         else if (GroupStyleProfileEntry.class != object.getClass())\r
104             return false;\r
105         GroupStyleProfileEntry other = (GroupStyleProfileEntry)object;\r
106         return entry.equals(other.entry);\r
107     }\r
108     \r
109     @Override\r
110     public String toString() {\r
111         return "" + style + " applied to " + group;\r
112     }\r
113 \r
114     @Override\r
115     public boolean isActive() {\r
116         return active;\r
117     }\r
118     \r
119     @Override\r
120     public double getPriority() {\r
121         return priority;\r
122     }\r
123 \r
124 }\r