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