Skip to content
Snippets Groups Projects
Commit 1f9505f5 authored by Simon Barner's avatar Simon Barner
Browse files

Change context menu interface to work on menu types

* Add base class TreeContextMenuItem
* Change addContextMenuEntry() to work on TreeContextMenuItem subtypes
* Provide generic (and therefore "final") implementation of
  ModelElementUIProvider.createContextMenu()

Issue-Ref: 4322
Issue-Url: af3#4322



Signed-off-by: default avatarSimon Barner <barner@fortiss.org>
parent f21365be
No related branches found
No related tags found
1 merge request!2094322/3797: Editing DSE project
DynamicTreeTableNameProvider.java 3ca45f24b94e97b02313e80b16ba8b370f541541 GREEN
ModelElementTreeViewer.java c27c57ae21b32de790c6fb5a86695dd5952fcf32 GREEN
ModelElementTreeViewer.java af87b0aa1780f3b02a60d09db9676b4e54903be5 YELLOW
......@@ -18,6 +18,7 @@ package org.fortiss.tooling.base.ui.javafx.control.treetableview;
import static javafx.embed.swt.SWTFXUtils.toFXImage;
import static org.fortiss.tooling.kernel.ui.util.KernelUIUtils.getName;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
......@@ -31,6 +32,8 @@ import org.fortiss.tooling.common.ui.javafx.control.treetableview.DynamicTreeVie
import org.fortiss.tooling.kernel.model.INamedElement;
import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
......@@ -77,21 +80,76 @@ public class ModelElementTreeViewer<T extends INamedElement> {
/** {@inheritDoc} */
@Override
public ContextMenu createContextMenu(T element) {
public final ContextMenu createContextMenu(T element) {
ContextMenu menu = new ContextMenu();
contextMenuEntries.forEach(entry -> menu.getItems().add(entry));
for(Class<? extends TreeContextMenuItem> entryType : contextMenuEntryTypes) {
try {
Constructor<? extends TreeContextMenuItem> ctr =
entryType.getConstructor(INamedElement.class);
TreeContextMenuItem entry = ctr.newInstance(element);
if(!entry.isHidden()) {
menu.getItems().add(entry);
entry.setDisable(entry.isDisabled());
}
} catch(Exception e) {
e.printStackTrace();
}
}
return menu;
}
}
/** Base class for {@link MenuItem}s used by {@link ModelElementTreeViewer}s. */
protected static abstract class TreeContextMenuItem extends MenuItem {
/** The model element on which the context menu operates. */
private INamedElement element;
/** Constructor. */
public TreeContextMenuItem(INamedElement element, String label) {
super(label);
init(element);
}
/** Constructor. */
public TreeContextMenuItem(INamedElement element, String label, Node graphic) {
super(label, graphic);
init(element);
}
/** Helper for constructors. */
private void init(INamedElement element) {
this.element = element;
setOnAction(createOnActionHandler());
}
/** Getter for {@link #element}. */
protected INamedElement getElement() {
return element;
}
/** Predicate iff this {@link TreeContextMenuItem} is hidden. */
public abstract boolean isHidden();
/** Predicate when this {@link TreeContextMenuItem} is disabled. */
public abstract boolean isDisabled();
/**
* Factory method for {@link EventHandler} that implements the action triggered by the
* context menu.
*/
protected abstract EventHandler<ActionEvent> createOnActionHandler();
}
/** References the constructed {@link DynamicTreeViewer} "controller". */
protected DynamicTreeViewer<T> dynTreeViewer;
/**
* List of context menu entries that supplied by clients (see
* {@link #addContextMenuEntry(MenuItem)}).
* List of context menu entries types that supplied by clients
* (see{@link #addContextMenuEntry(Class)}).
*/
protected List<MenuItem> contextMenuEntries = new ArrayList<>();
protected List<Class<? extends TreeContextMenuItem>> contextMenuEntryTypes = new ArrayList<>();
/** Constructor. */
public ModelElementTreeViewer(TreeView<T> treeView, T modelRoot,
......@@ -113,15 +171,11 @@ public class ModelElementTreeViewer<T extends INamedElement> {
}
/**
* Method to define context {@link MenuItem}s for items of the underlying content. The given
* function may use the selected element (given as a parameter to the function) to define
* dynamic behavior.
*
* @param menuItem
* {@link MenuItem} and its logic to be added to the context menu.
* Method to define context {@link TreeContextMenuItem}s. Depending on the selected content, the
* editor will show and enable the context menu.
*/
public void addContextMenuEntry(MenuItem menuItem) {
contextMenuEntries.add(menuItem);
public void addContextMenuEntry(Class<? extends TreeContextMenuItem> menuItemType) {
contextMenuEntryTypes.add(menuItemType);
}
/** Update the internal viewer. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment