|
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,21 @@ 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 | + contentProvider.setComparator(sortDirectionAscending ? sorter : sorter.reversed()); |
| 995 | + contentProvider.sortList(); |
| 996 | + refreshWidgets(); |
| 997 | + } |
| 998 | + |
954 | 999 | private Composite createNestedComposite(Composite parent, int numRows, boolean equalRows) { |
955 | 1000 | Composite nested = new Composite(parent, SWT.NONE); |
956 | 1001 | { |
@@ -1468,7 +1513,7 @@ private void applyPathMatcher() { |
1468 | 1513 | private class ContentProvider implements IStructuredContentProvider, ILazyContentProvider { |
1469 | 1514 |
|
1470 | 1515 | private List items; |
1471 | | - |
| 1516 | + private Comparator<LineItem> comparator; |
1472 | 1517 | /** |
1473 | 1518 | * Creates new instance of <code>ContentProvider</code>. |
1474 | 1519 | */ |
@@ -1551,6 +1596,25 @@ public void updateElement(int index) { |
1551 | 1596 |
|
1552 | 1597 | } |
1553 | 1598 |
|
| 1599 | + /** |
| 1600 | + * Sorts the current search results based on current comparator |
| 1601 | + */ |
| 1602 | + public void sortList() { |
| 1603 | + if (comparator == null) { |
| 1604 | + return; |
| 1605 | + } |
| 1606 | + items.sort(comparator); |
| 1607 | + } |
| 1608 | + |
| 1609 | + /** |
| 1610 | + * Sets a custom comparator for comparing LineItem objects. |
| 1611 | + * |
| 1612 | + * @param comparator a <code>Comparator<code> object that defines the custom comparison logic. |
| 1613 | + */ |
| 1614 | + public void setComparator(Comparator<LineItem> comparator) { |
| 1615 | + this.comparator = comparator; |
| 1616 | + } |
| 1617 | + |
1554 | 1618 | } |
1555 | 1619 |
|
1556 | 1620 | /** |
|
0 commit comments