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 *******************************************************************************/
13 package org.simantics.ui.workbench;
15 import java.util.function.Supplier;
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;
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
45 public abstract class ResourceEditorPart extends EditorPart implements IResourceEditorPart {
47 protected boolean disposed = false;
48 protected ResourceEditorSupport support;
51 * Override to define your own input resource editor input validator that
52 * the view uses by default in {@link #init(IEditorSite, IEditorInput)}.
56 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
61 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
62 init(site, input, getInputValidator());
65 protected void createSupport(ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
66 support = new ResourceEditorSupport(this, inputValidator);
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");
76 createSupport(inputValidator);
78 // Set initial part name according to the name given by IEditorInput
79 setPartName(getEditorInput().getName());
81 Session session = SimanticsUI.peekSession();
82 if (session != null) {
83 Supplier<Boolean> disposedCallback = () -> disposed;
85 new TitleRequest(site.getId(), getResourceInput()),
86 new TitleUpdater(site.getShell().getDisplay(), this::setPartName, disposedCallback));
88 new ToolTipRequest(site.getId(), getResourceInput()),
89 new TitleUpdater(site.getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
94 public void dispose() {
100 protected void activateValidation() {
101 support.activateValidation();
104 public ISessionContext getSessionContext() {
105 return support.getSessionContext();
108 public Session getSession() {
109 return support.getSession();
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
117 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
120 public void doSave(IProgressMonitor monitor) {
121 //System.out.println("[ResourceEditorPart] doSave: " + getPartName());
125 public void doSaveAs() {
126 //System.out.println("[ResourceEditorPart] doSaveAs: " + getPartName());
130 * A resource editor should never be dirty since its purpose is to reflect
131 * the current state of the graph model.
133 * @see org.eclipse.ui.part.EditorPart#isDirty()
136 public boolean isDirty() {
137 //System.out.println("[ResourceEditorPart] isDirty: " + getPartName());
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.
149 public IResourceEditorInput getResourceInput() {
150 return (IResourceEditorInput) getEditorInput();
157 public IStatusLineManager getStatusLineManager() {
158 IActionBars bars = getEditorSite().getActionBars();
159 IStatusLineManager mgr = bars.getStatusLineManager();
164 * @param message <code>null</code> to remove message
166 public void setStatusMessage(String message) {
167 getStatusLineManager().setMessage(message);
171 * @param message <code>null</code> to remove message
173 public void setStatusErrorMessage(String message) {
174 getStatusLineManager().setErrorMessage(message);
177 protected Resource getInputResource() {
178 return getResourceInput().getResource();
181 protected String getInputName() {
182 return getEditorInput().getName();
185 protected String getTitleText() {
186 return getInputName();
189 protected String getTitleTooltip() {
190 return getInputName();
193 protected void updateTitle() {
194 setPartName(getTitleText());
195 setTitleToolTip(getTitleTooltip());
199 * A utility method for easier invocation of Runnables asynchronously in the
204 protected void asyncExec(Runnable run) {
205 getSite().getShell().getDisplay().asyncExec(run);
208 @SuppressWarnings("unchecked")
210 public <T> T getAdapter(Class<T> adapter) {
211 if (adapter == Session.class)
212 return (T) getSession();
213 return super.getAdapter(adapter);