Skip to content

Commit

Permalink
feat: enable sorting of secondary column in preferences tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Dec 12, 2023
1 parent ee0477b commit 388292a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 168 deletions.
6 changes: 3 additions & 3 deletions org.eclipse.tm4e.languageconfiguration/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Require-Bundle: org.eclipse.core.expressions,
org.eclipse.ui.genericeditor,
org.eclipse.ui.ide,
org.eclipse.e4.ui.css.swt.theme,
org.eclipse.tm4e.registry;bundle-version="0.6.5",
org.eclipse.tm4e.core;bundle-version="0.6.1",
org.eclipse.tm4e.ui;bundle-version="0.7.1",
org.eclipse.tm4e.registry;bundle-version="0.6.6",
org.eclipse.tm4e.core;bundle-version="0.6.2",
org.eclipse.tm4e.ui;bundle-version="0.7.2",
com.google.gson;bundle-version="[2.10.1,3.0.0)"
Bundle-Activator: org.eclipse.tm4e.languageconfiguration.LanguageConfigurationPlugin
Export-Package: org.eclipse.tm4e.languageconfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
import org.eclipse.tm4e.languageconfiguration.internal.registry.ILanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.registry.LanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.registry.WorkingCopyLanguageConfigurationRegistryManager;
import org.eclipse.tm4e.languageconfiguration.internal.widgets.ColumnSelectionAdapter;
import org.eclipse.tm4e.languageconfiguration.internal.widgets.ColumnViewerComparator;
import org.eclipse.tm4e.languageconfiguration.internal.widgets.LanguageConfigurationPreferencesWidget;
import org.eclipse.tm4e.languageconfiguration.internal.wizards.LanguageConfigurationImportWizard;
import org.eclipse.tm4e.ui.internal.utils.UI;
import org.eclipse.tm4e.ui.internal.widgets.ColumnSelectionAdapter;
import org.eclipse.tm4e.ui.internal.widgets.ColumnViewerComparator;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
Expand Down Expand Up @@ -152,7 +152,7 @@ private void createDefinitionsListContent(final Composite parent) {
column.setText(label);
final int minWidth = computeMinimumColumnWidth(gc, label);
columnLayout.setColumnData(column, new ColumnWeightData(2, minWidth, true));
column.addSelectionListener(new ColumnSelectionAdapter(column, definitionViewer, i, viewerComparator));
column.addSelectionListener(new ColumnSelectionAdapter(definitionViewer, viewerComparator, 0));

if (i == 0) {
// Specify default sorting
Expand Down

This file was deleted.

This file was deleted.

5 changes: 3 additions & 2 deletions org.eclipse.tm4e.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ Require-Bundle: org.eclipse.core.expressions,
org.eclipse.ui.trace;resolution:=optional,
org.eclipse.ui.workbench.texteditor,
org.eclipse.e4.ui.css.swt.theme,
org.eclipse.tm4e.core;bundle-version="0.6.1",
org.eclipse.tm4e.registry;bundle-version="0.6.4",
org.eclipse.tm4e.core;bundle-version="0.6.2",
org.eclipse.tm4e.registry;bundle-version="0.6.6",
com.google.gson;bundle-version="[2.10.1,3.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: org.eclipse.tm4e.ui,
org.eclipse.tm4e.ui.internal.model;x-friends:="org.eclipse.tm4e.languageconfiguration,org.eclipse.tm4e.ui.tests",
org.eclipse.tm4e.ui.internal.themes;x-friends:="org.eclipse.tm4e.ui.tests",
org.eclipse.tm4e.ui.internal.utils;x-friends:="org.eclipse.tm4e.ui.tests,org.eclipse.tm4e.languageconfiguration",
org.eclipse.tm4e.ui.internal.widgets;x-friends:="org.eclipse.tm4e.languageconfiguration",
org.eclipse.tm4e.ui.model,
org.eclipse.tm4e.ui.text,
org.eclipse.tm4e.ui.themes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,19 @@ private void createGrammarListContent(final Composite parent) {
column1.setText(TMUIMessages.GrammarPreferencePage_column_scopeName);
int minWidth = computeMinimumColumnWidth(gc, TMUIMessages.GrammarPreferencePage_column_scopeName);
columnLayout.setColumnData(column1, new ColumnWeightData(2, minWidth, true));
column1.addSelectionListener(new ColumnSelectionAdapter(column1, grammarViewer, 0, viewerComparator));
column1.addSelectionListener(new ColumnSelectionAdapter(grammarViewer, viewerComparator));

final var column2 = new TableColumn(table, SWT.NONE);
column2.setText(TMUIMessages.GrammarPreferencePage_column_path);
minWidth = computeMinimumColumnWidth(gc, TMUIMessages.GrammarPreferencePage_column_path);
columnLayout.setColumnData(column2, new ColumnWeightData(2, minWidth, true));
column2.addSelectionListener(new ColumnSelectionAdapter(column2, grammarViewer, 1, viewerComparator));
column2.addSelectionListener(new ColumnSelectionAdapter(grammarViewer, viewerComparator, 0));

final var column3 = new TableColumn(table, SWT.NONE);
column3.setText(TMUIMessages.GrammarPreferencePage_column_pluginId);
minWidth = computeMinimumColumnWidth(gc, TMUIMessages.GrammarPreferencePage_column_pluginId);
columnLayout.setColumnData(column3, new ColumnWeightData(2, minWidth, true));
column3.addSelectionListener(new ColumnSelectionAdapter(column3, grammarViewer, 2, viewerComparator));
column3.addSelectionListener(new ColumnSelectionAdapter(grammarViewer, viewerComparator, 0));

gc.dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,17 @@ public Object[] getElements(final @Nullable Object inputElement) {
final var column1 = new TableColumn(table, SWT.NONE);
column1.setText(TMUIMessages.TaskTagsPreferencePage_column_tag);
column1.setWidth(computeMinimumColumnWidth("1234567890"));
column1.addSelectionListener(new ColumnSelectionAdapter(column1, markerConfigsTable, 0, tableColumnSorter));
column1.addSelectionListener(new ColumnSelectionAdapter(markerConfigsTable, tableColumnSorter));

final var column2 = new TableColumn(table, SWT.NONE);
column2.setText(TMUIMessages.TaskTagsPreferencePage_column_type);
column2.setWidth(column1.getWidth());
column2.addSelectionListener(new ColumnSelectionAdapter(column2, markerConfigsTable, 1, tableColumnSorter));
column2.addSelectionListener(new ColumnSelectionAdapter(markerConfigsTable, tableColumnSorter, 0));

final var column3 = new TableColumn(table, SWT.NONE);
column3.setText(TMUIMessages.TaskTagsPreferencePage_column_level);
column3.setWidth(column1.getWidth());
column3.addSelectionListener(new ColumnSelectionAdapter(column3, markerConfigsTable, 2, tableColumnSorter));
column3.addSelectionListener(new ColumnSelectionAdapter(markerConfigsTable, tableColumnSorter, 0));

// Specify default sorting
table.setSortColumn(column1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ private void createThemesTableContent(final Composite parent) {
column1.setText(TMUIMessages.ThemePreferencePage_column_name);
int minWidth = computeMinimumColumnWidth(gc, TMUIMessages.ThemePreferencePage_column_name);
columnLayout.setColumnData(column1, new ColumnWeightData(2, minWidth, true));
column1.addSelectionListener(new ColumnSelectionAdapter(column1, themesTable, 0, viewerComparator));
column1.addSelectionListener(new ColumnSelectionAdapter(themesTable, viewerComparator));

final var column2 = new TableColumn(table, SWT.NONE);
column2.setText(TMUIMessages.ThemePreferencePage_column_path);
minWidth = computeMinimumColumnWidth(gc, TMUIMessages.ThemePreferencePage_column_path);
columnLayout.setColumnData(column2, new ColumnWeightData(2, minWidth, true));
column2.addSelectionListener(new ColumnSelectionAdapter(column2, themesTable, 1, viewerComparator));
column2.addSelectionListener(new ColumnSelectionAdapter(themesTable, viewerComparator, 0));

final var column3 = new TableColumn(table, SWT.NONE);
column3.setText(TMUIMessages.ThemePreferencePage_column_pluginId);
minWidth = computeMinimumColumnWidth(gc, TMUIMessages.ThemePreferencePage_column_pluginId);
columnLayout.setColumnData(column3, new ColumnWeightData(2, minWidth, true));
column3.addSelectionListener(new ColumnSelectionAdapter(column3, themesTable, 2, viewerComparator));
column3.addSelectionListener(new ColumnSelectionAdapter(themesTable, viewerComparator, 0));

gc.dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,37 @@
*/
public final class ColumnSelectionAdapter extends SelectionAdapter {

private final TableColumn fTableColumn;
private final TableViewer tableViewer;
private final int fColumnIndex;
private final ColumnViewerComparator viewerComparator;
private final int[] secondarySortColumns;

public ColumnSelectionAdapter(final TableColumn column, final TableViewer tableViewer, final int index,
final ColumnViewerComparator vc) {
fTableColumn = column;
public ColumnSelectionAdapter(final TableViewer tableViewer,
final ColumnViewerComparator vc, int... secondarySortColumns) {
this.tableViewer = tableViewer;
fColumnIndex = index;
viewerComparator = vc;
this.secondarySortColumns = secondarySortColumns;
}

@Override
public void widgetSelected(@Nullable final SelectionEvent e) {
viewerComparator.setColumn(fColumnIndex);
final int dir = viewerComparator.getDirection();
final Table table = tableViewer.getTable();
table.setSortDirection(dir);
table.setSortColumn(fTableColumn);
tableViewer.refresh();
if (e != null && e.getSource() instanceof TableColumn tableColumn) {
int columnIndex = -1;
TableColumn[] columns = tableViewer.getTable().getColumns();
for (int i = 0; i < columns.length; i++) {
if (columns[i].equals(tableColumn)) {
columnIndex = i;
break;
}
}
if (columnIndex == -1)
return;

viewerComparator.setColumns(columnIndex, secondarySortColumns);
final int dir = viewerComparator.getDirection();
final Table table = tableViewer.getTable();
table.setSortDirection(dir);
table.setSortColumn(tableColumn);
tableViewer.refresh();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,71 @@
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* Sebastian Thomschke (Vegard IT) - add support for secondary sort columns
*/
package org.eclipse.tm4e.ui.internal.widgets;

import static org.eclipse.tm4e.core.internal.utils.StringUtils.nullToEmpty;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;

/**
* Viewer comparator which sort a given column.
*
* {@link TableViewer} comparator which sorts based on the given column(s).
*/
public final class ColumnViewerComparator extends ViewerComparator {

private int fSortColumn = 0;
private int fSortOrder = 1; // 1 = asc, -1 = desc
private int[] sortColumns = { 0 };
private int sortOrder = 1; // 1 = ascending, -1 = descending

/**
* Returns the {@linkplain SWT} style constant for the sort direction.
*
* @return {@link SWT#DOWN} for asc sorting, {@link SWT#UP} otherwise
* @return {@link SWT#DOWN} for ascending sorting, {@link SWT#UP} otherwise
*/
public int getDirection() {
return fSortOrder == 1 ? SWT.DOWN : SWT.UP;
return sortOrder == 1 ? SWT.DOWN : SWT.UP;
}

/**
* Sets the sort column. If the newly set sort column equals the previous
* set sort column, the sort direction changes.
*
* @param column
* @param primaryColumn
* New sort column
*/
public void setColumn(final int column) {
if (column == fSortColumn) {
fSortOrder *= -1;
public void setColumns(final int primaryColumn, int... secondaryColumns) {
if (primaryColumn == sortColumns[0]) {
sortOrder *= -1;
} else {
fSortColumn = column;
fSortOrder = 1;
if (secondaryColumns.length == 0) {
sortColumns = new int[] { primaryColumn };
} else {
sortColumns = new int[secondaryColumns.length + 1];
System.arraycopy(secondaryColumns, 0, sortColumns, 1, secondaryColumns.length);
sortColumns[0] = primaryColumn;
}
sortOrder = 1;
}
}

@Override
public int compare(@Nullable final Viewer viewer, @Nullable final Object e1, @Nullable final Object e2) {

if (viewer instanceof final TableViewer tableViewer) {
final IBaseLabelProvider baseLabel = tableViewer.getLabelProvider();

final String left = ((ITableLabelProvider) baseLabel).getColumnText(e1, fSortColumn);
final String right = ((ITableLabelProvider) baseLabel).getColumnText(e2, fSortColumn);
final int sortResult = getComparator().compare(left != null ? left : "", right != null ? right : "");
return sortResult * fSortOrder;
if (viewer instanceof final TableViewer tableViewer
&& tableViewer.getLabelProvider() instanceof ITableLabelProvider labelProvider) {
for (var column : sortColumns) {
final String left = labelProvider.getColumnText(e1, column);
final String right = labelProvider.getColumnText(e2, column);
final int sortResult = getComparator().compare(nullToEmpty(left), nullToEmpty(right));
if (sortResult != 0)
return sortResult * sortOrder;
}
return 0;
}

return super.compare(viewer, e1, e2);
}

}

0 comments on commit 388292a

Please sign in to comment.