/**
   * Updates the selected tool, and optionally notify listeners.
   *
   * @param tool selected tool.
   * @param notifyListeners true to notify listeners, false otherwise.
   */
  private void setTool(EditorTools tool, boolean notifyListeners) {
    if (mMissionProxy != null
        && mMissionProxy.getItems().size() > 0
        && tool != EditorTools.TRASH
        && tool != EditorTools.SELECTOR
        && tool != EditorTools.NONE) {

      MissionItemProxy lastMissionItem =
          mMissionProxy.getItems().get(mMissionProxy.getItems().size() - 1);
      switch (lastMissionItem.getMissionItem().getType()) {
        case LAND:
        case RETURN_TO_LAUNCH:
          tool = EditorTools.NONE;
          mEditorRadioGroup.clearCheck();
          Toast.makeText(
                  getActivity(), getString(R.string.editor_err_land_rtl_added), Toast.LENGTH_SHORT)
              .show();
          break;

        default:
          break;
      }
    }

    this.tool = tool;
    if (tool == EditorTools.NONE) {
      mEditorRadioGroup.clearCheck();
    }

    if (listener != null && notifyListeners) {
      listener.editorToolChanged(this.tool);
    }
  }
  @Override
  public void onApiConnected() {
    mMissionProxy = getMissionProxy();

    mSelectedProxies.clear();
    mSelectedProxies.addAll(mMissionProxy.selection.getSelected());

    mSelectedItems.clear();
    for (MissionItemProxy mip : mSelectedProxies) {
      mSelectedItems.add(mip.getMissionItem());
    }

    final View view = getView();
    if (view == null) return;

    List<MissionItemType> list =
        new LinkedList<MissionItemType>(Arrays.asList(MissionItemType.values()));

    if (mSelectedProxies.size() == 1) {
      final MissionItemProxy itemProxy = mSelectedProxies.get(0);
      final MissionItem currentItem = itemProxy.getMissionItem();

      if (currentItem instanceof Survey) {
        list.clear();
        list.add(MissionItemType.SURVEY);
      } else {
        list.remove(MissionItemType.SURVEY);
      }

      if ((currentItem instanceof StructureScanner)) {
        list.clear();
        list.add(MissionItemType.STRUCTURE_SCANNER);
      }

      if (mMissionProxy.getItems().indexOf(itemProxy) != 0) {
        list.remove(MissionItemType.TAKEOFF);
      }

      if (mMissionProxy.getItems().indexOf(itemProxy) != (mMissionProxy.getItems().size() - 1)) {
        list.remove(MissionItemType.LAND);
        list.remove(MissionItemType.RETURN_TO_LAUNCH);
      }

      if (currentItem instanceof MissionItem.Command) {
        list.remove(MissionItemType.LAND);
        list.remove(MissionItemType.SPLINE_WAYPOINT);
        list.remove(MissionItemType.CIRCLE);
        list.remove(MissionItemType.REGION_OF_INTEREST);
        list.remove(MissionItemType.WAYPOINT);
        list.remove(MissionItemType.STRUCTURE_SCANNER);
      }

      final TextView waypointIndex = (TextView) view.findViewById(R.id.WaypointIndex);
      if (waypointIndex != null) {
        final int itemOrder = mMissionProxy.getOrder(itemProxy);
        waypointIndex.setText(String.valueOf(itemOrder));
      }

      final TextView distanceView = (TextView) view.findViewById(R.id.DistanceValue);
      if (distanceView != null) {
        distanceView.setText(
            getLengthUnitProvider()
                .boxBaseValueToTarget(mMissionProxy.getDistanceFromLastWaypoint(itemProxy))
                .toString());
      }

      final TextView distanceLabelView = (TextView) view.findViewById(R.id.DistanceLabel);
      if (distanceLabelView != null) {
        distanceLabelView.setVisibility(View.VISIBLE);
      }
    } else if (mSelectedProxies.size() > 1) {
      // Remove the mission item types that don't apply to multiple items.
      list.removeAll(typeWithNoMultiEditSupport);
    } else {
      // Invalid state. We should not have been able to get here.
      throw new IllegalStateException(
          "Mission Detail Fragment cannot be shown when no " + "mission items is selected.");
    }

    commandAdapter =
        new AdapterMissionItems(
            getActivity(),
            android.R.layout.simple_list_item_1,
            list.toArray(new MissionItemType[list.size()]));

    typeSpinner = (SpinnerSelfSelect) view.findViewById(R.id.spinnerWaypointType);
    typeSpinner.setAdapter(commandAdapter);
    typeSpinner.setOnSpinnerItemSelectedListener(this);
  }