Commit | Line | Data |
---|---|---|
c0188b25 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal | |
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 | * Mathieu Denis - Initial Implementation and API (in TmfBaseColumnData of | |
11 | * statistics framework) | |
12 | * Bernd Hufmann - Added Annotations | |
13 | * Geneviève Bastien - Moved TmfBaseColumnData to this class and adapted | |
14 | * it for the abstract tree viewer | |
15 | *******************************************************************************/ | |
16 | ||
2bdf0193 | 17 | package org.eclipse.tracecompass.tmf.ui.viewers.tree; |
c0188b25 GB |
18 | |
19 | import org.eclipse.jdt.annotation.NonNull; | |
20 | import org.eclipse.jface.viewers.TreeViewer; | |
21 | import org.eclipse.jface.viewers.TreeViewerColumn; | |
22 | import org.eclipse.jface.viewers.Viewer; | |
23 | import org.eclipse.jface.viewers.ViewerComparator; | |
24 | import org.eclipse.swt.SWT; | |
25 | import org.eclipse.swt.events.SelectionAdapter; | |
26 | import org.eclipse.swt.events.SelectionEvent; | |
27 | import org.eclipse.swt.widgets.Tree; | |
28 | ||
29 | /** | |
30 | * Represents a column in an abstract tree viewer. It allows to define the | |
31 | * column's characteristics: text, width, alignment, tooltip, comparators, | |
32 | * percent providers, whether the column is movable, etc. | |
33 | * | |
34 | * @author Geneviève Bastien | |
c0188b25 GB |
35 | */ |
36 | public class TmfTreeColumnData { | |
37 | /** Name of the column. */ | |
38 | private final String fText; | |
39 | /** Width of the column. */ | |
40 | private int fWidth = -1; | |
41 | /** Alignment of the column. */ | |
42 | private int fAlignment = SWT.LEAD; | |
43 | /** Tooltip of the column. */ | |
44 | private String fTooltip = null; | |
45 | /** Used to sort elements of this column. If null, column is not sortable. */ | |
46 | private ViewerComparator fComparator = null; | |
47 | /** Whether the column is movable */ | |
48 | private boolean fMovable = false; | |
49 | /** Used to draw bar charts in this column. Can be null. */ | |
50 | private ITmfColumnPercentageProvider fPercentageProvider = null; | |
51 | ||
52 | /** Used to draw bar charts in columns. */ | |
53 | public interface ITmfColumnPercentageProvider { | |
54 | ||
55 | /** | |
56 | * Percentage provider. Returns a percentage (between 0 and 100) from | |
57 | * the given object. The object is usually an entry (a line of the tree | |
58 | * viewer). | |
59 | * | |
60 | * @param data | |
61 | * The data object corresponding to a line in the tree. | |
62 | * @return The value as a percentage (between 0 and 100) | |
63 | */ | |
64 | public double getPercentage(Object data); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Constructor with parameters | |
69 | * | |
70 | * @param text | |
71 | * Text of the column. The name will be shown at the top of the | |
72 | * column. | |
73 | */ | |
74 | public TmfTreeColumnData(String text) { | |
75 | fText = text; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Get the header text of a column | |
80 | * | |
81 | * @return The header text | |
82 | */ | |
83 | public String getText() { | |
84 | return fText; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Get the width of the column | |
89 | * | |
90 | * @return The column width | |
91 | */ | |
92 | public int getWidth() { | |
93 | return fWidth; | |
94 | } | |
95 | ||
96 | /** | |
97 | * Get the alignment of the column | |
98 | * | |
99 | * @return The alignment (for example SWT.LEAD, SWT.RIGHT, etc) | |
100 | */ | |
101 | public int getAlignment() { | |
102 | return fAlignment; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Get the tooltip text to go with this column | |
107 | * | |
108 | * @return The tooltip text | |
109 | */ | |
110 | public String getTooltip() { | |
111 | return fTooltip; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Get the comparator used to sort columns. If <code>null</code>, then the | |
116 | * column is not sortable | |
117 | * | |
118 | * @return The column comparator | |
119 | */ | |
120 | public ViewerComparator getComparator() { | |
121 | return fComparator; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Get the percentage provider for this column. This will allow to draw a | |
126 | * bar chart inside the cells of this columns | |
127 | * | |
128 | * @return The percentage provider | |
129 | */ | |
130 | public ITmfColumnPercentageProvider getPercentageProvider() { | |
131 | return fPercentageProvider; | |
132 | } | |
133 | ||
134 | /** | |
135 | * Return whether the column is movable or not | |
136 | * | |
137 | * @return True if column can be moved, false otherwise. | |
138 | */ | |
139 | public boolean isMovable() { | |
140 | return fMovable; | |
141 | } | |
142 | ||
143 | /** | |
144 | * Set the width of the column. If not set, -1 is used. | |
145 | * | |
146 | * @param width | |
147 | * Width of the column. Use -1 for tree viewer's default | |
148 | * behavior. | |
149 | */ | |
150 | public void setWidth(int width) { | |
151 | fWidth = width; | |
152 | } | |
153 | ||
154 | /** | |
155 | * Set the alignment of this column. If not set, default value is SWT.LEAD. | |
156 | * | |
157 | * @param alignment | |
158 | * Alignment of the column. For example, SWT.LEAD, SWT.RIGHT, | |
159 | * SWT.LEFT | |
160 | */ | |
161 | public void setAlignment(int alignment) { | |
162 | fAlignment = alignment; | |
163 | } | |
164 | ||
165 | /** | |
166 | * Set the tooltip associated with this column | |
167 | * | |
168 | * @param tooltip | |
169 | * the tooltip text | |
170 | */ | |
171 | public void setTooltip(String tooltip) { | |
172 | fTooltip = tooltip; | |
173 | } | |
174 | ||
175 | /** | |
176 | * Set the comparator used to sort the column | |
177 | * | |
178 | * @param comparator | |
179 | * The comparator. Use <code>null</code> to not sort the column. | |
180 | */ | |
181 | public void setComparator(ViewerComparator comparator) { | |
182 | fComparator = comparator; | |
183 | } | |
184 | ||
185 | /** | |
186 | * Set the percentage provider that will provide a percentage value to draw | |
187 | * a bar chart inside the cells of this column | |
188 | * | |
189 | * @param percentProvider | |
190 | * The percentage provider | |
191 | */ | |
192 | public void setPercentageProvider(ITmfColumnPercentageProvider percentProvider) { | |
193 | fPercentageProvider = percentProvider; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Set whether the column can be moved in the tree viewer. Default is false. | |
198 | * | |
199 | * @param movable | |
200 | * true if the column can be moved, false otherwise | |
201 | */ | |
202 | public void setMovable(boolean movable) { | |
203 | fMovable = movable; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Create a TreeColumn with this column's data and adds it to a {@link Tree} | |
208 | * | |
209 | * @param treeViewer | |
210 | * The {@link TreeViewer} object to add the column to | |
211 | * @return The newly created {@link TreeViewerColumn} | |
212 | */ | |
213 | @NonNull | |
214 | public TreeViewerColumn createColumn(final TreeViewer treeViewer) { | |
215 | final TreeViewerColumn column = new TreeViewerColumn(treeViewer, getAlignment()); | |
216 | final TmfTreeColumnData columnData = this; | |
217 | column.getColumn().setText(getText()); | |
218 | if (getWidth() != -1) { | |
219 | column.getColumn().setWidth(getWidth()); | |
220 | } | |
221 | if (getTooltip() != null) { | |
222 | column.getColumn().setToolTipText(getTooltip()); | |
223 | } | |
224 | column.getColumn().setMoveable(isMovable()); | |
225 | ||
226 | /* Add the comparator to sort the column */ | |
227 | if (getComparator() != null) { | |
228 | column.getColumn().addSelectionListener(new SelectionAdapter() { | |
229 | ||
230 | @Override | |
231 | public void widgetSelected(SelectionEvent e) { | |
232 | ||
233 | if (treeViewer.getTree().getSortDirection() == SWT.UP || treeViewer.getTree().getSortColumn() != column.getColumn()) { | |
234 | /* | |
235 | * Puts the descendant order if the old order was up | |
236 | * or if the selected column has changed. | |
237 | */ | |
238 | treeViewer.setComparator(columnData.getComparator()); | |
239 | treeViewer.getTree().setSortDirection(SWT.DOWN); | |
240 | } else { | |
241 | ViewerComparator reverseComparator; | |
242 | /* Initializes the reverse comparator. */ | |
243 | reverseComparator = new ViewerComparator() { | |
244 | @Override | |
245 | public int compare(Viewer viewer, Object e1, Object | |
246 | e2) { | |
247 | return -1 * columnData.getComparator().compare(viewer, e1, e2); | |
248 | } | |
249 | }; | |
250 | ||
251 | /* | |
252 | * Puts the ascendant ordering if the selected | |
253 | * column hasn't changed. | |
254 | */ | |
255 | treeViewer.setComparator(reverseComparator); | |
256 | treeViewer.getTree().setSortDirection(SWT.UP); | |
257 | } | |
258 | treeViewer.getTree().setSortColumn(column.getColumn()); | |
259 | } | |
260 | }); | |
261 | } | |
262 | ||
263 | return column; | |
264 | } | |
265 | } |