From: Hannu Niemistö Date: Fri, 20 Oct 2017 22:15:23 +0000 (+0300) Subject: (refs #7568) Reinitialize diagram editor when element is switched X-Git-Tag: v1.31.0~102 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=01829f2ac0839902c07f29ef723cbd37751bfc51;p=simantics%2Fplatform.git (refs #7568) Reinitialize diagram editor when element is switched Change-Id: Id8fe0e1b0704fae35336525263b76c7c540489f0 --- diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/WorkbenchSelectionProvider.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/WorkbenchSelectionProvider.java index 6609ba24b..a93a0ab41 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/WorkbenchSelectionProvider.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/ui/WorkbenchSelectionProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * Copyright (c) 2007, 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 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - #7586 *******************************************************************************/ package org.simantics.diagram.ui; @@ -75,40 +76,27 @@ public class WorkbenchSelectionProvider extends AbstractCanvasParticipant implem @Override public void addedToContext(ICanvasContext ctx) { super.addedToContext(ctx); - if (site != null && site.getSelectionProvider() != this) { - swt.asyncExec(new Runnable() { - @Override - public void run() { - if (site.getSelectionProvider() != WorkbenchSelectionProvider.this) - site.setSelectionProvider(WorkbenchSelectionProvider.this); - } - }); - } + swt.asyncExec(() -> { + if (site != null && site.getSelectionProvider() != WorkbenchSelectionProvider.this) + site.setSelectionProvider(WorkbenchSelectionProvider.this); + }); } @Override public void removedFromContext(ICanvasContext ctx) { - if (site != null && site.getSelectionProvider() == this) { - swt.asyncExec(new Runnable() { - @Override - public void run() { - if (site.getSelectionProvider() == WorkbenchSelectionProvider.this) - site.setSelectionProvider(null); - } - }); - } + swt.asyncExec(() -> { + if (site != null && site.getSelectionProvider() == WorkbenchSelectionProvider.this) + site.setSelectionProvider(null); + }); super.removedFromContext(ctx); } - + public void fireSelection(Iterable selection) { final ISelection s = constructAdaptableSelection(selection); - swt.asyncExec(new Runnable() { - @Override - public void run() { - currentSelection = s; - fireSelectionChanged(s); - schedulePostSelectionChanged(s); - } + swt.asyncExec(() -> { + currentSelection = s; + fireSelectionChanged(s); + schedulePostSelectionChanged(s); }); } @@ -117,7 +105,7 @@ public class WorkbenchSelectionProvider extends AbstractCanvasParticipant implem Iterable selection = (Iterable) newValue; fireSelection(selection); } - + @HintListener(Class = Selection.class, Field = "SELECTION0") public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { //System.out.println("selection removed: " + oldValue); @@ -129,23 +117,16 @@ public class WorkbenchSelectionProvider extends AbstractCanvasParticipant implem protected void schedulePostSelectionChanged(final ISelection s) { final int count = ++modCount; - ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() { - @Override - public void run() { - int newCount = modCount; - if (count != newCount) - return; - if (isRemoved()) - return; - swt.asyncExec(new Runnable() { - @Override - public void run() { - if (isRemoved()) - return; - firePostSelectionChanged(s); - } - }); - } + ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> { + int newCount = modCount; + if (count != newCount) + return; + if (isRemoved()) + return; + swt.asyncExec(() -> { + if (!isRemoved()) + firePostSelectionChanged(s); + }); }, POST_SELECTION_DELAY, TimeUnit.MILLISECONDS); } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java index 61e81194e..193334e0b 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java @@ -1,6 +1,16 @@ +/******************************************************************************* + * Copyright (c) 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ package org.simantics.modeling.ui.actions; - import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -21,11 +31,17 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.modeling.MigrateModel; import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ui.diagramEditor.DiagramEditor; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.ui.SWTUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * @author Hannu Niemistö + * @since 1.31.0 + */ public class SwitchComponentTypeContribution extends ContributionItem { private static final Logger LOGGER = LoggerFactory.getLogger(SwitchComponentTypeContribution.class); @@ -82,7 +98,18 @@ public class SwitchComponentTypeContribution extends ContributionItem { Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { - MigrateModel.changeComponentType(graph, elementToComponent(graph, resource), newComponentType); + Resource component = elementToComponent(graph, resource); + MigrateModel.changeComponentType(graph, component, newComponentType); + + Layer0 L0 = Layer0.getInstance(graph); + Resource composite = graph.getPossibleObject(component, L0.PartOf); + if (composite == null) + return; + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram); + if (diagram == null) + return; + SWTUtils.asyncExec(menu, () -> DiagramEditor.reinitializeDiagram(diagram)); } }); } 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..7734ce714 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,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. + * + *

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)); + } + }