]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/UserSelectedComparableFactoryQueryProcessor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / processors / UserSelectedComparableFactoryQueryProcessor.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.processors;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.HashMap;\r
16 \r
17 import org.simantics.browsing.ui.BuiltinKeys;\r
18 import org.simantics.browsing.ui.NodeContext;\r
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
20 import org.simantics.browsing.ui.BuiltinKeys.SelectedComparableFactoryKey;\r
21 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;\r
22 import org.simantics.browsing.ui.content.ComparableContextFactory;\r
23 import org.simantics.utils.datastructures.Pair;\r
24 \r
25 public class UserSelectedComparableFactoryQueryProcessor extends AbstractPrimitiveQueryProcessor<ComparableContextFactory> {\r
26 \r
27     HashMap<NodeContext, ComparableContextFactory> factories = new HashMap<NodeContext, ComparableContextFactory>();\r
28     HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>> updaters = new HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>>();\r
29 \r
30     @Override\r
31     public String toString() {\r
32         return "UserSelectedComparableFactoryProcessor";\r
33     }\r
34 \r
35     @Override\r
36     public Object getIdentifier() {\r
37         return BuiltinKeys.SelectedComparableFactoryKey.class;\r
38     }\r
39 \r
40     @Override\r
41     public ComparableContextFactory query(PrimitiveQueryUpdater updater, NodeContext context,\r
42             PrimitiveQueryKey<ComparableContextFactory> key) {\r
43         updaters.put(context, new Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>(key, updater));\r
44         ComparableContextFactory result = factories.get(context);\r
45         if (result != null)\r
46             return result;\r
47 \r
48         SelectedComparableFactoryKey k = (SelectedComparableFactoryKey) key;\r
49         Collection<ComparableContextFactory> factories = k.getParameter(0);\r
50         if (factories.isEmpty())\r
51             return null;\r
52 \r
53         return factories.iterator().next();\r
54     }\r
55 \r
56     public ComparableContextFactory getComparator(NodeContext context) {\r
57         return factories.get(context);\r
58     }\r
59 \r
60     /**\r
61      * Invoked by the user interfaces for selecting comparators.\r
62      * \r
63      * @param context\r
64      * @param factory\r
65      */\r
66     @SuppressWarnings("unchecked")\r
67     public void select(NodeContext context, ComparableContextFactory factory) {\r
68         factories.put(context, factory);\r
69         Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater> p = updaters.get(context);\r
70         if (p.second == null)\r
71             throw new IllegalArgumentException("context not found in cache");\r
72         p.second.scheduleReplace(context, (PrimitiveQueryKey<ComparableContextFactory>) p.first, factory);\r
73     }\r
74 \r
75 }\r