]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt/src/org/simantics/views/swt/SWTViewLoaderProcess.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt / src / org / simantics / views / swt / SWTViewLoaderProcess.java
1 package org.simantics.views.swt;
2
3 import org.eclipse.ui.IWorkbenchSite;
4 import org.simantics.Simantics;
5 import org.simantics.browsing.ui.common.ErrorLogger;
6 import org.simantics.browsing.ui.graph.impl.GraphInputSources;
7 import org.simantics.browsing.ui.graph.impl.InputSourceListener;
8 import org.simantics.browsing.ui.graph.impl.ObservableInputSource;
9 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
10 import org.simantics.browsing.ui.graph.impl.WorkbenchSessionContextInputSource;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.RequestProcessor;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.request.UniqueRead;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.layer0.variable.VariableRepository;
18 import org.simantics.db.layer0.variable.Variables;
19 import org.simantics.scenegraph.INode;
20 import org.simantics.scenegraph.loader.ScenegraphLoaderProcess;
21 import org.simantics.views.ontology.ViewsResources;
22 import org.simantics.views.swt.client.base.SWTRoot;
23
24 public class SWTViewLoaderProcess extends ScenegraphLoaderProcess {
25
26         final private ModelledView view;
27         final private IWorkbenchSite site;
28         
29         public static class ISL implements InputSourceListener {
30                 
31                 private SWTViewLoaderProcess process;
32                 
33                 ISL(SWTViewLoaderProcess process) {
34                         this.process = process;
35                 }
36                 
37                 void dispose() {
38                         process = null;
39                 }
40                 
41         @Override
42         public void inputChanged(SessionContextInputSource source) {
43                 if(process == null) return;
44             Object input = source.get( Simantics.getSessionContext() );
45             if (input != null)
46                 process.view.inputChanged(source.getProvider(), input);
47             else
48                 ErrorLogger.defaultLogWarning("input source " + source + " invalidly returned null", new Exception("for stack trace only"));
49         }
50         };
51         
52     ISL inputSourceListener = new ISL(this);
53         
54         public SWTViewLoaderProcess(ModelledView view, IWorkbenchSite site, String name) {
55                 super(name);
56                 this.view = view;
57                 this.site = site;
58                 this.root = new SWTRoot(site);
59         }
60
61         public SWTViewLoaderProcess(ModelledView view, IWorkbenchSite site) {
62                 this(view, site, "<unnamed SWTViewLoaderProcess>");
63         }
64
65         @Override
66         public INode getRoot() {
67                 return root;
68         }
69         
70         @Override
71         protected void initialize(RequestProcessor processor, final Variable configuration) throws DatabaseException {
72                 
73                 SessionContextInputSource inputSource = processor.sync(new UniqueRead<SessionContextInputSource>() {
74                         @Override
75                         public SessionContextInputSource perform(ReadGraph graph) throws DatabaseException {
76                                 ViewsResources VIEW = ViewsResources.getInstance(graph);
77                                 Resource represents = configuration.getRepresents(graph);
78                                 Resource inputSource = graph.getPossibleObject(represents, VIEW.HasInputSource);
79                                 if (inputSource == null)
80                                         return GraphInputSources.projectSource();
81                                 return graph.adapt(inputSource, SessionContextInputSource.class);
82                         }
83                 });
84
85         if(view != null)
86             view.setInputSource(inputSource);
87
88         if (inputSource instanceof ObservableInputSource)
89             ((ObservableInputSource) inputSource).setListener(inputSourceListener);
90
91         if (inputSource instanceof WorkbenchSessionContextInputSource)
92                         ((WorkbenchSessionContextInputSource) inputSource).init(site, view);
93
94         }
95         
96         @Override
97         public void dispose() {
98                 
99                 if (isDisposed())
100                         return;
101                 
102                 inputSourceListener.dispose();
103                 for(String uri : registeredURIs) VariableRepository.unregister(uri);
104                 SWTRoot r = (SWTRoot)root;
105                 r.dispose();
106                 
107                 super.dispose();
108                 
109         }
110
111 }