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