|
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;
|
@@ -840,19 +841,47 @@ public void getName(AccessibleEvent e) {
|
840 | 841 | // new ScrollListener(list.getTable().getVerticalBar());
|
841 | 842 | // new SelectionChangedListener(list);
|
842 | 843 |
|
| 844 | + final int[] sortColumnIndex = {-1}; |
| 845 | + final boolean[] sortDirectionAscending = {true}; |
| 846 | + Table table = list.getTable(); |
843 | 847 | TableViewerColumn col = new TableViewerColumn(list, SWT.RIGHT);
|
| 848 | + TableColumn column = col.getColumn(); |
| 849 | + column.setText(Messages.QuickSearchDialog_line); |
| 850 | + column.setWidth(40); |
| 851 | + column.addSelectionListener(new SelectionAdapter() { |
| 852 | + @Override |
| 853 | + public void widgetSelected(SelectionEvent e) { |
| 854 | + Comparator<LineItem> lineNumberComparator = Comparator.comparingInt(LineItem::getLineNumber); |
| 855 | + handleColumnSort( table, 0, lineNumberComparator, sortColumnIndex, sortDirectionAscending); |
| 856 | + } |
| 857 | + }); |
844 | 858 | col.setLabelProvider(LINE_NUMBER_LABEL_PROVIDER);
|
845 |
| - col.getColumn().setText(Messages.QuickSearchDialog_line); |
846 |
| - col.getColumn().setWidth(40); |
| 859 | + |
847 | 860 | col = new TableViewerColumn(list, SWT.LEFT);
|
848 |
| - col.getColumn().setText(Messages.QuickSearchDialog_text); |
| 861 | + column = col.getColumn(); |
| 862 | + column.setText(Messages.QuickSearchDialog_text); |
| 863 | + column.setWidth(400); |
| 864 | + column.addSelectionListener(new SelectionAdapter() { |
| 865 | + @Override |
| 866 | + public void widgetSelected(SelectionEvent e) { |
| 867 | + Comparator<LineItem> textComparator = Comparator.comparing( LineItem::getText); |
| 868 | + handleColumnSort( table, 1, textComparator, sortColumnIndex, sortDirectionAscending); |
| 869 | + } |
| 870 | + }); |
849 | 871 | col.setLabelProvider(LINE_TEXT_LABEL_PROVIDER);
|
850 |
| - col.getColumn().setWidth(400); |
| 872 | + |
851 | 873 | col = new TableViewerColumn(list, SWT.LEFT);
|
852 |
| - col.getColumn().setText(Messages.QuickSearchDialog_path); |
| 874 | + column = col.getColumn(); |
| 875 | + column.setText(Messages.QuickSearchDialog_path); |
| 876 | + column.setWidth(150); |
| 877 | + column.addSelectionListener(new SelectionAdapter() { |
| 878 | + @Override |
| 879 | + public void widgetSelected(SelectionEvent e) { |
| 880 | + Comparator<LineItem> lineItemComparator = Comparator.comparing( item -> item.getFile().getFullPath().toString()); |
| 881 | + handleColumnSort( table, 2,lineItemComparator, sortColumnIndex, sortDirectionAscending); |
| 882 | + } |
| 883 | + }); |
853 | 884 | col.setLabelProvider(LINE_FILE_LABEL_PROVIDER);
|
854 |
| - col.getColumn().setWidth(150); |
855 |
| - |
856 | 885 | new TableResizeHelper(list).enableResizing();
|
857 | 886 |
|
858 | 887 | //list.setLabelProvider(getItemsListLabelProvider());
|
@@ -951,6 +980,26 @@ public void keyPressed(KeyEvent e) {
|
951 | 980 | return dialogArea;
|
952 | 981 | }
|
953 | 982 |
|
| 983 | + private void handleColumnSort(Table table, int columnIndex, Comparator sorter,int[] sortColumnIndex, boolean[] sortDirectionAscending) { |
| 984 | + if (sortColumnIndex[0] == columnIndex) { |
| 985 | + sortDirectionAscending[0] = !sortDirectionAscending[0]; |
| 986 | + } else { |
| 987 | + sortColumnIndex[0] = columnIndex; |
| 988 | + sortDirectionAscending[0] = true; |
| 989 | + } |
| 990 | + table.setSortColumn(table.getColumn(columnIndex)); |
| 991 | + table.setSortDirection(sortDirectionAscending[0] ? SWT.UP : SWT.DOWN); |
| 992 | + |
| 993 | + if(sortDirectionAscending[0]) { |
| 994 | + contentProvider.setComparator(sorter); |
| 995 | + contentProvider.sortList(); |
| 996 | + } else { |
| 997 | + contentProvider.setComparator(sorter.reversed()); |
| 998 | + contentProvider.sortList(); |
| 999 | + } |
| 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 | {
|
@@ -1227,6 +1276,7 @@ protected IDialogSettings getDialogSettings() {
|
1227 | 1276 | public void refreshWidgets() {
|
1228 | 1277 | if (list != null && !list.getTable().isDisposed()) {
|
1229 | 1278 | int itemCount = contentProvider.getNumberOfElements();
|
| 1279 | + contentProvider.sortList(); |
1230 | 1280 | list.setItemCount(itemCount);
|
1231 | 1281 | if (itemCount < MAX_RESULTS) {
|
1232 | 1282 | listLabel.setText(NLS.bind(Messages.QuickSearchDialog_listLabel, itemCount));
|
@@ -1468,7 +1518,7 @@ private void applyPathMatcher() {
|
1468 | 1518 | private class ContentProvider implements IStructuredContentProvider, ILazyContentProvider {
|
1469 | 1519 |
|
1470 | 1520 | private List items;
|
1471 |
| - |
| 1521 | + private Comparator<LineItem> comparator; |
1472 | 1522 | /**
|
1473 | 1523 | * Creates new instance of <code>ContentProvider</code>.
|
1474 | 1524 | */
|
@@ -1511,6 +1561,9 @@ public void refresh() {
|
1511 | 1561 | */
|
1512 | 1562 | @Override
|
1513 | 1563 | public Object[] getElements(Object inputElement) {
|
| 1564 | + if(comparator!=null) { |
| 1565 | + items.sort(comparator); |
| 1566 | + } |
1514 | 1567 | return items.toArray();
|
1515 | 1568 | }
|
1516 | 1569 |
|
@@ -1551,6 +1604,25 @@ public void updateElement(int index) {
|
1551 | 1604 |
|
1552 | 1605 | }
|
1553 | 1606 |
|
| 1607 | + /** |
| 1608 | + * Sorts the current search results based on current comparator |
| 1609 | + */ |
| 1610 | + public void sortList() { |
| 1611 | + if(comparator==null) { |
| 1612 | + return; |
| 1613 | + } |
| 1614 | + items.sort(comparator); |
| 1615 | + } |
| 1616 | + |
| 1617 | + /** |
| 1618 | + * Sets a custom comparator for comparing LineItem objects. |
| 1619 | + * |
| 1620 | + * @param comparator a <code>Comparator<code> object that defines the custom comparison logic. |
| 1621 | + */ |
| 1622 | + public void setComparator(Comparator<LineItem> comparator) { |
| 1623 | + this.comparator = comparator; |
| 1624 | + } |
| 1625 | + |
1554 | 1626 | }
|
1555 | 1627 |
|
1556 | 1628 | /**
|
|
0 commit comments