Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change copy/paste of rows in NatTable to also handle imports #954

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void addEntry(final Object entry, final boolean isInput, final int index,
@Override
public void executeCompoundCommand(final CompoundCommand cmd) {
executeCommand(cmd);
natTable.refresh();
}

public Object removeEntry(final int index, final CompoundCommand cmd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElement;
import org.eclipse.fordiac.ide.ui.widget.DataObjectTransfer;
import org.eclipse.fordiac.ide.ui.widget.FordiacCopyDataCommandHandler;
import org.eclipse.fordiac.ide.ui.widget.ImportTransfer;
import org.eclipse.fordiac.ide.ui.widget.NatTableColumn;
import org.eclipse.fordiac.ide.ui.widget.NatTableColumnProvider;
import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler;
import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataToClipboardCommand;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
Expand All @@ -35,7 +36,7 @@
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;

public class CopyDataImportCommandHandler extends CopyDataCommandHandler {
public class CopyDataImportCommandHandler extends FordiacCopyDataCommandHandler {
private final NatTableColumnProvider<? extends NatTableColumn> columnProvider;
private final Map<? extends NatTableColumn, Function<EObject, LibraryElement>> colMapper;

Expand All @@ -54,9 +55,17 @@ protected void internalDoCommand(final CopyDataToClipboardCommand command,
final var imports = getImports(assembledCopiedDataStructure);

final Clipboard clipboard = new Clipboard(Display.getDefault());

final var objects = clipboard.getContents(DataObjectTransfer.getInstance());
final var textContent = clipboard.getContents(TextTransfer.getInstance());
clipboard.setContents(new Object[] { textContent, imports },
new Transfer[] { TextTransfer.getInstance(), ImportTransfer.getInstance() });
if (objects != null) {
clipboard.setContents(new Object[] { objects, imports },
new Transfer[] { DataObjectTransfer.getInstance(), ImportTransfer.getInstance() });
} else if (textContent != null) {
clipboard.setContents(new Object[] { textContent, imports },
new Transfer[] { TextTransfer.getInstance(), ImportTransfer.getInstance() });
}

clipboard.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@
import java.util.function.Function;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.fordiac.ide.model.libraryElement.AdapterDeclaration;
import org.eclipse.fordiac.ide.model.libraryElement.Attribute;
import org.eclipse.fordiac.ide.model.libraryElement.Event;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElement;
import org.eclipse.fordiac.ide.model.libraryElement.TypedConfigureableObject;
import org.eclipse.fordiac.ide.model.libraryElement.VarDeclaration;
import org.eclipse.fordiac.ide.model.typelibrary.AttributeTypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibrary;
import org.eclipse.fordiac.ide.ui.providers.RowHeaderDataProvider;
import org.eclipse.fordiac.ide.ui.widget.CommandExecutor;
import org.eclipse.fordiac.ide.ui.widget.NatTableColumn;
import org.eclipse.fordiac.ide.ui.widget.NatTableColumnProvider;
import org.eclipse.fordiac.ide.ui.widget.NatTableWidgetFactory;
import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.config.AbstractLayerConfiguration;
import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;

public class DefaultImportCopyPasteLayerConfiguration extends AbstractLayerConfiguration<NatTable> {

private static final String UNEXPECTED_VALUE = "Unexpected value: "; //$NON-NLS-1$
private final NatTableColumnProvider<? extends NatTableColumn> columnProvider;
private final CommandExecutor commandExecutor;

Expand All @@ -48,10 +55,17 @@ public DefaultImportCopyPasteLayerConfiguration(
public void configureTypedLayer(final NatTable layer) {
final SelectionLayer sel = NatTableWidgetFactory.getSelectionLayer(layer);
final NatTableColumn firstColumn = columnProvider.getColumns().get(0);

final RowHeaderDataProvider rowHeaderDataProvider = layer.getUnderlyingLayerByPosition(0,
0) instanceof final GridLayer gridLayer
&& gridLayer.getRowHeaderLayer().getUnderlyingLayerByPosition(0, 0) instanceof final DataLayer dataLayer
&& dataLayer.getDataProvider() instanceof final RowHeaderDataProvider rhdp ? rhdp : null;

sel.registerCommandHandler(
new CopyDataImportCommandHandler(sel, columnProvider, getMappersForColumn(firstColumn)));
sel.registerCommandHandler(new PasteDataImportFromClipboardCommandHandler(sel, commandExecutor,
getTypeResolver(firstColumn), columnProvider, getPasteableColumnList(firstColumn)));
sel.registerCommandHandler(
new PasteDataImportFromClipboardCommandHandler(sel, commandExecutor, getTypeResolver(firstColumn),
columnProvider, getPasteableColumnList(firstColumn), rowHeaderDataProvider));
}

private static BiFunction<TypeLibrary, String, TypeEntry> getTypeResolver(final NatTableColumn column) {
Expand All @@ -65,15 +79,17 @@ private static BiFunction<TypeLibrary, String, TypeEntry> getTypeResolver(final
};
case final VarDeclarationTableColumn col ->
(typeLib, name) -> typeLib.getDataTypeLibrary().getDerivedTypeEntry(name);
default -> throw new IllegalArgumentException("Unexpected value: " + column);
case final TypedElementTableColumn col -> TypeLibrary::find;
default -> throw new IllegalArgumentException(UNEXPECTED_VALUE + column);
};
}

private static List<? extends NatTableColumn> getPasteableColumnList(final NatTableColumn column) {
return switch (column) {
case final AttributeTableColumn col -> List.of(AttributeTableColumn.NAME, AttributeTableColumn.TYPE);
case final VarDeclarationTableColumn col -> List.of(VarDeclarationTableColumn.TYPE);
default -> throw new IllegalArgumentException("Unexpected value: " + column);
case final TypedElementTableColumn col -> List.of(TypedElementTableColumn.TYPE);
default -> throw new IllegalArgumentException(UNEXPECTED_VALUE + column);
};
}

Expand All @@ -85,7 +101,13 @@ private static Map<? extends NatTableColumn, Function<EObject, LibraryElement>>
AttributeTableColumn.TYPE, eObject -> ((Attribute) eObject).getType());
case final VarDeclarationTableColumn col ->
Map.of(VarDeclarationTableColumn.TYPE, eObject -> ((VarDeclaration) eObject).getType());
default -> throw new IllegalArgumentException("Unexpected value: " + column);
case final TypedElementTableColumn col -> Map.of(TypedElementTableColumn.TYPE, eObject -> (switch (eObject) {
case final TypedConfigureableObject tco -> tco.getType();
case final AdapterDeclaration adapterDecl -> adapterDecl.getType();
case final Event event -> event.getType();
default -> null;
}));
default -> throw new IllegalArgumentException(UNEXPECTED_VALUE + column);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@
import java.util.Objects;
import java.util.function.BiFunction;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.fordiac.ide.model.commands.create.AddNewImportCommand;
import org.eclipse.fordiac.ide.model.helpers.ImportHelper;
import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
import org.eclipse.fordiac.ide.model.libraryElement.LibraryElement;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibrary;
import org.eclipse.fordiac.ide.ui.editors.EditorUtils;
import org.eclipse.fordiac.ide.ui.providers.RowHeaderDataProvider;
import org.eclipse.fordiac.ide.ui.widget.CommandExecutor;
import org.eclipse.fordiac.ide.ui.widget.I4diacNatTableUtil;
import org.eclipse.fordiac.ide.ui.widget.ImportTransfer;
import org.eclipse.fordiac.ide.ui.widget.NatTableColumn;
import org.eclipse.fordiac.ide.ui.widget.NatTableColumnProvider;
import org.eclipse.fordiac.ide.ui.widget.PasteDataFromClipboardCommandHandler;
import org.eclipse.nebula.widgets.nattable.copy.command.PasteDataCommand;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.swt.dnd.Clipboard;
Expand All @@ -50,8 +51,9 @@ public class PasteDataImportFromClipboardCommandHandler extends PasteDataFromCli
public PasteDataImportFromClipboardCommandHandler(final SelectionLayer selectionLayer,
final CommandExecutor commandExecutor, final BiFunction<TypeLibrary, String, TypeEntry> typeResolver,
final NatTableColumnProvider<? extends NatTableColumn> columnProvider,
final List<? extends NatTableColumn> columns) {
super(selectionLayer);
final List<? extends NatTableColumn> columns, final RowHeaderDataProvider rowHeaderDataProvider) {
super(selectionLayer, commandExecutor instanceof final I4diacNatTableUtil section ? section : null,
rowHeaderDataProvider);
this.commandExecutor = commandExecutor;
this.typeResolver = typeResolver;
this.columnProvider = columnProvider;
Expand All @@ -60,12 +62,11 @@ public PasteDataImportFromClipboardCommandHandler(final SelectionLayer selection

@Override
protected boolean doCommand(final PasteDataCommand command) {
final LibraryElement rootElement = getRootElement();
final LibraryElement rootElement = EditorUtils.getCurrentActiveEditor().getAdapter(LibraryElement.class);
if (rootElement != null) {
final var namespaces = Arrays.stream(getClipboardContent()).map(imp -> getImportNamespace(rootElement, imp))
.filter(Objects::nonNull).toList();
namespaces.forEach(
namespace -> commandExecutor.executeCommand(new AddNewImportCommand(rootElement, namespace)));
Arrays.stream(getClipboardImports()).map(imp -> getImportNamespace(rootElement, imp))
.filter(Objects::nonNull).forEach(namespace -> commandExecutor
.executeCommand(new AddNewImportCommand(rootElement, namespace)));
}

super.doCommand(command);
Expand Down Expand Up @@ -114,19 +115,7 @@ private String getImportNamespace(final LibraryElement rootElement, final String
return null;
}

private LibraryElement getRootElement() {
if (selectionLayer.getUnderlyingLayerByPosition(0, 0) instanceof final DataLayer dataLayer) {
final ListDataProvider<?> provider = (ListDataProvider<?>) dataLayer.getDataProvider();
final var frow = provider.getRowObject(0);
if (frow instanceof final EObject eobj
&& EcoreUtil.getRootContainer(eobj) instanceof final LibraryElement libElement) {
return libElement;
}
}
return null;
}

protected static String[] getClipboardContent() {
protected static String[] getClipboardImports() {
final Clipboard clipboard = new Clipboard(Display.getDefault());
try {
if (clipboard.getContents(ImportTransfer.getInstance()) instanceof final String[] stringArray) {
Expand All @@ -138,6 +127,20 @@ protected static String[] getClipboardContent() {
}
}

@Override
protected void updateNewRow(final int rowIndex) {
if (selectionLayer.getUnderlyingLayerByPosition(0, 0) instanceof final DataLayer dataLayer) {
final IDataProvider dataProvider = dataLayer.getDataProvider();
for (final var column : columns) {
final int colIdx = columnProvider.getColumns().indexOf(column);
final var cellValue = dataProvider.getDataValue(colIdx, rowIndex);
if (conflicts.containsKey(cellValue)) {
dataProvider.setDataValue(colIdx, rowIndex, conflicts.get(cellValue));
}
}
}
}

@Override
public Class<PasteDataCommand> getCommandClass() {
return PasteDataCommand.class;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.fordiac.ide.ui.widget;

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.copy.action.CopyDataAction;
import org.eclipse.nebula.widgets.nattable.ui.action.IKeyAction;
import org.eclipse.swt.events.KeyEvent;

Expand All @@ -31,7 +32,7 @@ public CutDataFromTableAction(final I4diacNatTableUtil section) {

@Override
public void run(final NatTable natTable, final KeyEvent event) {
new CopyDataFromTableAction().run(natTable, event);
new CopyDataAction().run(natTable, event);
new DeleteDataFromTableAction(section).run(natTable, event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2025 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sebastian Hollersbacher - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.fordiac.ide.ui.widget;

import org.eclipse.gef.dnd.SimpleObjectTransfer;

public class DataObjectTransfer extends SimpleObjectTransfer {
private static final DataObjectTransfer INSTANCE = new DataObjectTransfer();
private static final String TYPE_NAME = "org.eclipse.4diac.clipboard.transfer.dataObject"; //$NON-NLS-1$
private static final int TYPE_ID = registerType(TYPE_NAME);

private DataObjectTransfer() {
}

public static DataObjectTransfer getInstance() {
return INSTANCE;
}

@Override
protected int[] getTypeIds() {
return new int[] { TYPE_ID };
}

@Override
protected String[] getTypeNames() {
return new String[] { TYPE_NAME };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* Copyright (c) 2025 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sebastian Hollersbacher - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.fordiac.ide.ui.widget;

import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataCommandHandler;
import org.eclipse.nebula.widgets.nattable.copy.command.CopyDataToClipboardCommand;
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;

public class FordiacCopyDataCommandHandler extends CopyDataCommandHandler {
public FordiacCopyDataCommandHandler(final SelectionLayer selectionLayer) {
super(selectionLayer);
}

@Override
protected void internalDoCommand(final CopyDataToClipboardCommand command,
final ILayerCell[][] assembledCopiedDataStructure) {
super.internalDoCommand(command, assembledCopiedDataStructure);

final Clipboard clipboard = new Clipboard(Display.getDefault());
final int[] rows = selectionLayer.getFullySelectedRowPositions();
if (rows.length > 0) {
final ListDataProvider<?> provider = (ListDataProvider<?>) ((DataLayer) selectionLayer
.getUnderlyingLayerByPosition(0, 0)).getDataProvider();

int i = 0;
final Object[] objects = new Object[rows.length];
for (final int row : rows) {
objects[i] = provider.getRowObject(row);
i++;
}

clipboard.setContents(new Object[] { objects }, new Transfer[] { DataObjectTransfer.getInstance() });
}

clipboard.dispose();
}
}
Loading
Loading