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
.linuxtools
.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
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;
53 /** Used to draw bar charts in columns. */
54 public interface ITmfColumnPercentageProvider
{
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
62 * The data object corresponding to a line in the tree.
63 * @return The value as a percentage (between 0 and 100)
65 public double getPercentage(Object data
);
69 * Constructor with parameters
72 * Text of the column. The name will be shown at the top of the
75 public TmfTreeColumnData(String text
) {
80 * Get the header text of a column
82 * @return The header text
84 public String
getText() {
89 * Get the width of the column
91 * @return The column width
93 public int getWidth() {
98 * Get the alignment of the column
100 * @return The alignment (for example SWT.LEAD, SWT.RIGHT, etc)
102 public int getAlignment() {
107 * Get the tooltip text to go with this column
109 * @return The tooltip text
111 public String
getTooltip() {
116 * Get the comparator used to sort columns. If <code>null</code>, then the
117 * column is not sortable
119 * @return The column comparator
121 public ViewerComparator
getComparator() {
126 * Get the percentage provider for this column. This will allow to draw a
127 * bar chart inside the cells of this columns
129 * @return The percentage provider
131 public ITmfColumnPercentageProvider
getPercentageProvider() {
132 return fPercentageProvider
;
136 * Return whether the column is movable or not
138 * @return True if column can be moved, false otherwise.
140 public boolean isMovable() {
145 * Set the width of the column. If not set, -1 is used.
148 * Width of the column. Use -1 for tree viewer's default
151 public void setWidth(int width
) {
156 * Set the alignment of this column. If not set, default value is SWT.LEAD.
159 * Alignment of the column. For example, SWT.LEAD, SWT.RIGHT,
162 public void setAlignment(int alignment
) {
163 fAlignment
= alignment
;
167 * Set the tooltip associated with this column
172 public void setTooltip(String tooltip
) {
177 * Set the comparator used to sort the column
180 * The comparator. Use <code>null</code> to not sort the column.
182 public void setComparator(ViewerComparator comparator
) {
183 fComparator
= comparator
;
187 * Set the percentage provider that will provide a percentage value to draw
188 * a bar chart inside the cells of this column
190 * @param percentProvider
191 * The percentage provider
193 public void setPercentageProvider(ITmfColumnPercentageProvider percentProvider
) {
194 fPercentageProvider
= percentProvider
;
198 * Set whether the column can be moved in the tree viewer. Default is false.
201 * true if the column can be moved, false otherwise
203 public void setMovable(boolean movable
) {
208 * Create a TreeColumn with this column's data and adds it to a {@link Tree}
211 * The {@link TreeViewer} object to add the column to
212 * @return The newly created {@link TreeViewerColumn}
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());
222 if (getTooltip() != null) {
223 column
.getColumn().setToolTipText(getTooltip());
225 column
.getColumn().setMoveable(isMovable());
227 /* Add the comparator to sort the column */
228 if (getComparator() != null) {
229 column
.getColumn().addSelectionListener(new SelectionAdapter() {
232 public void widgetSelected(SelectionEvent e
) {
234 if (treeViewer
.getTree().getSortDirection() == SWT
.UP
|| treeViewer
.getTree().getSortColumn() != column
.getColumn()) {
236 * Puts the descendant order if the old order was up
237 * or if the selected column has changed.
239 treeViewer
.setComparator(columnData
.getComparator());
240 treeViewer
.getTree().setSortDirection(SWT
.DOWN
);
242 ViewerComparator reverseComparator
;
243 /* Initializes the reverse comparator. */
244 reverseComparator
= new ViewerComparator() {
246 public int compare(Viewer viewer
, Object e1
, Object
248 return -1 * columnData
.getComparator().compare(viewer
, e1
, e2
);
253 * Puts the ascendant ordering if the selected
254 * column hasn't changed.
256 treeViewer
.setComparator(reverseComparator
);
257 treeViewer
.getTree().setSortDirection(SWT
.UP
);
259 treeViewer
.getTree().setSortColumn(column
.getColumn());