diff options
author | Mickael ADAM | 2016-11-28 09:56:45 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2016-12-07 13:52:15 +0000 |
commit | 424012d6f62146b03b1d6dab4f67b9a027530328 (patch) | |
tree | 23e0224a12ad754c1009393c7656b9414854856e /plugins/infra | |
parent | bf00ee033e80427cff7acbbf925d7cf5d78d7f8e (diff) | |
download | org.eclipse.papyrus-424012d6f62146b03b1d6dab4f67b9a027530328.tar.gz org.eclipse.papyrus-424012d6f62146b03b1d6dab4f67b9a027530328.tar.xz org.eclipse.papyrus-424012d6f62146b03b1d6dab4f67b9a027530328.zip |
Bug 502560 - [Table] Possibility to drag and drop an object from table
to a diagram
https://bugs.eclipse.org/bugs/show_bug.cgi?id=502560
- Add drag support to natTable
- create NatTableDragSourceListener similar to ViewerDragAdapter from
emf but with nattable instead of viewer
- TOFIX: drag support and ColumnReorderDragMode are not compliant.
Change-Id: I1d424c85f62b20e213e4a048b517dfd1f9871660
Signed-off-by: Mickael ADAM <mickael.adam@ALL4TEC.net>
Diffstat (limited to 'plugins/infra')
4 files changed, 222 insertions, 22 deletions
diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/listener/NatTableDragSourceListener.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/listener/NatTableDragSourceListener.java new file mode 100644 index 00000000000..cd9008ff221 --- /dev/null +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/listener/NatTableDragSourceListener.java @@ -0,0 +1,159 @@ +/***************************************************************************** + * Copyright (c) 2016 CEA LIST, ALL4TEC and others. + * + * 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: + * Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.infra.nattable.listener; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.edit.ui.dnd.LocalTransfer; +import org.eclipse.jface.util.LocalSelectionTransfer; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.eclipse.nebula.widgets.nattable.grid.GridRegion; +import org.eclipse.nebula.widgets.nattable.ui.util.CellEdgeDetectUtil; +import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager; +import org.eclipse.papyrus.infra.nattable.model.nattable.Table; +import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.NattablestylePackage; +import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.StringListValueStyle; +import org.eclipse.papyrus.infra.nattable.utils.NamedStyleConstants; +import org.eclipse.swt.dnd.DragSourceEvent; +import org.eclipse.swt.dnd.DragSourceListener; +import org.eclipse.swt.graphics.Point; + +/** + * A {@link DragSourceListener} for {@link NatTable}. + * Region where drag is enable is define throw String List Value style in configuration file. + * List of available region are available in {@link GridRegion}. + * + * @since 3.0 + */ +public class NatTableDragSourceListener implements DragSourceListener { + + /** The selection. */ + protected ISelection selection; + + /** The nattable. */ + private NatTable nattable; + + /** The nat table model manager. */ + private INattableModelManager tableManager; + + private Table table; + + /** + * Constructor. + * + * @param tableManager + * The table manager. + * @param nattable + * The NatTable. + * @param table + * The table. + * @param gridRegion + * the {@link GridRegion} source from the drag start in the nattable. + */ + public NatTableDragSourceListener(final INattableModelManager tableManager, final NatTable nattable, Table table) { + this.tableManager = tableManager; + this.nattable = nattable; + this.table = table; + } + + + /** + * {@inheritDoc} + * + * @see org.eclipse.swt.dnd.DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent) + */ + @Override + public void dragStart(final DragSourceEvent event) { + event.doit = false; + + for (int i = 0; i < getGridRegions().size() && !event.doit; i++) { + if (!isResizing(event) && this.nattable.getRegionLabelsByXY(event.x, event.y).hasLabel(getGridRegions().get(i))) { + event.doit = true; + } + } + + if (event.doit) { + selection = tableManager.getSelectionInTable(); + LocalSelectionTransfer.getTransfer().setSelection(selection); + LocalSelectionTransfer.getTransfer().setSelectionSetTime(System.currentTimeMillis()); + } + } + + + /** + * @return The grid region where drag is enabled. + */ + protected List<String> getGridRegions() { + List<String> regions = new ArrayList<String>(); + + // // Get in the table + StringListValueStyle values = (StringListValueStyle) table.getNamedStyle(NattablestylePackage.eINSTANCE.getStringListValueStyle(), NamedStyleConstants.DRAG_REGIONS); + if (null == values) { + // If not, get in the configuration + values = (StringListValueStyle) table.getTableConfiguration().getNamedStyle(NattablestylePackage.eINSTANCE.getStringListValueStyle(), NamedStyleConstants.DRAG_REGIONS); + } + + if (null != values) { + EList<String> stringListValue = values.getStringListValue(); + for (String region : stringListValue) { + if (table.isInvertAxis()) { + if (GridRegion.ROW_HEADER.equals(region)) { + region = GridRegion.COLUMN_HEADER; + } else if (GridRegion.COLUMN_HEADER.equals(region)) { + region = GridRegion.ROW_HEADER; + } + } + regions.add(region); + } + } + + return regions; + } + + + /** + * @return true if the drag event occurs during a resize. + */ + protected boolean isResizing(final DragSourceEvent event) { + return CellEdgeDetectUtil.getRowPositionToResize(this.nattable, new Point(event.x, event.y)) >= 0 + || CellEdgeDetectUtil.getColumnPositionToResize(this.nattable, new Point(event.x, event.y)) >= 0; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData(org.eclipse.swt.dnd.DragSourceEvent) + */ + @Override + public void dragSetData(final DragSourceEvent event) { + if (LocalTransfer.getInstance().isSupportedType(event.dataType)) { + event.data = selection; + } else if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType)) { + event.data = LocalSelectionTransfer.getTransfer().getSelection(); + } + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished(org.eclipse.swt.dnd.DragSourceEvent) + */ + @Override + public void dragFinished(final DragSourceEvent event) { + selection = null; + LocalTransfer.getInstance().javaToNative(null, null); + LocalSelectionTransfer.getTransfer().setSelection(null); + } +}
\ No newline at end of file diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/AbstractNattableWidgetManager.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/AbstractNattableWidgetManager.java index c5abacaf519..41229012fb6 100755 --- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/AbstractNattableWidgetManager.java +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/AbstractNattableWidgetManager.java @@ -13,6 +13,7 @@ * Christian W. Damus (CEA) - bug 430880 * Dirk Fauth <dirk.fauth@googlemail.com> - Bug 488234 * Nicolas FAUVERGUE(ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 504077 + * Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - Bug 502560: add drag to diagram support * *****************************************************************************/ package org.eclipse.papyrus.infra.nattable.manager.table; @@ -111,6 +112,7 @@ import org.eclipse.papyrus.infra.nattable.layerstack.BodyLayerStack; import org.eclipse.papyrus.infra.nattable.layerstack.ColumnHeaderLayerStack; import org.eclipse.papyrus.infra.nattable.layerstack.RowHeaderHierarchicalLayerStack; import org.eclipse.papyrus.infra.nattable.layerstack.RowHeaderLayerStack; +import org.eclipse.papyrus.infra.nattable.listener.NatTableDragSourceListener; import org.eclipse.papyrus.infra.nattable.listener.NatTableDropListener; import org.eclipse.papyrus.infra.nattable.model.nattable.NattablePackage; import org.eclipse.papyrus.infra.nattable.model.nattable.Table; @@ -156,6 +158,7 @@ import org.eclipse.papyrus.infra.widgets.util.NavigationTarget; import org.eclipse.swt.custom.CTabFolder; import org.eclipse.swt.custom.CTabItem; import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.DragSourceListener; import org.eclipse.swt.dnd.DropTarget; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.events.SelectionAdapter; @@ -826,13 +829,30 @@ public abstract class AbstractNattableWidgetManager implements INattableModelMan target.setTransfer(types); final NatTableDropListener dropListener = createDropListener(); target.addDropListener(dropListener); + + // Add drag support + natTable.addDragSupport(operations, types, createDragListener()); } + /** + * Instantiate a new {@link NatTableDropListener}. + * + * @return The Drop Listener. + */ protected NatTableDropListener createDropListener() { return new NatTableDropListener(this, this.selectionExtractor); } /** + * Instantiate a new {@link DragSourceListener}. + * + * @return The Drag Listener. + */ + protected DragSourceListener createDragListener() { + return new NatTableDragSourceListener(this, natTable, table); + } + + /** * Add a listener on the column reorder layer in order to update the model * * @param columnReorderLayer @@ -2153,6 +2173,7 @@ public abstract class AbstractNattableWidgetManager implements INattableModelMan * a {@link TableStructuredSelection} representing the current selection of the table or <code>null</code> when there is no selection * @since 2.0 */ + @Override public final TableStructuredSelection getSelectionInTable() { if (null != this.selectionProvider) { ISelection selection = this.selectionProvider.getSelection(); diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/INattableModelManager.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/INattableModelManager.java index f6a45868a84..752a79e79a0 100644 --- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/INattableModelManager.java +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/manager/table/INattableModelManager.java @@ -10,6 +10,7 @@ * Contributors: * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - Bug 502560: add drag to diagram support * *****************************************************************************/ package org.eclipse.papyrus.infra.nattable.manager.table; @@ -26,6 +27,7 @@ import org.eclipse.papyrus.infra.nattable.layerstack.BodyLayerStack; import org.eclipse.papyrus.infra.nattable.manager.axis.IAxisManager; import org.eclipse.papyrus.infra.nattable.model.nattable.Table; import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisprovider.AbstractAxisProvider; +import org.eclipse.papyrus.infra.nattable.provider.TableStructuredSelection; import org.eclipse.papyrus.infra.nattable.utils.LocationValue; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; @@ -91,12 +93,14 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo public Command getAddRowElementCommand(Collection<Object> objectsToAdd); public Command getAddColumnElementCommand(Collection<Object> objectsToAdd); - + /** * Get the command to add row elements at the index in parameter. * - * @param objectsToAdd The row elements to add. - * @param index The index where add the elements. + * @param objectsToAdd + * The row elements to add. + * @param index + * The index where add the elements. * @return The command. */ public Command getAddRowElementCommand(final Collection<Object> objectsToAdd, final int index); @@ -104,8 +108,10 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo /** * Get the command to add column elements at the index in parameter. * - * @param objectsToAdd The column elements to add. - * @param index The index where add the elements. + * @param objectsToAdd + * The column elements to add. + * @param index + * The index where add the elements. * @return The command. */ public Command getAddColumnElementCommand(final Collection<Object> objectsToAdd, final int index); @@ -127,7 +133,7 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo public void selectAll(); public void exportToXLS(); - + /** * This allows to export the table contents into a file. * @@ -169,7 +175,7 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo /** * * @return - * the "real"{@link AbstractAxisProvider}, that's to say that this method use the property {@link Table#isInvertAxis()} to return the real + * the "real"{@link AbstractAxisProvider}, that's to say that this method use the property {@link Table#isInvertAxis()} to return the real * vertical axis */ public AbstractAxisProvider getVerticalAxisProvider(); @@ -177,7 +183,7 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo /** * * @return - * the "real"{@link AbstractAxisProvider}, that's to say that this method use the property {@link Table#isInvertAxis()} to return the real + * the "real"{@link AbstractAxisProvider}, that's to say that this method use the property {@link Table#isInvertAxis()} to return the real * horizontal axis */ public AbstractAxisProvider getHorizontalAxisProvider(); @@ -212,14 +218,14 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo /** * * @return - * the row axis manager, managing the axis inversion + * the row axis manager, managing the axis inversion */ public IAxisManager getRowAxisManager(); /** * * @return - * the local preference store for the table instance or <code>null</code> + * the local preference store for the table instance or <code>null</code> */ public PreferenceStore getTablePreferenceStore(); @@ -229,4 +235,11 @@ public interface INattableModelManager extends ITableAxisElementProvider, IDispo * the table preference store */ public void setWorkspacePreferenceStore(final PreferenceStore store); + + /** + * @return the selection in table. + * + * @since 3.0 + */ + public TableStructuredSelection getSelectionInTable(); } diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/NamedStyleConstants.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/NamedStyleConstants.java index 591f82c06f9..1eaf2b3e34f 100644 --- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/NamedStyleConstants.java +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/NamedStyleConstants.java @@ -30,26 +30,26 @@ public class NamedStyleConstants { * The following constants are used during the initialization and retrieval of the Axis and Header dimensions in the tables */ - public static final String ROW_INDEX_WIDTH = "rowIndexWidth"; //$NON-NLS-1$1 + public static final String ROW_INDEX_WIDTH = "rowIndexWidth"; //$NON-NLS-1$ public static final String ROW_LABEL_WIDTH = "rowLabelWidth"; //$NON-NLS-1$ - + /** * The prefix of the name style to calculate the width of each row header. * This prefix is followed by the position of the column to edit width. */ public static final String ROW_LABEL_POSITION_PREFIX_WIDTH = "rowPosition"; //$NON-NLS-1$ - + /** * The suffix of the name style to calculate the width of each row header */ public static final String ROW_LABEL_POSITION_SUFFIX_WIDTH = "LabelWidth"; //$NON-NLS-1$ - + /** * The named style id for the slider composite width. */ public static final String ROW_HEADER_WIDTH = "rowHeaderWidth"; //$NON-NLS-1$ - + public static final String COLUMN_INDEX_HEIGHT = "columnIndexHeight"; //$NON-NLS-1$ @@ -64,7 +64,7 @@ public class NamedStyleConstants { * The following constants are used during the initialization and retrieval of the merge options in the tables */ - // public static final String TABLEMERGE = "tableMerge"; //$NON-NLS-1$ + // public static final String TABLEMERGE = "tableMerge"; //$NON-NLS-1$ public static final String MERGE_ROWS = "mergeRows"; //$NON-NLS-1$ @@ -79,7 +79,7 @@ public class NamedStyleConstants { public static final String MERGE_IN_SELECTED_COLUMNS = "mergeInSelectedColumns"; //$NON-NLS-1$ // the merge of all the elements in the table is not yet supported - // public static final String MERGE_TABLE = "mergeTable"; //$NON-NLS-1$ + // public static final String MERGE_TABLE = "mergeTable"; //$NON-NLS-1$ public static final String HIDDEN_CATEGORY_FOR_DEPTH = "hiddenCategoriesByDepth"; //$NON-NLS-1$ @@ -98,7 +98,7 @@ public class NamedStyleConstants { /** * key used to save a filter configuration id state when the system use a filter choosen by the user - * It is only used as name for a {@link StringValueStyle}, we use a specific key in order to not destroyed it when we unapply filter on a column where filter has been defined by the user + * It is only used as name for a {@link StringValueStyle}, we use a specific key in order to not destroyed it when we unapply filter on a column where filter has been definied by the user * * */ @@ -107,8 +107,8 @@ public class NamedStyleConstants { /** * This is for boolean value style {@link BooleanValueStyle}. This is used to expand all the rows in a tree table during the opening of the table. */ - public static final String EXPAND_ALL = "expandAll"; //$NON-NLS-1$ - + public static final String EXPAND_ALL = "expandAll"; //$NON-NLS-1$ + /** * This allows to manage the fill columns size to take all the container space. * @@ -116,7 +116,7 @@ public class NamedStyleConstants { */ @Deprecated public static final String FILL_COLUMNS_SIZE = "fillColumnsSize"; //$NON-NLS-1$ - + /** * This allows to determinate if the columns width must be managed as percentage. * This named style is not compatible with 'fillColumnsSize'. @@ -124,7 +124,7 @@ public class NamedStyleConstants { * @since 3.0 */ public static final String COLUMNS_WIDTH_AS_PERCENTAGE = "columnsWidthAsPercentage"; //$NON-NLS-1$ - + /** * This allows to determinate if the columns width must be saved or not. * This named style is not compatible with 'fillColumnsSize'. @@ -132,4 +132,11 @@ public class NamedStyleConstants { * @since 3.0 */ public static final String SAVE_COLUMNS_WIDTH = "saveColumnsWidth"; //$NON-NLS-1$ + + /** + * Enable the drag behavior regions from the natTable to an other widget/editor (diagram, table, ...) which can accept it. + * + * @since 3.0 + */ + public static final String DRAG_REGIONS = "dragRegions"; //$NON-NLS-1$ } |