]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/EvaluatorImpl.java
ca9b41718a8a35dea0ebc1b9d96d7caf8b77873c
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / EvaluatorImpl.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.browsing.ui.common;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.browsing.ui.Tester;\r
18 import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;\r
19 import org.simantics.browsing.ui.common.EvaluatorData.EvaluatorTree;\r
20 import org.simantics.browsing.ui.content.CheckedStateFactory;\r
21 import org.simantics.browsing.ui.content.ComparableContextFactory;\r
22 import org.simantics.browsing.ui.content.ImageDecoratorFactory;\r
23 import org.simantics.browsing.ui.content.ImagerFactory;\r
24 import org.simantics.browsing.ui.content.LabelDecoratorFactory;\r
25 import org.simantics.browsing.ui.content.LabelerFactory;\r
26 import org.simantics.browsing.ui.content.ViewpointFactory;\r
27 \r
28 /**\r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public class EvaluatorImpl implements Evaluator {\r
32 \r
33     private final EvaluatorTree<ViewpointFactory>      viewpointTree           = new EvaluatorTreeImpl<ViewpointFactory>("Viewpoint", Testers.PASS);\r
34     private final EvaluatorTree<LabelerFactory>        labelerTree             = new EvaluatorTreeImpl<LabelerFactory>("Labeler", Testers.PASS);\r
35     private final EvaluatorTree<CheckedStateFactory>     checkStateTree             = new EvaluatorTreeImpl<CheckedStateFactory>("Check state", Testers.PASS);\r
36     private final EvaluatorTree<LabelDecoratorFactory> labelDecoratorTree      = new EvaluatorTreeImpl<LabelDecoratorFactory>("Label decorator", Testers.PASS);\r
37     private final EvaluatorTree<ImagerFactory>         imagerTree              = new EvaluatorTreeImpl<ImagerFactory>("Imager", Testers.PASS);\r
38     private final EvaluatorTree<ImageDecoratorFactory> imageDecoratorTree              = new EvaluatorTreeImpl<ImageDecoratorFactory>("Image decorator", Testers.PASS);\r
39     private final EvaluatorTree<ComparableContextFactory>     comparableContextTree          = new EvaluatorTreeImpl<ComparableContextFactory>("Comparable", Testers.PASS);\r
40 \r
41     @Override\r
42     public Evaluator addViewpoint(ViewpointFactory factory, double preference) {\r
43         viewpointTree.addFactory(factory, preference);\r
44         return this;\r
45     }\r
46     @Override\r
47     public Evaluator addComparableContext(ComparableContextFactory factory, double preference) {\r
48         comparableContextTree.addFactory(factory, preference);\r
49         return this;\r
50     }\r
51     @Override\r
52     public Evaluator addLabeler(LabelerFactory factory, double preference) {\r
53         labelerTree.addFactory(factory, preference);\r
54         return this;\r
55     }\r
56     @Override\r
57     public Evaluator addCheckState(CheckedStateFactory factory, double preference) {\r
58         checkStateTree.addFactory(factory, preference);\r
59         return this;\r
60     }\r
61     @Override\r
62     public Evaluator addLabelDecorator(LabelDecoratorFactory factory, double preference) {\r
63         labelDecoratorTree.addFactory(factory, preference);\r
64         return this;\r
65     }\r
66     @Override\r
67     public Evaluator addComparator(ComparableContextFactory factory, double preference) {\r
68         comparableContextTree.addFactory(factory, preference);\r
69         return this;\r
70     }\r
71     @Override\r
72     public Evaluator addImager(ImagerFactory factory, double preference) {\r
73         imagerTree.addFactory(factory, preference);\r
74         return this;\r
75     }\r
76     @Override\r
77     public Evaluator addImageDecorator(ImageDecoratorFactory factory, double preference) {\r
78         imageDecoratorTree.addFactory(factory, preference);\r
79         return this;\r
80     }\r
81 \r
82     @Override\r
83     public EvaluatorTree<ComparableContextFactory> getComparableContextTree() {\r
84         return comparableContextTree;\r
85     }\r
86 \r
87     @Override\r
88     public EvaluatorTree<ImagerFactory> getImagerTree() {\r
89         return imagerTree;\r
90     }\r
91 \r
92     @Override\r
93     public EvaluatorTree<ImageDecoratorFactory> getImageDecoratorTree() {\r
94         return imageDecoratorTree;\r
95     }\r
96 \r
97     @Override\r
98     public EvaluatorTree<LabelDecoratorFactory> getLabelDecoratorTree() {\r
99         return labelDecoratorTree;\r
100     }\r
101 \r
102     @Override\r
103     public EvaluatorTree<LabelerFactory> getLabelerTree() {\r
104         return labelerTree;\r
105     }\r
106 \r
107     @Override\r
108     public EvaluatorTree<CheckedStateFactory> getCheckStateTree() {\r
109         return checkStateTree;\r
110     }\r
111 \r
112     @Override\r
113     public EvaluatorTree<ViewpointFactory> getViewpointTree() {\r
114         return viewpointTree;\r
115     }\r
116 \r
117     /**\r
118      * @param <Factory>\r
119      */\r
120     public class EvaluatorTreeImpl<Factory> implements EvaluatorTree<Factory> {\r
121         private final String                             factory;\r
122         private final Tester                             tester;\r
123         private final Collection<Preference<Factory>>    acceptedFactories = new ArrayList<Preference<Factory>>();\r
124         private final Collection<EvaluatorTree<Factory>> children          = new ArrayList<EvaluatorTree<Factory>>();\r
125 \r
126         public EvaluatorTreeImpl(String factory, Tester tester) {\r
127             if (tester == null)\r
128                 throw new NullPointerException("null tester");\r
129             this.tester = tester;\r
130             this.factory = factory;\r
131         }\r
132 \r
133         @Override\r
134         public EvaluatorTree<Factory> addFactory(Factory factory, double preference) {\r
135             if (factory == null)\r
136                 throw new NullPointerException("null factory");\r
137             acceptedFactories.add(new Preference<Factory>(factory, preference));\r
138             return this;\r
139         }\r
140 \r
141         @Override\r
142         public EvaluatorTree<Factory> addBranch(Tester tester) {\r
143             if (tester == null)\r
144                 throw new NullPointerException("null tester");\r
145             EvaluatorTree<Factory> result = new EvaluatorTreeImpl<Factory>(factory, tester);\r
146             children.add(result);\r
147             return result;\r
148         }\r
149 \r
150         @Override\r
151         public Collection<Preference<Factory>> getAcceptedFactories() {\r
152             return acceptedFactories;\r
153         }\r
154 \r
155         @Override\r
156         public Collection<EvaluatorTree<Factory>> getChildren() {\r
157             return children;\r
158         }\r
159 \r
160         @Override\r
161         public Tester getTester() {\r
162             return tester;\r
163         }\r
164 \r
165         @Override\r
166         public String toString() {\r
167             return factory + " tree [" + tester + "]";\r
168         }\r
169 \r
170     }\r
171 \r
172     @Override\r
173     public String toString() {\r
174         return "Evaluator";\r
175     }\r
176 \r
177 }