]> gerrit.simantics Code Review - simantics/platform.git/blob
289df6b888f754cd10ae10836cd72089701e032b
[simantics/platform.git] /
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.modeling.ui.diagramEditor;
13
14 import org.eclipse.jface.layout.GridDataFactory;
15 import org.eclipse.jface.layout.GridLayoutFactory;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.resource.LocalResourceManager;
18 import org.eclipse.jface.viewers.IPostSelectionProvider;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.ui.part.Page;
26 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
27 import org.simantics.browsing.ui.GraphExplorer;
28 import org.simantics.browsing.ui.common.ColumnKeys;
29 import org.simantics.browsing.ui.common.ErrorLogger;
30 import org.simantics.browsing.ui.common.EvaluatorData;
31 import org.simantics.browsing.ui.common.EvaluatorDataImpl;
32 import org.simantics.browsing.ui.common.processors.ComparableFactoryResolver;
33 import org.simantics.browsing.ui.common.processors.ComparableSelectorQueryProcessor;
34 import org.simantics.browsing.ui.common.processors.ImageDecoratorFactoryResolver;
35 import org.simantics.browsing.ui.common.processors.ImagerFactoryResolver;
36 import org.simantics.browsing.ui.common.processors.LabelDecoratorFactoryResolver;
37 import org.simantics.browsing.ui.common.processors.LabelerFactoryResolver;
38 import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
39 import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;
40 import org.simantics.browsing.ui.common.processors.ViewpointFactoryResolver;
41 import org.simantics.browsing.ui.graph.impl.AsyncReadGraphDataSource;
42 import org.simantics.browsing.ui.graph.impl.InheritsQueryProcessor;
43 import org.simantics.browsing.ui.graph.impl.ReadGraphDataSource;
44 import org.simantics.browsing.ui.graph.impl.RelatedObjectsQueryProcessor;
45 import org.simantics.browsing.ui.swt.ContextMenuInitializer;
46 import org.simantics.browsing.ui.swt.DefaultKeyListener;
47 import org.simantics.browsing.ui.swt.DefaultMouseListener;
48 import org.simantics.browsing.ui.swt.DefaultSelectionDataResolver;
49 import org.simantics.browsing.ui.swt.GraphExplorerFactory;
50 import org.simantics.browsing.ui.swt.TypesQueryProcessor;
51 import org.simantics.db.AsyncReadGraph;
52 import org.simantics.db.ReadGraph;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.exception.DatabaseException;
56 import org.simantics.db.management.ISessionContext;
57 import org.simantics.db.management.ISessionContextProvider;
58 import org.simantics.db.request.Read;
59 import org.simantics.modeling.ModelingResources;
60 import org.simantics.modeling.ui.Activator;
61 import org.simantics.modeling.ui.modelBrowser.ModelEvaluators;
62 import org.simantics.modeling.ui.modelBrowser.model.INode;
63 import org.simantics.modeling.ui.modelBrowser.model.INode2;
64 import org.simantics.structural.ui.menuContributions.LinkWithEditorContribution;
65 import org.simantics.ui.workbench.IResourceEditorInput2;
66 import org.simantics.utils.datastructures.Function;
67 import org.simantics.utils.datastructures.Functions;
68
69 /**
70  * TODO: change to use newer contribution mechanisms, get rid of
71  * {@link ModelEvaluators} use.
72  * 
73  * @author Tuukka Lehtonen
74  */
75 public class DiagramOutlinePage extends Page implements IContentOutlinePage, IPostSelectionProvider {
76
77     ISessionContextProvider sessionContextProvider;
78     IResourceEditorInput2    input;
79     LocalResourceManager    resourceManager;
80     Composite               composite;
81     GraphExplorer           explorer;
82     LinkWithEditor          linkWithEditor;
83
84     public DiagramOutlinePage(ISessionContextProvider contextProvider, IResourceEditorInput2 input) {
85         assert contextProvider != null;
86         assert input != null;
87
88         this.sessionContextProvider = contextProvider;
89         this.input = input;
90     }
91
92     @Override
93     public void createControl(Composite parent) {
94         resourceManager = new LocalResourceManager(JFaceResources.getResources());
95         composite = new Composite(parent, SWT.NONE);
96         GridLayoutFactory.fillDefaults().applyTo(composite);
97         this.explorer = GraphExplorerFactory.getInstance().selectionDataResolver(new DefaultSelectionDataResolver()).create(composite);
98                 Control control = explorer.getControl();
99         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
100         new ContextMenuInitializer("#GraphExplorerPopup").createContextMenu(control, selectionProvider, getSite());
101         GridDataFactory.fillDefaults().grab(true, true).applyTo(control);
102
103         // Consider ENTER presses to simulate mouse left button double clicks
104         Function<String[]> resolver = Functions.constant(ColumnKeys.SINGLE);
105         explorer.addListener(new DefaultKeyListener(sessionContextProvider, explorer, resolver));
106
107         // Default double click handling
108         explorer.addListener(new DefaultMouseListener(explorer));
109
110         ISessionContext sessionContext = sessionContextProvider.getSessionContext();
111         Session session = sessionContext != null ? sessionContext.getSession() : null;
112         EvaluatorData data = null;
113         if (session != null) {
114             data = createEvaluatorData(sessionContext);
115             explorer.setDataSource(new AsyncReadGraphDataSource(session));
116             explorer.setDataSource(new ReadGraphDataSource(session));
117         } else {
118             data = new EvaluatorDataImpl();
119             explorer.removeDataSource(AsyncReadGraph.class);
120             explorer.removeDataSource(ReadGraph.class);
121         }
122
123         explorer.setProcessor(new ComparableFactoryResolver(data));
124         explorer.setProcessor(new ViewpointFactoryResolver(data));
125         explorer.setProcessor(new LabelerFactoryResolver(data));
126         explorer.setProcessor(new ImagerFactoryResolver(data));
127         explorer.setProcessor(new LabelDecoratorFactoryResolver(data));
128         explorer.setProcessor(new ImageDecoratorFactoryResolver(data));
129         explorer.setPrimitiveProcessor(new TypesQueryProcessor());
130         explorer.setPrimitiveProcessor(new InheritsQueryProcessor());
131         explorer.setPrimitiveProcessor(new RelatedObjectsQueryProcessor());
132
133         explorer.setProcessor(new ComparableSelectorQueryProcessor());
134         explorer.setPrimitiveProcessor(new UserSelectedViewpointFactoryQueryProcessor());
135         explorer.setPrimitiveProcessor(new UserSelectedComparableFactoryQueryProcessor());
136
137         try {
138
139             // Look for a proper input resource to give to the model browser.
140             final Resource diagram = input.getResource();
141             if (diagram != null && session != null) {
142                 Object finalInput = session.syncRequest(new Read<Resource>() {
143                     @Override
144                     public Resource perform(ReadGraph graph) throws DatabaseException {
145                         ModelingResources mr = ModelingResources.getInstance(graph);
146                         return graph.getPossibleObject(diagram, mr.DiagramToComposite);
147                     }
148                 });
149                 if (finalInput != null)
150                     explorer.setRoot(finalInput);
151             }
152
153         } catch (DatabaseException e) {
154             ErrorLogger.defaultLogError(e);
155         }
156
157 //        linkWithEditor = new LinkWithEditor();
158 //        getSite().getActionBars().getToolBarManager().add(linkWithEditor);
159     }
160
161     @Override
162     public void dispose() {
163         if (resourceManager != null) {
164             resourceManager.dispose();
165             resourceManager = null;
166         }
167         super.dispose();
168     }
169
170     protected EvaluatorData createEvaluatorData(ISessionContext sessionContext) {
171         EvaluatorData data = new EvaluatorDataImpl();
172         data.addEvaluator(Resource.class, ModelEvaluators.createResourceEvaluator());
173         data.addEvaluator(INode.class, ModelEvaluators.createNodeEvaluator());
174         data.addEvaluator(INode2.class, ModelEvaluators.createNode2Evaluator());
175         return data;
176     }
177
178     @Override
179     public Control getControl() {
180         return composite;
181     }
182
183     @Override
184     public void setFocus() {
185         explorer.setFocus();
186     }
187
188     @Override
189     public void addSelectionChangedListener(ISelectionChangedListener listener) {
190         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
191         selectionProvider.addSelectionChangedListener(listener);
192     }
193
194     @Override
195     public ISelection getSelection() {
196         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
197         return selectionProvider.getSelection();
198     }
199
200     @Override
201     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
202         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
203         selectionProvider.removeSelectionChangedListener(listener);
204     }
205
206     @Override
207     public void setSelection(ISelection selection) {
208         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
209         selectionProvider.setSelection(selection);
210     }
211
212     @Override
213     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
214         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
215         selectionProvider.addPostSelectionChangedListener(listener);
216     }
217
218     @Override
219     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
220         IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
221         selectionProvider.removePostSelectionChangedListener(listener);
222     }
223
224     static class LinkWithEditor extends LinkWithEditorContribution {
225         public LinkWithEditor() {
226             super("DiagramOutlinePage.linkWithEditor", Activator.getDefault().getPreferenceStore());
227         }
228     }
229
230 }