blob: 25bb497f5c7adee6ba1479e487d6a9f442f18760 [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,
* Olivier L. Larouche - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.common.emf.ui.parts;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import org.eclipse.apogy.common.emf.ui.ApogyCommonEMFUIRCPConstants;
import org.eclipse.apogy.common.ui.composites.NoContentComposite;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.PersistState;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
abstract public class AbstractPart {
private Composite parentComposite;
@Inject
protected MPart mPart;
protected String perspectiveID;
@Inject
protected ESelectionService selectionService;
@Inject
protected EModelService modelService;
@Inject
protected MApplication application;
@PostConstruct
public void createPartControl(Composite parent, @Optional MPerspective perspective) {
userPostConstruct(this.mPart);
if (perspective != null) {
this.perspectiveID = perspective.getElementId();
}
this.parentComposite = parent;
this.parentComposite.setLayout(new FillLayout());
setNoContentComposite();
this.parentComposite.layout();
setEObject(getInitializeObject());
// DEBUG
reloadToolBarAndMenus();
}
@PostConstruct
public void postConstruct(Composite parent) {
// reloadToolBarAndMenus();
}
@PreDestroy
public void preDestroy() {
userPreDestroy(this.mPart);
}
@PersistState
public void persist() {
userPersistState(this.mPart);
}
/**
* Method called after the Part gas been constructed, but before its content is
* created.
*
* @param mPart The reference to the Part model.
*/
public void userPostConstruct(MPart mPart) {
}
/**
* Method called before the Part is destroyed.
*
* @param mPart The reference to the Part model.
*/
public void userPreDestroy(MPart mPart) {
}
/**
* Method called when "visual state persit" is performed.
*
* @param mPart The reference to the Part model.
*/
public void userPersistState(MPart mPart) {
}
/**
* Forces the tool-bar and menus to be reloaded from the model in the snippets.
*
* FIXME This is to work around a problem where tool-bars and menus seems to
* become corrupted.
*/
public void reloadToolBarAndMenus() {
MPerspective perspectiveCopy = (MPerspective) this.modelService.cloneSnippet(this.application,
this.perspectiveID,
(MWindow) this.modelService.find(ApogyCommonEMFUIRCPConstants.MAIN_WINDOW__ID, this.application));
if (perspectiveCopy != null) {
MPart partCopy = (MPart) this.modelService.find(this.mPart.getElementId(), perspectiveCopy);
if (partCopy != null) {
// Updates the menus and tools with the ones from the part copy.
if (this.mPart.getMenus() != null) {
this.mPart.getMenus().clear();
if (partCopy.getMenus() != null) {
this.mPart.getMenus().addAll(partCopy.getMenus());
}
}
if (this.mPart.getToolbar() != null && this.mPart.getToolbar().getChildren() != null) {
this.mPart.getToolbar().getChildren().clear();
if (partCopy.getToolbar() != null && partCopy.getToolbar().getChildren() != null) {
this.mPart.getToolbar().getChildren().addAll(partCopy.getToolbar().getChildren());
}
}
}
}
}
/**
* Specifies the {@link EObject} to initially set in the part.
*
* @return EObject
*/
abstract protected EObject getInitializeObject();
/**
* Specifies the {@link Composite} to create in the part.
*/
abstract protected void createContentComposite(Composite parent, int style);
/**
* Gets the content {@link Composite} in the part.
*
* @return {@link Composite}
*/
public Composite getActualComposite() {
if (this.parentComposite != null) {
if (!this.parentComposite.isDisposed() && this.parentComposite.getChildren().length > 0) {
return (Composite) this.parentComposite.getChildren()[0];
}
}
return null;
}
/**
* This method is called when the {@link EObject} needs to be changed or
* initialized in the content parentComposite.
*
* @param eObject Reference to the EObject.
*/
abstract protected void setCompositeContent(EObject eObject);
/**
* This method sets the {@link EObject} in the part. Null EObjects sets the
* content parentComposite to a nullSelection parentComposite according to the
* implemented part.
*
* @param eObject
*/
protected void setEObject(EObject eObject) {
if (this.parentComposite != null) {
if (eObject != null) {
if (getActualComposite() instanceof NoContentComposite) {
for (Control control : this.parentComposite.getChildren()) {
control.dispose();
}
createContentComposite(this.parentComposite, SWT.None);
this.parentComposite.layout();
}
setCompositeContent(eObject);
} else {
setNoContentComposite();
}
}
}
/**
* Sets the part's parentComposite to a {@link NoActiveSessionComposite}.
*/
protected void setNoContentComposite() {
if (getActualComposite() != null) {
for (Control control : this.parentComposite.getChildren()) {
control.dispose();
}
}
if (!this.parentComposite.isDisposed()) {
createNoContentComposite(this.parentComposite, SWT.None);
this.parentComposite.layout();
}
this.selectionService.setSelection(null);
}
/**
* Specifies the {@link NoContentComposite} to set in the part when there is no
* content to display.
*
* @return {@link NoContentComposite}
*/
abstract protected void createNoContentComposite(Composite parent, int style);
@PreDestroy
private void destroy() {
dispose();
}
/**
* Called when the part is disposed.
*/
protected void dispose() {
}
}