1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 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 * Semantum Oy - issue #4384
12 * Semantum Oy - issue #7737
13 *******************************************************************************/
14 package org.simantics.ui.workbench;
16 import java.util.function.Supplier;
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.IWorkbenchPartSite;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.internal.PartSite;
26 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
27 import org.eclipse.ui.part.EditorPart;
28 import org.simantics.Simantics;
29 import org.simantics.db.Resource;
30 import org.simantics.db.Session;
31 import org.simantics.db.common.request.ParametrizedRead;
32 import org.simantics.db.management.ISessionContext;
35 * ResourceEditorPart is a base implementation for editors that support
36 * {@link ResourceEditorInput} style inputs for working on top of the Simantics
40 * If you want your ResourceEditorPart implementation to receive notifications
41 * for all graph change events through the {@link ChangeListener} interface,
42 * just implement it and it will be automatically invoked by this base
46 * @author Tuukka Lehtonen
47 * @author Jani Simomaa
49 public abstract class ResourceEditorPart extends EditorPart implements IResourceEditorPart {
51 protected boolean disposed = false;
52 protected ResourceEditorSupport support;
55 * Override to define your own input resource editor input validator that
56 * the view uses by default in {@link #init(IEditorSite, IEditorInput)}.
60 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
65 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
66 init(site, input, getInputValidator());
69 protected void createSupport(ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
70 support = new ResourceEditorSupport(this, inputValidator);
73 protected void init(IEditorSite site, IEditorInput input,
74 ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
75 if (!(input instanceof IResourceEditorInput))
76 throw new PartInitException("Invalid input: must be IResourceEditorInput");
80 createSupport(inputValidator);
82 // Set initial part name according to the name given by IEditorInput
83 setPartName(getEditorInput().getName());
85 Session session = Simantics.peekSession();
86 if (session != null) {
87 Supplier<Boolean> disposedCallback = () -> disposed;
89 new TitleRequest(site.getId(), getResourceInput()),
90 new TitleUpdater(site.getShell().getDisplay(), this::safeSetPartName, disposedCallback));
92 new ToolTipRequest(site.getId(), getResourceInput()),
93 new TitleUpdater(site.getShell().getDisplay(), this::safeSetTitleToolTip, disposedCallback));
98 * Safely sets part name for parts whose IEditorInput is not yet disposed (e.g.
99 * removed from database)
103 protected void safeSetPartName(String partName) {
104 if (!disposed && checkCompatibilityPartNotBeingDisposed()) { // this is to fix bug https://gitlab.simantics.org/simantics/platform/issues/117
105 setPartName(partName);
109 @SuppressWarnings("restriction")
110 private boolean checkCompatibilityPartNotBeingDisposed() {
111 IWorkbenchPartSite site = getSite();
112 if (site instanceof PartSite) {
113 PartSite partSite = (PartSite) getSite();
114 Object object = partSite.getModel().getObject();
115 if (object instanceof CompatibilityEditor) {
116 CompatibilityEditor editor = (CompatibilityEditor) object;
117 return !editor.isBeingDisposed();
124 * Safely sets title tooltip for parts whose IEditorInput is not yet disposed (e.g.
125 * removed from database)
129 protected void safeSetTitleToolTip(String toolTip) {
131 setTitleToolTip(toolTip);
136 public void dispose() {
142 protected void activateValidation() {
143 support.activateValidation();
146 public ISessionContext getSessionContext() {
147 return support.getSessionContext();
150 public Session getSession() {
151 return support.getSession();
155 * A resource editor does not need to perform any save operations since the
156 * graph model is global and different parts of it need not be saved
159 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
162 public void doSave(IProgressMonitor monitor) {
163 //System.out.println("[ResourceEditorPart] doSave: " + getPartName());
167 public void doSaveAs() {
168 //System.out.println("[ResourceEditorPart] doSaveAs: " + getPartName());
172 * A resource editor should never be dirty since its purpose is to reflect
173 * the current state of the graph model.
175 * @see org.eclipse.ui.part.EditorPart#isDirty()
178 public boolean isDirty() {
179 //System.out.println("[ResourceEditorPart] isDirty: " + getPartName());
184 public boolean isSaveAsAllowed() {
185 // Graph edits are always immediately sent to "UndoCore" which means
186 // that resource graph editors do not support save-features as such.
191 public IResourceEditorInput getResourceInput() {
192 return (IResourceEditorInput) getEditorInput();
199 public IStatusLineManager getStatusLineManager() {
200 IActionBars bars = getEditorSite().getActionBars();
201 IStatusLineManager mgr = bars.getStatusLineManager();
206 * @param message <code>null</code> to remove message
208 public void setStatusMessage(String message) {
209 getStatusLineManager().setMessage(message);
213 * @param message <code>null</code> to remove message
215 public void setStatusErrorMessage(String message) {
216 getStatusLineManager().setErrorMessage(message);
219 protected Resource getInputResource() {
220 return getResourceInput().getResource();
223 protected String getInputName() {
224 return getEditorInput().getName();
227 protected String getTitleText() {
228 return getInputName();
231 protected String getTitleTooltip() {
232 return getInputName();
235 protected void updateTitle() {
236 setPartName(getTitleText());
237 setTitleToolTip(getTitleTooltip());
241 * A utility method for easier invocation of Runnables asynchronously in the
246 protected void asyncExec(Runnable run) {
247 getSite().getShell().getDisplay().asyncExec(run);
250 @SuppressWarnings("unchecked")
252 public <T> T getAdapter(Class<T> adapter) {
253 if (adapter == Session.class)
254 return (T) getSession();
255 return super.getAdapter(adapter);