]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorPart.java
5689cfa4b7dee2b8a655ad59e6cbe2746f868c27
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorPart.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 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  *     Semantum Oy - issue #4384
12  *******************************************************************************/
13 package org.simantics.ui.workbench;
14
15 import java.util.function.Supplier;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.action.IStatusLineManager;
19 import org.eclipse.ui.IActionBars;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorSite;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.part.EditorPart;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.common.request.ParametrizedRead;
27 import org.simantics.db.event.ChangeListener;
28 import org.simantics.db.management.ISessionContext;
29 import org.simantics.ui.SimanticsUI;
30
31 /**
32  * ResourceEditorPart is a base implementation for editors that support
33  * {@link ResourceEditorInput} style inputs for working on top of the Simantics
34  * graph database.
35  * 
36  * <p>
37  * If you want your ResourceEditorPart implementation to receive notifications
38  * for all graph change events through the {@link ChangeListener} interface,
39  * just implement it and it will be automatically invoked by this base
40  * implementation.
41  * </p>
42  * 
43  * @author Tuukka Lehtonen
44  */
45 public abstract class ResourceEditorPart extends EditorPart implements IResourceEditorPart {
46
47     protected boolean               disposed = false;
48     protected ResourceEditorSupport support;
49
50     /**
51      * Override to define your own input resource editor input validator that
52      * the view uses by default in {@link #init(IEditorSite, IEditorInput)}.
53      * 
54      * @return
55      */
56     protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
57         return null;
58     }
59
60     @Override
61     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
62         init(site, input, getInputValidator());
63     }
64     
65     protected void createSupport(ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
66         support = new ResourceEditorSupport(this, inputValidator);
67     }
68
69     protected void init(IEditorSite site, IEditorInput input,
70             ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
71         if (!(input instanceof IResourceEditorInput))
72             throw new PartInitException("Invalid input: must be IResourceEditorInput");
73
74         setSite(site);
75         setInput(input);
76         createSupport(inputValidator);
77
78         // Set initial part name according to the name given by IEditorInput
79         setPartName(getEditorInput().getName());
80
81         Session session = SimanticsUI.peekSession();
82         if (session != null) {
83             Supplier<Boolean> disposedCallback = () -> disposed;
84             session.asyncRequest(
85                     new TitleRequest(site.getId(), getResourceInput()),
86                     new TitleUpdater(site.getShell().getDisplay(), this::setPartName, disposedCallback));
87             session.asyncRequest(
88                     new ToolTipRequest(site.getId(), getResourceInput()),
89                     new TitleUpdater(site.getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
90         }
91     }
92
93     @Override
94     public void dispose() {
95         disposed = true;
96         support.dispose();
97         super.dispose();
98     }
99
100     protected void activateValidation() {
101         support.activateValidation();
102     }
103
104     public ISessionContext getSessionContext() {
105         return support.getSessionContext();
106     }
107
108     public Session getSession() {
109         return support.getSession();
110     }
111
112     /**
113      * A resource editor does not need to perform any save operations since the
114      * graph model is global and different parts of it need not be saved
115      * separately.
116      * 
117      * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
118      */
119     @Override
120     public void doSave(IProgressMonitor monitor) {
121         //System.out.println("[ResourceEditorPart] doSave: " + getPartName());
122     }
123
124     @Override
125     public void doSaveAs() {
126         //System.out.println("[ResourceEditorPart] doSaveAs: " + getPartName());
127     }
128
129     /**
130      * A resource editor should never be dirty since its purpose is to reflect
131      * the current state of the graph model.
132      * 
133      * @see org.eclipse.ui.part.EditorPart#isDirty()
134      */
135     @Override
136     public boolean isDirty() {
137         //System.out.println("[ResourceEditorPart] isDirty: " + getPartName());
138         return false;
139     }
140
141     @Override
142     public boolean isSaveAsAllowed() {
143         // Graph edits are always immediately sent to "UndoCore" which means
144         // that resource graph editors do not support save-features as such.
145         return false;
146     }
147
148     @Override
149     public IResourceEditorInput getResourceInput() {
150         return (IResourceEditorInput) getEditorInput();
151     }
152
153     //-------
154     // UTILS
155     //-------
156
157     public IStatusLineManager getStatusLineManager() {
158         IActionBars bars = getEditorSite().getActionBars();
159         IStatusLineManager mgr = bars.getStatusLineManager();
160         return mgr;
161     }
162
163     /**
164      * @param message <code>null</code> to remove message
165      */
166     public void setStatusMessage(String message) {
167         getStatusLineManager().setMessage(message);
168     }
169
170     /**
171      * @param message <code>null</code> to remove message
172      */
173     public void setStatusErrorMessage(String message) {
174         getStatusLineManager().setErrorMessage(message);
175     }
176
177     protected Resource getInputResource() {
178         return getResourceInput().getResource();
179     }
180
181     protected String getInputName() {
182         return getEditorInput().getName();
183     }
184
185     protected String getTitleText() {
186         return getInputName();
187     }
188
189     protected String getTitleTooltip() {
190         return getInputName();
191     }
192
193     protected void updateTitle() {
194         setPartName(getTitleText());
195         setTitleToolTip(getTitleTooltip());
196     }
197
198     /**
199      * A utility method for easier invocation of Runnables asynchronously in the
200      * SWT UI thread.
201      * 
202      * @param run
203      */
204     protected void asyncExec(Runnable run) {
205         getSite().getShell().getDisplay().asyncExec(run);
206     }
207
208     @SuppressWarnings("unchecked")
209     @Override
210     public <T> T getAdapter(Class<T> adapter) {
211         if (adapter == Session.class)
212             return (T) getSession();
213         return super.getAdapter(adapter);
214     }
215
216 }