Program: 148729
Project: 148729
Buggy source:
public IStatus doAction(){
  try {
    int index=0;
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
      icd=(XSDIdentityConstraintDefinition)selection.getFirstElement();
    }
 else     if (selection.getFirstElement() instanceof XSDXPathDefinition) {
      XSDXPathDefinition xpath=(XSDXPathDefinition)selection.getFirstElement();
      icd=(XSDIdentityConstraintDefinition)xpath.getContainer();
      if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))       index=icd.getFields().indexOf(xpath) + 1;
 else       index=0;
    }
 else {
      MessageDialog.openError(this.page.getSite().getShell(),"String_Node_Str","String_Node_Str" + selection.getFirstElement().getClass().getName());
      return Status.CANCEL_STATUS;
    }
    List<String> childNames=Util.getChildElementNames((XSDElementDeclaration)icd.getContainer());
    SelectFieldDialog id=new SelectFieldDialog(page.getSite().getShell(),"String_Node_Str",childNames,null);
    id.create();
    id.setBlockOnOpen(true);
    int ret=id.open();
    if (ret == Dialog.CANCEL) {
      return Status.CANCEL_STATUS;
    }
    String field=id.getField();
    if (field.length() == 0)     return Status.CANCEL_STATUS;
    ;
    XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
    XSDXPathDefinition xpath=factory.createXSDXPathDefinition();
    xpath.setValue(field);
    xpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
    icd.getFields().add(index,xpath);
    icd.updateElement();
    page.refresh();
    page.getTreeViewer().setSelection(new StructuredSelection(xpath),true);
    page.markDirty();
  }
 catch (  Exception e) {
    e.printStackTrace();
    MessageDialog.openError(page.getSite().getShell(),"String_Node_Str","String_Node_Str" + e.getLocalizedMessage());
    return Status.CANCEL_STATUS;
  }
  return Status.OK_STATUS;
}

Fixed version (ground truth):
public IStatus doAction(){
  try {
    int index=0;
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
      icd=(XSDIdentityConstraintDefinition)selection.getFirstElement();
    }
 else     if (selection.getFirstElement() instanceof XSDXPathDefinition) {
      XSDXPathDefinition xpath=(XSDXPathDefinition)selection.getFirstElement();
      icd=(XSDIdentityConstraintDefinition)xpath.getContainer();
      if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))       index=icd.getFields().indexOf(xpath) + 1;
 else       index=0;
    }
 else {
      MessageDialog.openError(this.page.getSite().getShell(),"String_Node_Str","String_Node_Str" + selection.getFirstElement().getClass().getName());
      return Status.CANCEL_STATUS;
    }
    List<String> childNames=Util.getChildElementNames("String_Node_Str",(XSDElementDeclaration)icd.getContainer());
    SelectFieldDialog id=new SelectFieldDialog(page.getSite().getShell(),"String_Node_Str",childNames,null);
    id.create();
    id.setBlockOnOpen(true);
    int ret=id.open();
    if (ret == Dialog.CANCEL) {
      return Status.CANCEL_STATUS;
    }
    String field=id.getField();
    if (field.length() == 0)     return Status.CANCEL_STATUS;
    ;
    XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
    XSDXPathDefinition xpath=factory.createXSDXPathDefinition();
    xpath.setValue(field);
    xpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
    icd.getFields().add(index,xpath);
    icd.updateElement();
    page.refresh();
    page.getTreeViewer().setSelection(new StructuredSelection(xpath),true);
    page.markDirty();
  }
 catch (  Exception e) {
    e.printStackTrace();
    MessageDialog.openError(page.getSite().getShell(),"String_Node_Str","String_Node_Str" + e.getLocalizedMessage());
    return Status.CANCEL_STATUS;
  }
  return Status.OK_STATUS;
}