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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.processors;
\r
14 import java.util.Collection;
\r
15 import java.util.HashMap;
\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
25 public class UserSelectedViewpointFactoryQueryProcessor extends AbstractPrimitiveQueryProcessor<ViewpointFactory> {
\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
31 public String toString() {
\r
32 return "UserSelectedViewpointFactoryProcessor";
\r
36 public Object getIdentifier() {
\r
37 return BuiltinKeys.SelectedViewpointFactoryKey.class;
\r
41 public ViewpointFactory query(PrimitiveQueryUpdater updater, NodeContext context,
\r
42 PrimitiveQueryKey<ViewpointFactory> key) {
\r
44 updaters.put(context, new Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>(key, updater));
\r
45 ViewpointFactory result = selections.get(context);
\r
49 SelectedViewpointFactoryKey k = (SelectedViewpointFactoryKey) key;
\r
50 Collection<ViewpointFactory> factories = k.getParameter(0);
\r
52 if (factories.isEmpty())
\r
55 return factories.iterator().next();
\r
58 public ViewpointFactory getSelection(NodeContext context) {
\r
59 return selections.get(context);
\r
63 * Invoked by the user interfaces for selecting viewpoints.
\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