Skip to content

Commit

Permalink
Adding a Listener to refresh SemanticView after model change.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebausson-obeo authored and pdulth committed Sep 4, 2023
1 parent 0aa8934 commit 2cbbdf6
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <b>ALWAYS</b> triggers a refresh of the associated queries.
*
Expand Down Expand Up @@ -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;
};
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -353,6 +357,7 @@ public void selectionChanged(SelectionChangedEvent event) {
updateSelectionProvider(provider);
refreshPropertyPage(provider);
updateStatusLine(provider.getSelection());
modelChangeListener.resourceSetChanged(null);
}
};
}
Expand Down Expand Up @@ -589,6 +594,8 @@ public void dispose() {

getSite().getPage().removeSelectionListener(getSelectionListener());

modelChangeListener.dispose();

model = null;
super.dispose();
}
Expand Down Expand Up @@ -1175,6 +1182,7 @@ public void refresh(boolean forceRefresh) {
setFocusOnViewer();
}
SiriusReferenceFinderCache.INSTANCE.disable();
modelChangeListener.changeContext(this);
}
}

Expand Down

0 comments on commit 2cbbdf6

Please sign in to comment.