Example #1
0
 @Override
 public void setValueAt(Object value, int row, int col) {
   switch (col) {
     case USERNAMECOL:
       // Directly changing the username should only be possible if the username was previously
       // null or ""
       // check to see if user name already exists
       if (((String) value).equals("")) {
         value = null;
       } else {
         NamedBean nB = getByUserName((String) value);
         if (nB != null) {
           log.error("User name is not unique " + value);
           String msg = Bundle.getMessage("WarningUserName", new Object[] {("" + value)});
           JOptionPane.showMessageDialog(
               null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
           return;
         }
       }
       NamedBean nBean = getBySystemName(sysNameList.get(row));
       nBean.setUserName((String) value);
       if (nbMan.inUse(sysNameList.get(row), nBean)) {
         String msg =
             Bundle.getMessage(
                 "UpdateToUserName", new Object[] {getBeanType(), value, sysNameList.get(row)});
         int optionPane =
             JOptionPane.showConfirmDialog(
                 null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
         if (optionPane == JOptionPane.YES_OPTION) {
           // This will update the bean reference from the systemName to the userName
           try {
             nbMan.updateBeanFromSystemToUser(nBean);
           } catch (JmriException ex) {
             // We should never get an exception here as we already check that the username is not
             // valid
           }
         }
       }
       fireTableRowsUpdated(row, row);
       break;
     case COMMENTCOL:
       getBySystemName(sysNameList.get(row)).setComment((String) value);
       fireTableRowsUpdated(row, row);
       break;
     case VALUECOL:
       // button fired, swap state
       NamedBean t = getBySystemName(sysNameList.get(row));
       clickOn(t);
       break;
     case DELETECOL:
       // button fired, delete Bean
       deleteBean(row, col);
       break;
     default:
       break;
   }
 }
Example #2
0
  /** Generic method to change the user name of a Bean */
  public void renameBean(String _newName) {
    NamedBean nBean = bean;
    String oldName = nBean.getUserName();

    String value = _newName;

    if (value.equals(oldName)) {
      // name not changed.
      return;
    } else {
      NamedBean nB = getByUserName(value);
      if (nB != null) {
        log.error("User name is not unique " + value); // NOI18N
        String msg;
        msg =
            java.text.MessageFormat.format(
                Bundle.getMessage("WarningUserName"), new Object[] {("" + value)});
        JOptionPane.showMessageDialog(
            null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
        return;
      }
    }

    nBean.setUserName(value);
    if (!value.equals("")) {
      if (oldName == null || oldName.equals("")) {
        if (!nbMan.inUse(nBean.getSystemName(), nBean)) {
          return;
        }
        String msg =
            java.text.MessageFormat.format(
                Bundle.getMessage("UpdateToUserName"),
                new Object[] {getBeanType(), value, nBean.getSystemName()});
        int optionPane =
            JOptionPane.showConfirmDialog(
                null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
        if (optionPane == JOptionPane.YES_OPTION) {
          // This will update the bean reference from the systemName to the userName
          try {
            nbMan.updateBeanFromSystemToUser(nBean);
          } catch (jmri.JmriException ex) {
            // We should never get an exception here as we already check that the username is not
            // valid
          }
        }

      } else {
        nbMan.renameBean(oldName, value, nBean);
      }

    } else {
      // This will update the bean reference from the old userName to the SystemName
      nbMan.updateBeanFromUserToSystem(nBean);
    }
  }
Example #3
0
 public void removeName(int row, int column) {
   NamedBean nBean = getBySystemName(sysNameList.get(row));
   String msg = Bundle.getMessage("UpdateToSystemName", new Object[] {getBeanType()});
   int optionPane =
       JOptionPane.showConfirmDialog(
           null, msg, Bundle.getMessage("UpdateToSystemNameTitle"), JOptionPane.YES_NO_OPTION);
   if (optionPane == JOptionPane.YES_OPTION) {
     nbMan.updateBeanFromUserToSystem(nBean);
   }
   nBean.setUserName(null);
   fireTableRowsUpdated(row, row);
 }
Example #4
0
 /** Generic method to remove the user name from a bean. */
 public void removeName() {
   String msg =
       java.text.MessageFormat.format(
           Bundle.getMessage("UpdateToSystemName"), new Object[] {getBeanType()});
   int optionPane =
       JOptionPane.showConfirmDialog(
           null, msg, Bundle.getMessage("UpdateToSystemNameTitle"), JOptionPane.YES_NO_OPTION);
   if (optionPane == JOptionPane.YES_OPTION) {
     nbMan.updateBeanFromUserToSystem(bean);
   }
   bean.setUserName(null);
 }
Example #5
0
  public void moveBean(int row, int column) {
    final NamedBean t = getBySystemName(sysNameList.get(row));
    String currentName = t.getUserName();
    NamedBean oldNameBean = getBySystemName(sysNameList.get(row));

    if ((currentName == null) || currentName.equals("")) {
      JOptionPane.showMessageDialog(null, "Can not move an empty UserName");
      return;
    }

    JComboBox<String> box = new JComboBox<>();
    List<String> nameList = getManager().getSystemNameList();
    for (int i = 0; i < nameList.size(); i++) {
      NamedBean nb = getBySystemName(nameList.get(i));
      // Only add items that do not have a username assigned.
      if (nb.getDisplayName().equals(nameList.get(i))) {
        box.addItem(nameList.get(i));
      }
    }

    int retval =
        JOptionPane.showOptionDialog(
            null,
            "Move " + getBeanType() + " " + currentName + " from " + oldNameBean.getSystemName(),
            "Move UserName",
            0,
            JOptionPane.INFORMATION_MESSAGE,
            null,
            new Object[] {"Cancel", "OK", box},
            null);
    log.debug(
        "Dialog value "
            + retval
            + " selected "
            + box.getSelectedIndex()
            + ":"
            + box.getSelectedItem());
    if (retval != 1) {
      return;
    }
    String entry = (String) box.getSelectedItem();
    NamedBean newNameBean = getBySystemName(entry);
    if (oldNameBean != newNameBean) {
      oldNameBean.setUserName("");
      newNameBean.setUserName(currentName);
      InstanceManager.getDefault(NamedBeanHandleManager.class)
          .moveBean(oldNameBean, newNameBean, currentName);
      if (nbMan.inUse(newNameBean.getSystemName(), newNameBean)) {
        String msg =
            Bundle.getMessage(
                "UpdateToUserName",
                new Object[] {getBeanType(), currentName, sysNameList.get(row)});
        int optionPane =
            JOptionPane.showConfirmDialog(
                null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
        if (optionPane == JOptionPane.YES_OPTION) {
          try {
            nbMan.updateBeanFromSystemToUser(newNameBean);
          } catch (JmriException ex) {
            // We should never get an exception here as we already check that the username is not
            // valid
          }
        }
      }
      fireTableRowsUpdated(row, row);
      InstanceManager.getDefault(UserPreferencesManager.class)
          .showInfoMessage(
              "Reminder",
              getBeanType() + " " + Bundle.getMessage("UpdateComplete"),
              getMasterClassName(),
              "remindSaveReLoad");
      // JOptionPane.showMessageDialog(null, getBeanType() + " " +
      // Bundle.getMessage("UpdateComplete"));
    }
  }
Example #6
0
  public void renameBean(int row, int column) {
    NamedBean nBean = getBySystemName(sysNameList.get(row));
    String oldName = nBean.getUserName();
    JTextField _newName = new JTextField(20);
    _newName.setText(oldName);
    Object[] renameBeanOption = {"Cancel", "OK", _newName};
    int retval =
        JOptionPane.showOptionDialog(
            null,
            "Rename UserName From " + oldName,
            "Rename " + getBeanType(),
            0,
            JOptionPane.INFORMATION_MESSAGE,
            null,
            renameBeanOption,
            renameBeanOption[2]);

    if (retval != 1) {
      return;
    }
    String value = _newName.getText().trim();

    if (value.equals(oldName)) {
      // name not changed.
      return;
    } else {
      NamedBean nB = getByUserName(value);
      if (nB != null) {
        log.error("User name is not unique " + value);
        String msg = Bundle.getMessage("WarningUserName", new Object[] {("" + value)});
        JOptionPane.showMessageDialog(
            null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.ERROR_MESSAGE);
        return;
      }
    }

    nBean.setUserName(value);
    fireTableRowsUpdated(row, row);
    if (!value.equals("")) {
      if (oldName == null || oldName.equals("")) {
        if (!nbMan.inUse(sysNameList.get(row), nBean)) {
          return;
        }
        String msg =
            Bundle.getMessage(
                "UpdateToUserName", new Object[] {getBeanType(), value, sysNameList.get(row)});
        int optionPane =
            JOptionPane.showConfirmDialog(
                null, msg, Bundle.getMessage("UpdateToUserNameTitle"), JOptionPane.YES_NO_OPTION);
        if (optionPane == JOptionPane.YES_OPTION) {
          // This will update the bean reference from the systemName to the userName
          try {
            nbMan.updateBeanFromSystemToUser(nBean);
          } catch (JmriException ex) {
            // We should never get an exception here as we already check that the username is not
            // valid
          }
        }

      } else {
        nbMan.renameBean(oldName, value, nBean);
      }

    } else {
      // This will update the bean reference from the old userName to the SystemName
      nbMan.updateBeanFromUserToSystem(nBean);
    }
  }
Example #7
0
  /**
   * Activates a Light Control by control type. This method tests the control type, and set up a
   * control mechanism, appropriate for the control type.
   */
  public void activateLightControl() {
    // skip if Light Control is already active
    if (!_active) {
      // activate according to control type
      switch (_controlType) {
        case Light.SENSOR_CONTROL:
          _namedControlSensor = null;
          if (_controlSensorName.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensorName);
            _namedControlSensor = nbhm.getNamedBeanHandle(_controlSensorName, sen);
          }
          if (_namedControlSensor != null) {
            // if sensor state is currently known, set light accordingly
            int kState = _namedControlSensor.getBean().getKnownState();
            if (kState == Sensor.ACTIVE) {
              if (_controlSensorSense == Sensor.ACTIVE) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            } else if (kState == Sensor.INACTIVE) {
              if (_controlSensorSense == Sensor.INACTIVE) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            }

            // listen for change in sensor state
            _namedControlSensor
                .getBean()
                .addPropertyChangeListener(
                    _sensorListener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if (!_parentLight.getEnabled()) {
                              return; // ignore property change if user disabled Light
                            }
                            if (e.getPropertyName().equals("KnownState")) {
                              int now = _namedControlSensor.getBean().getKnownState();
                              if (now == Sensor.ACTIVE) {
                                if (_controlSensorSense == Sensor.ACTIVE) {
                                  // Turn light on
                                  _parentLight.setState(Light.ON);
                                } else {
                                  // Turn light off
                                  _parentLight.setState(Light.OFF);
                                }
                              } else if (now == Sensor.INACTIVE) {
                                if (_controlSensorSense == Sensor.INACTIVE) {
                                  // Turn light on
                                  _parentLight.setState(Light.ON);
                                } else {
                                  // Turn light off
                                  _parentLight.setState(Light.OFF);
                                }
                              }
                            }
                          }
                        },
                    _controlSensorName,
                    "Light Control " + _parentLight.getDisplayName());
            _active = true;
          } else {
            // control sensor does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Sensor that does not exist: "
                    + _controlSensorName);
            return;
          }
          break;

        case Light.FAST_CLOCK_CONTROL:
          if (_clock == null) {
            _clock = InstanceManager.timebaseInstance();
          }
          // set up time as minutes in a day
          _timeOn = _fastClockOnHour * 60 + _fastClockOnMin;
          _timeOff = _fastClockOffHour * 60 + _fastClockOffMin;
          // initialize light based on current fast time
          updateClockControlLight();
          // set up to listen for time changes on a minute basis
          _clock.addMinuteChangeListener(
              _timebaseListener =
                  new java.beans.PropertyChangeListener() {
                    public void propertyChange(java.beans.PropertyChangeEvent e) {
                      if (_parentLight.getEnabled()) { // don't change light if not enabled
                        // update control if light is enabled
                        updateClockControlLight();
                      }
                    }
                  });
          _active = true;
          break;
        case Light.TURNOUT_STATUS_CONTROL:
          _controlTurnout =
              InstanceManager.turnoutManagerInstance().provideTurnout(_controlTurnoutName);
          if (_controlTurnout != null) {
            // set light based on current turnout state if known
            int tState = _controlTurnout.getKnownState();
            if (tState == Turnout.CLOSED) {
              if (_turnoutState == Turnout.CLOSED) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            } else if (tState == Turnout.THROWN) {
              if (_turnoutState == Turnout.THROWN) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            }

            // listen for change in turnout state
            _controlTurnout.addPropertyChangeListener(
                _turnoutListener =
                    new java.beans.PropertyChangeListener() {
                      public void propertyChange(java.beans.PropertyChangeEvent e) {
                        if (!_parentLight.getEnabled()) {
                          return; // ignore property change if user disabled light
                        }
                        if (e.getPropertyName().equals("KnownState")) {
                          int now = _controlTurnout.getKnownState();
                          if (now == Turnout.CLOSED) {
                            if (_turnoutState == Turnout.CLOSED) {
                              // Turn light on
                              _parentLight.setState(Light.ON);
                            } else {
                              // Turn light off
                              _parentLight.setState(Light.OFF);
                            }
                          } else if (now == Turnout.THROWN) {
                            if (_turnoutState == Turnout.THROWN) {
                              // Turn light on
                              _parentLight.setState(Light.ON);
                            } else {
                              // Turn light off
                              _parentLight.setState(Light.OFF);
                            }
                          }
                        }
                      }
                    });
            _active = true;
          } else {
            // control turnout does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Turnout that does not exist: "
                    + _controlSensorName);
            return;
          }
          break;
        case Light.TIMED_ON_CONTROL:
          if (_timedSensorName.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_timedSensorName);
            _namedTimedControlSensor = nbhm.getNamedBeanHandle(_timedSensorName, sen);
          }
          if (_namedTimedControlSensor != null) {
            // set initial state off
            _parentLight.setState(Light.OFF);
            // listen for change in timed control sensor state
            _namedTimedControlSensor
                .getBean()
                .addPropertyChangeListener(
                    _timedSensorListener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if (!_parentLight.getEnabled()) {
                              return; // ignore property change if user disabled light
                            }
                            if (e.getPropertyName().equals("KnownState")) {
                              int now = _namedTimedControlSensor.getBean().getKnownState();
                              if (!_lightOnTimerActive) {
                                if (now == Sensor.ACTIVE) {
                                  // Turn light on
                                  _parentLight.setState(Light.ON);
                                  // Create a timer if one does not exist
                                  if (_timedControlTimer == null) {
                                    _timedControlListener = new TimeLight();
                                    _timedControlTimer =
                                        new Timer(_timeOnDuration, _timedControlListener);
                                  }
                                  // Start the Timer to turn the light OFF
                                  _lightOnTimerActive = true;
                                  _timedControlTimer.start();
                                }
                              }
                            }
                          }
                        },
                    _timedSensorName,
                    "Light Control " + _parentLight.getDisplayName());
            _active = true;
          } else {
            // timed control sensor does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Sensor that does not exist: "
                    + _timedSensorName);
            return;
          }
          break;
        case Light.TWO_SENSOR_CONTROL:
          _namedControlSensor = null;
          _namedControlSensor2 = null;
          if (_controlSensorName.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensorName);
            _namedControlSensor = nbhm.getNamedBeanHandle(_controlSensorName, sen);
          }
          if (_controlSensor2Name.length() > 0) {
            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensor2Name);
            _namedControlSensor2 = nbhm.getNamedBeanHandle(_controlSensor2Name, sen);
          }
          if ((_namedControlSensor != null) && (_namedControlSensor2 != null)) {
            // if sensor state is currently known, set light accordingly
            int kState = _namedControlSensor.getBean().getKnownState();
            int kState2 = _namedControlSensor2.getBean().getKnownState();
            if (_controlSensorSense == Sensor.ACTIVE) {
              if ((kState == Sensor.ACTIVE) || (kState2 == Sensor.ACTIVE)) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            } else if (_controlSensorSense == Sensor.INACTIVE) {
              if ((kState == Sensor.INACTIVE) || (kState2 == Sensor.INACTIVE)) {
                // Turn light on
                _parentLight.setState(Light.ON);
              } else {
                // Turn light off
                _parentLight.setState(Light.OFF);
              }
            }

            // listen for change in sensor states
            _namedControlSensor
                .getBean()
                .addPropertyChangeListener(
                    _sensorListener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            twoSensorChanged(e);
                          }
                        },
                    _controlSensorName,
                    "Light Control " + _parentLight.getDisplayName());
            _namedControlSensor2
                .getBean()
                .addPropertyChangeListener(
                    _sensor2Listener =
                        new java.beans.PropertyChangeListener() {
                          public void propertyChange(java.beans.PropertyChangeEvent e) {
                            twoSensorChanged(e);
                          }
                        },
                    _controlSensor2Name,
                    "Light Control " + _parentLight.getDisplayName());
            _active = true;
          } else {
            // at least one control sensor does not exist
            log.error(
                "Light "
                    + _parentLight.getSystemName()
                    + " is linked to a Sensor that does not exist: ");
            return;
          }
          break;
        default:
          log.warn(
              "Unexpected control type when activating Light: " + _parentLight.getSystemName());
      }
    }
  }