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