Skip to content

Commit

Permalink
fix strange behaviour on grids, fixes apache#3801
Browse files Browse the repository at this point in the history
remove active widget when moving around rows
  • Loading branch information
hansva committed Nov 14, 2024
1 parent 09b93ea commit 8afd444
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -337,105 +331,6 @@ private void addDataGrid(boolean refresh) {
wTabFolder.layout(true, true);

wFields.nrNonEmpty();
wFields.setTableViewModifyListener(
new TableView.ITableViewModifyListener() {

private Integer getIdxByValue(List<Integer> 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<Integer> nonEmptyIndexesBeforeChanges = wFields.getNonEmptyIndexes();
wFields.nrNonEmpty();
List<Integer> nonEmptyIndexesAfterChanges = wFields.getNonEmptyIndexes();
if (CollectionUtils.isEqualCollection(
nonEmptyIndexesBeforeChanges, nonEmptyIndexesAfterChanges)) {
// count of fields rows didn't change
return;
}
Collection<Integer> 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());
}

Expand Down
25 changes: 25 additions & 0 deletions ui/src/main/java/org/apache/hop/ui/core/widget/TableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 8afd444

Please sign in to comment.