1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.diagramEditor;
\r
14 import org.eclipse.jface.layout.GridDataFactory;
\r
15 import org.eclipse.jface.layout.GridLayoutFactory;
\r
16 import org.eclipse.jface.resource.JFaceResources;
\r
17 import org.eclipse.jface.resource.LocalResourceManager;
\r
18 import org.eclipse.jface.viewers.IPostSelectionProvider;
\r
19 import org.eclipse.jface.viewers.ISelection;
\r
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
\r
21 import org.eclipse.jface.viewers.ISelectionProvider;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.widgets.Composite;
\r
24 import org.eclipse.swt.widgets.Control;
\r
25 import org.eclipse.ui.part.Page;
\r
26 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
\r
27 import org.simantics.browsing.ui.GraphExplorer;
\r
28 import org.simantics.browsing.ui.common.ColumnKeys;
\r
29 import org.simantics.browsing.ui.common.ErrorLogger;
\r
30 import org.simantics.browsing.ui.common.EvaluatorData;
\r
31 import org.simantics.browsing.ui.common.EvaluatorDataImpl;
\r
32 import org.simantics.browsing.ui.common.processors.ComparableFactoryResolver;
\r
33 import org.simantics.browsing.ui.common.processors.ComparableSelectorQueryProcessor;
\r
34 import org.simantics.browsing.ui.common.processors.ImageDecoratorFactoryResolver;
\r
35 import org.simantics.browsing.ui.common.processors.ImagerFactoryResolver;
\r
36 import org.simantics.browsing.ui.common.processors.LabelDecoratorFactoryResolver;
\r
37 import org.simantics.browsing.ui.common.processors.LabelerFactoryResolver;
\r
38 import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
\r
39 import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;
\r
40 import org.simantics.browsing.ui.common.processors.ViewpointFactoryResolver;
\r
41 import org.simantics.browsing.ui.graph.impl.AsyncReadGraphDataSource;
\r
42 import org.simantics.browsing.ui.graph.impl.InheritsQueryProcessor;
\r
43 import org.simantics.browsing.ui.graph.impl.ReadGraphDataSource;
\r
44 import org.simantics.browsing.ui.graph.impl.RelatedObjectsQueryProcessor;
\r
45 import org.simantics.browsing.ui.swt.ContextMenuInitializer;
\r
46 import org.simantics.browsing.ui.swt.DefaultKeyListener;
\r
47 import org.simantics.browsing.ui.swt.DefaultMouseListener;
\r
48 import org.simantics.browsing.ui.swt.DefaultSelectionDataResolver;
\r
49 import org.simantics.browsing.ui.swt.GraphExplorerFactory;
\r
50 import org.simantics.browsing.ui.swt.TypesQueryProcessor;
\r
51 import org.simantics.db.AsyncReadGraph;
\r
52 import org.simantics.db.ReadGraph;
\r
53 import org.simantics.db.Resource;
\r
54 import org.simantics.db.Session;
\r
55 import org.simantics.db.exception.DatabaseException;
\r
56 import org.simantics.db.management.ISessionContext;
\r
57 import org.simantics.db.management.ISessionContextProvider;
\r
58 import org.simantics.db.request.Read;
\r
59 import org.simantics.modeling.ModelingResources;
\r
60 import org.simantics.modeling.ui.Activator;
\r
61 import org.simantics.modeling.ui.modelBrowser.ModelEvaluators;
\r
62 import org.simantics.modeling.ui.modelBrowser.model.INode;
\r
63 import org.simantics.modeling.ui.modelBrowser.model.INode2;
\r
64 import org.simantics.structural.ui.menuContributions.LinkWithEditorContribution;
\r
65 import org.simantics.ui.workbench.IResourceEditorInput2;
\r
66 import org.simantics.utils.datastructures.Function;
\r
67 import org.simantics.utils.datastructures.Functions;
\r
70 * TODO: change to use newer contribution mechanisms, get rid of
\r
71 * {@link ModelEvaluators} use.
\r
73 * @author Tuukka Lehtonen
\r
75 public class DiagramOutlinePage extends Page implements IContentOutlinePage, IPostSelectionProvider {
\r
77 ISessionContextProvider sessionContextProvider;
\r
78 IResourceEditorInput2 input;
\r
79 LocalResourceManager resourceManager;
\r
80 Composite composite;
\r
81 GraphExplorer explorer;
\r
82 LinkWithEditor linkWithEditor;
\r
84 public DiagramOutlinePage(ISessionContextProvider contextProvider, IResourceEditorInput2 input) {
\r
85 assert contextProvider != null;
\r
86 assert input != null;
\r
88 this.sessionContextProvider = contextProvider;
\r
93 public void createControl(Composite parent) {
\r
94 resourceManager = new LocalResourceManager(JFaceResources.getResources());
\r
95 composite = new Composite(parent, SWT.NONE);
\r
96 GridLayoutFactory.fillDefaults().applyTo(composite);
\r
97 this.explorer = GraphExplorerFactory.getInstance().selectionDataResolver(new DefaultSelectionDataResolver()).create(composite);
\r
98 Control control = explorer.getControl();
\r
99 ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
\r
100 new ContextMenuInitializer("#GraphExplorerPopup").createContextMenu(control, selectionProvider, getSite());
\r
101 GridDataFactory.fillDefaults().grab(true, true).applyTo(control);
\r
103 // Consider ENTER presses to simulate mouse left button double clicks
\r
104 Function<String[]> resolver = Functions.constant(ColumnKeys.SINGLE);
\r
105 explorer.addListener(new DefaultKeyListener(sessionContextProvider, explorer, resolver));
\r
107 // Default double click handling
\r
108 explorer.addListener(new DefaultMouseListener(explorer));
\r
110 ISessionContext sessionContext = sessionContextProvider.getSessionContext();
\r
111 Session session = sessionContext != null ? sessionContext.getSession() : null;
\r
112 EvaluatorData data = null;
\r
113 if (session != null) {
\r
114 data = createEvaluatorData(sessionContext);
\r
115 explorer.setDataSource(new AsyncReadGraphDataSource(session));
\r
116 explorer.setDataSource(new ReadGraphDataSource(session));
\r
118 data = new EvaluatorDataImpl();
\r
119 explorer.removeDataSource(AsyncReadGraph.class);
\r
120 explorer.removeDataSource(ReadGraph.class);
\r
123 explorer.setProcessor(new ComparableFactoryResolver(data));
\r
124 explorer.setProcessor(new ViewpointFactoryResolver(data));
\r
125 explorer.setProcessor(new LabelerFactoryResolver(data));
\r
126 explorer.setProcessor(new ImagerFactoryResolver(data));
\r
127 explorer.setProcessor(new LabelDecoratorFactoryResolver(data));
\r
128 explorer.setProcessor(new ImageDecoratorFactoryResolver(data));
\r
129 explorer.setPrimitiveProcessor(new TypesQueryProcessor());
\r
130 explorer.setPrimitiveProcessor(new InheritsQueryProcessor());
\r
131 explorer.setPrimitiveProcessor(new RelatedObjectsQueryProcessor());
\r
133 explorer.setProcessor(new ComparableSelectorQueryProcessor());
\r
134 explorer.setPrimitiveProcessor(new UserSelectedViewpointFactoryQueryProcessor());
\r
135 explorer.setPrimitiveProcessor(new UserSelectedComparableFactoryQueryProcessor());
\r
139 // Look for a proper input resource to give to the model browser.
\r
140 final Resource diagram = input.getResource();
\r
141 if (diagram != null && session != null) {
\r
142 Object finalInput = session.syncRequest(new Read<Resource>() {
\r
144 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
145 ModelingResources mr = ModelingResources.getInstance(graph);
\r
146 return graph.getPossibleObject(diagram, mr.DiagramToComposite);
\r
149 if (finalInput != null)
\r
150 explorer.setRoot(finalInput);
\r
153 } catch (DatabaseException e) {
\r
154 ErrorLogger.defaultLogError(e);
\r
157 // linkWithEditor = new LinkWithEditor();
\r
158 // getSite().getActionBars().getToolBarManager().add(linkWithEditor);
\r
162 public void dispose() {
\r
163 if (resourceManager != null) {
\r
164 resourceManager.dispose();
\r
165 resourceManager = null;
\r
170 protected EvaluatorData createEvaluatorData(ISessionContext sessionContext) {
\r
171 EvaluatorData data = new EvaluatorDataImpl();
\r
172 data.addEvaluator(Resource.class, ModelEvaluators.createResourceEvaluator());
\r
173 data.addEvaluator(INode.class, ModelEvaluators.createNodeEvaluator());
\r
174 data.addEvaluator(INode2.class, ModelEvaluators.createNode2Evaluator());
\r
179 public Control getControl() {
\r
184 public void setFocus() {
\r
185 explorer.setFocus();
\r
189 public void addSelectionChangedListener(ISelectionChangedListener listener) {
\r
190 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
\r
191 selectionProvider.addSelectionChangedListener(listener);
\r
195 public ISelection getSelection() {
\r
196 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
\r
197 return selectionProvider.getSelection();
\r
201 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
\r
202 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
\r
203 selectionProvider.removeSelectionChangedListener(listener);
\r
207 public void setSelection(ISelection selection) {
\r
208 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
\r
209 selectionProvider.setSelection(selection);
\r
213 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
\r
214 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
\r
215 selectionProvider.addPostSelectionChangedListener(listener);
\r
219 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
\r
220 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
\r
221 selectionProvider.removePostSelectionChangedListener(listener);
\r
224 static class LinkWithEditor extends LinkWithEditorContribution {
\r
225 public LinkWithEditor() {
\r
226 super("DiagramOutlinePage.linkWithEditor", Activator.getDefault().getPreferenceStore());
\r