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.graph.impl;
14 import java.util.Collection;
15 import java.util.Collections;
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;
28 * @author Tuukka Lehtonen
30 public class RelatedObjectsQueryProcessor extends LazyGraphQueryProcessor<Collection<Resource>> {
33 public String toString() {
34 return "RelatedObjectsProcessor";
38 public Object getIdentifier() {
39 return CommonKeys.RelatedObjectsKey.class;
43 protected Collection<Resource> initial() {
44 return Collections.emptySet();
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);
52 RelatedObjectsKey k = (RelatedObjectsKey) key;
53 final Resource relation = k.getParameter(0);
55 return Collections.emptyList();
57 return graph.getObjects(subject, relation);
60 @SuppressWarnings("unchecked")
61 public static <T> T adaptToSingle(Object o, Class<T> clazz) {
62 if (clazz.isInstance(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();
71 return adaptToSingle(obj, clazz);