diff --git a/common/plugins/org.polarsys.capella.common.ui.toolkit.browser/src/org/polarsys/capella/common/ui/toolkit/browser/view/ISemanticBrowserViewPart.java b/common/plugins/org.polarsys.capella.common.ui.toolkit.browser/src/org/polarsys/capella/common/ui/toolkit/browser/view/ISemanticBrowserViewPart.java index 711b25c5f8..de3b2d9360 100644 --- a/common/plugins/org.polarsys.capella.common.ui.toolkit.browser/src/org/polarsys/capella/common/ui/toolkit/browser/view/ISemanticBrowserViewPart.java +++ b/common/plugins/org.polarsys.capella.common.ui.toolkit.browser/src/org/polarsys/capella/common/ui/toolkit/browser/view/ISemanticBrowserViewPart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2022 THALES GLOBAL SERVICES. + * Copyright (c) 2006, 2023 THALES GLOBAL SERVICES. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -26,6 +26,11 @@ */ public interface ISemanticBrowserViewPart extends IViewPart { + /** + * Semantic browser id. + */ + public static final String SEMANTIC_BROWSER_ID = "org.polarsys.capella.core.ui.semantic.browser.view.SemanticBrowserID"; //$NON-NLS-1$ + /** * Sets the input of the Semantic Browser and ALWAYS triggers a refresh of the associated queries. * @@ -97,4 +102,8 @@ public interface ISemanticBrowserViewPart extends IViewPart { public ISemanticBrowserModel getModel(); public void setInputOnViewers(Object input); + + public static String getSemanticBrowserID() { + return SEMANTIC_BROWSER_ID; + }; } diff --git a/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserModelChangeListener.java b/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserModelChangeListener.java new file mode 100644 index 0000000000..a7d905fda9 --- /dev/null +++ b/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserModelChangeListener.java @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (c) 2023 THALES GLOBAL SERVICES. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.polarsys.capella.core.ui.semantic.browser.view; + +import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.transaction.NotificationFilter; +import org.eclipse.emf.transaction.ResourceSetChangeEvent; +import org.eclipse.emf.transaction.ResourceSetListenerImpl; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.sirius.common.ui.tools.api.util.EclipseUIUtil; +import org.polarsys.capella.common.platform.sirius.ted.SemanticEditingDomainFactory.SemanticResourceSet; +import org.polarsys.capella.core.data.capellamodeller.util.CapellamodellerResourceImpl; + +/** + * Model change listener for Browser Semantic View. Allow postCommit refresh of view on model changes. + * + * @author ebausson + */ +public class SemanticBrowserModelChangeListener extends ResourceSetListenerImpl { + + private SemanticBrowserElementFilter filter; + + private TransactionalEditingDomain editingDomain; + + private SemanticBrowserView view; + + protected SemanticBrowserModelChangeListener(SemanticBrowserView view) { + super(); + this.filter = new SemanticBrowserElementFilter(); + this.view = view; + } + + // Documentation copied from the interface + @Override + public NotificationFilter getFilter() { + return filter; + } + + @Override + public boolean isPostcommitOnly() { + return true; + } + + @Override + public void resourceSetChanged(ResourceSetChangeEvent event) { + super.resourceSetChanged(event); + if (this.view != null) { + EclipseUIUtil.displayAsyncExec(view.viewRefreshRunnable); + } + } + + public void changeContext(SemanticBrowserView view) { + if (view.getRootElement() != null) { + refreshSession(view); + } else { + dispose(); + } + } + + /* Clear listener before deletion */ + public void dispose() { + if (editingDomain != null) { + editingDomain.removeResourceSetListener(this); + this.editingDomain = null; + } + this.view = null; + } + + /** + * handle change in session and context when view change + * + * @param view + */ + private void refreshSession(SemanticBrowserView view) { + TransactionalEditingDomain newEditingDomain = resolveSession(view.getRootElement()).getTransactionalEditingDomain(); + if (editingDomain == null) { + editingDomain = newEditingDomain; + newEditingDomain.addResourceSetListener(this); + this.view = null; + } else if (editingDomain != newEditingDomain) { + editingDomain.removeResourceSetListener(this); + editingDomain = newEditingDomain; + newEditingDomain.addResourceSetListener(this); + } + this.view = view; + } + + private Session resolveSession(EObject eObject) { + return SessionManager.INSTANCE.getSession(eObject); + } + + class SemanticBrowserElementFilter extends NotificationFilter.Custom { + + @Override + public boolean matches(Notification notification) { + Object notifier = notification.getNotifier(); + if (notifier instanceof CapellamodellerResourceImpl) { + CapellamodellerResourceImpl capellaModellerResource = (CapellamodellerResourceImpl) notifier; + if (capellaModellerResource.getResourceSet() instanceof SemanticResourceSet) { + SemanticResourceSet resourceSet = (SemanticResourceSet) capellaModellerResource.getResourceSet(); + return resourceSet.getEditingDomain().equals(editingDomain); + } + } + return false; + } + } + +} diff --git a/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserView.java b/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserView.java index 0b8508efe3..70f93769fd 100644 --- a/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserView.java +++ b/core/plugins/org.polarsys.capella.core.ui.semantic.browser/src/org/polarsys/capella/core/ui/semantic/browser/view/SemanticBrowserView.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2022 THALES GLOBAL SERVICES. + * Copyright (c) 2006, 2023 THALES GLOBAL SERVICES. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -188,11 +188,6 @@ public void notify(final Session updated, final int notification) { * Text of the label for Referencing Elements browser. */ private static final String REFERENCING_ELEMENTS_LABEL_TXT = Messages.SemanticBrowserView_Referencing_Elements_Title; - - /** - * Semantic browser id. - */ - public static final String SEMANTIC_BROWSER_ID = "org.polarsys.capella.core.ui.semantic.browser.view.SemanticBrowserID"; //$NON-NLS-1$ /** * Memento persistence tag. */ @@ -251,12 +246,21 @@ public void notify(final Session updated, final int notification) { private TabbedPropertySheetPage propertySheetPage; private IDoubleClickListener viewerDoubleClickListener; private ISelectionChangedListener viewerSelectionListener; + private SemanticBrowserModelChangeListener modelChangeListener; + + protected Runnable viewRefreshRunnable = new Runnable() { + @Override + public void run() { + refresh(true); + } + }; /** * Constructor. */ public SemanticBrowserView() { model = new SemanticBrowserModel(); + this.modelChangeListener = new SemanticBrowserModelChangeListener(this); } @Override @@ -353,6 +357,7 @@ public void selectionChanged(SelectionChangedEvent event) { updateSelectionProvider(provider); refreshPropertyPage(provider); updateStatusLine(provider.getSelection()); + modelChangeListener.resourceSetChanged(null); } }; } @@ -589,6 +594,8 @@ public void dispose() { getSite().getPage().removeSelectionListener(getSelectionListener()); + modelChangeListener.dispose(); + model = null; super.dispose(); } @@ -1175,6 +1182,7 @@ public void refresh(boolean forceRefresh) { setFocusOnViewer(); } SiriusReferenceFinderCache.INSTANCE.disable(); + modelChangeListener.changeContext(this); } }