Example #1
0
  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;
  }
Example #2
0
  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;
  }
Example #3
0
  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");
    }
  }
Example #4
0
  public static void draw(Pane canvas, ContextMenu contextMenu) {
    System.out.println("Number of Vm's Present " + application.Data.vmMap.keySet().size());
    System.out.println("Number of Hub's Present " + application.Data.hubMap.keySet().size());
    // the pane should be cleared each time
    canvas.getChildren().clear();

    // if(!Data.vmMap.isEmpty() && !Data.hubMap.isEmpty()) {
    // if(!(Data.hubMap.size() == 0)) {
    canvas.getChildren().add(createVlanNode("V2", canvas, contextMenu));
    // }

    // we don't actually want to change the value of Data.hubStartPosY
    // instead we initially set our tempPosY to the startPos and alter that
    int tempPosX = Data.hubStartPosX;
    int tempPosY = Data.hubStartPosY;
    // Draw a Blue rectangle for each hub
    for (Map.Entry<String, HUB> hubEntry : application.Data.hubMap.entrySet()) {

      String currentHubName = hubEntry.getKey();
      HUB currentHub = application.Data.hubMap.get(currentHubName);
      currentHub.setPosX(tempPosX);
      currentHub.setPosY(tempPosY);
      canvas.getChildren().add(application.Graphics.createHUBNode(currentHub, canvas, contextMenu));
      // cashe wither the hub has connections
      // if it don't then theres no need to draw the lines
      boolean haveConnections = !currentHub.getInfs().isEmpty();
      // ----- Draws a horizontal line from the hub to the middle of the space
      // between hubs and vms
      if (haveConnections) {
        drawLine(canvas, tempPosX + 100, tempPosY + 50, tempPosX + 150, tempPosY + 50);
      }

      // Draw a Red rectangle for each vm
      tempPosX += 200;
      for (Map.Entry<String, VM> vmEntry : application.Data.vmMap.entrySet()) {
        String currentVMName = vmEntry.getKey();
        VM currentVM = application.Data.vmMap.get(currentVMName);
        for (Map.Entry<String, String> vmInterface : currentVM.getInterfaces().entrySet()) {
          // If the the first three octals of the vm's interface match
          // the first three octals of the hub's subnet
          // Draw the vm to the left of the hub as they are connected
          int ipClass = Data.getIPClass(application.Data.hubMap.get(currentHubName).getNetmask());
          String replaceRegex = "\\.\\d{1," + String.valueOf(ipClass) + "}\\z";
          if (vmInterface
              .getValue()
              .replaceAll(replaceRegex, "")
              .equals(
                  application.Data.hubMap
                      .get(currentHubName)
                      .getSubnet()
                      .replaceAll(replaceRegex, ""))) {
            application.Data.hubMap
                .get(currentHubName)
                .addInf(currentVM.getName() + "." + vmInterface.getKey());
            currentVM.setPosX(tempPosX);
            currentVM.setPosY(tempPosY);
            canvas
                .getChildren()
                .add(application.Graphics.createVMNode(currentVM, canvas, contextMenu));

            // draws a horizontal line from each vm to the middle of
            // the space between hubs and vms
            if (haveConnections) {
              drawLine(canvas, tempPosX, tempPosY + 50, tempPosX - 50, tempPosY + 50);
            }

            tempPosY += 150;
          }
        }
      }
      if (!haveConnections) {
        tempPosY += 150;
      }

      // draw vertical in the middle of the space between hubs and vms
      if (haveConnections) {
        drawLine(canvas, tempPosX - 50, currentHub.getPosY() + 50, tempPosX - 50, tempPosY - 100);
      }
      // draw horizontal line from the vlan to the hub
      drawLine(
          canvas,
          Data.vlanstartPosX + 100,
          currentHub.getPosY() + 50,
          currentHub.getPosX(),
          currentHub.getPosY() + 50);
      tempPosX += 200;
      // tempPosY += 150;
    }
  }