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