@Override
  public void setSelection(int position) {
    if (position == getSelectedItemPosition()) {
      return;
    }
    if (mVariableField == null) {
      return;
    }
    int type = ((VariableViewAdapter) getAdapter()).getVariableAction(position);

    switch (type) {
      case VariableViewAdapter.ACTION_SELECT_VARIABLE:
        super.setSelection(position);
        if (mVariableField != null) {
          String varName = getAdapter().getItem(position).toString();
          mVariableField.setVariable(varName);
        }
        break;
      case VariableViewAdapter.ACTION_RENAME_VARIABLE:
        if (mCallback != null) {
          mCallback.onVariableRequest(
              VariableRequestCallback.REQUEST_RENAME, mVariableField.getVariable());
        }
        break;
      case VariableViewAdapter.ACTION_DELETE_VARIABLE:
        if (mCallback != null) {
          mCallback.onVariableRequest(
              VariableRequestCallback.REQUEST_DELETE, mVariableField.getVariable());
        }
        break;
    }
  }
  @Override
  public void setField(Field field) {
    FieldVariable variableField = (FieldVariable) field;

    if (mVariableField == variableField) {
      return;
    }

    if (mVariableField != null) {
      mVariableField.unregisterObserver(mFieldObserver);
    }
    mVariableField = variableField;
    if (mVariableField != null) {
      // Update immediately.
      refreshSelection();
      mVariableField.registerObserver(mFieldObserver);
    } else {
      setSelection(0);
    }
  }