/**
 * Retrieve control information from window and update Light Control
 *    Returns 'true' if no errors or warnings.
 */
/*private*/ bool AddEditLightDialog::setControlInformation(LightControl* g)
{
 // Get control information
 if (sensorControl==(typeBox->currentText()))
 {
  // Set type of control
  g->setControlType(Light::SENSOR_CONTROL);
  // Get sensor control information
  QString sensorName = field1a->text().trimmed();
  Sensor* s = NULL;
  if (sensorName.length() < 1)
  {
   // no sensor name entered
   g->setControlType(Light::NO_CONTROL);
  }
  else
  {
   // name was entered, check for user name first
   s = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                               getByUserName(sensorName);
   if (s==NULL)
   {
    // not user name, try system name
    s =  ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                        getBySystemName(sensorName);
    if (s!=NULL)
    {
     // update sensor system name in case it changed
     sensorName = s->getSystemName();
     field1a->setText(sensorName);
    }
   }
  }
  int sState = Sensor::ACTIVE;
  if ( stateBox->currentText()==(tr("Inactive")) )
  {
   sState = Sensor::INACTIVE;
  }
  g->setControlSensorName(sensorName);
  g->setControlSensorSense(sState);
  if (s==NULL)
  {
   ui->status1->setText( tr("Error: Sensor could not be provided. Light Control not created.") );
   return (false);
  }
 }
 else if (fastClockControl==(typeBox->currentText()))
 {
  // Set type of control
  g->setControlType(Light::FAST_CLOCK_CONTROL);
  // read and parse the hours and minutes in the two fields
  bool error = false;
  int onHour = 0;
  int onMin = 0;
  int offHour = 0;
  int offMin = 0;
  bool bOk;
  QString s = field1b->text();
  if ( (s.length() != 5) || (s.at(2) != ':') )
  {
   ui->status1->setText( tr("Format error in time entry, please reenter as hh:mm.") );
   error = true;
  }
  if (!error)
  {
//   try
//   {
   onHour = s.mid(0,2).toInt(&bOk);
   if ( (onHour < 0) || (onHour > 24) )
   {
    ui->status1->setText( tr("Number out of range in time entry, please reenter as hh:mm.") );
    error = true;
   }
//   }
//   catch (Exception e)
   if(!bOk)
   {
    ui->status1->setText( tr("Bad character in time field, please reenter as hh:mm.") );
    error = true;
   }
  }
  if (!error)
  {
//    try {
   onMin = s.mid(3,5).toInt(&bOk);
   if ( (onMin < 0) || (onMin > 59) )
   {
    ui->status1->setText( tr("Number out of range in time entry, please reenter as hh:mm.") );
    error = true;
   }
//  }
//    catch (Exception e)
  if(!bOk)
  {
   ui->status1->setText( tr("Bad character in time field, please reenter as hh:mm.") );
   error = true;
  }
 }
 s = field2a->text();
 if ( (s.length() != 5) || (s.at(2) != ':') )
 {
  ui->status1->setText( tr("Format error in time entry, please reenter as hh:mm.") );
  error = true;
 }
 if (!error)
 {
//    try {
  offHour = s.mid(0,2).toInt(&bOk);
  if ( (offHour < 0) || (offHour > 24) )
  {
   ui->status1->setText( tr("Number out of range in time entry, please reenter as hh:mm.") );
   error = true;
  }
// }
//    catch (Exception e)
 if(!bOk)
 {
  ui->status1->setText( tr("Bad character in time field, please reenter as hh:mm.") );
  error = true;
 }
}
if (!error)
{
//    try {
 offMin = s.mid(3,5).toInt(&bOk);
 if ( (offMin < 0) || (offMin > 59) )
 {
  ui->status1->setText( tr("Number out of range in time entry, please reenter as hh:mm.") );
  error = true;
 }
//    }
//    catch (Exception e)
 if(!bOk)
 {
  ui->status1->setText( tr("Bad character in time field, please reenter as hh:mm.") );
  error = true;
 }
}

if (error) {
    return (false);
}
g->setFastClockControlSchedule(onHour,onMin,offHour,offMin);
}
 else if (turnoutStatusControl==(typeBox->currentText()))
 {
  bool error = false;

  Turnout* t = NULL;
  // Set type of control
  g->setControlType(Light::TURNOUT_STATUS_CONTROL);
  // Get turnout control information
  QString turnoutName = field1c->text().trimmed();
  if (turnoutName.length() < 1)
  {
   // valid turnout system name was not entered
   g->setControlType(Light::NO_CONTROL);
  }
  else
  {
   // Ensure that this Turnout is not already a Light
   if (turnoutName.at(1)=='T')
   {
    // must be a standard format name (not just a number)
    QString testSN = turnoutName.mid(0,1)+"L"+
                    turnoutName.mid(2,turnoutName.length());
    Light* testLight = ((ProxyLightManager*)InstanceManager::lightManagerInstance())->
                                    getBySystemName(testSN);
    if (testLight != NULL)
    {
     // Requested turnout bit is already assigned to a Light
     ui->status2->setText( tr("Warning: Requested Turnout uses the same address as Light:")+" "+testSN+"." );
     ui->status2->setVisible(true);
     error = true;
    }
   }
   if (!error)
   {
    // Requested turnout bit is not assigned to a Light
    t = ((ProxyTurnoutManager*)InstanceManager::turnoutManagerInstance())->
                                getByUserName(turnoutName);
    if (t==NULL)
    {
     // not user name, try system name
     t = ((ProxyTurnoutManager*)InstanceManager::turnoutManagerInstance())->
                                getBySystemName(turnoutName.toUpper());
     if (t!=NULL)
     {
      // update turnout system name in case it changed
      turnoutName = t->getSystemName();
      field1c->setText(turnoutName);
     }
    }
   }
  }
  // Initialize the requested Turnout State
  int tState = Turnout::CLOSED;
  if ( stateBox->currentText()==(((ProxyTurnoutManager*)InstanceManager::
            turnoutManagerInstance())->getThrownText()) )
  {
   tState = Turnout::THROWN;
  }
  g->setControlTurnout(turnoutName);
  g->setControlTurnoutState(tState);
  if (t==NULL)
  {
   ui->status1->setText( tr("Error: Turnout could not be provided. Light Control not created.") );
   return (false);
  }
 }
 else if (timedOnControl==(typeBox->currentText()))
 {
  // Set type of control
  g->setControlType(Light::TIMED_ON_CONTROL);
  // Get trigger sensor control information
  Sensor* s = NULL;
  QString triggerSensorName = field1d->text();
  if (triggerSensorName.length() < 1)
  {
   // Trigger sensor not entered, or invalidly entered
   g->setControlType(Light::NO_CONTROL);
  }
  else
  {
   // name entered, try user name first
   s = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                getByUserName(triggerSensorName);
   if (s==NULL)
   {
    // not user name, try system name
    s = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                    getBySystemName(triggerSensorName);
    if (s!=NULL)
    {
     // update sensor system name in case it changed
     triggerSensorName = s->getSystemName();
     field1d->setText(triggerSensorName);
    }
   }
  }
  g->setControlTimedOnSensorName(triggerSensorName);
  int dur = 0;
  bool bOk;
//try
//{
  dur = field2b->text().toInt(&bOk);
//}
//catch (Exception e)
  if(!bOk)
  {
   if (s!=NULL)
   {
    ui->status1->setText(tr("Error: Duration entry had invalid format. Light Control not created.") );
    return (false);
   }
  }
  g->setTimedOnDuration(dur);
  if (s==NULL)
  {
   ui->status1->setText( tr("Error: Trigger Sensor has invalid name. Light Control not created.") );
   return (false);
  }
 }
 else if (twoSensorControl==(typeBox->currentText()))
 {
  // Set type of control
  g->setControlType(Light::TWO_SENSOR_CONTROL);
  // Get sensor control information
  QString sensorName = field1a->text().trimmed();
  Sensor* s = NULL;
  QString sensor2Name = field1a2->text().trimmed();
  Sensor* s2 = NULL;
  if ( (sensorName.length() < 1) || (sensor2Name.length() <1) )
  {
   // no sensor name entered
   g->setControlType(Light::NO_CONTROL);
  }
  else
  {
   // name was entered, check for user name first
   s = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                       getByUserName(sensorName);
   if (s==NULL)
   {
    // not user name, try system name
    s = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                getBySystemName(sensorName);
    if (s!=NULL)
    {
     // update sensor system name in case it changed
     sensorName = s->getSystemName();
     field1a->setText(sensorName);
    }
   }
   s2 = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
                       getByUserName(sensor2Name);
   if (s2==NULL)
   {
    // not user name, try system name
    s2 = ((ProxySensorManager*)InstanceManager::sensorManagerInstance())->
            getBySystemName(sensor2Name);
    if (s2!=NULL)
    {
     // update sensor system name in case it changed
     sensor2Name = s2->getSystemName();
     field1a2->setText(sensor2Name);
    }
   }
  }
  int sState = Sensor::ACTIVE;
  if ( stateBox->currentText()==(tr("Inactive")) )
  {
   sState = Sensor::INACTIVE;
  }
  g->setControlSensorName(sensorName);
  g->setControlSensor2Name(sensor2Name);
  g->setControlSensorSense(sState);
  if (s==NULL)
  {
   ui->status1->setText( tr("Error: Sensor could not be provided. Light Control not created.") );
   return (false);
  }
 }
 else if (noControl==(typeBox->currentText()))
 {
  // Set type of control
  g->setControlType(Light::NO_CONTROL);
 }
 else
 {
  log->error("Unexpected control type: "+typeBox->currentText());
 }
 return (true);
}