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