From 626ada0dc0a4e5f72a1bc79a21472a786084b565 Mon Sep 17 00:00:00 2001
From: Nils Christian Ehmke <nie@informatik.uni-kiel.de>
Date: Wed, 10 Dec 2014 09:55:37 +0100
Subject: [PATCH] Added table sorting, corrected another issue

---
 .../gui/model/domain/ExecutionEntry.java      |  3 ++
 src/main/java/kieker/gui/view/MainWindow.java | 15 ++++++
 .../gui/view/util/TreeColumnSortListener.java | 46 +++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 src/main/java/kieker/gui/view/util/TreeColumnSortListener.java

diff --git a/src/main/java/kieker/gui/model/domain/ExecutionEntry.java b/src/main/java/kieker/gui/model/domain/ExecutionEntry.java
index ff5de283..f854928e 100644
--- a/src/main/java/kieker/gui/model/domain/ExecutionEntry.java
+++ b/src/main/java/kieker/gui/model/domain/ExecutionEntry.java
@@ -120,6 +120,9 @@ public final class ExecutionEntry {
 		} else {
 			this.percent = 100.0f;
 		}
+		for (final ExecutionEntry executionEntry : this.children) {
+			executionEntry.updatePercent();
+		}
 	}
 
 	@Override
diff --git a/src/main/java/kieker/gui/view/MainWindow.java b/src/main/java/kieker/gui/view/MainWindow.java
index a092d254..893bc763 100644
--- a/src/main/java/kieker/gui/view/MainWindow.java
+++ b/src/main/java/kieker/gui/view/MainWindow.java
@@ -25,12 +25,14 @@ import kieker.gui.model.Properties;
 import kieker.gui.model.domain.AggregatedExecutionEntry;
 import kieker.gui.model.domain.ExecutionEntry;
 import kieker.gui.model.domain.RecordEntry;
+import kieker.gui.view.util.AbstractDirectedComparator;
 import kieker.gui.view.util.AggregatedExecutionTracesTreeSetDataListener;
 import kieker.gui.view.util.ExecutionTracesTreeSetDataListener;
 import kieker.gui.view.util.RecordEntryTimestampComparator;
 import kieker.gui.view.util.RecordEntryTypeComparator;
 import kieker.gui.view.util.RecordsTableSetDataListener;
 import kieker.gui.view.util.TableColumnSortListener;
+import kieker.gui.view.util.TreeColumnSortListener;
 
 import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.jface.viewers.TreeViewer;
@@ -364,6 +366,19 @@ public final class MainWindow {
 		this.recordsTableTypeColumn.addListener(SWT.Selection, new TableColumnSortListener<RecordEntry>(new RecordEntryTypeComparator()));
 		this.recordsTableRecordColumn.addListener(SWT.Selection, new TableColumnSortListener<RecordEntry>(new RecordEntryTimestampComparator()));
 
+		this.treeColumn_11.addListener(SWT.Selection, new TreeColumnSortListener(new AbstractDirectedComparator<ExecutionEntry>() {
+
+			@Override
+			public int compare(final ExecutionEntry arg0, final ExecutionEntry arg1) {
+				int result = Long.compare(arg0.getDuration(), arg1.getDuration());
+				if (this.getDirection() == SWT.UP) {
+					result = -result;
+				}
+				return result;
+
+			}
+		}));
+
 		this.explorerTree.addSelectionListener(new SelectionAdapter() {
 
 			@Override
diff --git a/src/main/java/kieker/gui/view/util/TreeColumnSortListener.java b/src/main/java/kieker/gui/view/util/TreeColumnSortListener.java
new file mode 100644
index 00000000..65794711
--- /dev/null
+++ b/src/main/java/kieker/gui/view/util/TreeColumnSortListener.java
@@ -0,0 +1,46 @@
+package kieker.gui.view.util;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeColumn;
+
+public final class TreeColumnSortListener<T> implements Listener {
+
+	private final AbstractDirectedComparator<T> comparator;
+
+	public TreeColumnSortListener(final AbstractDirectedComparator<T> comparator) {
+		this.comparator = comparator;
+	}
+
+	@Override
+	public void handleEvent(final Event event) {
+		// Get the necessary information from the event
+		final TreeColumn currentColumn = (TreeColumn) event.widget;
+		final Tree tree = currentColumn.getParent();
+		final TreeColumn sortColumn = tree.getSortColumn();
+
+		// Determine new sort column and direction
+		int direction = tree.getSortDirection();
+		if (sortColumn == currentColumn) {
+			direction = ((direction == SWT.UP) ? SWT.DOWN : SWT.UP);
+		} else {
+			tree.setSortColumn(currentColumn);
+			direction = SWT.UP;
+		}
+
+		// Sort the data
+		this.comparator.setDirection(direction);
+		final List<T> entries = (List<T>) tree.getData();
+		Collections.sort(entries, this.comparator);
+
+		// Update the data displayed in the table
+		tree.setSortDirection(direction);
+		tree.clearAll(true);
+	}
+
+}
-- 
GitLab