1 /*******************************************************************************
2 * Copyright (c) 2012, 2017 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
12 *******************************************************************************/
13 package org.simantics.modeling.ui.diagramEditor;
15 import java.lang.reflect.Constructor;
16 import java.util.function.Predicate;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExecutableExtension;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IEditorReference;
27 import org.eclipse.ui.IEditorSite;
28 import org.eclipse.ui.IPartListener2;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchPartReference;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.part.EditorPart;
36 import org.osgi.framework.Bundle;
37 import org.simantics.db.Resource;
38 import org.simantics.diagram.ui.WorkbenchSelectionProvider;
39 import org.simantics.g2d.diagram.IDiagram;
40 import org.simantics.modeling.ui.diagramEditor.DiagramViewer.DiagramViewerHost;
41 import org.simantics.ui.workbench.IResourceEditorInput;
42 import org.simantics.ui.workbench.IResourceEditorInput2;
43 import org.simantics.ui.workbench.IResourceEditorPart2;
44 import org.simantics.ui.workbench.ResourceEditorSupport;
45 import org.simantics.utils.DataContainer;
46 import org.simantics.utils.threads.IThreadWorkQueue;
47 import org.simantics.utils.threads.SWTThread;
48 import org.simantics.utils.ui.ErrorLogger;
51 * A class for diagram editor parts that contains logic for destruction and
52 * (re)initialization of the actual diagram viewer and its controls during the
53 * life cycle of this editor part.
56 * To use this class in an editor part extension, define the following in the
57 * <code>class</code> attribute of the extension:
60 * class="org.simantics.modeling.ui.diagramEditor.DiagramEditor:viewer=%VIEWER%"
63 * where <code>%VIEWER%</code> is the name of the class that either is or
64 * extends {@link org.simantics.modeling.ui.diagramEditor.DiagramViewer}. The
65 * <code>viewer</code> argument tells {@link DiagramEditor} where to find the
66 * initializer for the diagram editor controls. The initializer must have a
67 * default constructor.
70 * This class is not intended to be extended by clients. Customizations should
71 * be performed through the viewer class.
73 * @author Tuukka Lehtonen
74 * @author Antti Villberg
76 public class DiagramEditor extends EditorPart implements IResourceEditorPart2, IPartListener2, DiagramViewerHost, IExecutableExtension {
79 * The {@value #ARG_VIEWER} argument for this editor part class tells the
80 * name of the class to use for initializing the diagram viewer, i.e.
81 * {@link #viewer}. The class is instantiated through reflection using the
82 * class loader of the bundle named {@link #viewerContributor}.
84 * @see #setInitializationData(IConfigurationElement, String, Object)
86 public static final String ARG_VIEWER = "viewer";
88 private Composite parent;
90 private String viewerContributor;
91 private String viewerClassName;
93 private ResourceEditorSupport support;
94 private DiagramViewer viewer;
97 * Used for distributing the reference to the IDiagram eventually loaded by
98 * the diagram viewer to both the diagram viewer and
99 * {@link #createSelectionProvider()}. {@link DiagramViewerLoadJob} is what
100 * ultimately does the actual loading and sets this container's value.
101 * @see #createSelectionProvider()
102 * @see DiagramViewerLoadJob
104 protected DataContainer<IDiagram> diagramContainer = new DataContainer<IDiagram>();
105 protected IThreadWorkQueue swt;
106 protected WorkbenchSelectionProvider selectionProvider;
109 * Reads the class arguments from the string in the data argument.
111 * @see org.eclipse.ui.part.EditorPart#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
112 * java.lang.String, java.lang.Object)
113 * @see #createViewer()
116 public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
117 super.setInitializationData(cfig, propertyName, data);
119 if (data instanceof String) {
120 viewerContributor = cfig.getContributor().getName();
122 String[] parameters = ((String) data).split(";");
124 for (String parameter : parameters) {
125 String[] keyValue = parameter.split("=");
126 if (keyValue.length > 2) {
127 ErrorLogger.defaultLogWarning("Invalid parameter '" + parameter + ". Complete view argument: " + data, null);
130 String key = keyValue[0];
131 String value = keyValue.length > 1 ? keyValue[1] : "";
133 if (ARG_VIEWER.equals(key)) {
134 viewerClassName = value;
140 protected DiagramViewer createViewer() throws PartInitException {
141 if (viewerClassName == null)
142 throw new PartInitException(
143 "DiagramViewer contributor class was not specified in editor extension's class attribute viewer-argument. contributor is '"
144 + viewerContributor + "'");
147 Bundle b = Platform.getBundle(viewerContributor);
149 throw new PartInitException("DiagramViewer '" + viewerClassName + "' contributor bundle '"
150 + viewerContributor + "' was not found in the platform.");
152 Class<?> clazz = b.loadClass(viewerClassName);
153 if (!DiagramViewer.class.isAssignableFrom(clazz))
154 throw new PartInitException("DiagramViewer class '" + viewerClassName + "' is not assignable to "
155 + DiagramViewer.class + ".");
157 Constructor<?> ctor = clazz.getConstructor();
158 return (DiagramViewer) ctor.newInstance();
159 } catch (Exception e) {
160 throw new PartInitException("Failed to instantiate DiagramViewer implementation '" + viewerClassName
161 + "' from bundle '" + viewerContributor + "'. See exception for details.", e);
166 public IResourceEditorInput getResourceInput() {
167 return viewer.getResourceInput();
171 public IResourceEditorInput2 getResourceInput2() {
172 return viewer.getResourceInput2();
175 public DiagramViewer getViewer() {
179 public Resource getRuntimeResource() {
180 DiagramViewer viewer = this.viewer;
181 return viewer != null ? viewer.getRuntime() : null;
184 public Resource getInputResource() {
185 DiagramViewer viewer = this.viewer;
186 return viewer != null ? viewer.getInputResource() : null;
190 public void doSave(IProgressMonitor monitor) {
194 public void doSaveAs() {
198 public boolean isDirty() {
203 public boolean isSaveAsAllowed() {
208 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
212 viewer = createViewer();
214 // selectionProvider MUST be created and attached to the workbench site:
215 // 1. only once during the life-cycle of this editor part
216 // 2. in SWT UI thread
217 // 3. at least before returning from #createPartControl
218 swt = SWTThread.getThreadAccess(PlatformUI.getWorkbench().getDisplay());
219 selectionProvider = createSelectionProvider();
221 viewer.init(this, site, input, diagramContainer, selectionProvider);
223 getSite().getPage().addPartListener(this);
225 support = new ResourceEditorSupport(this, viewer.getInputValidator());
226 support.activateValidation();
230 public void createPartControl(Composite parent) {
231 this.parent = parent;
235 private void initializeViewer() {
236 parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
237 viewer.createPartControl(parent);
238 // It is possible that something goes wrong and the parent gets disposed already
239 if(parent.isDisposed()) return;
244 public void setFocus() {
250 * Override this to customize the kind of selection provider created for
251 * this {@link DiagramEditor}.
253 * @return the selection provider to set for the site
255 protected WorkbenchSelectionProvider createSelectionProvider() {
256 return new DiagramViewerSelectionProvider(swt, getSite(), diagramContainer);
259 @SuppressWarnings("unchecked")
260 public <T> T getAdapter(Class<T> adapter) {
261 if (adapter == DiagramViewer.class)
264 return (T) super.getAdapter(adapter);
266 Object result = viewer.getAdapter(adapter);
269 return super.getAdapter(adapter);
273 public void dispose() {
274 getSite().getPage().removePartListener(this);
276 if (support != null) {
281 DISPOSING_POLICY.removeDisposer(disposer);
288 public void doSetPartName(String name) {
293 public void doSetTitleToolTip(String name) {
294 setTitleToolTip(name);
297 // BEGIN: IPartListener2 implementation
300 public void partActivated(IWorkbenchPartReference partRef) {
304 public void partBroughtToTop(IWorkbenchPartReference partRef) {
308 public void partClosed(IWorkbenchPartReference partRef) {
312 public void partDeactivated(IWorkbenchPartReference partRef) {
316 public void partOpened(IWorkbenchPartReference partRef) {
320 * Disposes of the diagram viewer if not already disposed.
323 public void partHidden(IWorkbenchPartReference partRef) {
324 IWorkbenchPart part = partRef.getPart(false);
325 if (this.equals(part)) {
326 DISPOSING_POLICY.addDisposer(disposer);
330 private static final DisposingPolicy DISPOSING_POLICY =
331 new DisposingPolicy();
333 private Runnable disposer = () -> tryDisposeViewer();
335 private void tryDisposeViewer() {
336 if (viewer != null) {
337 Composite viewerComposite = viewer.getComposite();
340 if (viewerComposite != null) {
341 viewerComposite.dispose();
347 * Initializes the diagram viewer if not already initialized.
350 public void partVisible(IWorkbenchPartReference partRef) {
351 IWorkbenchPart part = partRef.getPart(false);
352 if (this.equals(part)) {
353 DISPOSING_POLICY.removeDisposer(disposer);
354 if (viewer == null) {
356 viewer = createViewer();
357 viewer.init(this, getEditorSite(), getEditorInput(), diagramContainer, selectionProvider);
359 } catch (PartInitException e) {
360 // This should never happen!
361 ErrorLogger.defaultLogError(e);
368 public void partInputChanged(IWorkbenchPartReference partRef) {
371 // END: IPartListener2 implementation
374 * Reinitialize this diagram editor from scratch.
376 * <p>Must be invoked from the SWT thread.</p>
378 public void reinitializeViewer() {
379 if (viewer != null) {
380 DISPOSING_POLICY.removeDisposer(disposer);
383 viewer = createViewer();
384 viewer.init(this, getEditorSite(), getEditorInput(), diagramContainer, selectionProvider);
386 } catch (PartInitException e) {
387 // This should never happen!
388 ErrorLogger.defaultLogError(e);
394 * Reinitializes all {@link DiagramEditor} instances in all workbench windows for which
395 * the specified predicate returns <code>true</code>.
397 * <p>Must be invoked from the SWT thread.</p>
400 * tester for editor inputs
402 public static void reinitializeDiagram(Predicate<IEditorInput> predicate) {
403 for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
404 for (IWorkbenchPage page : window.getPages()) {
405 for (IEditorReference editorRef : page.getEditorReferences()) {
407 IEditorInput input = editorRef.getEditorInput();
408 if (predicate.test(input)) {
409 IEditorPart editor = editorRef.getEditor(false);
410 if (editor instanceof DiagramEditor)
411 ((DiagramEditor) editor).reinitializeViewer();
413 } catch (PartInitException e) {
414 ErrorLogger.defaultLogError(e);
422 * Reinitializes all DiagramEditor instances in all workbench windows that have
423 * the specified <code>diagram</code> as their input.
425 * <p>Must be invoked from the SWT thread.</p>
428 * the diagram resource for which to reinitialize all DiagramEditors
431 public static void reinitializeDiagram(Resource diagram) {
432 reinitializeDiagram(input -> input instanceof IResourceEditorInput
433 && ((IResourceEditorInput) input).getResource().equals(diagram));