Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e58dc41645b4f43f62327edd029b64d02cc9b562 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.launch.ui.editor.tabs;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * Provides table utilities.
 */
public final class TableUtils {

	/**
	 * Determines the current visible width of the table and
	 * adjust the column width according to there relative weight.
	 *
	 * @param viewer The viewer or <code>null</code>.
	 */
	public static void adjustTableColumnWidth(Viewer viewer) {
		if (!(viewer instanceof TableViewer)) return;

		final TableViewer tableViewer = (TableViewer)viewer;
		final Table table = tableViewer.getTable();
		table.addControlListener(new ControlListener() {

			@Override
			public void controlResized(ControlEvent e) {
				int sumColumnWidth = 0;
				int tableWidth = table.getSize().x - table.getVerticalBar().getSize().x;

				TableColumn[] columns = table.getColumns();

				// Summarize the table column width
				for (TableColumn column : columns) {
					Object widthHint = column.getData("widthHint"); //$NON-NLS-1$
					sumColumnWidth += widthHint instanceof Integer ? ((Integer)widthHint).intValue() : column.getWidth();
				}

				// Calculate the new width for each column
				int sumColumnWidth2 = 0;
				TableColumn maxColumn = null;
				for (TableColumn column : columns) {
					Object widthHint = column.getData("widthHint"); //$NON-NLS-1$
					int width = widthHint instanceof Integer ? ((Integer)widthHint).intValue() : column.getWidth();
					int weight = (width * 100) / sumColumnWidth;
					int newWidth = (weight * tableWidth) / 100;
					sumColumnWidth2 += newWidth;
					column.setWidth(newWidth);
					if (maxColumn == null || maxColumn.getWidth() < column.getWidth()) {
						maxColumn = column;
					}
				}

				// If we end up with a slighter larger width of all columns than
				// the table widget is, reduce the size of the largest column
				if (sumColumnWidth2 > tableWidth && maxColumn != null) {
					int delta = sumColumnWidth2 - tableWidth + 2;
					maxColumn.setWidth(maxColumn.getWidth() - delta);
				}

				table.removeControlListener(this);
			}

			@Override
			public void controlMoved(ControlEvent e) {
			}
		});
	}
}

Back to the top