1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.processors;
14 import java.util.Collection;
15 import java.util.HashMap;
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;
25 public class UserSelectedViewpointFactoryQueryProcessor extends AbstractPrimitiveQueryProcessor<ViewpointFactory> {
27 HashMap<NodeContext, ViewpointFactory> selections = new HashMap<NodeContext, ViewpointFactory>();
28 HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>> updaters = new HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>>();
31 public String toString() {
32 return "UserSelectedViewpointFactoryProcessor";
36 public Object getIdentifier() {
37 return BuiltinKeys.SelectedViewpointFactoryKey.class;
41 public ViewpointFactory query(PrimitiveQueryUpdater updater, NodeContext context,
42 PrimitiveQueryKey<ViewpointFactory> key) {
44 updaters.put(context, new Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>(key, updater));
45 ViewpointFactory result = selections.get(context);
49 SelectedViewpointFactoryKey k = (SelectedViewpointFactoryKey) key;
50 Collection<ViewpointFactory> factories = k.getParameter(0);
52 if (factories.isEmpty())
55 return factories.iterator().next();
58 public ViewpointFactory getSelection(NodeContext context) {
59 return selections.get(context);
63 * Invoked by the user interfaces for selecting viewpoints.
68 @SuppressWarnings("unchecked")
69 public void select(NodeContext context, ViewpointFactory factory) {
70 selections.put(context, factory);
71 Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater> p = updaters.get(context);
73 throw new IllegalArgumentException("context not found in cache");
74 p.second.scheduleReplace(context, (PrimitiveQueryKey<ViewpointFactory>) p.first, factory);