Esempio n. 1
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");
    }
  }
Esempio n. 2
0
  private static void vmNodeHidingListener(
      PopOver popover, VM vmObject, 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
      VM oldVM = vmObject;

      // only update with it's not in edit mode
      if (!toggleBtn.isSelected()) {
        VM newVmObject = new VM();
        TreeMap<String, String> newInterfaces = new TreeMap<String, 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 vmName = ((TextField) childNode.get(i + 1)).getText();
                  // only validate name if it's different than
                  // the current one
                  if (!oldVM.getName().equals(vmName)) {
                    // make sure name input is a valid name
                    if (Validator.validateName(vmName)) {
                      newVmObject.setName(vmName);
                    } else {
                      // if input is not valid, warn user
                      // and keep the old one
                      creatAlert(((Label) childNode.get(i)).getText(), "VM");
                      newVmObject.setName(oldVM.getName());
                    }
                  } else {
                    // set it to old name if it did not
                    // change
                    newVmObject.setName(oldVM.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("OS.*")) {
                  String vmOs = ((TextField) childNode.get(i + 1)).getText();
                  if (Validator.validateOs(vmOs)) {
                    newVmObject.setOs(vmOs);
                  } else {
                    creatAlert(((Label) childNode.get(i)).getText(), "VM");
                    newVmObject.setOs(oldVM.getOs());
                  }
                } else if (((Label) childNode.get(i)).getText().matches("Ver.*")) {
                  String vmVer = ((TextField) childNode.get(i + 1)).getText();
                  if (Validator.validateVer(vmVer)) {
                    newVmObject.setVer(Double.parseDouble(vmVer));
                  } else {
                    creatAlert(((Label) childNode.get(i)).getText(), "VM");
                    newVmObject.setVer(oldVM.getVer());
                  }
                } else if (((Label) childNode.get(i)).getText().matches("Src.*")) {
                  String vmSrc = ((TextField) childNode.get(i + 1)).getText();
                  if (Validator.validateSrc(vmSrc)) {
                    newVmObject.setSrc(vmSrc);
                  } else {
                    creatAlert(((Label) childNode.get(i)).getText(), "VM");
                    newVmObject.setSrc(oldVM.getSrc());
                  }
                } else if (((Label) childNode.get(i)).getText().matches("(\\w+?).(\\w+?\\d+?.*)")) {
                  String ipLabel = ((Label) childNode.get(i)).getText();
                  String vmIp = ((TextField) childNode.get(i + 1)).getText().trim();
                  // does the eth# interface already exist?
                  if (oldVM.getInterfaces().containsKey(ipLabel)) {
                    // is the new ip value equal to the old
                    // one
                    if (!oldVM.getInterfaces().get(ipLabel).equals(vmIp)) {
                      // if it's a new ip, validate and
                      // set it
                      if (Validator.validateIp(vmIp)) {
                        newInterfaces.put(ipLabel, vmIp);
                      } else {
                        // if not, alert user and set to
                        // old ip
                        creatAlert(ipLabel, "VM");
                        newInterfaces.put(ipLabel, oldVM.getInterfaces().get(ipLabel));
                      }
                    } else {
                      // if ip value didn't change just
                      // set to old value
                      newInterfaces.put(ipLabel, oldVM.getInterfaces().get(ipLabel));
                    }
                  } else {
                    // if that eth# doesn't exist yet
                    // validate it and set it
                    // only set the new eth# if the new ip
                    // value is valid
                    if (!vmIp.isEmpty()) {
                      if (Validator.validateIp(vmIp)) {
                        newInterfaces.put(ipLabel, vmIp);
                      } else {
                        // tell user there was an error
                        // and don't insert the new eth#
                        // interface
                        creatAlert(ipLabel, "VM");
                      }
                    }
                  }
                }
              }
            }
          }
        }
        newVmObject.setInterfaces(newInterfaces);
        Data.vmMap.replace(oldVM.getName(), newVmObject);
        // 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 VM
        // Object name
        LinkedHashMap<String, VM> updatedMap =
            Data.replaceVMKey(Data.vmMap, oldVM.getName(), newVmObject.getName());
        Data.vmMap = updatedMap;
        draw(canvas, contextMenu);
      }
    } catch (IndexOutOfBoundsException e) {
      System.out.println("Something went Wrong");
    }
  }