1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 * Semantum Oy - issue #4384
\r
12 *******************************************************************************/
\r
13 package org.simantics.ui.workbench;
\r
15 import java.util.function.Supplier;
\r
17 import org.eclipse.core.runtime.IProgressMonitor;
\r
18 import org.eclipse.jface.action.IStatusLineManager;
\r
19 import org.eclipse.ui.IActionBars;
\r
20 import org.eclipse.ui.IEditorInput;
\r
21 import org.eclipse.ui.IEditorSite;
\r
22 import org.eclipse.ui.PartInitException;
\r
23 import org.eclipse.ui.part.EditorPart;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.Session;
\r
26 import org.simantics.db.common.request.ParametrizedRead;
\r
27 import org.simantics.db.event.ChangeListener;
\r
28 import org.simantics.db.management.ISessionContext;
\r
29 import org.simantics.ui.SimanticsUI;
\r
32 * ResourceEditorPart is a base implementation for editors that support
\r
33 * {@link ResourceEditorInput} style inputs for working on top of the Simantics
\r
37 * If you want your ResourceEditorPart implementation to receive notifications
\r
38 * for all graph change events through the {@link ChangeListener} interface,
\r
39 * just implement it and it will be automatically invoked by this base
\r
43 * @author Tuukka Lehtonen
\r
45 public abstract class ResourceEditorPart extends EditorPart implements IResourceEditorPart {
\r
47 protected boolean disposed = false;
\r
48 protected ResourceEditorSupport support;
\r
51 * Override to define your own input resource editor input validator that
\r
52 * the view uses by default in {@link #init(IEditorSite, IEditorInput)}.
\r
56 protected ParametrizedRead<IResourceEditorInput, Boolean> getInputValidator() {
\r
61 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
\r
62 init(site, input, getInputValidator());
\r
65 protected void createSupport(ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
\r
66 support = new ResourceEditorSupport(this, inputValidator);
\r
69 protected void init(IEditorSite site, IEditorInput input,
\r
70 ParametrizedRead<IResourceEditorInput, Boolean> inputValidator) throws PartInitException {
\r
71 if (!(input instanceof IResourceEditorInput))
\r
72 throw new PartInitException("Invalid input: must be IResourceEditorInput");
\r
76 createSupport(inputValidator);
\r
78 // Set initial part name according to the name given by IEditorInput
\r
79 setPartName(getEditorInput().getName());
\r
81 Session session = SimanticsUI.peekSession();
\r
82 if (session != null) {
\r
83 Supplier<Boolean> disposedCallback = () -> disposed;
\r
84 session.asyncRequest(
\r
85 new TitleRequest(site.getId(), getResourceInput()),
\r
86 new TitleUpdater(site.getShell().getDisplay(), this::setPartName, disposedCallback));
\r
87 session.asyncRequest(
\r
88 new ToolTipRequest(site.getId(), getResourceInput()),
\r
89 new TitleUpdater(site.getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
\r
94 public void dispose() {
\r
100 protected void activateValidation() {
\r
101 support.activateValidation();
\r
104 public ISessionContext getSessionContext() {
\r
105 return support.getSessionContext();
\r
108 public Session getSession() {
\r
109 return support.getSession();
\r
113 * A resource editor does not need to perform any save operations since the
\r
114 * graph model is global and different parts of it need not be saved
\r
117 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
\r
120 public void doSave(IProgressMonitor monitor) {
\r
121 //System.out.println("[ResourceEditorPart] doSave: " + getPartName());
\r
125 public void doSaveAs() {
\r
126 //System.out.println("[ResourceEditorPart] doSaveAs: " + getPartName());
\r
130 * A resource editor should never be dirty since its purpose is to reflect
\r
131 * the current state of the graph model.
\r
133 * @see org.eclipse.ui.part.EditorPart#isDirty()
\r
136 public boolean isDirty() {
\r
137 //System.out.println("[ResourceEditorPart] isDirty: " + getPartName());
\r
142 public boolean isSaveAsAllowed() {
\r
143 // Graph edits are always immediately sent to "UndoCore" which means
\r
144 // that resource graph editors do not support save-features as such.
\r
149 public IResourceEditorInput getResourceInput() {
\r
150 return (IResourceEditorInput) getEditorInput();
\r
157 public IStatusLineManager getStatusLineManager() {
\r
158 IActionBars bars = getEditorSite().getActionBars();
\r
159 IStatusLineManager mgr = bars.getStatusLineManager();
\r
164 * @param message <code>null</code> to remove message
\r
166 public void setStatusMessage(String message) {
\r
167 getStatusLineManager().setMessage(message);
\r
171 * @param message <code>null</code> to remove message
\r
173 public void setStatusErrorMessage(String message) {
\r
174 getStatusLineManager().setErrorMessage(message);
\r
177 protected Resource getInputResource() {
\r
178 return getResourceInput().getResource();
\r
181 protected String getInputName() {
\r
182 return getEditorInput().getName();
\r
185 protected String getTitleText() {
\r
186 return getInputName();
\r
189 protected String getTitleTooltip() {
\r
190 return getInputName();
\r
193 protected void updateTitle() {
\r
194 setPartName(getTitleText());
\r
195 setTitleToolTip(getTitleTooltip());
\r
199 * A utility method for easier invocation of Runnables asynchronously in the
\r
204 protected void asyncExec(Runnable run) {
\r
205 getSite().getShell().getDisplay().asyncExec(run);
\r
208 @SuppressWarnings("unchecked")
\r
210 public <T> T getAdapter(Class<T> adapter) {
\r
211 if (adapter == Session.class)
\r
212 return (T) getSession();
\r
213 return super.getAdapter(adapter);
\r