Class AbstractTreePart<C extends org.eclipse.emf.ecore.EObject,E extends TreeElement<C>>
- Type Parameters:
C- container typeE- tree element type
TreeElement keeps it. The part builds the tree from the current
container, updates it when the container or its fields change, and sends the selection to other
parts through PlatformTopics.
How to use
Extend the class and register the subclass as a part inApplication.e4xmi. e4 creates
the part, injects the fields and calls postConstruct(Composite). The subclass implements
three methods:
getContainer()returns the current container, usually from the application data;createRootTreeElement(EObject)creates the root element for the container;getChildren(TreeElement)returns the children of an element.
initializeTreeElementTypesToNamesMap(Map), the container fields that update the tree in
initializeDependentContainerListFields(List) and
initializeDependentContainerValueFields(List), toolbar buttons in
createToolbar(Composite), the context menu in createMenuItems(Menu, TreeElement).
Tree elements
UseTreeElement as it is, without a subclass. A group is an element without an object
(new TreeElement<>(type, container)), a model object is an element with one
(new TreeElement<>(type, container, object)).
Updates
The tree is rebuilt when a new container is loaded (PlatformTopics.NEW_SCENARIO,
onNewContainer()). It is also refreshed when a dependent field of the container changes.
The subclass lists these fields: list features in
initializeDependentContainerListFields(List), single-value features in
initializeDependentContainerValueFields(List); the part observes them on the current
container. After other changes of the model, call refresh().
treeViewer.refresh() only repaints the labels and keeps the old children.
Selection
When the user selects an element,onSelectionChanged(TreeElement) sends events about
it. The part selects an element itself when another part sends
PlatformTopics.PROPERTY_SELECTION_CHANGED with an object or a type
(onExternalSelection(Object)), or PlatformTopics.CHANGE_SELECTED_TREE_ELEMENT
with a type. Such a selection does not call onSelectionChanged(TreeElement) and does not
send events. Only one element can be selected.
Services and subscriptions
The protected fields with@Inject are set before postConstruct(Composite); a
subclass uses them and does not declare its own fields for the same services.
RedrawRestrictionManager has no @Optional: the application registers it as a
service in its Activator, otherwise e4 cannot create the part. Subscribe with
subscribe(String, Consumer): the part removes such subscriptions, the observers of the
container fields and its listener of redrawRestrictionManager when the tree is disposed.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.eclipse.e4.ui.model.application.MApplicationprotected org.eclipse.e4.core.commands.ECommandServiceprotected org.eclipse.core.databinding.observable.value.IObservableValue<C> protected org.eclipse.e4.core.services.events.IEventBrokerprotected org.eclipse.e4.core.commands.EHandlerServiceprotected com.amalgamasimulation.desktop.utils.MessageManagerprotected org.eclipse.e4.ui.workbench.modeling.EModelServiceprotected org.eclipse.e4.ui.workbench.modeling.EPartServiceprotected RedrawRestrictionManagerprotected org.eclipse.swt.widgets.ToolBar -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidCollapses the tree so that only the root and its children are visible.protected voidcreateContents(org.eclipse.swt.widgets.Composite parent) protected voidcreateMenuItems(org.eclipse.swt.widgets.Menu menu, E treeElement) Adds items to the context menu of an element.protected abstract EcreateRootTreeElement(C container) Creates the root element for a new container.protected voidcreateToolbar(org.eclipse.swt.widgets.Composite parent) Createstoolbarwith two buttons: "Expand all" callsexpandAll(), "Collapse all" callscollapseAll().protected voidExpands all elements.getChildren(E treeElement) Returns the children of an element in the order they are shown.protected abstract CReturns the current container.protected ObjectgetPropertySelection(E treeElement) Returns the object sent inPlatformTopics.PROPERTY_SELECTION_CHANGEDfor a selected element: the container for the root,TreeElement.getObject()for the other elements.protected org.eclipse.swt.graphics.ColorgetTreeElementColor(E treeElement) Returns the text color of an element, for example grey for an object that is turned off.protected org.eclipse.swt.graphics.FontgetTreeElementFont(E treeElement) Returns the font of an element, for example bold for the active element.protected org.eclipse.swt.graphics.ImagegetTreeElementIcon(E treeElement) Returns the icon of an element: the image atAbstractTreeElementType.getIconPath()of its type.protected StringgetTreeElementName(E treeElement) Returns the text of an element: the function frominitializeTreeElementTypesToNamesMap(Map)for its type, otherwise the type name.protected voidinitializeDependentContainerListFields(List<org.eclipse.emf.ecore.EStructuralFeature> list) Fills the list features of the container whose change refreshes the tree.protected voidinitializeDependentContainerValueFields(List<org.eclipse.emf.ecore.EStructuralFeature> list) Fills the single-value features of the container whose change refreshes the tree.protected voidFills the names of elements by type.protected voidonDoubleClick(E treeElement) Called when the user double-clicks an element.protected voidonExternalSelection(Object eventData) Selects the element for an object selected in another part.protected voidShows the current container.protected voidonSelectionChanged(E treeElement) Called when the user selects an element.voidpostConstruct(org.eclipse.swt.widgets.Composite parent) final voidrefresh()AsksgetChildren(TreeElement)for the children of every element again and updates the names, icons, fonts and colors.protected final booleanselectTreeElement(Predicate<E> predicate) final voidsetRedrawRestricted(boolean redrawRestricted) Turns the redraw restriction on or off.protected final <T> voidSubscribes a handler to a topic throughmessageManagerand unsubscribes it when the tree is disposed.withCount(Function<E, Collection<?>> collection) Returns a name in the form "Type name (N)", where N is the size of the collection the function returns for the element.
-
Field Details
-
eventBroker
@Inject protected org.eclipse.e4.core.services.events.IEventBroker eventBroker -
messageManager
@Inject protected com.amalgamasimulation.desktop.utils.MessageManager messageManager -
modelService
@Inject protected org.eclipse.e4.ui.workbench.modeling.EModelService modelService -
application
@Inject protected org.eclipse.e4.ui.model.application.MApplication application -
partService
@Inject protected org.eclipse.e4.ui.workbench.modeling.EPartService partService -
handlerService
@Inject protected org.eclipse.e4.core.commands.EHandlerService handlerService -
commandService
@Inject protected org.eclipse.e4.core.commands.ECommandService commandService -
redrawRestrictionManager
-
currentContainerObservable
protected org.eclipse.core.databinding.observable.value.IObservableValue<C extends org.eclipse.emf.ecore.EObject> currentContainerObservable -
treeViewer
-
toolbar
protected org.eclipse.swt.widgets.ToolBar toolbar
-
-
Constructor Details
-
AbstractTreePart
public AbstractTreePart()
-
-
Method Details
-
getContainer
Returns the current container. The part calls it once when it is created and then on eachPlatformTopics.NEW_SCENARIOevent. The event data is not used, so the container must already be in the application data when the event is sent.- Returns:
- the current container, or
nullwhen none is open; the tree is empty then
-
createRootTreeElement
Creates the root element for a new container. The root is the only element of the first level.- Parameters:
container- the container fromgetContainer(), notnull; the method is not called when there is no container- Returns:
- the root element, not
null
-
getChildren
Returns the children of an element in the order they are shown. The tree calls it for every element of the tree, collapsed ones included, when a container is loaded and on eachrefresh(). Between these calls the tree keeps the children it got last time.The method may create new elements on each call: equal elements keep their expanded and selected state. An element must not be equal to an element under another parent.
- Parameters:
treeElement- the element whose children are needed, notnull- Returns:
- the children, an empty list for a leaf; not
null
-
postConstruct
@PostConstruct public void postConstruct(org.eclipse.swt.widgets.Composite parent) -
createToolbar
protected void createToolbar(org.eclipse.swt.widgets.Composite parent) Createstoolbarwith two buttons: "Expand all" callsexpandAll(), "Collapse all" callscollapseAll().- Parameters:
parent- the composite above the tree
-
createContents
protected void createContents(org.eclipse.swt.widgets.Composite parent) -
initializeTreeElementTypesToNamesMap
protected void initializeTreeElementTypesToNamesMap(Map<AbstractTreeElementType, Function<E, String>> map) Fills the names of elements by type. Called once when the part is created. An element whose type is not in the map showsAbstractTreeElementType.getName().Each function gets the element and runs every time the tree updates its label, so the name follows the model after
refresh(). For a group with a count, usewithCount(Function).- Parameters:
map- an empty map to fill
-
withCount
Returns a name in the form "Type name (N)", where N is the size of the collection the function returns for the element.- Parameters:
collection- returns the objects to count for the element; notnulland must not returnnull- Returns:
- a name function for
initializeTreeElementTypesToNamesMap(Map)
-
getTreeElementName
Returns the text of an element: the function frominitializeTreeElementTypesToNamesMap(Map)for its type, otherwise the type name.- Parameters:
treeElement- the element, notnull- Returns:
- the text of the element
-
getTreeElementIcon
Returns the icon of an element: the image atAbstractTreeElementType.getIconPath()of its type.PlatformImageslooks for the file first in the bundle of the subclass, then in the bundles it has already loaded images from.- Parameters:
treeElement- the element, notnull- Returns:
- the icon of the element
-
getTreeElementFont
Returns the font of an element, for example bold for the active element. Returnsnullby default, which means the tree font.- Parameters:
treeElement- the element, notnull- Returns:
- the font, or
nullfor the tree font
-
getTreeElementColor
Returns the text color of an element, for example grey for an object that is turned off. Returnsnullby default, which means the tree color.- Parameters:
treeElement- the element, notnull- Returns:
- the text color, or
nullfor the tree color
-
initializeDependentContainerListFields
protected void initializeDependentContainerListFields(List<org.eclipse.emf.ecore.EStructuralFeature> list) Fills the list features of the container whose change refreshes the tree.The features belong to the container class itself. The part observes them on the current container. Adding, removing or moving an object in the list calls
refresh(); a change inside an object of the list does not.- Parameters:
list- an empty list to fill
-
initializeDependentContainerValueFields
protected void initializeDependentContainerValueFields(List<org.eclipse.emf.ecore.EStructuralFeature> list) Fills the single-value features of the container whose change refreshes the tree.The features belong to the container class itself. The part observes them on the current container; a new value calls
refresh().- Parameters:
list- an empty list to fill
-
refresh
public final void refresh()AsksgetChildren(TreeElement)for the children of every element again and updates the names, icons, fonts and colors. Expanded and selected elements stay as they were if equal elements are still in the tree.Call this method after a change of the model that the dependent fields do not cover.
treeViewer.refresh()is not enough: it repaints the labels but keeps the old children. -
setRedrawRestricted
public final void setRedrawRestricted(boolean redrawRestricted) Turns the redraw restriction on or off. While it is on,refresh()only remembers the request; when it is turned off, one refresh runs if there was a request.The part already listens to
redrawRestrictionManager. An application with its own source of the restriction subscribes this method to it.- Parameters:
redrawRestricted-trueto restrict redrawing,falseto allow it
-
onNewContainer
protected void onNewContainer()Shows the current container. Takes it fromgetContainer(), creates the root withcreateRootTreeElement(EObject)(no root fornull, the tree is empty), switches the observers of the dependent fields to the container, rebuilds the tree and collapses it withcollapseAll().Called on each
PlatformTopics.NEW_SCENARIOevent and once when the part is created. -
onSelectionChanged
Called when the user selects an element. By default sends witheventBroker.send:PlatformTopics.CHANGE_VISIBILITY_TABLE_PAGEwith the element type;PlatformTopics.CHANGE_SELECTED_OBJECT_IN_TABLE_PAGEwithTreeElement.getObject(),nullfor a group;PlatformTopics.PROPERTY_SELECTION_CHANGEDwithgetPropertySelection(TreeElement), if it is notnull.
- Parameters:
treeElement- the selected element, notnull
-
getPropertySelection
Returns the object sent inPlatformTopics.PROPERTY_SELECTION_CHANGEDfor a selected element: the container for the root,TreeElement.getObject()for the other elements. For a group it isnull, and the event is not sent.- Parameters:
treeElement- the selected element, notnull- Returns:
- the object to send, or
nullto send nothing
-
onExternalSelection
Selects the element for an object selected in another part. Called on eachPlatformTopics.PROPERTY_SELECTION_CHANGEDevent, including the ones this part sends. For a type, the method selects the first element of this type; for another object, the first element whoseTreeElement.getObject()equals it.- Parameters:
eventData- the event data;nullis ignored
-
selectTreeElement
-
onDoubleClick
Called when the user double-clicks an element. Does nothing by default.- Parameters:
treeElement- the element, notnull
-
createMenuItems
Adds items to the context menu of an element. Called each time the user opens the menu over an element. Does nothing by default.- Parameters:
menu- the menu to filltreeElement- the element under the mouse, notnull
-
expandAll
protected void expandAll()Expands all elements. Called by the "Expand all" button. -
collapseAll
protected void collapseAll()Collapses the tree so that only the root and its children are visible. Called by the "Collapse all" button and after each new container. -
subscribe
Subscribes a handler to a topic throughmessageManagerand unsubscribes it when the tree is disposed. The handler gets the event data cast toT.- Type Parameters:
T- type of the event data- Parameters:
topic- the topichandler- the handler of the event data
-