/** Called to operate on the CharacterData blocks. Returns true if taxa altered */
  public boolean operateOnDatas(ListableVector datas, MesquiteTable table) {
    boolean anyExcluded = false;
    for (int im = 0; im < datas.size(); im++) {
      CharacterData data = (CharacterData) datas.elementAt(im);
      if (data.numCharsCurrentlyIncluded() < data.getNumChars()) anyExcluded = true;
    }
    if (anyExcluded) queryOptions();
    if (getProject() != null) getProject().incrementProjectWindowSuppression();
    for (int im = 0; im < datas.size(); im++) {
      CharacterData data = (CharacterData) datas.elementAt(im);
      CharacterData starter = data.makeCharacterData(data.getMatrixManager(), data.getTaxa());

      starter.addToFile(
          getProject().getHomeFile(), getProject(), findElementManager(CharacterData.class));

      boolean success =
          starter.concatenate(data, false, duplicateExcludedCharacters, false, false, false, false);
      if (success) {
        starter.setName(datas.getUniqueName(data.getName() + " (duplicate)"));
      }
    }
    if (getProject() != null) getProject().decrementProjectWindowSuppression();
    resetAllMenuBars();
    return true;
  }
  /*.................................................................................................................*/
  void zapData(CharacterData data, int it) {
    Taxa taxa = data.getTaxa();
    if (it < 0 || it >= taxa.getNumTaxa()) return;
    Associable tInfo = data.getTaxaInfo(false);
    int myColumn = -1;
    if (getEmployer() instanceof ListModule) {

      myColumn = ((ListModule) getEmployer()).getMyColumn(this);
    }
    if (tInfo != null) tInfo.deassignAssociated(it);
    for (int ic = 0; ic < data.getNumChars(); ic++) data.deassign(ic, it);

    data.notifyListeners(this, new Notification(MesquiteListener.DATA_CHANGED));
    outputInvalid();
    parametersChanged();
  }
  /*.................................................................................................................*/
  public boolean arrowTouchInRow(
      Graphics g, int ic, int x, int y, boolean doubleClick, int modifiers) {
    if (MesquiteEvent.rightClick(modifiers)) {
      MesquitePopup popup = new MesquitePopup(table.getMatrixPanel());

      String copyMenuText = "Copy ";
      if (observedStates != null) {
        CharacterData data = observedStates.getParentData();
        if (data != null) {
          copyMenuText += data.getName() + " Data";
          copyMenuText += " [from " + data.getTaxa().getTaxonName(ic) + "]";
        }
      }
      MesquiteCommand mcCopy = makeCommand("copyData", this);
      mcCopy.setDefaultArguments("" + ic);
      MesquiteCheckMenuItem mCopyItem =
          new MesquiteCheckMenuItem(copyMenuText, this, mcCopy, null, null);
      popup.add(mCopyItem);

      String pasteMenuText = "Paste ";
      if (StringUtil.notEmpty(localCopyDataClipboard) && localCopyData != null) {
        pasteMenuText += localCopyData.getName() + " Data";
        if (StringUtil.notEmpty(localCopyDataTaxon)) {
          pasteMenuText += " [from " + localCopyDataTaxon + "] ";
        }
      }
      MesquiteCommand mcPaste = makeCommand("pasteData", this); // only if something in clipboard
      mcPaste.setDefaultArguments("" + ic);
      MesquiteCheckMenuItem mPasteItem =
          new MesquiteCheckMenuItem(pasteMenuText, this, mcPaste, null, null);
      mPasteItem.setEnabled(StringUtil.notEmpty(localCopyDataClipboard));
      popup.add(mPasteItem);

      MesquiteCommand mcDelete = makeCommand("deleteDataTouched", this);
      mcDelete.setDefaultArguments("" + ic);
      MesquiteCheckMenuItem mDeleteItem =
          new MesquiteCheckMenuItem("Delete Data", this, mcDelete, null, null);
      popup.add(mDeleteItem);

      popup.showPopup(x, y + 18);

      return true;
    }
    return false;
  }
  /*.................................................................................................................*/
  public Object doCommand(String commandName, String arguments, CommandChecker checker) {
    if (checker.compare(
        this.getClass(), "Returns the matrix source", null, commandName, "getMatrixSource")) {
      return matrixSourceTask;
    } else if (checker.compare(
        this.getClass(), "Copies the data for selected taxon", null, commandName, "copyData")) {
      if (observedStates == null) return null;
      CharacterData data = observedStates.getParentData();
      if (data == null) return null;
      int it = MesquiteInteger.fromString(parser.getFirstToken(arguments));
      if (MesquiteInteger.isCombinable(it)) {
        StringBuffer sb = new StringBuffer();
        data.copyDataFromRowIntoBuffer(it, sb);
        if (StringUtil.notEmpty(sb.toString())) {
          localCopyDataClipboard = sb.toString();
          localCopyData = data;
          localCopyDataTaxon = data.getTaxa().getTaxonName(it);
        } else {
          localCopyDataClipboard = null;
          localCopyData = null;
          localCopyDataTaxon = null;
        }
      }
      return null;
    } else if (checker.compare(
        this.getClass(), "Pastes the data for selected taxon", null, commandName, "pasteData")) {
      if (observedStates == null) return null;
      CharacterData data = observedStates.getParentData();
      if (data == null) return null;
      int it = MesquiteInteger.fromString(parser.getFirstToken(arguments));
      if (MesquiteInteger.isCombinable(it) && StringUtil.notEmpty(localCopyDataClipboard)) {
        data.pasteDataFromStringIntoTaxon(it, localCopyDataClipboard);
      }
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Pastes the data for selected taxon",
        null,
        commandName,
        "deleteDataTouched")) {
      if (observedStates == null) return null;
      CharacterData data = observedStates.getParentData();
      if (data == null) return null;
      int it = MesquiteInteger.fromString(parser.getFirstToken(arguments));
      Debugg.println("prepare to delete row: " + it);
      if (MesquiteInteger.isCombinable(it)) {
        if (!AlertDialog.query(
            containerOfModule(),
            "Delete Data?",
            "Are you sure you want to delete the data for taxon "
                + data.getTaxa().getTaxonName(it)
                + " in the matrix \""
                + data.getName()
                + "\"",
            "No",
            "Yes")) {
          zapData(data, it);
        }
      }
      return null;
    } else if (checker.compare(
        this.getClass(), "Deletes the data for selected taxa", null, commandName, "deleteData")) {
      if (observedStates == null) return null;
      captureCharacterDataFromObservedStates();
      if (data == null) return null;
      if (!AlertDialog.query(
          containerOfModule(),
          "Delete Data?",
          "Are you sure you want to delete the data for these taxa in the matrix \""
              + data.getName()
              + "\"",
          "No",
          "Yes")) zapData(data);
      return null;
    } else if (checker.compare(
        this.getClass(),
        "deleteds () and anything between",
        null,
        commandName,
        "deletePrepended")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }

      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if ((!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          while (!StringUtil.blank(note) && note.indexOf("(") >= 0) {
            int start = note.indexOf("(");
            int end = note.indexOf(")");
            String firstBit = "";
            if (start > 0) firstBit = note.substring(0, start);
            note = firstBit + note.substring(end + 1, note.length());
          }
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(this.getClass(), "deletes *", null, commandName, "deleteStar")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if ((!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          while (!StringUtil.blank(note) && note.indexOf("*") >= 0) {
            int start = note.indexOf("*");
            String firstBit = "";
            if (start > 0) firstBit = note.substring(0, start);
            note = firstBit + note.substring(start + 1, note.length());
          }
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Prepends to the note the sequence length (including N\'s and ?\'s) for the selected taxa",
        null,
        commandName,
        "prependLength")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          if (StringUtil.blank(note)) note = "(" + sequenceLength(it) + ")";
          else note = "(" + sequenceLength(it) + ") " + note;
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Prepends to the note the number of non-missing sites (not including N\'s and ?\'s) for the selected taxa",
        null,
        commandName,
        "prependNumSites")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) {
          String note = getNote(it);
          if (StringUtil.blank(note)) note = "(" + numSites(it) + ")";
          else note = "(" + numSites(it) + ") " + note;
          setNote(it, note);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else if (checker.compare(
        this.getClass(),
        "Deletes the notes for the selected taxa",
        null,
        commandName,
        "deleteAnnotation")) {
      if (observedStates == null || taxa == null) return null;
      boolean anySelected = taxa.anySelected();
      int myColumn = -1;
      if (getEmployer() instanceof ListModule) {

        myColumn = ((ListModule) getEmployer()).getMyColumn(this);
        if (table != null)
          anySelected = anySelected || table.anyCellSelectedInColumnAnyWay(myColumn);
      }
      for (int it = 0; it < taxa.getNumTaxa(); it++) {
        if (hasData(it) && (!anySelected || selected(taxa, it, myColumn))) {
          setNote(it, null);
        }
      }
      outputInvalid();
      parametersChanged();
      return null;
    } else return super.doCommand(commandName, arguments, checker);
  }