]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GroupStyleProfileEntry.java
Set initial priority for GroupStyleProfileEntry
[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.style.setPriority(this.priority);
48         GroupStyleProfileEntry.this.group = graph.adapt(group, Group.class);
49     }
50
51     @Override
52     public void activate(RequestProcessor processor, final Resource runtimeDiagram, final EvaluationContext observer) {
53
54         try {
55             processor.syncRequest(new ReadRequest() {
56
57                 @Override
58                 public void run(ReadGraph graph) throws DatabaseException {
59
60                     GroupStyleProfileEntry.this.style.activate(graph, runtimeDiagram, entry, GroupStyleProfileEntry.this.group, observer);
61
62                 }
63
64             });
65
66             active = true;
67
68         } catch (DatabaseException e) {
69             ErrorLogger.defaultLogError(e);
70         }
71     }
72
73     @Override
74     public void deactivate(Resource diagram, EvaluationContext observer) {
75         if (style == null) {
76             System.out.println("GroupStyleProfileEntry has null style!");
77             return;
78         }
79
80         style.deactivate(diagram, entry, group, observer);
81         active = false;
82     }
83
84     @Override
85     public void apply(EvaluationContext observer) {
86         if (style == null) {
87             System.out.println("GroupStyleProfileEntry has null style!");
88             return;
89         }
90         style.apply(entry, group, observer);
91     }
92
93     @Override
94     public int hashCode() {
95         return entry.hashCode();
96     }
97
98     @Override
99     public boolean equals(Object object) {
100         if (this == object)
101             return true;
102         else if (object == null)
103             return false;
104         else if (GroupStyleProfileEntry.class != object.getClass())
105             return false;
106         GroupStyleProfileEntry other = (GroupStyleProfileEntry)object;
107         return entry.equals(other.entry);
108     }
109     
110     @Override
111     public String toString() {
112         return "" + style + " applied to " + group;
113     }
114
115     @Override
116     public boolean isActive() {
117         return active;
118     }
119     
120     @Override
121     public double getPriority() {
122         return priority;
123     }
124
125 }