Commit | Line | Data |
---|---|---|
853beb54 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | * Matthew Khouzam - Initial API and implementation | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.tracecompass.tmf.ui.viewers.table; | |
15 | ||
16 | import java.util.HashMap; | |
17 | import java.util.Map; | |
18 | ||
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; | |
36 | ||
37 | /** | |
38 | * Generic {@link TableViewer} wrapper with most standard features enabled | |
39 | * | |
40 | * <pre> | |
41 | * It provides the following features: | |
42 | * - Sortable columns | |
43 | * - Movable columns | |
44 | * - Resizable columns | |
45 | * - Tracking last clicked columns | |
46 | * </pre> | |
47 | * | |
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)} | |
56 | * | |
34c87ae8 | 57 | * @since 2.0 |
853beb54 MK |
58 | */ |
59 | public class TmfSimpleTableViewer extends TmfViewer { | |
60 | ||
61 | private final class MouseColumnListener extends MouseAdapter { | |
62 | @Override | |
63 | public void mouseDown(MouseEvent e) { | |
64 | ViewerCell cell = fTableViewer.getCell(new Point(e.x, e.y)); | |
65 | fSelectedColumn = (cell != null) ? cell.getColumnIndex() : -1; | |
66 | } | |
67 | } | |
68 | ||
69 | private final class ColumnSorter extends SelectionAdapter { | |
70 | private final TableColumn fColumn; | |
71 | ||
72 | private ColumnSorter(TableColumn column) { | |
73 | fColumn = column; | |
74 | } | |
75 | ||
76 | @Override | |
77 | public void widgetSelected(SelectionEvent e) { | |
78 | ||
79 | Table table = fTableViewer.getTable(); | |
80 | table.setSortDirection(getSortDirection()); | |
81 | TableColumn prevSortcolumn = table.getSortColumn(); | |
82 | if (prevSortcolumn == fColumn) { | |
83 | flipSortDirection(); | |
84 | } | |
85 | table.setSortColumn(fColumn); | |
86 | ViewerCompoundComparator comparator = fComparators.get(fColumn.getText()); | |
87 | if (fDirection == SWT.DOWN) { | |
88 | fTableViewer.setComparator(comparator); | |
89 | } else { | |
90 | fTableViewer.setComparator(new InvertSorter(comparator)); | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | private class InvertSorter extends ViewerCompoundComparator { | |
96 | private final ViewerComparator fViewerComparator; | |
97 | ||
98 | public InvertSorter(ViewerComparator vc) { | |
99 | fViewerComparator = vc; | |
100 | } | |
101 | ||
102 | @Override | |
103 | public int compare(Object e1, Object e2) { | |
104 | return -fViewerComparator.compare(null, e1, e2); | |
105 | } | |
106 | ||
107 | } | |
108 | ||
109 | private static final int DEFAULT_COL_WIDTH = 200; | |
110 | private final TableViewer fTableViewer; | |
111 | private final Map<String, ViewerCompoundComparator> fComparators = new HashMap<>(); | |
112 | ||
113 | private int fDirection; | |
114 | private int fSelectedColumn; | |
115 | ||
116 | /** | |
117 | * Constructor that initializes the parent of the viewer | |
118 | * | |
119 | * @param table | |
120 | * the {@link TableViewer} to wrap | |
121 | */ | |
122 | public TmfSimpleTableViewer(TableViewer table) { | |
123 | super(table.getControl().getParent()); | |
124 | fTableViewer = table; | |
125 | createColumns(); | |
126 | ||
127 | final Table tableControl = fTableViewer.getTable(); | |
128 | tableControl.setHeaderVisible(true); | |
129 | tableControl.setLinesVisible(true); | |
130 | ||
131 | fDirection = SWT.DOWN; | |
132 | fTableViewer.setUseHashlookup(true); | |
133 | fTableViewer.getControl().addMouseListener(new MouseColumnListener()); | |
134 | refresh(); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Create a column for the table | |
139 | * | |
140 | * @param name | |
141 | * the name of the column (must be unique) | |
142 | * @param provider | |
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 | |
147 | */ | |
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$ | |
151 | } | |
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); | |
162 | } | |
163 | ||
164 | /** | |
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. | |
169 | */ | |
170 | protected void createColumns() { | |
171 | // override me! | |
172 | } | |
173 | ||
174 | private void flipSortDirection() { | |
175 | if (fDirection == SWT.DOWN) { | |
176 | fDirection = SWT.UP; | |
177 | } else { | |
178 | fDirection = SWT.DOWN; | |
179 | } | |
180 | ||
181 | } | |
182 | ||
183 | private int getSortDirection() { | |
184 | return fDirection; | |
185 | } | |
186 | ||
187 | @Override | |
188 | public final Control getControl() { | |
189 | return fTableViewer.getControl(); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Gets the wrapped table viewer | |
194 | * | |
195 | * @return the table viewer | |
196 | */ | |
197 | public final TableViewer getTableViewer() { | |
198 | return fTableViewer; | |
199 | } | |
200 | ||
201 | /** | |
202 | * Get the selected column index | |
203 | * | |
204 | * @return the selected column index or -1 | |
205 | */ | |
206 | public final int getColumnIndex() { | |
207 | return fSelectedColumn; | |
208 | } | |
209 | ||
210 | @Override | |
211 | public final void refresh() { | |
212 | fTableViewer.refresh(); | |
213 | } | |
214 | } |