]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/StandardWorkbenchSelectionElement.java
Add workbenchselection json fetcher to SCL interface
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / StandardWorkbenchSelectionElement.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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.model;
13
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;
33
34 public class StandardWorkbenchSelectionElement extends AdaptableHintContext {
35
36         private static final Logger LOGGER = LoggerFactory.getLogger(StandardWorkbenchSelectionElement.class);
37         
38         public static WorkbenchSelectionElement nodeContextToWorkbenchSelectionElement (NodeContext context) {
39                 return (new StandardWorkbenchSelectionElement(context));
40         }
41         
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;
47     
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);
54         }
55         return null;
56     }
57     
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;
66                 }
67         }
68         return null;
69     }
70     
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;
79                 }
80         }
81         return null;
82     }
83
84     private Object extractInput(Object content) {
85         if(content instanceof NodeContext) {
86                 NodeContext context = (NodeContext)content;
87                 return context.getConstant(BuiltinKeys.INPUT);
88         }
89         return null;
90     }
91
92     public StandardWorkbenchSelectionElement(Object content) {
93         super(new Key[0]);
94         this.content = content;
95         this.wse = extractWse(content);
96         this.resource = extractResource(content);
97         this.variable = extractVariable(content);
98         this.input = extractInput(content);
99     }
100
101     @SuppressWarnings("unchecked")
102     @Override
103     public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
104         if (wse != null) {
105             T result = wse.getContent(contentType);
106             if (result != null)
107                 return result;
108         }
109
110         if (contentType instanceof AnyResource) {
111             if (resource != null)
112                 return (T) resource;
113             if (variable == null)
114                 return null;
115             try {
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);
119             }
120         }
121         else if (contentType instanceof AnyVariable) {
122             if (variable != null)
123                 return (T) variable;
124             if (resource == null)
125                 return null;
126             try {
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);
130             }
131         } else if (contentType instanceof ExplorerInputContentType) {
132             return (T) input;
133         }
134         return null;
135     }
136
137     @SuppressWarnings("rawtypes")
138     @Override
139     public Object getAdapter(Class adapter) {
140         if(WorkbenchSelectionElement.class == adapter) {
141                 return wse;
142         }
143         if(NodeContext.class == adapter) {
144             if(content instanceof NodeContext)
145                 return (NodeContext)content;
146             else
147                 return null;
148         }
149         return super.getAdapter(adapter);
150     }
151
152 }