From 8afd44416bc9a79254516698d06f04416bfea9c3 Mon Sep 17 00:00:00 2001 From: Hans Van Akelyen Date: Thu, 14 Nov 2024 11:06:41 +0100 Subject: [PATCH] fix strange behaviour on grids, fixes #3801 remove active widget when moving around rows --- .../transforms/datagrid/DataGridDialog.java | 109 +----------------- .../apache/hop/ui/core/widget/TableView.java | 25 ++++ 2 files changed, 27 insertions(+), 107 deletions(-) diff --git a/plugins/transforms/datagrid/src/main/java/org/apache/hop/pipeline/transforms/datagrid/DataGridDialog.java b/plugins/transforms/datagrid/src/main/java/org/apache/hop/pipeline/transforms/datagrid/DataGridDialog.java index bf76de258b0..596a6a1c9ca 100644 --- a/plugins/transforms/datagrid/src/main/java/org/apache/hop/pipeline/transforms/datagrid/DataGridDialog.java +++ b/plugins/transforms/datagrid/src/main/java/org/apache/hop/pipeline/transforms/datagrid/DataGridDialog.java @@ -18,10 +18,7 @@ package org.apache.hop.pipeline.transforms.datagrid; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; import java.util.List; -import org.apache.commons.collections4.CollectionUtils; import org.apache.hop.core.Const; import org.apache.hop.core.Props; import org.apache.hop.core.row.value.ValueMetaFactory; @@ -56,7 +53,6 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; @@ -193,10 +189,8 @@ public String open() { new ColumnInfo( BaseMessages.getString(PKG, "DataGridDialog.Value.SetEmptyString"), ColumnInfo.COLUMN_TYPE_CCOMBO, - new String[] { - BaseMessages.getString(PKG, SYSTEM_COMBO_YES), - BaseMessages.getString(PKG, "System.Combo.No") - }), + BaseMessages.getString(PKG, SYSTEM_COMBO_YES), + BaseMessages.getString(PKG, "System.Combo.No")), }; wFields = @@ -337,105 +331,6 @@ private void addDataGrid(boolean refresh) { wTabFolder.layout(true, true); wFields.nrNonEmpty(); - wFields.setTableViewModifyListener( - new TableView.ITableViewModifyListener() { - - private Integer getIdxByValue(List list, Integer value) { - for (int i = 0; i < list.size(); i++) { - if (list.get(i).equals(value)) { - return i; - } - } - return null; - } - - @Override - public void moveRow(int position1, int position2) { - // if one of rows is empty -- don't move data - if (!wFields.getNonEmptyIndexes().contains(position1) - || !wFields.getNonEmptyIndexes().contains(position2)) { - wFields.nrNonEmpty(); - return; - } - - Integer fieldRealPosition1 = getIdxByValue(wFields.getNonEmptyIndexes(), position1); - Integer fieldRealPosition2 = getIdxByValue(wFields.getNonEmptyIndexes(), position2); - if (fieldRealPosition1 == null || fieldRealPosition2 == null) { - return; // can not happen (prevent warnings) - } - // data table have one technical column - int dataPosition1 = fieldRealPosition1 + 1; - int dataPosition2 = fieldRealPosition2 + 1; - - for (TableItem item : wData.table.getItems()) { - String value1 = item.getText(dataPosition1); - String value2 = item.getText(dataPosition2); - item.setText(dataPosition2, value1); - item.setText(dataPosition1, value2); - } - wFields.nrNonEmpty(); - } - - @Override - public void insertRow(int rowIndex) { - wFields.nrNonEmpty(); - } - - @Override - public void cellFocusLost(int rowIndex) { - List nonEmptyIndexesBeforeChanges = wFields.getNonEmptyIndexes(); - wFields.nrNonEmpty(); - List nonEmptyIndexesAfterChanges = wFields.getNonEmptyIndexes(); - if (CollectionUtils.isEqualCollection( - nonEmptyIndexesBeforeChanges, nonEmptyIndexesAfterChanges)) { - // count of fields rows didn't change - return; - } - Collection disjunction = - CollectionUtils.disjunction( - nonEmptyIndexesBeforeChanges, nonEmptyIndexesAfterChanges); - Integer disjunctionIdx = (Integer) disjunction.toArray()[0]; - if (nonEmptyIndexesAfterChanges.contains(disjunctionIdx)) { - // new Field was added - Integer idxByValue = getIdxByValue(nonEmptyIndexesAfterChanges, disjunctionIdx); - if (idxByValue == null) { - return; // can not happen (preventing warnings) - } - - idxByValue++; // data table have one technical column - TableColumn column = new TableColumn(wData.table, SWT.NONE, idxByValue); - column.pack(); - } else { - // Field was deleted - Integer removeColumn = getIdxByValue(nonEmptyIndexesBeforeChanges, disjunctionIdx); - if (removeColumn == null) { - return; // can not happen (preventing warnings) - } - removeColumn++; // data table have one technical column - wData.table.getColumn(removeColumn).dispose(); - wFields.nrNonEmpty(); - } - } - - @Override - public void delete(int[] items) { - Arrays.sort(items); - for (int i = items.length - 1; i >= 0; i--) { - int index = items[i]; - if (!wFields.getNonEmptyIndexes().contains(index)) { - continue; - } - Integer removeColumn = getIdxByValue(wFields.getNonEmptyIndexes(), index); - if (removeColumn == null) { - return; // can not happen (preventing warnings) - } - removeColumn++; // data table have one technical column - wData.table.getColumn(removeColumn).dispose(); - } - wFields.nrNonEmpty(); - } - }); - wFields.setContentListener(modifyEvent -> wFields.nrNonEmpty()); } diff --git a/ui/src/main/java/org/apache/hop/ui/core/widget/TableView.java b/ui/src/main/java/org/apache/hop/ui/core/widget/TableView.java index 2cbcbe32459..c6ba28d9b6b 100644 --- a/ui/src/main/java/org/apache/hop/ui/core/widget/TableView.java +++ b/ui/src/main/java/org/apache/hop/ui/core/widget/TableView.java @@ -1959,6 +1959,14 @@ private void moveRows(int offset) { selectionIndicies = moveRowsUp(selectionIndicies); } + // Check if there is an active control (active textbox/combobox/...) and dispose it when + // deleting a row + Control activeControl = getActiveTableItem().getDisplay().getFocusControl(); + // Check if the table is the parent + if (activeControl != null && activeControl.getParent().equals(table)) { + activeControl.dispose(); + } + activeTableRow = selectedIndex + offset; table.setSelection(activeTableRow); table.setSelection(selectionIndicies); @@ -2246,6 +2254,15 @@ public void delSelected() { // Which items do we delete? int[] items = table.getSelectionIndices(); + table.setSelection(items); + + // Check if there is an active control (active textbox/combobox/...) and dispose it when + // deleting a row + Control activeControl = getActiveTableItem().getDisplay().getFocusControl(); + // Check if the table is the parent + if (activeControl != null && activeControl.getParent().equals(table)) { + activeControl.dispose(); + } if (items.length == 0) { return; @@ -2341,6 +2358,14 @@ public void keepSelected() { ta.setDelete(before, itemsToDelete); addUndo(ta); + // Check if there is an active control (active textbox/combobox/...) and dispose it when + // deleting a row + Control activeControl = getActiveTableItem().getDisplay().getFocusControl(); + // Check if the table is the parent + if (activeControl != null && activeControl.getParent().equals(table)) { + activeControl.dispose(); + } + // Delete non-selected items. table.remove(itemsToDelete);