1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.modeling.ui.diagramEditor;
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;
70 * TODO: change to use newer contribution mechanisms, get rid of
71 * {@link ModelEvaluators} use.
73 * @author Tuukka Lehtonen
75 public class DiagramOutlinePage extends Page implements IContentOutlinePage, IPostSelectionProvider {
77 ISessionContextProvider sessionContextProvider;
78 IResourceEditorInput2 input;
79 LocalResourceManager resourceManager;
81 GraphExplorer explorer;
82 LinkWithEditor linkWithEditor;
84 public DiagramOutlinePage(ISessionContextProvider contextProvider, IResourceEditorInput2 input) {
85 assert contextProvider != null;
88 this.sessionContextProvider = contextProvider;
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);
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));
107 // Default double click handling
108 explorer.addListener(new DefaultMouseListener(explorer));
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));
118 data = new EvaluatorDataImpl();
119 explorer.removeDataSource(AsyncReadGraph.class);
120 explorer.removeDataSource(ReadGraph.class);
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());
133 explorer.setProcessor(new ComparableSelectorQueryProcessor());
134 explorer.setPrimitiveProcessor(new UserSelectedViewpointFactoryQueryProcessor());
135 explorer.setPrimitiveProcessor(new UserSelectedComparableFactoryQueryProcessor());
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>() {
144 public Resource perform(ReadGraph graph) throws DatabaseException {
145 ModelingResources mr = ModelingResources.getInstance(graph);
146 return graph.getPossibleObject(diagram, mr.DiagramToComposite);
149 if (finalInput != null)
150 explorer.setRoot(finalInput);
153 } catch (DatabaseException e) {
154 ErrorLogger.defaultLogError(e);
157 // linkWithEditor = new LinkWithEditor();
158 // getSite().getActionBars().getToolBarManager().add(linkWithEditor);
162 public void dispose() {
163 if (resourceManager != null) {
164 resourceManager.dispose();
165 resourceManager = null;
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());
179 public Control getControl() {
184 public void setFocus() {
189 public void addSelectionChangedListener(ISelectionChangedListener listener) {
190 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
191 selectionProvider.addSelectionChangedListener(listener);
195 public ISelection getSelection() {
196 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
197 return selectionProvider.getSelection();
201 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
202 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
203 selectionProvider.removeSelectionChangedListener(listener);
207 public void setSelection(ISelection selection) {
208 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
209 selectionProvider.setSelection(selection);
213 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
214 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
215 selectionProvider.addPostSelectionChangedListener(listener);
219 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
220 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
221 selectionProvider.removePostSelectionChangedListener(listener);
224 static class LinkWithEditor extends LinkWithEditorContribution {
225 public LinkWithEditor() {
226 super("DiagramOutlinePage.linkWithEditor", Activator.getDefault().getPreferenceStore());