blob: aa3aca4e1b3f8985e6cfcf2f1907828e18c12dd5 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2016 ALL4TEC & CEA LIST.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ALL4TEC & CEA LIST - initial API and implementation
******************************************************************************/
package org.polarsys.esf.core.common.ui.view;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.ViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.handlers.CollapseAllHandler;
import org.eclipse.ui.handlers.ExpandAllHandler;
import org.polarsys.esf.core.common.ui.CommonUIActivator;
import org.polarsys.esf.core.common.ui.CommonUIActivator.Implementation;
/**
* Abstract base of all the Pages to use in a PageBookView,
* and which includes a tree.
*
* This implementation doesn't manage the columns in the tree,
* but it inherits of all the standard mechanisms like the filters, the editing domain mechanisms, etc.
*
* @author $Author: jdumont $
* @version $Revision: 83 $
*/
public abstract class AbstractTreePage
extends AbstractColumnViewerPage {
/**
* Default constructor, which allows to specify if the viewer must be read only or not.
*
* @param pReadOnlyMode <code>true</code> if this view is in read only mode, <code>false</code> otherwise
*/
public AbstractTreePage(final boolean pReadOnlyMode) {
super(pReadOnlyMode);
}
/**
* Advanced constructor which takes the name of the required column, an array with the optional columns name,
* and a boolean which specifies if the viewer must be in read only mode or not.
*
* The required column will mandatory be visible and can't be hidden.
*
* @param pRequiredColumnName The name of the required column name
* @param pColumnsName The array of optional columns name. This list must not contain the required column name
* @param pReadOnlyMode <code>true</code> if this view is in read only mode, <code>false</code> otherwise
*/
public AbstractTreePage(
final String pRequiredColumnName,
final String[] pColumnsName,
final boolean pReadOnlyMode) {
super(pRequiredColumnName, pColumnsName, pReadOnlyMode);
}
/**
* {@inheritDoc}
*/
@Override
protected TreeViewer createViewer(final Composite pParent) {
return new TreeViewer(pParent, SWT.MULTI);
}
/**
* {@inheritDoc}
*/
@Override
public TreeViewer getViewer() {
return (TreeViewer) super.getViewer();
}
/**
* {@inheritDoc}
*
* Overridden to expand or collapse the current element on which
* the double click is performed.
*/
@Override
protected void handleDoubleClick(final DoubleClickEvent pEvent) {
// Get the element on which the double click is performed
final IStructuredSelection vSelection = (IStructuredSelection) pEvent.getSelection();
final Object vSelectedeElement = vSelection.getFirstElement();
// If the element is expandable, change is expand state
if (getViewer().isExpandable(vSelectedeElement)) {
getViewer().setExpandedState(vSelectedeElement, !getViewer().getExpandedState(vSelectedeElement));
}
}
/**
* Create the expand all action.
*
* @return The expand all action created
*/
protected IAction createExpandAllAction() {
// Get the action image
final ImageDescriptor vImageDescriptor =
CommonUIActivator.getPlugin().getImageRegistry().getDescriptor(
Implementation.ICON_EXPAND_ALL_KEY);
// Get the action label
final String vActionLabel =
CommonUIActivator.getMessages().getString("AbstractTreePage.action.expandall"); //$NON-NLS-1$
// Create the action
final IAction vExpandAllAction = new Action(vActionLabel, vImageDescriptor) {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// Prepare the expand action asynchronously, on the UI thread
AbstractTreePage.this.getSite().getShell().getDisplay().asyncExec(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// Create the action handler on the viewer
final ExpandAllHandler vExpandAllHandler =
new ExpandAllHandler(AbstractTreePage.this.getViewer());
// And finally execute it
vExpandAllHandler.execute(null);
}
});
}
};
return vExpandAllAction;
}
/**
* Create the collapse all action.
*
* @return The collapse all action created
*/
protected IAction createCollapseAllAction() {
// Get the action image
final ImageDescriptor vImageDescriptor =
CommonUIActivator.getPlugin().getImageRegistry().getDescriptor(
Implementation.ICON_COLLAPSE_ALL_KEY);
// Get the action label
final String vActionLabel =
CommonUIActivator.getMessages().getString("AbstractTreePage.action.collapseall"); //$NON-NLS-1$
// Create the action
final IAction vCollapseAllAction = new Action(vActionLabel, vImageDescriptor) {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// Prepare the collapse action asynchronously, on the UI thread
AbstractTreePage.this.getSite().getShell().getDisplay().asyncExec(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// Create the action handler on the viewer
final CollapseAllHandler vCollapseAllHandler =
new CollapseAllHandler(AbstractTreePage.this.getViewer());
// And finally execute it
vCollapseAllHandler.execute(null);
}
});
}
};
return vCollapseAllAction;
}
/**
* {@inheritDoc}
*/
@Override
public void setLinesVisible(final boolean pShow) {
getViewer().getTree().setLinesVisible(pShow);
}
/**
* {@inheritDoc}
*/
@Override
public void setHeaderVisible(final boolean pShow) {
getViewer().getTree().setHeaderVisible(pShow);
}
/**
* {@inheritDoc}
*/
@Override
protected boolean isViewerHeaderInitiallyVisible() {
return getViewer().getTree().getHeaderVisible();
}
/**
* {@inheritDoc}
*/
@Override
protected boolean isViewerLinesInitiallyVisible() {
return getViewer().getTree().getLinesVisible();
}
/**
* {@inheritDoc}
*/
@Override
protected boolean hideColumn(final String pColumnName) {
boolean vColumnHidden = false;
// Try to find the column by its name
final TreeColumn vColumn = getColumn(pColumnName);
// If the column is found, dispose it
if (vColumn != null) {
vColumn.dispose();
// Remember that the column was hidden
vColumnHidden = true;
}
return vColumnHidden;
}
/**
* {@inheritDoc}
*/
@Override
protected boolean isColumnVisible(final String pColumnName) {
return getColumn(pColumnName) != null;
}
/**
* Find and return an existing column in the tree viewer from its name.
*
* @param pColumnName The name of the column to find
* @return The column found if any, and <code>null</code> otherwise
*/
protected TreeColumn getColumn(final String pColumnName) {
TreeColumn vColumnFound = null;
// Check if the searched name is valid
if (StringUtils.isNotEmpty(pColumnName)) {
// Get an iterator on the list of existing columns in the tree viewer
final TreeColumn[] vColumnsArray = getViewer().getTree().getColumns();
// Loop on the columns array until the column is found
for (int i = 0; i < vColumnsArray.length && vColumnFound == null; i++) {
TreeColumn vTreeColumn = vColumnsArray[i];
if (vTreeColumn.getText().equals(pColumnName)) {
vColumnFound = vTreeColumn;
}
}
}
return vColumnFound;
}
/**
* {@inheritDoc}
*
* This is customised to add the expand all and collapse all actions.
*/
@Override
protected void addToolBarActions() {
addExpandAllAction();
addCollapseAllAction();
super.addToolBarActions();
}
/**
* Add to the toolbar the action to expand all the tree content.
*/
protected void addExpandAllAction() {
addToolBarAction(createExpandAllAction());
}
/**
* Add to the toolbar the action to collapse all the tree content.
*/
protected void addCollapseAllAction() {
addToolBarAction(createCollapseAllAction());
}
/**
* {@inheritDoc}
*/
@Override
protected TreeViewerColumn createRequiredViewerColumn() {
// Nothing to do as this implementation doesn't manage multiple columns
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected ViewerColumn createViewerColumn(final String pColumnName) {
// Nothing to do as this implementation doesn't manage multiple columns
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected ViewerColumn createViewerColumn(final String pColumnName, final int pIndex) {
// Nothing to do as this implementation doesn't manage multiple columns
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected ViewerColumn createViewerColumn(
final String pColumnName,
final ColumnLabelProvider pColumnLabelProvider) {
// Nothing to do as this implementation doesn't manage multiple columns
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected ViewerColumn createViewerColumn(
final String pColumnName,
final ColumnLabelProvider pColumnLabelProvider,
final EditingSupport pEditingSupport) {
// Nothing to do as this implementation doesn't manage multiple columns
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected ViewerColumn createViewerColumn(final String pColumnName, final String pPropertyID) {
// Nothing to do as this implementation doesn't manage multiple columns
return null;
}
}