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