]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditor.java
(refs #7568) Reinitialize diagram editor when element is switched
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / DiagramEditor.java
index 58073330ae91f1ba8c2a3ecfac29c2b38aacb3ad..7734ce7145f60dc20ba7b27c21a769a7b36ab17a 100644 (file)
@@ -1,6 +1,6 @@
 /*******************************************************************************
- * Copyright (c) 2012 Association for Decentralized Information Management in
- * Industry THTH ry.
+ * Copyright (c) 2012, 2017 Association for Decentralized Information Management
+ * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -8,10 +8,12 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - #7586
  *******************************************************************************/
 package org.simantics.modeling.ui.diagramEditor;
 
 import java.lang.reflect.Constructor;
+import java.util.function.Predicate;
 
 import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.IExecutableExtension;
@@ -20,10 +22,14 @@ import org.eclipse.core.runtime.Platform;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IEditorReference;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IPartListener2;
+import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.IWorkbenchPartReference;
+import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.part.EditorPart;
@@ -323,13 +329,8 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I
     
     private static final DisposingPolicy DISPOSING_POLICY = 
             new DisposingPolicy();
-    
-    private Runnable disposer = new Runnable() {
-        @Override
-        public void run() {
-            tryDisposeViewer();
-        }
-    };
+
+    private Runnable disposer = () -> tryDisposeViewer();
 
     private void tryDisposeViewer() {
         if (viewer != null) {
@@ -341,7 +342,7 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I
             }
         }
     }
-    
+
     /**
      * Initializes the diagram viewer if not already initialized.
      */
@@ -369,4 +370,67 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I
 
     // END: IPartListener2 implementation
 
+    /**
+     * Reinitialize this diagram editor from scratch.
+     * 
+     * <p>Must be invoked from the SWT thread.</p>
+     */
+    public void reinitializeViewer() {
+        if (viewer != null) {
+            DISPOSING_POLICY.removeDisposer(disposer);
+            tryDisposeViewer();
+            try {
+                viewer = createViewer();
+                viewer.init(this, getEditorSite(), getEditorInput(), diagramContainer, selectionProvider);
+                initializeViewer();
+            } catch (PartInitException e) {
+                // This should never happen!
+                ErrorLogger.defaultLogError(e);
+            }
+        }
+    }
+
+    /**
+     * Reinitializes all {@link DiagramEditor} instances in all workbench windows for which
+     * the specified predicate returns <code>true</code>.
+     * 
+     * <p>Must be invoked from the SWT thread.</p>
+     * 
+     * @param predicate
+     *            tester for editor inputs
+     */
+    public static void reinitializeDiagram(Predicate<IEditorInput> predicate) {
+        for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
+            for (IWorkbenchPage page : window.getPages()) {
+                for (IEditorReference editorRef : page.getEditorReferences()) {
+                    try {
+                        IEditorInput input = editorRef.getEditorInput();
+                        if (predicate.test(input)) {
+                            IEditorPart editor = editorRef.getEditor(false);
+                            if (editor instanceof DiagramEditor)
+                                ((DiagramEditor) editor).reinitializeViewer();
+                        }
+                    } catch (PartInitException e) {
+                        ErrorLogger.defaultLogError(e);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Reinitializes all DiagramEditor instances in all workbench windows that have
+     * the specified <code>diagram</code> as their input.
+     * 
+     * <p>Must be invoked from the SWT thread.</p>
+     * 
+     * @param diagram
+     *            the diagram resource for which to reinitialize all DiagramEditors
+     *            for
+     */
+    public static void reinitializeDiagram(Resource diagram) {
+        reinitializeDiagram(input -> input instanceof IResourceEditorInput
+                && ((IResourceEditorInput) input).getResource().equals(diagram));
+    }
+
 }