|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2000, 2020 IBM Corporation and others. |
| 2 | + * Copyright (c) 2000, 2025 IBM Corporation and others. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials
|
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0
|
|
32 | 32 | import java.util.ArrayList;
|
33 | 33 | import java.util.Arrays;
|
34 | 34 | import java.util.Collections;
|
| 35 | +import java.util.Comparator; |
35 | 36 | import java.util.Deque;
|
36 | 37 | import java.util.LinkedList;
|
37 | 38 | import java.util.List;
|
@@ -403,6 +404,9 @@ public void update(ViewerCell cell) {
|
403 | 404 | private Combo searchIn;
|
404 | 405 | private Label listLabel;
|
405 | 406 |
|
| 407 | + private int sortColumnIndex = -1; |
| 408 | + private boolean sortDirectionAscending = true; |
| 409 | + |
406 | 410 | /**
|
407 | 411 | * Creates a new instance of the class.
|
408 | 412 | *
|
@@ -840,19 +844,45 @@ public void getName(AccessibleEvent e) {
|
840 | 844 | // new ScrollListener(list.getTable().getVerticalBar());
|
841 | 845 | // new SelectionChangedListener(list);
|
842 | 846 |
|
| 847 | + Table table = list.getTable(); |
843 | 848 | TableViewerColumn col = new TableViewerColumn(list, SWT.RIGHT);
|
| 849 | + TableColumn column = col.getColumn(); |
| 850 | + column.setText(Messages.QuickSearchDialog_line); |
| 851 | + column.setWidth(40); |
| 852 | + column.addSelectionListener(new SelectionAdapter() { |
| 853 | + @Override |
| 854 | + public void widgetSelected(SelectionEvent e) { |
| 855 | + Comparator<LineItem> lineNumberComparator = Comparator.comparingInt(LineItem::getLineNumber); |
| 856 | + handleColumnSort( table, 0, lineNumberComparator); |
| 857 | + } |
| 858 | + }); |
844 | 859 | col.setLabelProvider(LINE_NUMBER_LABEL_PROVIDER);
|
845 |
| - col.getColumn().setText(Messages.QuickSearchDialog_line); |
846 |
| - col.getColumn().setWidth(40); |
| 860 | + |
847 | 861 | col = new TableViewerColumn(list, SWT.LEFT);
|
848 |
| - col.getColumn().setText(Messages.QuickSearchDialog_text); |
| 862 | + column = col.getColumn(); |
| 863 | + column.setText(Messages.QuickSearchDialog_text); |
| 864 | + column.setWidth(400); |
| 865 | + column.addSelectionListener(new SelectionAdapter() { |
| 866 | + @Override |
| 867 | + public void widgetSelected(SelectionEvent e) { |
| 868 | + Comparator<LineItem> textComparator = Comparator.comparing( LineItem::getText); |
| 869 | + handleColumnSort( table, 1, textComparator); |
| 870 | + } |
| 871 | + }); |
849 | 872 | col.setLabelProvider(LINE_TEXT_LABEL_PROVIDER);
|
850 |
| - col.getColumn().setWidth(400); |
| 873 | + |
851 | 874 | col = new TableViewerColumn(list, SWT.LEFT);
|
852 |
| - col.getColumn().setText(Messages.QuickSearchDialog_path); |
| 875 | + column = col.getColumn(); |
| 876 | + column.setText(Messages.QuickSearchDialog_path); |
| 877 | + column.setWidth(150); |
| 878 | + column.addSelectionListener(new SelectionAdapter() { |
| 879 | + @Override |
| 880 | + public void widgetSelected(SelectionEvent e) { |
| 881 | + Comparator<LineItem> lineItemComparator = Comparator.comparing( item -> item.getFile().getFullPath().toString()); |
| 882 | + handleColumnSort( table, 2,lineItemComparator); |
| 883 | + } |
| 884 | + }); |
853 | 885 | col.setLabelProvider(LINE_FILE_LABEL_PROVIDER);
|
854 |
| - col.getColumn().setWidth(150); |
855 |
| - |
856 | 886 | new TableResizeHelper(list).enableResizing();
|
857 | 887 |
|
858 | 888 | //list.setLabelProvider(getItemsListLabelProvider());
|
@@ -951,6 +981,25 @@ public void keyPressed(KeyEvent e) {
|
951 | 981 | return dialogArea;
|
952 | 982 | }
|
953 | 983 |
|
| 984 | + private void handleColumnSort(Table table, int columnIndex, Comparator sorter) { |
| 985 | + if (sortColumnIndex == columnIndex) { |
| 986 | + sortDirectionAscending = !sortDirectionAscending; |
| 987 | + } else { |
| 988 | + sortColumnIndex = columnIndex; |
| 989 | + sortDirectionAscending = true; |
| 990 | + } |
| 991 | + table.setSortColumn(table.getColumn(columnIndex)); |
| 992 | + table.setSortDirection(sortDirectionAscending ? SWT.UP : SWT.DOWN); |
| 993 | + |
| 994 | + if (sortDirectionAscending) { |
| 995 | + contentProvider.setComparator(sorter); |
| 996 | + } else { |
| 997 | + contentProvider.setComparator(sorter.reversed()); |
| 998 | + } |
| 999 | + contentProvider.sortList(); |
| 1000 | + refreshWidgets(); |
| 1001 | + } |
| 1002 | + |
954 | 1003 | private Composite createNestedComposite(Composite parent, int numRows, boolean equalRows) {
|
955 | 1004 | Composite nested = new Composite(parent, SWT.NONE);
|
956 | 1005 | {
|
@@ -1468,7 +1517,7 @@ private void applyPathMatcher() {
|
1468 | 1517 | private class ContentProvider implements IStructuredContentProvider, ILazyContentProvider {
|
1469 | 1518 |
|
1470 | 1519 | private List items;
|
1471 |
| - |
| 1520 | + private Comparator<LineItem> comparator; |
1472 | 1521 | /**
|
1473 | 1522 | * Creates new instance of <code>ContentProvider</code>.
|
1474 | 1523 | */
|
@@ -1551,6 +1600,26 @@ public void updateElement(int index) {
|
1551 | 1600 |
|
1552 | 1601 | }
|
1553 | 1602 |
|
| 1603 | + /** |
| 1604 | + * Sorts the current search results based on current comparator |
| 1605 | + */ |
| 1606 | + public void sortList() { |
| 1607 | + if (comparator == null) { |
| 1608 | + return; |
| 1609 | + } |
| 1610 | + items.sort(comparator); |
| 1611 | + } |
| 1612 | + |
| 1613 | + /** |
| 1614 | + * Sets a custom comparator for comparing LineItem objects. |
| 1615 | + * |
| 1616 | + * @param comparator a <code>Comparator<code> object that defines the custom comparison logic. |
| 1617 | + */ |
| 1618 | + public void setComparator(Comparator<LineItem> comparator) { |
| 1619 | + this.comparator = comparator; |
| 1620 | + items.sort(comparator); |
| 1621 | + } |
| 1622 | + |
1554 | 1623 | }
|
1555 | 1624 |
|
1556 | 1625 | /**
|
|
0 commit comments