]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/PropertyVariablesImpl.java
Add utility class org.simantics.modeling.help.HelpContexts
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / PropertyVariablesImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.modeling;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.SelectionHints;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.utils.datastructures.hints.HintContext;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class PropertyVariablesImpl extends HintContext implements PropertyVariables, IAdaptable {
28
29     private final Variable container;
30     private final Variable configuration;
31     private final Variable visualized;
32     private final Variable modified;
33     private final String   suffix;
34
35     public PropertyVariablesImpl(Variable container, Variable visualized, Variable modified) {
36         this(container, null, visualized, modified, null);
37     }
38
39     public PropertyVariablesImpl(Variable container, Variable visualized, Variable modified, String suffix) {
40         this(container, null, visualized, modified, suffix);
41     }
42
43     public PropertyVariablesImpl(Variable container, Variable configuration, Variable visualized, Variable modified) {
44         this(container, configuration, visualized, modified, null);
45     }
46
47     public PropertyVariablesImpl(Variable container, Variable configuration, Variable visualized, Variable modified, String suffix) {
48         if (visualized == null)
49             throw new NullPointerException("null visualized variable");
50         this.container = container;
51         this.configuration = configuration;
52         this.visualized = visualized;
53         this.modified = modified;
54         this.suffix = suffix;
55         setHintWithoutNotification(SelectionHints.KEY_MAIN, visualized);
56         setHintWithoutNotification(SelectionHints.KEY_SELECTION_PROPERTY, visualized);
57     }
58
59     @Override
60     public Variable getContainer() {
61         return container;
62     }
63
64     public Variable getConfiguration() {
65         return configuration;
66     }
67
68     @Override
69     public Variable getVisualVariable() {
70         return visualized;
71     }
72
73     @Override
74     public Variable getModificationVariable() {
75         return modified;
76     }
77
78     @Override
79     public String getSuffix() {
80         return suffix;
81     }
82
83     @Override
84     public PropertyVariables withSuffix(String suffix) {
85         return new PropertyVariablesImpl(container, visualized, modified, suffix);
86     }
87
88     @Override
89     public PropertyVariables resolved(ReadGraph graph) throws DatabaseException {
90         if (suffix == null)
91             return this;
92
93         Variable c = configuration;
94         Variable v = visualized;
95         Variable m = modified;
96         if (c != null)
97             c = c.browsePossible(graph, suffix);
98         if (v != null)
99             v = v.browsePossible(graph, suffix);
100         if (v == null)
101             return null;
102         if (m != null)
103             m = m.browsePossible(graph, suffix);
104         return new PropertyVariablesImpl(container, c, v, m, null);
105     }
106
107     @Override
108     public int hashCode() {
109         final int prime = 31;
110         int result = 1;
111         result = prime * result + ((container == null) ? 0 : container.hashCode());
112         result = prime * result + ((configuration == null) ? 0 : configuration.hashCode());
113         result = prime * result + ((modified == null) ? 0 : modified.hashCode());
114         result = prime * result + ((visualized == null) ? 0 : visualized.hashCode());
115         return result;
116     }
117
118     @Override
119     public boolean equals(Object obj) {
120         if (this == obj)
121             return true;
122         if (obj == null)
123             return false;
124         if (getClass() != obj.getClass())
125             return false;
126         PropertyVariablesImpl other = (PropertyVariablesImpl) obj;
127         if (container == null) {
128             if (other.container != null)
129                 return false;
130         } else if (!container.equals(other.container))
131             return false;
132         if (configuration == null) {
133             if (other.configuration != null)
134                 return false;
135         } else if (!configuration.equals(other.configuration))
136             return false;
137         if (modified == null) {
138             if (other.modified != null)
139                 return false;
140         } else if (!modified.equals(other.modified))
141             return false;
142         if (visualized == null) {
143             if (other.visualized != null)
144                 return false;
145         } else if (!visualized.equals(other.visualized))
146             return false;
147         return true;
148     }
149
150     @SuppressWarnings("rawtypes")
151     @Override
152     public Object getAdapter(Class adapter) {
153         if (adapter == Variable.class)
154             return visualized;
155         return null;
156     }
157
158     public static List<PropertyVariables> resolve(ReadGraph graph, List<PropertyVariables> vars) throws DatabaseException {
159         List<PropertyVariables> result = new ArrayList<PropertyVariables>(vars.size());
160         for (PropertyVariables var : vars) {
161             PropertyVariables v = var.resolved(graph);
162             if (v != null)
163                 result.add(v);
164         }
165         return result;
166     }
167
168 }