Uri
Uri

Reputation: 89729

How to figure out the preferred size of a JFace TreeViewer or SWT Tree?

I am writing an Eclipse plugin which has a hover control that consists of two controls, one on top of another:

An HTML viewer (BrowserInformationControl) for which I Can easily compute the size hint

A TreeViewer that contains a Tree.

I have set up my tree with actual contents. However, I can't find a way to figure out the size of the tree or the size hint for it once rendered.

Upvotes: 2

Views: 3134

Answers (1)

VonC
VonC

Reputation: 1323433

According to the API of org.eclipse.swt.widgets.Tree, this should be done with:

public Point computeSize(int wHint,
                     int hHint,
                     boolean changed)

The preferred size of a control is the size that it would best be displayed at.

The width hint and height hint arguments allow the caller to ask a control questions such as "Given a particular width, how high does the control need to be to show all of the contents?"
To indicate that the caller does not wish to constrain a particular dimension, the constant SWT.DEFAULT is passed for the hint.

If the changed flag is true, it indicates that the receiver's contents have changed, therefore any caches that a layout manager containing the control may have been keeping need to be flushed. When the control is resized, the changed flag will be false, so layout manager caches can be retained.


Note that on Windows, the behavior of computeSize() was flawed: see this message and this bug: fixed for eclipse 3.4M1 and forward.

Example of use of computeSize() in this message.

The use of getBound() might be another interesting alternative.

Upvotes: 4

Related Questions