blob: 691d50d4018213b39ed1e5395a17895d2dc81463 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2015 ALL4TEC.
* 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:
* Jonathan Dumont (ALL4TEC) - initial API and implementation
******************************************************************************/
package org.polarsys.esf.core.common.ui.view;
import java.util.Iterator;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.PropertyColumnLabelProvider;
import org.polarsys.esf.core.common.ui.actions.NoModifiesActionBarContributor;
import org.polarsys.esf.core.common.ui.actions.ViewActionBarContributor;
import org.polarsys.esf.core.common.ui.editingsupport.ValidatedPropertyEditingSupport;
import org.polarsys.esf.core.common.ui.provider.FirstColumnLabelProvider;
/**
* Base for pages which are looking at a ESF Model,
* and which will be displayed in a page book view.
*
* This allows to put several pages in a same view, in order
* to link the view content to the active editor for example.
*
* @author $Author: jdumont $
* @version $Revision: 83 $
*/
public abstract class AbstractModelPage
extends AbstractTreeTablePage {
/** Default column width. */
private static final int COLUMN_WIDTH = 150;
/** The resource set on which the page will be plugged. */
private ResourceSet mResourceSet = null;
/** The resource, contained in the resource set, which will be used as input for the page viewer. */
private Resource mResource = null;
/** The source part, containing the resource set and the editing domain, to link to this page. */
private IWorkbenchPart mSourcePart = null;
/**
* Default constructor.
*
* @param pSourcePart The source part to link to this page
* @param pRequiredColumnName Name of the required columns for the viewer
* @param pColumnsName Names of other columns
* @param pReadOnlyMode <code>true</code> if the page must be in read only mode
*/
public AbstractModelPage(
final IWorkbenchPart pSourcePart,
final String pRequiredColumnName,
final String[] pColumnsName,
final boolean pReadOnlyMode) {
super(pRequiredColumnName, pColumnsName, pReadOnlyMode);
mSourcePart = pSourcePart;
}
/**
* Return the part linked to this page instance and used
* to get the source model.
*
* @return The sourcePart
*/
public IWorkbenchPart getSourcePart() {
return mSourcePart;
}
/**
* {@inheritDoc}
*/
@Override
protected IBaseLabelProvider createLabelProvider() {
return new FirstColumnLabelProvider(getAdapterFactory());
}
/**
* {@inheritDoc}
*
* For a page used to display an ESF model content, from the source editor linked
* to this instance, the initial input is the resource found in this editor.
*
* Thus, this method will initialise the {@link #mResource} and {@link #mResourceSet} properties
* to ensure that the page is plugged to the editor data.
*/
@Override
protected Object getInitialInput() {
// Check if the source part is able to provide an editing domain provider
// If this is not the case, no input can be found
if (mSourcePart instanceof IEditingDomainProvider) {
// Get the resource set on which works the source part
mResourceSet = ((IEditingDomainProvider) mSourcePart).getEditingDomain().getResourceSet();
// Then loop on the resource set to find the ESF Resource to use as input
Iterator<Resource> vIterator = mResourceSet.getResources().iterator();
while (vIterator.hasNext() && mResource == null) {
Resource vResource = (Resource) vIterator.next();
// Check if the current resource is a ESF model
// TODO : Check the right resource type
if (vResource instanceof Resource) {
// Remember of this resource to use it as input
mResource = vResource;
}
}
}
return mResource;
}
/**
* {@inheritDoc}
*/
@Override
protected ResourceSet getResourceSet() {
return mResourceSet;
}
/**
* {@inheritDoc}
*
* This method is overridden to ensure that the source part editing domain will be used if possible
* instead of creating a new one.
*/
@Override
protected EditingDomain createEditingDomain() {
EditingDomain vEditingDomain = null;
if (mSourcePart instanceof IEditingDomainProvider) {
// Get the editing domain of the source part if possible
vEditingDomain = (AdapterFactoryEditingDomain) ((IEditingDomainProvider) mSourcePart).getEditingDomain();
// Get the command stack of the editing domain to add a listener on it
addCommandStackListener(vEditingDomain.getCommandStack());
} else {
// Call the parent method otherwise to create a default editing domain
vEditingDomain = super.createEditingDomain();
}
return vEditingDomain;
}
/**
* {@inheritDoc}
*
* Overridden to add an action bar contributor without any action able to modify model.
*/
@Override
protected ViewActionBarContributor createActionBarContributor() {
return new NoModifiesActionBarContributor();
}
/**
* Initialise the width of the viewer columns, only if their actual size is less than column width constant.
* Moreover, the column alignment is set to center.
*/
protected void setColumnsWidthAndAlignment() {
TreeColumn[] vColummnsArray = getViewer().getTree().getColumns();
for (TreeColumn vColumn : vColummnsArray) {
vColumn.setAlignment(SWT.CENTER);
if (vColumn.getWidth() < COLUMN_WIDTH) {
vColumn.setWidth(COLUMN_WIDTH);
}
}
}
/**
* Add a column in view, and initialise it, to ensure that :
* <ul>
* <li>it's editable if needed.</li>
* <li>it's linked to to right properties.</li>
* <li>its width is set.</li>
* </ul>
*
* @param pColumnName The column name, used to identify it
*/
protected void addColumnViewer(final String pColumnName) {
// Get the editable state for the column given
boolean vIsEditable = isColumnEditable(pColumnName);
// Create a column label provider in function of the target column
ColumnLabelProvider vColumnLabelProvider = createColumnLabelProvider(pColumnName);
// Get the property id associated to the given column
String vPropertyId = getAssociatedColumnPropertyID(pColumnName);
// Try to create an editing support if the column is editable,
// and associated to a property in the model
if (vIsEditable && StringUtils.isNotEmpty(vPropertyId)) {
// Create an editing support for column which can be modified
EditingSupport vEditingSupport =
new ValidatedPropertyEditingSupport(
getViewer(),
getPropertySourceProvider(),
vPropertyId,
getActionBars().getStatusLineManager());
// Then create the column with this editing support
createViewerColumn(pColumnName, vColumnLabelProvider, vEditingSupport);
} else {
// Create a non editable column
createViewerColumn(pColumnName, vColumnLabelProvider);
}
// Once that a column has been added, reset the width
// and alignment of each of them
setColumnsWidthAndAlignment();
}
/**
* Create a column label provider in function of the target column.
* The label provider may built by default is based on the property id associated
* to the given column.
*
* @param pColumnName The name of the column concerned
* @return The created column label provider
*/
protected ColumnLabelProvider createColumnLabelProvider(final String pColumnName) {
ColumnLabelProvider vColumnLabelProvider = null;
// Get the id of the property linked to this column
String vPropertyId = getAssociatedColumnPropertyID(pColumnName);
if (StringUtils.isNotEmpty(vPropertyId)) {
// Create a new label provider for this property
vColumnLabelProvider = new PropertyColumnLabelProvider(
getPropertySourceProvider(),
vPropertyId);
} else {
// Create a default label provider
vColumnLabelProvider = new ColumnLabelProvider();
}
return vColumnLabelProvider;
}
/**
* Return a boolean specifying if the column, identified by the name given in parameter
* is editable or not.
*
* Return false by default.
*
* @param pColumnName The column name
* @return <code>true</code> if the column is editable
*/
protected boolean isColumnEditable(final String pColumnName) {
return false;
}
/**
* {@inheritDoc}
*
* In the model pages implementations, the drag and drop actions are disabled, as
* these pages are made to view to model, not to modify its structure.
*/
@Override
protected void addDragSupport() {
// Nothing to do
}
/**
* {@inheritDoc}
*
* In the model pages implementations, the drag and drop actions are disabled, as
* these pages are made to view to model, not to modify its structure.
*/
@Override
protected void addDropSupport() {
// Nothing to do
}
/**
* Get the associated property Id for a column whose name is given in parameter.
*
* Return an empty String by default.
*
* @param pColumnName Column name
* @return The associated property Id found, or an empty String if no property is associated
*/
protected String getAssociatedColumnPropertyID(final String pColumnName) {
return StringUtils.EMPTY;
}
/**
* {@inheritDoc}
*
* This default implementation create a new column for each name returned
* by the method {@link #getColumnsName()}.
*/
@Override
protected void createInitialViewerColumns() {
// Loop on all the columns name to create a new one for each
for (String vColumnName : getColumnsName()) {
// Only the analysis closed column can be edited
addColumnViewer(vColumnName);
}
}
}