Skip to content

Commit

Permalink
Fixed copy/paste with import of same type in different packages
Browse files Browse the repository at this point in the history
Fixed issue where copying the same type of different packages only used
the first type for all cells
  • Loading branch information
sebHollersbacher authored and oberlehner committed Jan 22, 2025
1 parent 8b3ce2e commit ef1094f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
package org.eclipse.fordiac.ide.gef.nat;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.fordiac.ide.model.helpers.PackageNameHelper;
Expand Down Expand Up @@ -69,9 +70,12 @@ protected void internalDoCommand(final CopyDataToClipboardCommand command,
clipboard.dispose();
}

private String[] getImports(final ILayerCell[][] assembledCopiedDataStructure) {
private Map<Object, List<Object>> getImports(final ILayerCell[][] assembledCopiedDataStructure) {
if (selectionLayer.getUnderlyingLayerByPosition(0, 0) instanceof final DataLayer dataLayer) {
final ListDataProvider<?> provider = (ListDataProvider<?>) dataLayer.getDataProvider();

final var rowIndices = selectionLayer.getSelectedRowPositions().stream()
.flatMap(range -> range.getMembers().stream()).sorted().toList();
return Arrays.stream(assembledCopiedDataStructure).flatMap(Arrays::stream).filter(Objects::nonNull)
.filter(cell -> colMapper.containsKey(columnProvider.getColumns().get(cell.getColumnIndex())))
.map(cell -> {
Expand All @@ -81,12 +85,14 @@ private String[] getImports(final ILayerCell[][] assembledCopiedDataStructure) {
if (PackageNameHelper.getPackageName(element).isEmpty()) {
return null;
}
return PackageNameHelper.getFullTypeName(element);
return Map.entry(PackageNameHelper.getFullTypeName(element),
rowIndices.indexOf(cell.getRowIndex()));
}
return null;
}).filter(Objects::nonNull).filter(Predicate.not(String::isEmpty)).distinct()
.toArray(String[]::new);
}).filter(Objects::nonNull).filter(entry -> !entry.getKey().isEmpty())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
return new String[0];
return Map.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.fordiac.ide.gef.nat;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -47,6 +46,7 @@ public class PasteDataImportFromClipboardCommandHandler extends PasteDataFromCli
private final Map<String, String> conflicts = new HashMap<>();
private final NatTableColumnProvider<? extends NatTableColumn> columnProvider;
private final List<? extends NatTableColumn> columns;
private Map<String, List<Integer>> importContent;

public PasteDataImportFromClipboardCommandHandler(final SelectionLayer selectionLayer,
final CommandExecutor commandExecutor, final BiFunction<TypeLibrary, String, TypeEntry> typeResolver,
Expand All @@ -64,8 +64,9 @@ public PasteDataImportFromClipboardCommandHandler(final SelectionLayer selection
protected boolean doCommand(final PasteDataCommand command) {
final LibraryElement rootElement = EditorUtils.getCurrentActiveEditor().getAdapter(LibraryElement.class);
if (rootElement != null) {
Arrays.stream(getClipboardImports()).map(imp -> getImportNamespace(rootElement, imp))
.filter(Objects::nonNull).forEach(namespace -> commandExecutor
importContent = getClipboardImports();
importContent.keySet().stream().map(imp -> getImportNamespace(rootElement, imp)).filter(Objects::nonNull)
.forEach(namespace -> commandExecutor
.executeCommand(new AddNewImportCommand(rootElement, namespace)));
}

Expand All @@ -86,8 +87,11 @@ protected String[][] parseContent(final Object contents) {
for (final var column : columns) {
final int idx = columnProvider.getColumns().indexOf(column);
final int colIndex = idx - location.getColumnPosition();
for (final String[] row : content) {
if (colIndex >= 0 && colIndex < row.length && conflicts.containsKey(row[colIndex])) {
for (int i = 0; i < content.length; i++) {
final String[] row = content[i];
if (colIndex >= 0 && colIndex < row.length && conflicts.containsKey(row[colIndex])
&& this.importContent.containsKey(conflicts.get(row[colIndex]))
&& this.importContent.get(conflicts.get(row[colIndex])).contains(i)) {
row[colIndex] = conflicts.get(row[colIndex]);
}
}
Expand Down Expand Up @@ -115,27 +119,33 @@ private String getImportNamespace(final LibraryElement rootElement, final String
return null;
}

protected static String[] getClipboardImports() {
@SuppressWarnings("unchecked")
protected static Map<String, List<Integer>> getClipboardImports() {
final Clipboard clipboard = new Clipboard(Display.getDefault());
try {
if (clipboard.getContents(ImportTransfer.getInstance()) instanceof final String[] stringArray) {
return stringArray;
final var content = clipboard.getContents(ImportTransfer.getInstance());
if (content instanceof final Map<?, ?> map) {
return (Map<String, List<Integer>>) map;
}
return new String[0];
return Map.of();
} finally {
clipboard.dispose();
}
}

@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));
protected void updateNewRow(final int[] rowIndices) {
for (int i = 0; i < rowIndices.length; i++) {
final int rowIndex = rowIndices[i];
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) && this.importContent.containsKey(conflicts.get(cellValue))
&& this.importContent.get(conflicts.get(cellValue)).contains(i)) {
dataProvider.setDataValue(colIdx, rowIndex, conflicts.get(cellValue));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ private void pasteClipboardElementsContents(final Object contents) {
section.executeCompoundCommand(cmpCommand);
for (final int ind : selectedIndices) {
selectionLayer.selectRow(0, ind, false, true);
updateNewRow(ind);
}
updateNewRow(selectedIndices);
}
}

protected void updateNewRow(final int rowIndex) {
protected void updateNewRow(final int[] rowIndices) {
// allow subclasses to provide additional functionality
}

Expand Down

0 comments on commit ef1094f

Please sign in to comment.