1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 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.model;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.simantics.browsing.ui.BuiltinKeys;
16 import org.simantics.browsing.ui.GraphExplorer;
17 import org.simantics.browsing.ui.NodeContext;
18 import org.simantics.browsing.ui.common.AdaptableHintContext;
19 import org.simantics.browsing.ui.model.nodetypes.NodeType;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.request.PossibleVariable;
23 import org.simantics.db.layer0.request.PossibleVariableRepresents;
24 import org.simantics.db.layer0.variable.Variable;
25 import org.simantics.ui.selection.AnyResource;
26 import org.simantics.ui.selection.AnyVariable;
27 import org.simantics.ui.selection.ExplorerColumnContentType;
28 import org.simantics.ui.selection.ExplorerInputContentType;
29 import org.simantics.ui.selection.WorkbenchSelectionContentType;
30 import org.simantics.ui.selection.WorkbenchSelectionElement;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class StandardWorkbenchSelectionElement extends AdaptableHintContext {
36 private static final Logger LOGGER = LoggerFactory.getLogger(StandardWorkbenchSelectionElement.class);
38 public static WorkbenchSelectionElement nodeContextToWorkbenchSelectionElement (NodeContext context) {
39 return (new StandardWorkbenchSelectionElement(context));
42 final public WorkbenchSelectionElement wse;
43 final public Object content;
44 final public Resource resource;
45 final public Variable variable;
46 final public Object input;
48 private WorkbenchSelectionElement extractWse(Object content) {
49 if(content instanceof NodeContext) {
50 NodeContext context = (NodeContext)content;
51 Object input = context.getConstant(NodeType.TYPE);
52 if(input instanceof NodeType)
53 return ((NodeType)input).getWorkbenchSelectionElement(context);
58 private Resource extractResource(Object content) {
59 if(content instanceof NodeContext) {
60 NodeContext context = (NodeContext)content;
61 Object input = context.getConstant(BuiltinKeys.INPUT);
62 if(input instanceof Resource) return (Resource)input;
63 if(input instanceof IAdaptable) {
64 Resource var = (Resource)((IAdaptable)input).getAdapter(Resource.class);
65 if(var != null) return var;
71 private Variable extractVariable(Object content) {
72 if(content instanceof NodeContext) {
73 NodeContext context = (NodeContext)content;
74 Object input = context.getConstant(BuiltinKeys.INPUT);
75 if(input instanceof Variable) return (Variable)input;
76 if(input instanceof IAdaptable) {
77 Variable var = (Variable)((IAdaptable)input).getAdapter(Variable.class);
78 if(var != null) return var;
84 private Object extractInput(Object content) {
85 if(content instanceof NodeContext) {
86 NodeContext context = (NodeContext)content;
87 return context.getConstant(BuiltinKeys.INPUT);
92 public StandardWorkbenchSelectionElement(Object content) {
94 this.content = content;
95 this.wse = extractWse(content);
96 this.resource = extractResource(content);
97 this.variable = extractVariable(content);
98 this.input = extractInput(content);
101 @SuppressWarnings("unchecked")
103 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
105 T result = wse.getContent(contentType);
110 if (contentType instanceof AnyResource) {
111 if (resource != null)
113 if (variable == null)
116 return (T) ((AnyResource) contentType).processor.syncRequest(new PossibleVariableRepresents(variable));
117 } catch (DatabaseException e) {
118 LOGGER.error("Unexpected error occurred while resolving Resource from Variable " + variable, e);
121 else if (contentType instanceof AnyVariable) {
122 if (variable != null)
124 if (resource == null)
127 return (T) ((AnyVariable) contentType).processor.syncRequest(new PossibleVariable(resource));
128 } catch (DatabaseException e) {
129 LOGGER.error("Unexpected error occurred while resolving Variable from Resource " + resource, e);
131 } else if (contentType instanceof ExplorerInputContentType) {
137 @SuppressWarnings("rawtypes")
139 public Object getAdapter(Class adapter) {
140 if(WorkbenchSelectionElement.class == adapter) {
143 if(NodeContext.class == adapter) {
144 if(content instanceof NodeContext)
145 return (NodeContext)content;
149 return super.getAdapter(adapter);