Skip to content

Commit

Permalink
7091: JOverflow Instance View table should be sortable by clicking he…
Browse files Browse the repository at this point in the history
…aders

Reviewed-by: hirt
  • Loading branch information
aptmac committed Oct 18, 2021
1 parent c725078 commit 5787b9a
Showing 1 changed file with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, Red Hat Inc. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021, Red Hat Inc. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -42,9 +42,12 @@
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;

import org.openjdk.jmc.joverflow.heap.model.JavaField;
import org.openjdk.jmc.joverflow.heap.model.JavaObject;
import org.openjdk.jmc.joverflow.heap.model.JavaObjectArray;
Expand All @@ -53,17 +56,49 @@
import org.openjdk.jmc.joverflow.ui.model.JavaThingItem;

public class JavaThingTreeViewer<T extends JavaThingItem> extends TreeViewer {
private static final String ELLIPSIS = "..."; //$NON-NLS-1$
private static final String NAME_COLUMN = "Name"; //$NON-NLS-1$
private static final String SIZE_COLUMN = "Size"; //$NON-NLS-1$
private static final String VALUE_COLUMN = "Value"; //$NON-NLS-1$

public JavaThingTreeViewer(Composite parent, int style) {
super(parent, style);

setContentProvider(new JavaThingItemContentProvider());

createTreeViewerColumn("Name", T::getName);
createTreeViewerColumn("Value", T::getValue);
createTreeViewerColumn("Size", T::getSize);
createTreeViewerColumn(NAME_COLUMN, T::getName);
createTreeViewerColumn(VALUE_COLUMN, T::getValue);
createTreeViewerColumn(SIZE_COLUMN, T::getSize);

getTree().setLinesVisible(true);
getTree().setHeaderVisible(true);

setComparator(new ViewerComparator() {
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
JavaThingItem item1 = (JavaThingItem) e1;
JavaThingItem item2 = (JavaThingItem) e2;
// keep the overflow ellipsis row at the bottom of the view
if (item1.getName().equals(ELLIPSIS)) {
return SWT.MAX;
} else if (item2.getName().equals(ELLIPSIS)) {
return -SWT.MAX;
}
int result = 0;
int sortDirection = getTree().getSortDirection() == SWT.UP ? 1
: getTree().getSortDirection() == SWT.DOWN ? -1 : SWT.NONE;
if (getTree().getSortColumn() != null) {
if (getTree().getSortColumn().getText().equals(NAME_COLUMN)) {
result = item1.getName().compareTo(item2.getName());
} else if (getTree().getSortColumn().getText().equals(VALUE_COLUMN)) {
result = item1.getValue().compareTo(item2.getValue());
} else if (getTree().getSortColumn().getText().equals(SIZE_COLUMN)) {
result = Integer.parseInt(item1.getSize()) - Integer.parseInt(item2.getSize());
}
}
return result * sortDirection;
}
});
}

private void createTreeViewerColumn(String label, Function<T, String> labelProvider) {
Expand All @@ -79,6 +114,24 @@ public String getText(Object element) {
return labelProvider.apply((T) element);
}
});

column.getColumn().addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
int newSortDirection = SWT.DOWN;
if (getTree().getSortColumn() != null
&& getTree().getSortColumn().getText().equals(column.getColumn().getText())) {
newSortDirection = getTree().getSortDirection() == SWT.UP ? SWT.DOWN : SWT.UP;
}
getTree().setSortColumn(column.getColumn());
getTree().setSortDirection(newSortDirection);
refresh();
}

@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}

private class JavaThingItemContentProvider implements ITreeContentProvider {
Expand Down

0 comments on commit 5787b9a

Please sign in to comment.