private static VBox createHUBPopOverContent(HUB hubObject, Pane canvas, ContextMenu contextMenu) { // this content container is everything that is going to go on the // PopOver VBox content = new VBox(5); content.getStyleClass().add("popover-content"); content.setId("contentPane"); // the first row contains the big label and the toggle button addHeader(hubObject.getName(), controller.MyController.btnEdit, 25, content); // just a line separator Separator hr = new Separator(Orientation.HORIZONTAL); hr.minWidth(Control.USE_COMPUTED_SIZE); content.getChildren().add(hr); // the first row of the form (Hub name) addRow("Name: ", hubObject.getName(), 15, content, false); // Here is the same layout as row 1 (Hub subnet) addRow("Subnet:", hubObject.getSubnet(), 15, content, false); // Row 3 (Hub netmask) addRow("Netmask:", hubObject.getNetmask(), 15, content, false); // this will dynamically add rows to the formPane base on the # of inf // entries for (String inf : hubObject.getInfs()) { addRow("Inf:", inf, 15, content, false); } hubBtnListener(content, hubObject, canvas, contextMenu); addButtonRow("Add Interfaces", content, "hub"); return content; }
private static void hubNodeHidingListener( PopOver popover, HUB hubObject, Pane canvas, ContextMenu contextMenu) { try { // eveything in the popover VBox contentPane = (VBox) popover.getContentNode(); // find the header row HBox headerRow = (HBox) contentPane.getChildren().get(0); // find the toggle button ToggleButton toggleBtn = (ToggleButton) headerRow.getChildren().get(1); // the old key used to find the correlating vmObject HUB oldHub = hubObject; // only update with it's not in edit mode if (!toggleBtn.isSelected()) { HUB newHubObject = new HUB(); TreeSet<String> newInterfaces = new TreeSet<String>(); for (Node row : contentPane.getChildren()) { if (row instanceof HBox) { ObservableList<Node> childNode = ((HBox) row).getChildren(); for (int i = 0; i < childNode.size(); i++) { if (childNode.get(i) instanceof Label) { if (((Label) childNode.get(i)).getText().matches("Name.*")) { String hubName = ((TextField) childNode.get(i + 1)).getText(); // only validate name if it's different than // the current one if (!oldHub.getName().equals(hubName)) { // make sure name input is a valid name if (Validator.validateName(hubName)) { newHubObject.setName(hubName); } else { // if input is not valid, warn user // and keep the old one creatAlert(((Label) childNode.get(i)).getText(), "HUB"); newHubObject.setName(oldHub.getName()); } } else { // set it to old name if it did not // change newHubObject.setName(oldHub.getName()); } // same general idea goes for the rest of // the labels // if input is different // then test it's validation before making // changes // if it's not valid then warn the user // else just set it to the old one } else if (((Label) childNode.get(i)).getText().matches("Subnet.*")) { String hubSubnet = ((TextField) childNode.get(i + 1)).getText(); if (!oldHub.getSubnet().equals(hubSubnet)) { if (Validator.validateIp(hubSubnet)) { newHubObject.setSubnet(hubSubnet); } else { creatAlert(((Label) childNode.get(i)).getText(), "HUB"); newHubObject.setSubnet(oldHub.getSubnet()); } } else { newHubObject.setSubnet(oldHub.getSubnet()); } } else if (((Label) childNode.get(i)).getText().matches("Netmask.*")) { String hubNetmask = ((TextField) childNode.get(i + 1)).getText(); if (Validator.validateNetmask(hubNetmask)) { newHubObject.setNetmask(hubNetmask); } else { creatAlert(((Label) childNode.get(i)).getText(), "HUB"); newHubObject.setNetmask(oldHub.getNetmask()); } } else if (((Label) childNode.get(i)).getText().matches("Inf.*")) { String infValue = ((TextField) childNode.get(i + 1)).getText(); // if field is not empty if (!infValue.isEmpty()) { // if the interface is not in the old // ones if (!oldHub.getInfs().contains(infValue)) { String hubNetmask = null; if (!hubObject.getSubnet().equals(hubNetmask)) { hubNetmask = newHubObject.getNetmask(); } else { hubNetmask = oldHub.getNetmask(); } // if new inf value is valid then // insert the new one if (Validator.validateHubInf(infValue) && Validator.validateSubnetting( newHubObject.getNetmask(), infValue, newHubObject.getSubnet())) { newInterfaces.add(infValue); } else { creatAlert("Inf.", "HUB"); } } else { newInterfaces.add(infValue); } } } } } } } newHubObject.setInfs(newInterfaces); Data.hubMap.replace(oldHub.getName(), newHubObject); // here we don't want to simply delete old entry because of the // coordinates // so we update the key to a different key if they change the // Hub Object name LinkedHashMap<String, HUB> updatedMap = Data.replaceHUBKey(Data.hubMap, oldHub.getName(), newHubObject.getName()); Data.hubMap = updatedMap; draw(canvas, contextMenu); } } catch (IndexOutOfBoundsException e) { System.out.println("Something went wrong"); } }
private static Node createHUBNode(HUB hubObject, Pane canvas, ContextMenu contextMenu) { // each hub is represented by a blue rectangle Rectangle node = new Rectangle(Data.nodeLength, Data.nodeWidth); node.setFill(Color.AQUAMARINE); // the hub name in the rectangle Label lnodeName = new Label(hubObject.getName()); // this stackpane stack the label on top of the rectangle to make them // one entity StackPane nodeContainer = new StackPane(); nodeContainer.getChildren().addAll(node, lnodeName); nodeContainer.relocate(hubObject.getPosX(), hubObject.getPosY()); // add a popover when one of the hub is clicked nodeContainer.addEventHandler( MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if (event.getButton() == MouseButton.PRIMARY) { PopOver popOver = new PopOver(createHUBPopOverContent(hubObject, canvas, contextMenu)); popOver.setDetachable(false); popOver.show(nodeContainer); popOver.setOnHiding( new EventHandler<WindowEvent>() { @Override public void handle(WindowEvent e) { hubNodeHidingListener(popOver, hubObject, canvas, contextMenu); } }); } if (event.getButton() == MouseButton.SECONDARY) { // if user right click on a HUB then enable the "delete" // menu contextMenu.getItems().get(2).setDisable(false); // when user select an object to delete contextMenu .getItems() .get(2) .setOnAction( new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { String hubName = hubObject.getName(); TreeSet<String> interfaces = hubObject.getInfs(); for (String inf : interfaces) { Pattern pat = Pattern.compile(Data.hubInfPattern); Matcher matcher = pat.matcher(inf); // delete the VM interface that is connected to // the hub if (matcher.find()) { String vmName = matcher.group(1); String vmEth = matcher.group(2); Data.vmMap.get(vmName).removeInf(vmEth); } } // delete the hub Data.hubMap.remove(hubName); draw(canvas, contextMenu); } }); // disable the "delete option when the popover closes" contextMenu.setOnHidden( new EventHandler<WindowEvent>() { @Override public void handle(WindowEvent e) { contextMenu.getItems().get(2).setDisable(true); } }); } } }); return nodeContainer; }