]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/RelatedObjectsQueryProcessor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / RelatedObjectsQueryProcessor.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.graph.impl;
13
14 import java.util.Collection;
15 import java.util.Collections;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.NodeContext.PrimitiveQueryKey;
21 import org.simantics.browsing.ui.graph.impl.CommonKeys.RelatedObjectsKey;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.utils.Container;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class RelatedObjectsQueryProcessor extends LazyGraphQueryProcessor<Collection<Resource>> {
31
32     @Override
33     public String toString() {
34         return "RelatedObjectsProcessor";
35     }
36
37     @Override
38     public Object getIdentifier() {
39         return CommonKeys.RelatedObjectsKey.class;
40     }
41
42     @Override
43     protected Collection<Resource> initial() {
44         return Collections.emptySet();
45     }
46
47     @Override
48     protected Collection<Resource> compute(ReadGraph graph, NodeContext context, PrimitiveQueryKey<Container<Collection<Resource>>> key) throws DatabaseException {
49         Object input = context.getConstant(BuiltinKeys.INPUT);
50         Resource subject = adaptToSingle(input, Resource.class);
51
52         RelatedObjectsKey k = (RelatedObjectsKey) key;
53         final Resource relation = k.getParameter(0);
54         if (relation == null)
55             return Collections.emptyList();
56
57         return graph.getObjects(subject, relation);
58     }
59
60     @SuppressWarnings("unchecked")
61     public static <T> T adaptToSingle(Object o, Class<T> clazz) {
62         if (clazz.isInstance(o)) {
63             return (T) o;
64         } else if (o instanceof IAdaptable) {
65             IAdaptable a = (IAdaptable) o;
66             return (T) a.getAdapter(clazz);
67         } else if (o instanceof Container<?>) {
68             Object obj = ((Container<?>) o).get();
69             if (obj == o)
70                 return null;
71             return adaptToSingle(obj, clazz);
72         }
73         return null;
74     }
75
76 }