From 4f5ac43a0680dd7e41dd99d74fb61d45093430fd Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Wed, 28 Nov 2018 11:46:13 -0800 Subject: [PATCH] corefx - updated context menu logic to be contained within classes, fixed issue with service display in details panel for nodes with no default services --- corefx/src/main/java/com/core/Controller.java | 6 +- .../core/graph/AbstractNodeContextMenu.java | 22 ++++++ .../core/graph/CorePopupGraphMousePlugin.java | 68 ++++--------------- .../java/com/core/graph/EmaneContextMenu.java | 23 +++++++ .../java/com/core/graph/GraphContextMenu.java | 22 ++++++ .../java/com/core/graph/LinkContextMenu.java | 21 ++++++ .../java/com/core/graph/NodeContextMenu.java | 21 ++++++ .../java/com/core/graph/WlanContextMenu.java | 23 +++++++ .../main/java/com/core/ui/NodeDetails.java | 21 +++--- .../com/core/ui/dialogs/NodeEmaneDialog.java | 5 +- 10 files changed, 162 insertions(+), 70 deletions(-) create mode 100644 corefx/src/main/java/com/core/graph/AbstractNodeContextMenu.java create mode 100644 corefx/src/main/java/com/core/graph/EmaneContextMenu.java create mode 100644 corefx/src/main/java/com/core/graph/GraphContextMenu.java create mode 100644 corefx/src/main/java/com/core/graph/LinkContextMenu.java create mode 100644 corefx/src/main/java/com/core/graph/NodeContextMenu.java create mode 100644 corefx/src/main/java/com/core/graph/WlanContextMenu.java diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index 537e7e18..3c611921 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -184,8 +184,10 @@ public class Controller implements Initializable { Integer nodeId = nodeIdOptional.get(); MobilityConfig mobilityConfig = mobilityConfigMap.get(nodeId); CoreNode node = networkGraph.getVertex(nodeId); - mobilityPlayer.show(node, mobilityConfig); - Platform.runLater(() -> bottom.getChildren().add(mobilityPlayer)); + if (node != null) { + mobilityPlayer.show(node, mobilityConfig); + Platform.runLater(() -> bottom.getChildren().add(mobilityPlayer)); + } } } diff --git a/corefx/src/main/java/com/core/graph/AbstractNodeContextMenu.java b/corefx/src/main/java/com/core/graph/AbstractNodeContextMenu.java new file mode 100644 index 00000000..d04097c2 --- /dev/null +++ b/corefx/src/main/java/com/core/graph/AbstractNodeContextMenu.java @@ -0,0 +1,22 @@ +package com.core.graph; + +import com.core.Controller; +import com.core.data.CoreNode; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.control.MenuItem; + +abstract class AbstractNodeContextMenu extends GraphContextMenu { + final CoreNode coreNode; + + AbstractNodeContextMenu(Controller controller, CoreNode coreNode) { + super(controller); + this.coreNode = coreNode; + } + + void addMenuItem(String text, EventHandler handler) { + MenuItem menuItem = new MenuItem(text); + menuItem.setOnAction(handler); + getItems().add(menuItem); + } +} diff --git a/corefx/src/main/java/com/core/graph/CorePopupGraphMousePlugin.java b/corefx/src/main/java/com/core/graph/CorePopupGraphMousePlugin.java index 5803e7bc..3b96c9fa 100644 --- a/corefx/src/main/java/com/core/graph/CorePopupGraphMousePlugin.java +++ b/corefx/src/main/java/com/core/graph/CorePopupGraphMousePlugin.java @@ -9,17 +9,12 @@ import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor; import edu.uci.ics.jung.algorithms.layout.Layout; import edu.uci.ics.jung.visualization.control.EditingPopupGraphMousePlugin; import javafx.application.Platform; -import javafx.event.ActionEvent; -import javafx.event.EventHandler; import javafx.scene.control.ContextMenu; -import javafx.scene.control.MenuItem; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.awt.event.MouseEvent; import java.awt.geom.Point2D; -import java.util.ArrayList; -import java.util.List; public class CorePopupGraphMousePlugin extends EditingPopupGraphMousePlugin { private static final Logger logger = LogManager.getLogger(); @@ -45,15 +40,13 @@ public class CorePopupGraphMousePlugin extends EditingPopupGraphMousePlugi final CoreNode node = pickSupport.getVertex(graphLayout, p.getX(), p.getY()); final CoreLink link = pickSupport.getEdge(graphLayout, p.getX(), p.getY()); - ContextMenu contextMenu = new ContextMenu(); - - // edge picked + final ContextMenu contextMenu; if (node != null) { - List menuItems = handleNodeContext(node); - contextMenu.getItems().addAll(menuItems); + contextMenu = handleNodeContext(node); } else if (link != null) { - List menuItems = handleLinkContext(link); - contextMenu.getItems().addAll(menuItems); + contextMenu = new LinkContextMenu(controller, link); + } else { + contextMenu = new ContextMenu(); } if (!contextMenu.getItems().isEmpty()) { @@ -63,60 +56,23 @@ public class CorePopupGraphMousePlugin extends EditingPopupGraphMousePlugi } } - private MenuItem createMenuItem(String text, EventHandler handler) { - MenuItem menuItem = new MenuItem(text); - menuItem.setOnAction(handler); - return menuItem; - } - - private List handleNodeContext(final CoreNode node) { - boolean isRunning = controller.getCoreClient().isRunning(); - - List menuItems = new ArrayList<>(); - + private ContextMenu handleNodeContext(final CoreNode node) { + ContextMenu contextMenu = new ContextMenu(); switch (node.getType()) { case NodeType.DEFAULT: - menuItems.add(createMenuItem("Services", - event -> controller.getNodeServicesDialog().showDialog(node))); + contextMenu = new NodeContextMenu(controller, node); break; case NodeType.WLAN: - menuItems.add(createMenuItem("WLAN Settings", - event -> controller.getNodeWlanDialog().showDialog(node))); - menuItems.add(createMenuItem("Mobility", - event -> controller.getMobilityDialog().showDialog(node))); - menuItems.add(createMenuItem("Link MDRs", - event -> networkGraph.linkMdrs(node))); + contextMenu = new WlanContextMenu(controller, node); break; case NodeType.EMANE: - menuItems.add(createMenuItem("EMANE Settings", - event -> controller.getNodeEmaneDialog().showDialog(node))); - menuItems.add(createMenuItem("Mobility", - event -> controller.getMobilityDialog().showDialog(node))); - menuItems.add(createMenuItem("Link MDRs", - event -> networkGraph.linkMdrs(node))); + contextMenu = new EmaneContextMenu(controller, node); break; default: + logger.warn("no context menu for node: {}", node.getType()); break; } - if (!isRunning) { - menuItems.add(createMenuItem("Delete Node", - event -> controller.deleteNode(node))); - } - - return menuItems; - } - - private List handleLinkContext(final CoreLink link) { - boolean isRunning = controller.getCoreClient().isRunning(); - - List menuItems = new ArrayList<>(); - - if (!isRunning) { - menuItems.add(createMenuItem("Delete Link", - event -> networkGraph.removeLink(link))); - } - - return menuItems; + return contextMenu; } } diff --git a/corefx/src/main/java/com/core/graph/EmaneContextMenu.java b/corefx/src/main/java/com/core/graph/EmaneContextMenu.java new file mode 100644 index 00000000..be567d0c --- /dev/null +++ b/corefx/src/main/java/com/core/graph/EmaneContextMenu.java @@ -0,0 +1,23 @@ +package com.core.graph; + +import com.core.Controller; +import com.core.data.CoreNode; + +class EmaneContextMenu extends AbstractNodeContextMenu { + EmaneContextMenu(Controller controller, CoreNode coreNode) { + super(controller, coreNode); + setup(); + } + + private void setup() { + addMenuItem("EMANE Settings", + event -> controller.getNodeEmaneDialog().showDialog(coreNode)); + if (!controller.getCoreClient().isRunning()) { + addMenuItem("Mobility", + event -> controller.getMobilityDialog().showDialog(coreNode)); + addMenuItem("Link MDRs", + event -> controller.getNetworkGraph().linkMdrs(coreNode)); + addMenuItem("Delete Node", event -> controller.deleteNode(coreNode)); + } + } +} diff --git a/corefx/src/main/java/com/core/graph/GraphContextMenu.java b/corefx/src/main/java/com/core/graph/GraphContextMenu.java new file mode 100644 index 00000000..c93ecba9 --- /dev/null +++ b/corefx/src/main/java/com/core/graph/GraphContextMenu.java @@ -0,0 +1,22 @@ +package com.core.graph; + +import com.core.Controller; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.control.ContextMenu; +import javafx.scene.control.MenuItem; + +abstract class GraphContextMenu extends ContextMenu { + final Controller controller; + + GraphContextMenu(Controller controller) { + super(); + this.controller = controller; + } + + void addMenuItem(String text, EventHandler handler) { + MenuItem menuItem = new MenuItem(text); + menuItem.setOnAction(handler); + getItems().add(menuItem); + } +} diff --git a/corefx/src/main/java/com/core/graph/LinkContextMenu.java b/corefx/src/main/java/com/core/graph/LinkContextMenu.java new file mode 100644 index 00000000..d9b11115 --- /dev/null +++ b/corefx/src/main/java/com/core/graph/LinkContextMenu.java @@ -0,0 +1,21 @@ +package com.core.graph; + +import com.core.Controller; +import com.core.data.CoreLink; + +class LinkContextMenu extends GraphContextMenu { + final CoreLink coreLink; + + LinkContextMenu(Controller controller, CoreLink coreLink) { + super(controller); + this.coreLink = coreLink; + setup(); + } + + private void setup() { + if (!controller.getCoreClient().isRunning()) { + addMenuItem("Delete Link", + event -> controller.getNetworkGraph().removeLink(coreLink)); + } + } +} diff --git a/corefx/src/main/java/com/core/graph/NodeContextMenu.java b/corefx/src/main/java/com/core/graph/NodeContextMenu.java new file mode 100644 index 00000000..a328b3da --- /dev/null +++ b/corefx/src/main/java/com/core/graph/NodeContextMenu.java @@ -0,0 +1,21 @@ +package com.core.graph; + +import com.core.Controller; +import com.core.data.CoreNode; + +class NodeContextMenu extends AbstractNodeContextMenu { + NodeContextMenu(Controller controller, CoreNode coreNode) { + super(controller, coreNode); + setup(); + } + + private void setup() { + if (controller.getCoreClient().isRunning()) { + addMenuItem("Manage Services", event -> { + }); + } else { + addMenuItem("Services", event -> controller.getNodeServicesDialog().showDialog(coreNode)); + addMenuItem("Delete Node", event -> controller.deleteNode(coreNode)); + } + } +} diff --git a/corefx/src/main/java/com/core/graph/WlanContextMenu.java b/corefx/src/main/java/com/core/graph/WlanContextMenu.java new file mode 100644 index 00000000..6fe29132 --- /dev/null +++ b/corefx/src/main/java/com/core/graph/WlanContextMenu.java @@ -0,0 +1,23 @@ +package com.core.graph; + +import com.core.Controller; +import com.core.data.CoreNode; + +class WlanContextMenu extends AbstractNodeContextMenu { + WlanContextMenu(Controller controller, CoreNode coreNode) { + super(controller, coreNode); + setup(); + } + + private void setup() { + addMenuItem("WLAN Settings", + event -> controller.getNodeWlanDialog().showDialog(coreNode)); + if (!controller.getCoreClient().isRunning()) { + addMenuItem("Mobility", + event -> controller.getMobilityDialog().showDialog(coreNode)); + addMenuItem("Link MDRs", + event -> controller.getNetworkGraph().linkMdrs(coreNode)); + addMenuItem("Delete Node", event -> controller.deleteNode(coreNode)); + } + } +} diff --git a/corefx/src/main/java/com/core/ui/NodeDetails.java b/corefx/src/main/java/com/core/ui/NodeDetails.java index 19f3b350..c1e9751c 100644 --- a/corefx/src/main/java/com/core/ui/NodeDetails.java +++ b/corefx/src/main/java/com/core/ui/NodeDetails.java @@ -22,6 +22,7 @@ import javafx.scene.layout.GridPane; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.Collections; import java.util.Set; public class NodeDetails extends ScrollPane { @@ -105,16 +106,18 @@ public class NodeDetails extends ScrollPane { // display custom or default node services Set services = node.getServices(); if (services.isEmpty()) { - services = controller.getDefaultServices().get(node.getModel()); + services = controller.getDefaultServices().getOrDefault(node.getModel(), Collections.emptySet()); } - addSeparator(); - addLabel("Services"); - JFXListView listView = new JFXListView<>(); - listView.setMouseTransparent(true); - listView.setFocusTraversable(false); - listView.getItems().setAll(services); - gridPane.add(listView, 0, index++, 2, 1); - + if (!services.isEmpty()) { + addSeparator(); + addLabel("Services"); + JFXListView listView = new JFXListView<>(); + listView.setMouseTransparent(true); + listView.setFocusTraversable(false); + listView.getItems().setAll(services); + gridPane.add(listView, 0, index++, 2, 1); + } + JFXScrollPane.smoothScrolling(scrollPane); } diff --git a/corefx/src/main/java/com/core/ui/dialogs/NodeEmaneDialog.java b/corefx/src/main/java/com/core/ui/dialogs/NodeEmaneDialog.java index 80a04ae5..8c34c399 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/NodeEmaneDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/NodeEmaneDialog.java @@ -38,9 +38,8 @@ public class NodeEmaneDialog extends StageDialog { } public void setModels(List models) { - modelCombo.getItems().clear(); - models.stream().sorted() - .forEach(model -> modelCombo.getItems().add(model)); + models.sort(String::compareTo); + modelCombo.getItems().setAll(models); modelCombo.getSelectionModel().selectFirst(); }