bool 
AbstractTaskFactory::replace(const OrderedTaskPoint &new_tp,
                             const unsigned position,
                             const bool auto_mutate)
{
  if (auto_mutate) {
    if (validType(new_tp, position))
      // ok to replace directly
      return m_task.replace(new_tp, position);

    // will need to convert type of candidate
    OrderedTaskPoint *tp;
    if (position == 0) {
      // candidate must be transformed into a startpoint
      tp = createStart(new_tp.get_waypoint());
    } else if (is_position_finish(position) && (position + 1 == m_task.task_size())) {
      // this point must be mutated into a finish
      tp = createFinish(new_tp.get_waypoint());
    } else {
      // this point must be mutated into an intermediate
      tp = createIntermediate(new_tp.get_waypoint());
    }

    bool success = m_task.replace(*tp, position);
    delete tp;
    return success;
  }

  return m_task.replace(new_tp, position);
}
static void
RefreshView()
{
  wTaskView->invalidate();

  OrderedTaskPoint* tp = ordered_task->get_tp(active_index);
  if (!tp)
    return;

  Refreshing = true; // tell onChange routines not to save form!

  TPLabelObservationZone ozv;
  ObservationZoneConstVisitor &visitor = ozv;
  visitor.Visit(*tp->get_oz());

  WndFrame* wfrm = NULL;

  wfrm = ((WndFrame*)wf->FindByName(_T("lblType")));
  if (wfrm)
    wfrm->SetCaption(OrderedTaskPointName(ordered_task->get_factory().getType(*tp)));

  WndButton* wb;
  wb = ((WndButton*)wf->FindByName(_T("butPrevious")));
  if (wb)
    wb->set_enabled(active_index > 0);

  wb = ((WndButton*)wf->FindByName(_T("butNext")));
  if (wb)
    wb->set_enabled(active_index < (ordered_task->task_size() - 1));

  wb = (WndButton*)wf->FindByName(_T("cmdOptionalStarts"));
  assert(wb);
  wb->set_visible(active_index == 0);
  if (ordered_task->optional_start_points_size() == 0)
    wb->SetCaption(_("Enable Alternate Starts"));
  else {
    TCHAR tmp[50];
    _stprintf(tmp, _T("%s (%d)"), _("Edit Alternates"),
        ordered_task->optional_start_points_size());
    wb->SetCaption(tmp);
  }

  EnableSizeEdit(ordered_task->get_factory_type() != TaskBehaviour::FACTORY_FAI_GENERAL);

  TCHAR bufType[100];
  TCHAR bufNamePrefix[100];
  TPLabelTaskPoint tpv(bufType, bufNamePrefix);
  TaskPointConstVisitor &tp_visitor = tpv;
  tp_visitor.Visit(*tp);
  wf->SetCaption(tpv.textType);

  wfrm = ((WndFrame*)wf->FindByName(_T("lblLocation")));
  if (wfrm) {
    TCHAR buff[100];
    _stprintf(buff, _T("%s %s"), tpv.textNamePrefix,
        tp->get_waypoint().Name.c_str());
    wfrm->SetCaption(buff);
  }
  Refreshing = false; // reactivate onChange routines
}
bool 
AbstractTaskFactory::append(const OrderedTaskPoint &new_tp,
                            const bool auto_mutate)
{
  if (m_task.is_max_size())
    return false;

  if (auto_mutate) {
    if (!m_task.task_size()) {
      // empty task, so add as a start point
      if (validType(new_tp, m_task.task_size())) {
        // candidate is ok, so add it
        return m_task.append(new_tp);
      } else {
        // candidate must be transformed into a startpoint
        StartPoint* sp = createStart(new_tp.get_waypoint());
        bool success = m_task.append(*sp);
        delete sp;
        return success;
      }
    }

    // non-empty task

    if (m_task.has_finish()) {
      // old finish must be mutated into an intermediate point
      IntermediateTaskPoint* sp = createIntermediate(m_task.getTaskPoint(
          m_task.task_size() - 1)->get_waypoint());

      m_task.replace(*sp, m_task.task_size()-1);
      delete sp;
    }

    if (validType(new_tp, m_task.task_size()))
      // ok to append directly
      return m_task.append(new_tp);

    // this point must be mutated into a finish
    FinishPoint* sp = createFinish(new_tp.get_waypoint());
    bool success = m_task.append(*sp);
    delete sp;
    return success;
  }

  return m_task.append(new_tp);
}
bool 
AbstractTaskFactory::insert(const OrderedTaskPoint &new_tp,
                            const unsigned position,
                            const bool auto_mutate)
{
  if (position >= m_task.task_size())
    return append(new_tp, auto_mutate);

  if (auto_mutate) {
    if (position == 0) {
      if (m_task.has_start()) {
        // old start must be mutated into an intermediate point
        IntermediateTaskPoint* sp =
            createIntermediate(m_task.getTaskPoint(0)->get_waypoint());
        m_task.replace(*sp, 0);
        delete sp;
      }
      if (validType(new_tp, 0)) {
        return m_task.insert(new_tp, 0);
      } else {
        // candidate must be transformed into a startpoint
        StartPoint* sp = createStart(new_tp.get_waypoint());
        bool success = m_task.insert(*sp, 0);
        delete sp;
        return success;
      }
    } else {
      if (new_tp.is_intermediate()) {
        // candidate ok for direct insertion
        return m_task.insert(new_tp, position);
      } else {
        // candidate must be transformed into a intermediatepoint
        IntermediateTaskPoint* sp = createIntermediate(new_tp.get_waypoint());
        bool success = m_task.insert(*sp, position);
        delete sp;
        return success;
      }
    }
  }

  return m_task.insert(new_tp, position);
}
bool
AbstractTaskFactory::append_optional_start(const OrderedTaskPoint &new_tp,
                                           const bool auto_mutate)
{
  if (auto_mutate && !validType(new_tp, 0)) {
    // candidate must be transformed into a startpoint of appropriate type
    StartPoint* sp = createStart(new_tp.get_waypoint());
    bool success = m_task.append_optional_start(*sp);
    delete sp;
    return success;
  }
  // ok to add directly
  return m_task.append_optional_start(new_tp);
}