Package org.eclipse.jface.widgets

# Class TreeFactory

java.lang.Object
org.eclipse.jface.widgets.AbstractWidgetFactory<F,C,Composite>
org.eclipse.jface.widgets.AbstractControlFactory<TreeFactory,Tree>
org.eclipse.jface.widgets.TreeFactory

public final class TreeFactory extends AbstractControlFactory<TreeFactory,Tree>
This class provides a convenient shorthand for creating and initializing `Tree`. This offers several benefits over creating Tree normal way:

- The same factory can be used many times to create several Tree instances
- The setters on TreeFactory all return "this", allowing them to be chained
- TreeFactory accepts a Lambda for `SelectionEvent` (see `onSelect(java.util.function.Consumer<org.eclipse.swt.events.SelectionEvent>)`)
- TreeFactory accepts a Lambda for `TreeEvent` (see `onExpand(java.util.function.Consumer<org.eclipse.swt.events.TreeEvent>)`)
- TreeFactory accepts a Lambda for `TreeEvent` (see `onCollapse(java.util.function.Consumer<org.eclipse.swt.events.TreeEvent>)`)

Example usage:

     Tree tree = TreeFactory.newTree(SWT.CHECK) //
                    .headerVisible(true) //
                    .linesVisible(true) //
                    .onSelect(e -> treeClicked(e)) //
                    .onExpand(e -> treeExpanded(e)) //
                    .create(parent);

The above example creates a tree, sets some properties, registers a SelectionListener and a TreeListener for expansion and finally creates the tree in "parent".

Note that this class does not extend `AbstractCompositeFactory` even though Tree extends Composite. This is because Tree is not supposed to be used like a Composite.

Since:
  3.18

## Method Summary

Modifier and Type
Method
Description
`TreeFactory`
`headerVisible(boolean visible)`
Marks the receiver's header as visible if the argument is true, and marks it invisible otherwise.
`TreeFactory`
`itemCount(int count)`
Sets the number of root-level items contained in the receiver.
`TreeFactory`
`linesVisible(boolean visible)`
Marks the receiver's lines as visible if the argument is true, and marks it invisible otherwise.
`static TreeFactory`
`newTree(int style)`
Creates a new TreeFactory with the given style.
`TreeFactory`
`onCollapse(Consumer<TreeEvent> consumer)`
Creates a `TreeListener` and registers it for the collapsed event.
`TreeFactory`
`onExpand(Consumer<TreeEvent> consumer)`
Creates a `TreeListener` and registers it for the expanded event.
`TreeFactory`
`onSelect(Consumer<SelectionEvent> consumer)`
Creates a `SelectionListener` and registers it for the widgetSelected event.

### Methods inherited from class org.eclipse.jface.widgets.AbstractControlFactory

`background, enabled, font, foreground, layoutData, orientation, supplyLayoutData, tooltip`

### Methods inherited from class org.eclipse.jface.widgets.AbstractWidgetFactory

`addProperty, cast, create, data, data`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### newTree

public static TreeFactory newTree(int style)
Creates a new TreeFactory with the given style. Refer to `Tree(Composite, int)` for possible styles.

Returns:
  a new TreeFactory instance

### linesVisible

public TreeFactory linesVisible(boolean visible)
Marks the receiver's lines as visible if the argument is true, and marks it invisible otherwise. Note that some platforms draw grid lines while others may draw alternating row colors.

Parameters:
  `visible` - the visibility state
Returns:
  this
See Also:
  - `Tree.setLinesVisible(boolean)`

### headerVisible

public TreeFactory headerVisible(boolean visible)
Marks the receiver's header as visible if the argument is true, and marks it invisible otherwise.

Parameters:
  `visible` - the visibility state
Returns:
  this
See Also:
  - `Tree.setHeaderVisible(boolean)`

### itemCount

public TreeFactory itemCount(int count)
Sets the number of root-level items contained in the receiver.

Parameters:
  `count` - the number of items
Returns:
  this
See Also:
  - `Tree.setItemCount(int)`

### onSelect

public TreeFactory onSelect(Consumer<SelectionEvent> consumer)
Creates a `SelectionListener` and registers it for the widgetSelected event. If the receiver is selected by the user the given consumer is invoked. The `SelectionEvent` is passed to the consumer.

Parameters:
  `consumer` - the consumer whose accept method is called
Returns:
  this
See Also:
  - `Tree.addSelectionListener(SelectionListener)`
  - `SelectionListener.widgetSelectedAdapter(Consumer)`

### onCollapse

public TreeFactory onCollapse(Consumer<TreeEvent> consumer)
Creates a `TreeListener` and registers it for the collapsed event. If the receiver is collapsed by the user the given consumer is invoked. The `TreeEvent` is passed to the consumer.

Parameters:
  `consumer` - the consumer whose accept method is called
Returns:
  this
See Also:
  - `Tree.addTreeListener(TreeListener)`
  - `SelectionListener.widgetSelectedAdapter(Consumer)`

### onExpand

public TreeFactory onExpand(Consumer<TreeEvent> consumer)
Creates a `TreeListener` and registers it for the expanded event. If the receiver is expanded by the user the given consumer is invoked. The `TreeEvent` is passed to the consumer.

Parameters:
  `consumer` - the consumer whose accept method is called
Returns:
  this
See Also:
  - `Tree.addTreeListener(TreeListener)`
  - `SelectionListener.widgetSelectedAdapter(Consumer)`