Ejemplo n.º 1
0
  void loadBlock(Element elem) {
    if (elem.getAttribute("systemName") == null) {
      log.error("unexpected null in systemName " + elem + " " + elem.getAttributes());
      return;
    }
    String sysName = elem.getAttribute("systemName").getValue();
    String userName = null;
    if (elem.getAttribute("userName") != null) {
      userName = elem.getAttribute("userName").getValue();
    }
    if (log.isDebugEnabled()) {
      log.debug("Load block sysName= " + sysName + " userName= "******"Null block!! sysName= " + sysName + ", userName= "******"comment");
    if (c != null) {
      block.setComment(c);
    }
    if (elem.getAttribute("units") != null) {
      block.setMetricUnits(elem.getAttribute("units").getValue().equals("true"));
    } else {
      block.setMetricUnits(false);
    }
    if (elem.getAttribute("length") != null) {
      block.setLength(Float.valueOf(elem.getAttribute("length").getValue()).floatValue());
    }
    if (elem.getAttribute("curve") != null) {
      block.setCurvature(Integer.parseInt((elem.getAttribute("curve")).getValue()));
    }
    List<Element> sensors = elem.getChildren("sensor");
    if (sensors.size() > 1) {
      log.error("More than one sensor present: " + sensors.size());
    }
    if (sensors.size() > 0) {
      // sensor
      String name = sensors.get(0).getAttribute("systemName").getValue();
      block.setSensor(name);
    }
    Element errSensor = elem.getChild("errorSensor");
    if (errSensor != null) {
      // sensor
      String name = errSensor.getAttribute("systemName").getValue();
      // Sensor sensor = InstanceManager.sensorManagerInstance().provideSensor(name);
      block.setErrorSensor(name);
    }
    Element reporter = elem.getChild("reporter");
    if (reporter != null) {
      // sensor
      String name = reporter.getAttribute("systemName").getValue();
      // Sensor sensor = InstanceManager.sensorManagerInstance().provideSensor(name);
      try {
        Reporter rep = InstanceManager.reporterManagerInstance().getReporter(name);
        if (rep != null) {
          block.setReporter(rep);
        }
      } catch (Exception ex) {
        log.error("No Reporter named \"" + name + "\" found. threw exception: " + ex);
      }
      if (reporter.getAttribute("reportCurrent") != null) {
        block.setReportingCurrent(reporter.getAttribute("reportCurrent").getValue().equals("true"));
      } else {
        block.setReportingCurrent(false);
      }
    }
    if (elem.getAttribute("permissive") != null) {
      block.setPermissiveWorking(elem.getAttribute("permissive").getValue().equals("true"));
    } else {
      block.setPermissiveWorking(false);
    }
    if (elem.getAttribute("speedNotch") != null) {
      try {
        block.setBlockSpeed(elem.getAttribute("speedNotch").getValue());
      } catch (jmri.JmriException ex) {
        JOptionPane.showMessageDialog(
            null, ex.getMessage() + "\n" + elem.getAttribute("speedNotch").getValue());
      }
    }

    List<Element> portals = elem.getChildren("portal");
    for (int k = 0; k < portals.size(); k++) {
      block.addPortal(loadPortal(portals.get(k)));
    }

    List<Element> paths = elem.getChildren("path");
    for (int j = 0; j < paths.size(); j++) {
      if (!block.addPath(loadPath(paths.get(j), block))) {
        log.error(
            "load: block \""
                + sysName
                + "\" failed to add path \""
                + paths.get(j).getName()
                + "\" in block \""
                + block.getSystemName()
                + "\"");
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  public void setValueAt(Object value, int row, int col) {
    if (log.isDebugEnabled()) {
      log.debug("setValueAt: row= " + row + ", col= " + col + ", value= " + value);
    }
    if (super.getRowCount() == row) {
      switch (col) {
        case SYSNAMECOL:
          OBlock block = _manager.createNewOBlock((String) value, tempRow[USERNAMECOL]);
          if (block == null) {
            block = _manager.getOBlock(tempRow[USERNAMECOL]);
            String name = (String) value + " / " + tempRow[USERNAMECOL];
            if (block != null) {
              name = block.getDisplayName();
            } else {
              block = _manager.getOBlock((String) value);
              if (block != null) {
                name = block.getDisplayName();
              }
            }
            JOptionPane.showMessageDialog(
                null,
                Bundle.getMessage("CreateDuplBlockErr", name),
                Bundle.getMessage("ErrorTitle"),
                JOptionPane.WARNING_MESSAGE);
            return;
          }
          if (tempRow[SENSORCOL] != null) {
            if (!sensorExists(tempRow[SENSORCOL])) {
              JOptionPane.showMessageDialog(
                  null,
                  Bundle.getMessage("NoSuchSensorErr", tempRow[SENSORCOL]),
                  Bundle.getMessage("ErrorTitle"),
                  JOptionPane.WARNING_MESSAGE);
            }
          }
          block.setComment(tempRow[COMMENTCOL]);
          float len = 0.0f;
          try {
            len = IntlUtilities.floatValue(tempRow[LENGTHCOL]);
          } catch (ParseException e) {
            JOptionPane.showMessageDialog(
                null,
                Bundle.getMessage("BadNumber", tempRow[LENGTHCOL]),
                Bundle.getMessage("ErrorTitle"),
                JOptionPane.WARNING_MESSAGE);
          }
          if (tempRow[UNITSCOL].equals(Bundle.getMessage("cm"))) {
            block.setLength(len * 10.0f);
            block.setMetricUnits(true);
          } else {
            block.setLength(len * 25.4f);
            block.setMetricUnits(false);
          }
          if (tempRow[CURVECOL].equals(noneText)) {
            block.setCurvature(Block.NONE);
          } else if (tempRow[CURVECOL].equals(gradualText)) {
            block.setCurvature(Block.GRADUAL);
          } else if (tempRow[CURVECOL].equals(tightText)) {
            block.setCurvature(Block.TIGHT);
          } else if (tempRow[CURVECOL].equals(severeText)) {
            block.setCurvature(Block.SEVERE);
          }
          block.setPermissiveWorking(
              tempRow[PERMISSIONCOL].equals(Bundle.getMessage("Permissive")));
          block.setBlockSpeedName(tempRow[SPEEDCOL]);

          if (tempRow[ERR_SENSORCOL] != null) {
            if (tempRow[ERR_SENSORCOL].trim().length() > 0) {
              if (!sensorExists(tempRow[ERR_SENSORCOL])) {
                JOptionPane.showMessageDialog(
                    null,
                    Bundle.getMessage("NoSuchSensorErr", tempRow[ERR_SENSORCOL]),
                    Bundle.getMessage("ErrorTitle"),
                    JOptionPane.WARNING_MESSAGE);
              }
            }
          }
          if (tempRow[REPORTERCOL] != null) {
            Reporter rep = null;
            try {
              rep = InstanceManager.reporterManagerInstance().getReporter(tempRow[REPORTERCOL]);
              if (rep != null) {
                block.setReporter(rep);
                block.setReportingCurrent(
                    tempRow[REPORT_CURRENTCOL].equals(Bundle.getMessage("Current")));
              }
            } catch (Exception ex) {
              log.error(
                  "No Reporter named \""
                      + tempRow[REPORTERCOL]
                      + "\" found. threw exception: "
                      + ex);
            }
            if (rep == null) {
              JOptionPane.showMessageDialog(
                  null,
                  Bundle.getMessage("NoSuchReporterErr", tempRow[REPORTERCOL]),
                  Bundle.getMessage("ErrorTitle"),
                  JOptionPane.WARNING_MESSAGE);
            }
            block.setReporter(rep);
          }
          initTempRow();
          fireTableDataChanged();
          return;
        case DELETE_COL: // clear
          initTempRow();
          fireTableRowsUpdated(row, row);
          return;
        case LENGTHCOL:
          try {
            _tempLen = IntlUtilities.floatValue(value.toString());
            if (tempRow[UNITSCOL].equals(Bundle.getMessage("cm"))) {
              _tempLen *= 10f;
            } else {
              _tempLen *= 25.4f;
            }
          } catch (ParseException e) {
            JOptionPane.showMessageDialog(
                null,
                Bundle.getMessage("BadNumber", tempRow[LENGTHCOL]),
                Bundle.getMessage("ErrorTitle"),
                JOptionPane.WARNING_MESSAGE);
          }
          return;
        case UNITSCOL:
          if ((((Boolean) value).booleanValue())) {
            tempRow[UNITSCOL] = Bundle.getMessage("cm");
          } else {
            tempRow[UNITSCOL] = Bundle.getMessage("in");
          }
          fireTableRowsUpdated(row, row);
          return;
        case REPORT_CURRENTCOL:
          if (((Boolean) value).booleanValue()) { // toggle
            tempRow[REPORT_CURRENTCOL] = Bundle.getMessage("Current");
          } else {
            tempRow[REPORT_CURRENTCOL] = Bundle.getMessage("Last");
          }
          return;
        case PERMISSIONCOL:
          if (((Boolean) value).booleanValue()) { // toggle
            tempRow[PERMISSIONCOL] = Bundle.getMessage("Permissive");
          } else {
            tempRow[PERMISSIONCOL] = Bundle.getMessage("Absolute");
          }
          return;
      }
      tempRow[col] = (String) value;
      return;
    }
    String name = sysNameList.get(row);
    OBlock block = _manager.getBySystemName(name);
    switch (col) {
      case USERNAMECOL:
        OBlock b = _manager.getOBlock((String) value);
        if (b != null) {
          JOptionPane.showMessageDialog(
              null,
              Bundle.getMessage("CreateDuplBlockErr", block.getDisplayName()),
              Bundle.getMessage("ErrorTitle"),
              JOptionPane.WARNING_MESSAGE);
          return;
        }
        block.setUserName((String) value);
        fireTableRowsUpdated(row, row);
        return;
      case COMMENTCOL:
        block.setComment((String) value);
        fireTableRowsUpdated(row, row);
        return;
      case STATECOL:
        /* currently STATECOL is not editable.  Maybe allow it and (or not) set the sensor?  Why?
        int state = 0;
        try {
            state = Integer.valueOf((String) value);
        } catch (NumberFormatException nfe) {
        }
        if (state < 0) {
            state = -state;
        }
        block.setState(state % 255);
        fireTableRowsUpdated(row, row); */
        return;
      case SENSORCOL:
        if (!block.setSensor((String) value)) {
          JOptionPane.showMessageDialog(
              null,
              Bundle.getMessage("NoSuchSensorErr", (String) value),
              Bundle.getMessage("ErrorTitle"),
              JOptionPane.WARNING_MESSAGE);
        }
        fireTableRowsUpdated(row, row);
        return;
      case LENGTHCOL:
        try {
          float len = IntlUtilities.floatValue(value.toString());
          if (block.isMetric()) {
            block.setLength(len * 10.0f);
          } else {
            block.setLength(len * 25.4f);
          }
          fireTableRowsUpdated(row, row);
        } catch (ParseException e) {
          JOptionPane.showMessageDialog(
              null,
              Bundle.getMessage("BadNumber", value),
              Bundle.getMessage("ErrorTitle"),
              JOptionPane.WARNING_MESSAGE);
        }
        return;
      case UNITSCOL:
        block.setMetricUnits(((Boolean) value).booleanValue());
        fireTableRowsUpdated(row, row);
        return;
      case CURVECOL:
        String cName = (String) value;
        if (cName.equals(noneText)) {
          block.setCurvature(Block.NONE);
        } else if (cName.equals(gradualText)) {
          block.setCurvature(Block.GRADUAL);
        } else if (cName.equals(tightText)) {
          block.setCurvature(Block.TIGHT);
        } else if (cName.equals(severeText)) {
          block.setCurvature(Block.SEVERE);
        }
        fireTableRowsUpdated(row, row);
        return;
      case ERR_SENSORCOL:
        boolean err = false;
        try {
          if (((String) value).trim().length() == 0) {
            block.setErrorSensor(null);
            err = true;
          } else {
            err = block.setErrorSensor((String) value);
            fireTableRowsUpdated(row, row);
          }
        } catch (Exception ex) {
          log.error("getSensor(" + (String) value + ") threw exception: " + ex);
        }
        if (err) {
          JOptionPane.showMessageDialog(
              null,
              Bundle.getMessage("NoSuchSensorErr", (String) value),
              Bundle.getMessage("ErrorTitle"),
              JOptionPane.WARNING_MESSAGE);
        }
        fireTableRowsUpdated(row, row);
        return;
      case REPORTERCOL:
        Reporter rep = null;
        try {
          rep = InstanceManager.reporterManagerInstance().getReporter((String) value);
          if (rep != null) {
            block.setReporter(rep);
            fireTableRowsUpdated(row, row);
          }
        } catch (Exception ex) {
          log.error("No Reporter named \"" + (String) value + "\" found. threw exception: " + ex);
        }
        if (rep == null) {
          JOptionPane.showMessageDialog(
              null,
              Bundle.getMessage("NoSuchReporterErr", tempRow[REPORTERCOL]),
              Bundle.getMessage("ErrorTitle"),
              JOptionPane.WARNING_MESSAGE);
        }
        block.setReporter(rep);
        fireTableRowsUpdated(row, row);
        return;
      case REPORT_CURRENTCOL:
        if (block.getReporter() != null) {
          block.setReportingCurrent(((Boolean) value).booleanValue());
          fireTableRowsUpdated(row, row);
        }
        return;
      case PERMISSIONCOL:
        block.setPermissiveWorking(((Boolean) value).booleanValue());
        fireTableRowsUpdated(row, row);
        return;
      case SPEEDCOL:
        block.setBlockSpeedName((String) value);
        fireTableRowsUpdated(row, row);
        return;
      case EDIT_COL:
        _parent.openBlockPathFrame(block.getSystemName());
        return;
      case DELETE_COL:
        deleteBean(block);
        block = null;
        return;
    }
    super.setValueAt(value, row, col);
  }