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.SelectedComparableFactoryKey;
21 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
22 import org.simantics.browsing.ui.content.ComparableContextFactory;
23 import org.simantics.utils.datastructures.Pair;
25 public class UserSelectedComparableFactoryQueryProcessor extends AbstractPrimitiveQueryProcessor<ComparableContextFactory> {
27 HashMap<NodeContext, ComparableContextFactory> factories = new HashMap<NodeContext, ComparableContextFactory>();
28 HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>> updaters = new HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>>();
31 public String toString() {
32 return "UserSelectedComparableFactoryProcessor";
36 public Object getIdentifier() {
37 return BuiltinKeys.SelectedComparableFactoryKey.class;
41 public ComparableContextFactory query(PrimitiveQueryUpdater updater, NodeContext context,
42 PrimitiveQueryKey<ComparableContextFactory> key) {
43 updaters.put(context, new Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>(key, updater));
44 ComparableContextFactory result = factories.get(context);
48 SelectedComparableFactoryKey k = (SelectedComparableFactoryKey) key;
49 Collection<ComparableContextFactory> factories = k.getParameter(0);
50 if (factories.isEmpty())
53 return factories.iterator().next();
56 public ComparableContextFactory getComparator(NodeContext context) {
57 return factories.get(context);
61 * Invoked by the user interfaces for selecting comparators.
66 @SuppressWarnings("unchecked")
67 public void select(NodeContext context, ComparableContextFactory factory) {
68 factories.put(context, factory);
69 Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater> p = updaters.get(context);
71 throw new IllegalArgumentException("context not found in cache");
72 p.second.scheduleReplace(context, (PrimitiveQueryKey<ComparableContextFactory>) p.first, factory);