1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Alexandre Montplaisir - Initial API and implementation
11 * Matthew Khouzam - Initial API and implementation
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.table
;
16 import java
.util
.HashMap
;
19 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
20 import org
.eclipse
.jface
.viewers
.IContentProvider
;
21 import org
.eclipse
.jface
.viewers
.TableViewer
;
22 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
23 import org
.eclipse
.jface
.viewers
.ViewerCell
;
24 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.events
.MouseAdapter
;
27 import org
.eclipse
.swt
.events
.MouseEvent
;
28 import org
.eclipse
.swt
.events
.SelectionAdapter
;
29 import org
.eclipse
.swt
.events
.SelectionEvent
;
30 import org
.eclipse
.swt
.events
.SelectionListener
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Table
;
34 import org
.eclipse
.swt
.widgets
.TableColumn
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.TmfViewer
;
38 * Generic {@link TableViewer} wrapper with most standard features enabled
41 * It provides the following features:
45 * - Tracking last clicked columns
48 * The person extending this class must implement the {@link #createColumns()},
49 * they must also supply a content provider of the {@link TableViewer} obtained
50 * by {@link #getTableViewer()},
51 * {@link TableViewer#setContentProvider(IContentProvider)} along with an input
52 * to {@link TableViewer#setInput(Object)}. They can also add selection
53 * listeners to the {@link Table} obtained from the {@link TableViewer} by
54 * calling {@link TableViewer#getTable()} with
55 * {@link Table#addSelectionListener(SelectionListener)}
59 public class TmfSimpleTableViewer
extends TmfViewer
{
61 private final class MouseColumnListener
extends MouseAdapter
{
63 public void mouseDown(MouseEvent e
) {
64 ViewerCell cell
= fTableViewer
.getCell(new Point(e
.x
, e
.y
));
65 fSelectedColumn
= (cell
!= null) ? cell
.getColumnIndex() : -1;
69 private final class ColumnSorter
extends SelectionAdapter
{
70 private final TableColumn fColumn
;
72 private ColumnSorter(TableColumn column
) {
77 public void widgetSelected(SelectionEvent e
) {
79 Table table
= fTableViewer
.getTable();
80 table
.setSortDirection(getSortDirection());
81 TableColumn prevSortcolumn
= table
.getSortColumn();
82 if (prevSortcolumn
== fColumn
) {
85 table
.setSortColumn(fColumn
);
86 ViewerCompoundComparator comparator
= fComparators
.get(fColumn
.getText());
87 if (fDirection
== SWT
.DOWN
) {
88 fTableViewer
.setComparator(comparator
);
90 fTableViewer
.setComparator(new InvertSorter(comparator
));
95 private class InvertSorter
extends ViewerCompoundComparator
{
96 private final ViewerComparator fViewerComparator
;
98 public InvertSorter(ViewerComparator vc
) {
99 fViewerComparator
= vc
;
103 public int compare(Object e1
, Object e2
) {
104 return -fViewerComparator
.compare(null, e1
, e2
);
109 private static final int DEFAULT_COL_WIDTH
= 200;
110 private final TableViewer fTableViewer
;
111 private final Map
<String
, ViewerCompoundComparator
> fComparators
= new HashMap
<>();
113 private int fDirection
;
114 private int fSelectedColumn
;
117 * Constructor that initializes the parent of the viewer
120 * the {@link TableViewer} to wrap
122 public TmfSimpleTableViewer(TableViewer table
) {
123 super(table
.getControl().getParent());
124 fTableViewer
= table
;
127 final Table tableControl
= fTableViewer
.getTable();
128 tableControl
.setHeaderVisible(true);
129 tableControl
.setLinesVisible(true);
131 fDirection
= SWT
.DOWN
;
132 fTableViewer
.setUseHashlookup(true);
133 fTableViewer
.getControl().addMouseListener(new MouseColumnListener());
138 * Create a column for the table
141 * the name of the column (must be unique)
143 * the provider of the column
144 * @param viewerComparator
145 * the comparator associated with clicking on the column, if it
146 * is null, a string comparator will be used
148 protected final void createColumn(String name
, ColumnLabelProvider provider
, ViewerCompoundComparator viewerComparator
) {
149 if (fComparators
.containsKey(name
)) {
150 throw new IllegalArgumentException("Cannot have two columns with the same name"); //$NON-NLS-1$
152 TableViewerColumn col
= new TableViewerColumn(fTableViewer
, SWT
.NONE
);
153 col
.setLabelProvider(provider
);
154 final TableColumn column
= col
.getColumn();
155 column
.setWidth(DEFAULT_COL_WIDTH
);
156 column
.setText(name
);
157 column
.setResizable(true);
158 column
.setMoveable(true);
159 column
.addSelectionListener(new ColumnSorter(column
));
160 final ViewerCompoundComparator comparator
= (viewerComparator
== null) ? ViewerCompoundComparator
.STRING_COMPARATOR
: viewerComparator
;
161 fComparators
.put(name
, comparator
);
165 * Column initializer, called in the constructor. This needs to be
166 * overridden. Use the
167 * {@link #createColumn(String, ColumnLabelProvider, ViewerCompoundComparator)}
168 * method to help create columns.
170 protected void createColumns() {
174 private void flipSortDirection() {
175 if (fDirection
== SWT
.DOWN
) {
178 fDirection
= SWT
.DOWN
;
183 private int getSortDirection() {
188 public final Control
getControl() {
189 return fTableViewer
.getControl();
193 * Gets the wrapped table viewer
195 * @return the table viewer
197 public final TableViewer
getTableViewer() {
202 * Get the selected column index
204 * @return the selected column index or -1
206 public final int getColumnIndex() {
207 return fSelectedColumn
;
211 public final void refresh() {
212 fTableViewer
.refresh();