blob: a2bac84049fcd0921bb023ba1ba4f6be88890bdf [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* 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:
* Pierre Allard,
* Regent L'Archeveque,
* Sebastien Gemme - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.rcp;
import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade;
import org.eclipse.apogy.workspace.ApogyWorkspaceFacade;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
import org.eclipse.e4.ui.workbench.lifecycle.ProcessAdditions;
import org.eclipse.e4.ui.workbench.lifecycle.ProcessRemovals;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.ResourceSetListener;
import org.eclipse.emf.transaction.ResourceSetListenerImpl;
/**
* This is a stub implementation containing e4 LifeCycle annotated
* methods.<br />
* There is a corresponding entry in <em>plugin.xml</em> (under the
* <em>org.eclipse.core.runtime.products' extension point</em>) that references
* this class.
**/
public class E4LifeCycle {
private ResourceSetListener resourceSetListener;
private IEclipseContext workbenchContext;
private AdapterImpl workspaceAdapter;
@PostContextCreate
void postContextCreate(IEclipseContext workbenchContext) {
this.workbenchContext = workbenchContext;
ApogyCommonTransactionFacade.INSTANCE.getDefaultEditingDomain()
.addResourceSetListener(getResourceSetListener());
ApogyWorkspaceFacade.INSTANCE.eAdapters().add(getWorkspaceAdapter());
}
@PreSave
void preSave(IEclipseContext workbenchContext) {
}
@ProcessAdditions
void processAdditions(IEclipseContext workbenchContext) {
}
@ProcessRemovals
void processRemovals(IEclipseContext workbenchContext) {
}
private ResourceSetListener getResourceSetListener() {
if (this.resourceSetListener == null) {
this.resourceSetListener = new ResourceSetListenerImpl() {
@Override
public void resourceSetChanged(ResourceSetChangeEvent event) {
processEvent();
}
};
}
return this.resourceSetListener;
}
private void processEvent() {
IEventBroker eventBroker = (IEventBroker) this.workbenchContext.get(IEventBroker.class.getName());
if (eventBroker != null) {
eventBroker.send(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
}
}
private Adapter getWorkspaceAdapter() {
if (this.workspaceAdapter == null) {
this.workspaceAdapter = new AdapterImpl() {
@Override
public void notifyChanged(Notification msg) {
processEvent();
}
};
}
return this.workspaceAdapter;
}
}