1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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 * 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 *******************************************************************************/
17 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
;
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
;
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.
34 * @author Geneviève Bastien
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;
52 /** Used to draw bar charts in columns. */
53 public interface ITmfColumnPercentageProvider
{
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
61 * The data object corresponding to a line in the tree.
62 * @return The value as a percentage (between 0 and 100)
64 public double getPercentage(Object data
);
68 * Constructor with parameters
71 * Text of the column. The name will be shown at the top of the
74 public TmfTreeColumnData(String text
) {
79 * Get the header text of a column
81 * @return The header text
83 public String
getText() {
88 * Get the width of the column
90 * @return The column width
92 public int getWidth() {
97 * Get the alignment of the column
99 * @return The alignment (for example SWT.LEAD, SWT.RIGHT, etc)
101 public int getAlignment() {
106 * Get the tooltip text to go with this column
108 * @return The tooltip text
110 public String
getTooltip() {
115 * Get the comparator used to sort columns. If <code>null</code>, then the
116 * column is not sortable
118 * @return The column comparator
120 public ViewerComparator
getComparator() {
125 * Get the percentage provider for this column. This will allow to draw a
126 * bar chart inside the cells of this columns
128 * @return The percentage provider
130 public ITmfColumnPercentageProvider
getPercentageProvider() {
131 return fPercentageProvider
;
135 * Return whether the column is movable or not
137 * @return True if column can be moved, false otherwise.
139 public boolean isMovable() {
144 * Set the width of the column. If not set, -1 is used.
147 * Width of the column. Use -1 for tree viewer's default
150 public void setWidth(int width
) {
155 * Set the alignment of this column. If not set, default value is SWT.LEAD.
158 * Alignment of the column. For example, SWT.LEAD, SWT.RIGHT,
161 public void setAlignment(int alignment
) {
162 fAlignment
= alignment
;
166 * Set the tooltip associated with this column
171 public void setTooltip(String tooltip
) {
176 * Set the comparator used to sort the column
179 * The comparator. Use <code>null</code> to not sort the column.
181 public void setComparator(ViewerComparator comparator
) {
182 fComparator
= comparator
;
186 * Set the percentage provider that will provide a percentage value to draw
187 * a bar chart inside the cells of this column
189 * @param percentProvider
190 * The percentage provider
192 public void setPercentageProvider(ITmfColumnPercentageProvider percentProvider
) {
193 fPercentageProvider
= percentProvider
;
197 * Set whether the column can be moved in the tree viewer. Default is false.
200 * true if the column can be moved, false otherwise
202 public void setMovable(boolean movable
) {
207 * Create a TreeColumn with this column's data and adds it to a {@link Tree}
210 * The {@link TreeViewer} object to add the column to
211 * @return The newly created {@link TreeViewerColumn}
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());
221 if (getTooltip() != null) {
222 column
.getColumn().setToolTipText(getTooltip());
224 column
.getColumn().setMoveable(isMovable());
226 /* Add the comparator to sort the column */
227 if (getComparator() != null) {
228 column
.getColumn().addSelectionListener(new SelectionAdapter() {
231 public void widgetSelected(SelectionEvent e
) {
233 if (treeViewer
.getTree().getSortDirection() == SWT
.UP
|| treeViewer
.getTree().getSortColumn() != column
.getColumn()) {
235 * Puts the descendant order if the old order was up
236 * or if the selected column has changed.
238 treeViewer
.setComparator(columnData
.getComparator());
239 treeViewer
.getTree().setSortDirection(SWT
.DOWN
);
241 ViewerComparator reverseComparator
;
242 /* Initializes the reverse comparator. */
243 reverseComparator
= new ViewerComparator() {
245 public int compare(Viewer viewer
, Object e1
, Object
247 return -1 * columnData
.getComparator().compare(viewer
, e1
, e2
);
252 * Puts the ascendant ordering if the selected
253 * column hasn't changed.
255 treeViewer
.setComparator(reverseComparator
);
256 treeViewer
.getTree().setSortDirection(SWT
.UP
);
258 treeViewer
.getTree().setSortColumn(column
.getColumn());