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.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;
32 * ResourceEditorPart is a base implementation for editors that support
33 * {@link ResourceEditorInput} style inputs for working on top of the Simantics
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
43 * @author Tuukka Lehtonen
44 * @author Jani Simomaa
46 public abstract class ResourceEditorPart extends EditorPart implements IResourceEditorPart {
48 protected boolean disposed = false;
49 protected ResourceEditorSupport support;
52 * Override to define your own input resource editor input validator that
53 * the view uses by default in {@link #init(IEditorSite, IEditorInput)}.
57 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
62 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
63 init(site, input, getInputValidator());
66 protected void createSupport(ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
67 support = new ResourceEditorSupport(this, inputValidator);
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");
77 createSupport(inputValidator);
79 // Set initial part name according to the name given by IEditorInput
80 setPartName(getEditorInput().getName());
82 Session session = SimanticsUI.peekSession();
83 if (session != null) {
84 Supplier<Boolean> disposedCallback = () -> disposed;
86 new TitleRequest(site.getId(), getResourceInput()),
87 new TitleUpdater(site.getShell().getDisplay(), this::safeSetPartName, disposedCallback));
89 new ToolTipRequest(site.getId(), getResourceInput()),
90 new TitleUpdater(site.getShell().getDisplay(), this::safeSetTitleToolTip, disposedCallback));
95 * Safely sets part name for parts whose IEditorInput is not yet disposed (e.g.
96 * removed from database)
100 protected void safeSetPartName(String partName) {
102 setPartName(partName);
107 * Safely sets title tooltip for parts whose IEditorInput is not yet disposed (e.g.
108 * removed from database)
112 protected void safeSetTitleToolTip(String toolTip) {
114 setTitleToolTip(toolTip);
119 public void dispose() {
125 protected void activateValidation() {
126 support.activateValidation();
129 public ISessionContext getSessionContext() {
130 return support.getSessionContext();
133 public Session getSession() {
134 return support.getSession();
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
142 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
145 public void doSave(IProgressMonitor monitor) {
146 //System.out.println("[ResourceEditorPart] doSave: " + getPartName());
150 public void doSaveAs() {
151 //System.out.println("[ResourceEditorPart] doSaveAs: " + getPartName());
155 * A resource editor should never be dirty since its purpose is to reflect
156 * the current state of the graph model.
158 * @see org.eclipse.ui.part.EditorPart#isDirty()
161 public boolean isDirty() {
162 //System.out.println("[ResourceEditorPart] isDirty: " + getPartName());
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.
174 public IResourceEditorInput getResourceInput() {
175 return (IResourceEditorInput) getEditorInput();
182 public IStatusLineManager getStatusLineManager() {
183 IActionBars bars = getEditorSite().getActionBars();
184 IStatusLineManager mgr = bars.getStatusLineManager();
189 * @param message <code>null</code> to remove message
191 public void setStatusMessage(String message) {
192 getStatusLineManager().setMessage(message);
196 * @param message <code>null</code> to remove message
198 public void setStatusErrorMessage(String message) {
199 getStatusLineManager().setErrorMessage(message);
202 protected Resource getInputResource() {
203 return getResourceInput().getResource();
206 protected String getInputName() {
207 return getEditorInput().getName();
210 protected String getTitleText() {
211 return getInputName();
214 protected String getTitleTooltip() {
215 return getInputName();
218 protected void updateTitle() {
219 setPartName(getTitleText());
220 setTitleToolTip(getTitleTooltip());
224 * A utility method for easier invocation of Runnables asynchronously in the
229 protected void asyncExec(Runnable run) {
230 getSite().getShell().getDisplay().asyncExec(run);
233 @SuppressWarnings("unchecked")
235 public <T> T getAdapter(Class<T> adapter) {
236 if (adapter == Session.class)
237 return (T) getSession();
238 return super.getAdapter(adapter);