]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/processors/UserSelectedComparableFactoryQueryProcessor.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / processors / UserSelectedComparableFactoryQueryProcessor.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.SelectedComparableFactoryKey;
21 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
22 import org.simantics.browsing.ui.content.ComparableContextFactory;
23 import org.simantics.utils.datastructures.Pair;
24
25 public class UserSelectedComparableFactoryQueryProcessor extends AbstractPrimitiveQueryProcessor<ComparableContextFactory> {
26
27     HashMap<NodeContext, ComparableContextFactory> factories = new HashMap<NodeContext, ComparableContextFactory>();
28     HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>> updaters = new HashMap<NodeContext, Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater>>();
29
30     @Override
31     public String toString() {
32         return "UserSelectedComparableFactoryProcessor";
33     }
34
35     @Override
36     public Object getIdentifier() {
37         return BuiltinKeys.SelectedComparableFactoryKey.class;
38     }
39
40     @Override
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);
45         if (result != null)
46             return result;
47
48         SelectedComparableFactoryKey k = (SelectedComparableFactoryKey) key;
49         Collection<ComparableContextFactory> factories = k.getParameter(0);
50         if (factories.isEmpty())
51             return null;
52
53         return factories.iterator().next();
54     }
55
56     public ComparableContextFactory getComparator(NodeContext context) {
57         return factories.get(context);
58     }
59
60     /**
61      * Invoked by the user interfaces for selecting comparators.
62      * 
63      * @param context
64      * @param factory
65      */
66     @SuppressWarnings("unchecked")
67     public void select(NodeContext context, ComparableContextFactory factory) {
68         factories.put(context, factory);
69         Pair<PrimitiveQueryKey<?>, PrimitiveQueryUpdater> p = updaters.get(context);
70         if (p.second == null)
71             throw new IllegalArgumentException("context not found in cache");
72         p.second.scheduleReplace(context, (PrimitiveQueryKey<ComparableContextFactory>) p.first, factory);
73     }
74
75 }