Search in sources :

Example 1 with SensorGroupConditional

use of jmri.implementation.SensorGroupConditional in project JMRI by JMRI.

the class DefaultConditionalManager method createNewConditional.

/**
     * Method to create a new Conditional if the Conditional does not exist If
     * the parent Logix cannot be found, the userName cannot be checked, but the
     * Conditional is still created. The scenario can happen when a Logix is
     * loaded from a file after its Conditionals.
     *
     * @param systemName properly formatted system name for the new Conditional
     * @param userName must not be null, use "" instead
     * @return null if a Conditional with the same systemName or userName
     *         already exists, or if there is trouble creating a new Conditional
     */
@Override
public Conditional createNewConditional(String systemName, String userName) {
    Conditional c = null;
    // Check system name
    if (systemName == null || systemName.length() < 1) {
        log.error("createNewConditional: systemName is null or empty");
        return null;
    }
    c = getBySystemName(systemName);
    if (c != null) {
        // Conditional already exists
        return null;
    }
    // Get the potential parent Logix
    Logix lgx = getParentLogix(systemName);
    if (lgx == null) {
        log.error("Unable to find the parent logix for condtional '{}'", systemName);
        return null;
    }
    // Check the user name
    if (userName != null && userName.length() > 0) {
        c = getByUserName(lgx, userName);
        if (c != null) {
            // Duplicate user name within the parent Logix
            return null;
        }
    }
    // Conditional does not exist, create a new Conditional
    if (systemName.startsWith(SensorGroupFrame.logixSysName)) {
        c = new SensorGroupConditional(systemName, userName);
    } else {
        c = new DefaultConditional(systemName, userName);
    }
    // save in the maps
    //@        register(c);
    boolean addCompleted = lgx.addConditional(systemName, c);
    if (!addCompleted) {
        return null;
    }
    return c;
}
Also used : DefaultConditional(jmri.implementation.DefaultConditional) Logix(jmri.Logix) SensorGroupConditional(jmri.implementation.SensorGroupConditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) DefaultConditional(jmri.implementation.DefaultConditional) Conditional(jmri.Conditional)

Example 2 with SensorGroupConditional

use of jmri.implementation.SensorGroupConditional in project JMRI by JMRI.

the class SensorGroupFrame method addPressed.

void addPressed() {
    deleteGroup(false);
    String group = _nameField.getText();
    if (group == null || group.length() == 0) {
        javax.swing.JOptionPane.showMessageDialog(this, "Please enter a name for this group.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
        return;
    }
    Logix logix = getSystemLogix();
    logix.deActivateLogix();
    String cSystemName = ConditionalSystemPrefix + group.toUpperCase();
    String cUserName = ConditionalUserPrefix + group;
    // add new Conditional
    ArrayList<ConditionalVariable> variableList = new ArrayList<ConditionalVariable>();
    ArrayList<ConditionalAction> actionList = new ArrayList<ConditionalAction>();
    int count = 0;
    for (int i = 0; i < _sensorModel.getRowCount(); i++) {
        if ((Boolean) _sensorModel.getValueAt(i, BeanTableModel.INCLUDE_COLUMN)) {
            String sensor = (String) _sensorModel.getValueAt(i, BeanTableModel.SNAME_COLUMN);
            variableList.add(new ConditionalVariable(false, Conditional.OPERATOR_OR, Conditional.TYPE_SENSOR_ACTIVE, sensor, true));
            actionList.add(new DefaultConditionalAction(Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE, Conditional.ACTION_SET_SENSOR, sensor, Sensor.INACTIVE, ""));
            count++;
        }
    }
    if (count < 2) {
        javax.swing.JOptionPane.showMessageDialog(this, "A Sensor Group needs to have at least 2 sensors to be useful.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
    Conditional c = new SensorGroupConditional(cSystemName, cUserName);
    //        InstanceManager.getDefault(jmri.ConditionalManager.class).register(c);
    c.setStateVariables(variableList);
    c.setLogicType(Conditional.ALL_OR, "");
    c.setAction(actionList);
    // Update the Logix Conditional names list
    logix.addConditional(cSystemName, 0);
    // Update the Logix Conditional hash map
    logix.addConditional(cSystemName, c);
    logix.setEnabled(true);
    logix.activateLogix();
    ((DefaultListModel<String>) _sensorGroupList.getModel()).addElement(cUserName.substring(ConditionalUserPrefix.length()));
    clear();
}
Also used : DefaultConditionalAction(jmri.implementation.DefaultConditionalAction) ArrayList(java.util.ArrayList) SensorGroupConditional(jmri.implementation.SensorGroupConditional) DefaultListModel(javax.swing.DefaultListModel) Conditional(jmri.Conditional) SensorGroupConditional(jmri.implementation.SensorGroupConditional) ConditionalVariable(jmri.ConditionalVariable) Logix(jmri.Logix) ConditionalAction(jmri.ConditionalAction) DefaultConditionalAction(jmri.implementation.DefaultConditionalAction)

Aggregations

Conditional (jmri.Conditional)2 Logix (jmri.Logix)2 SensorGroupConditional (jmri.implementation.SensorGroupConditional)2 ArrayList (java.util.ArrayList)1 DefaultListModel (javax.swing.DefaultListModel)1 ConditionalAction (jmri.ConditionalAction)1 ConditionalVariable (jmri.ConditionalVariable)1 DefaultConditional (jmri.implementation.DefaultConditional)1 DefaultConditionalAction (jmri.implementation.DefaultConditionalAction)1