X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FdiagramEditor%2FDiagramEditor.java;h=ddba8ebfb322a9323b22bf08dd3bcca4eb7d8fd5;hb=0475b9e2a9331ef8f3dcd61567e5d071ae8ef561;hp=58073330ae91f1ba8c2a3ecfac29c2b38aacb3ad;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditor.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditor.java index 58073330a..ddba8ebfb 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditor.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramEditor.java @@ -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,22 +8,30 @@ * * 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.Set; +import java.util.function.Predicate; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExecutableExtension; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Platform; +import org.eclipse.osgi.util.NLS; 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; @@ -77,7 +85,7 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I * * @see #setInitializationData(IConfigurationElement, String, Object) */ - public static final String ARG_VIEWER = "viewer"; + public static final String ARG_VIEWER = "viewer"; //$NON-NLS-1$ private Composite parent; @@ -113,16 +121,16 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I if (data instanceof String) { viewerContributor = cfig.getContributor().getName(); - String[] parameters = ((String) data).split(";"); + String[] parameters = ((String) data).split(";"); //$NON-NLS-1$ for (String parameter : parameters) { - String[] keyValue = parameter.split("="); + String[] keyValue = parameter.split("="); //$NON-NLS-1$ if (keyValue.length > 2) { - ErrorLogger.defaultLogWarning("Invalid parameter '" + parameter + ". Complete view argument: " + data, null); + ErrorLogger.defaultLogWarning(NLS.bind(Messages.DiagramEditor_InvalidParameter, parameter, data), null); continue; } String key = keyValue[0]; - String value = keyValue.length > 1 ? keyValue[1] : ""; + String value = keyValue.length > 1 ? keyValue[1] : ""; //$NON-NLS-1$ if (ARG_VIEWER.equals(key)) { viewerClassName = value; @@ -134,25 +142,25 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I protected DiagramViewer createViewer() throws PartInitException { if (viewerClassName == null) throw new PartInitException( - "DiagramViewer contributor class was not specified in editor extension's class attribute viewer-argument. contributor is '" - + viewerContributor + "'"); + "DiagramViewer contributor class was not specified in editor extension's class attribute viewer-argument. contributor is '" //$NON-NLS-1$ + + viewerContributor + "'"); //$NON-NLS-1$ try { Bundle b = Platform.getBundle(viewerContributor); if (b == null) - throw new PartInitException("DiagramViewer '" + viewerClassName + "' contributor bundle '" - + viewerContributor + "' was not found in the platform."); + throw new PartInitException("DiagramViewer '" + viewerClassName + "' contributor bundle '" //$NON-NLS-1$ //$NON-NLS-2$ + + viewerContributor + "' was not found in the platform."); //$NON-NLS-1$ Class clazz = b.loadClass(viewerClassName); if (!DiagramViewer.class.isAssignableFrom(clazz)) - throw new PartInitException("DiagramViewer class '" + viewerClassName + "' is not assignable to " - + DiagramViewer.class + "."); + throw new PartInitException("DiagramViewer class '" + viewerClassName + "' is not assignable to " //$NON-NLS-1$ //$NON-NLS-2$ + + DiagramViewer.class + "."); //$NON-NLS-1$ Constructor ctor = clazz.getConstructor(); return (DiagramViewer) ctor.newInstance(); } catch (Exception e) { - throw new PartInitException("Failed to instantiate DiagramViewer implementation '" + viewerClassName - + "' from bundle '" + viewerContributor + "'. See exception for details.", e); + throw new PartInitException("Failed to instantiate DiagramViewer implementation '" + viewerClassName //$NON-NLS-1$ + + "' from bundle '" + viewerContributor + "'. See exception for details.", e); //$NON-NLS-1$ //$NON-NLS-2$ } } @@ -323,13 +331,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 +344,7 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I } } } - + /** * Initializes the diagram viewer if not already initialized. */ @@ -369,4 +372,82 @@ public class DiagramEditor extends EditorPart implements IResourceEditorPart2, I // END: IPartListener2 implementation + /** + * Reinitialize this diagram editor from scratch. + * + *

Must be invoked from the SWT thread.

+ */ + 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 true. + * + *

Must be invoked from the SWT thread.

+ * + * @param predicate + * tester for editor inputs + */ + public static void reinitializeDiagram(Predicate 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 diagram as their input. + * + *

Must be invoked from the SWT thread.

+ * + * @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)); + } + + /** + * Reinitializes all DiagramEditor instances in all workbench windows that have + * the specified diagrams as their input. + * + *

Must be invoked from the SWT thread.

+ * + * @param diagrams + * collection of diagram resources for which to reinitialize all DiagramEditors + * for + */ + public static void reinitializeDiagram(Set diagrams) { + reinitializeDiagram(input -> input instanceof IResourceEditorInput + && diagrams.contains(((IResourceEditorInput) input).getResource())); + } + }