Program: 148978
Project: 148978
Buggy source:
public IStatus doAction(){
  try {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    XSDParticle selParticle=(XSDParticle)selection.getFirstElement();
    if (!(selParticle.getTerm() instanceof XSDElementDeclaration))     return Status.CANCEL_STATUS;
    XSDElementDeclaration decl=(XSDElementDeclaration)selParticle.getContent();
    XSDElementDeclaration ref=null;
    if (decl.isElementDeclarationReference()) {
      ref=decl.getResolvedElementDeclaration();
    }
    EList eDecls=decl.getSchema().getElementDeclarations();
    ArrayList<String> elementDeclarations=new ArrayList<String>();
    for (Iterator iter=eDecls.iterator(); iter.hasNext(); ) {
      XSDElementDeclaration d=(XSDElementDeclaration)iter.next();
      elementDeclarations.add(d.getQName() + (d.getTargetNamespace() != null ? "String_Node_Str" + d.getTargetNamespace() : "String_Node_Str"));
    }
    elementDeclarations.add("String_Node_Str");
    IStructuredContentProvider provider=(IStructuredContentProvider)page.getTreeViewer().getContentProvider();
    XSDIdentityConstraintDefinition identify=null;
    XSDXPathDefinition keyPath=null;
    List<Object> keyInfo=Util.getKeyInfo(decl);
    if (keyInfo != null && keyInfo.size() > 0) {
      identify=(XSDIdentityConstraintDefinition)keyInfo.get(0);
      keyPath=(XSDXPathDefinition)keyInfo.get(1);
      identify.getFields().remove(keyPath);
    }
    dialog=new BusinessElementInputDialog(this,page.getSite().getShell(),"String_Node_Str",decl.getName(),ref == null ? null : ref.getQName(),elementDeclarations,selParticle.getMinOccurs(),selParticle.getMaxOccurs());
    dialog.setBlockOnOpen(true);
    int ret=dialog.open();
    if (ret == Window.CANCEL) {
      return Status.CANCEL_STATUS;
    }
    XSDElementDeclaration newRef=null;
    if (!"String_Node_Str".equals(refName.trim())) {
      newRef=Util.findReference(refName,schema);
      if (newRef == null) {
        MessageDialog.openError(this.page.getSite().getShell(),"String_Node_Str","String_Node_Str" + refName + "String_Node_Str");
        return Status.CANCEL_STATUS;
      }
    }
    decl.setName("String_Node_Str".equals(this.elementName) ? null : this.elementName);
    if (keyPath != null) {
      XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
      XSDXPathDefinition field=factory.createXSDXPathDefinition();
      field.setVariety(keyPath.getVariety());
      field.setValue(elementName);
      identify.getFields().add(field);
    }
    if (newRef != null) {
      decl.setResolvedElementDeclaration(newRef);
      decl.setTypeDefinition(null);
      Element elem=decl.getElement();
      if (elem.getAttributes().getNamedItem("String_Node_Str") != null)       elem.getAttributes().removeNamedItem("String_Node_Str");
      decl.updateElement();
    }
 else     if (ref != null) {
      XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
      XSDElementDeclaration newD=(XSDElementDeclaration)factory.createXSDElementDeclaration();
      newD.setName(this.elementName);
      newD.updateElement();
      XSDSimpleTypeDefinition stringType=((XSDTreeContentProvider)page.getTreeViewer().getContentProvider()).getXsdSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001,"String_Node_Str");
      newD.setTypeDefinition(stringType);
      if (selParticle.getContainer() instanceof XSDModelGroup) {
        XSDModelGroup group=((XSDModelGroup)selParticle.getContainer());
        ((XSDModelGroup)selParticle.getContainer()).getContents().remove(selParticle);
        selParticle=factory.createXSDParticle();
        selParticle.setContent(newD);
        group.getContents().add(selParticle);
      }
    }
    selParticle.setMinOccurs(this.minOccurs);
    selParticle.setMaxOccurs(this.maxOccurs);
    selParticle.updateElement();
    page.refresh();
    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 {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    XSDParticle selParticle=(XSDParticle)selection.getFirstElement();
    if (!(selParticle.getTerm() instanceof XSDElementDeclaration))     return Status.CANCEL_STATUS;
    XSDElementDeclaration decl=(XSDElementDeclaration)selParticle.getContent();
    XSDElementDeclaration ref=null;
    if (decl.isElementDeclarationReference()) {
      ref=decl.getResolvedElementDeclaration();
    }
    EList eDecls=decl.getSchema().getElementDeclarations();
    ArrayList<String> elementDeclarations=new ArrayList<String>();
    for (Iterator iter=eDecls.iterator(); iter.hasNext(); ) {
      XSDElementDeclaration d=(XSDElementDeclaration)iter.next();
      elementDeclarations.add(d.getQName() + (d.getTargetNamespace() != null ? "String_Node_Str" + d.getTargetNamespace() : "String_Node_Str"));
    }
    elementDeclarations.add("String_Node_Str");
    IStructuredContentProvider provider=(IStructuredContentProvider)page.getTreeViewer().getContentProvider();
    XSDIdentityConstraintDefinition identify=null;
    XSDXPathDefinition keyPath=null;
    List<Object> keyInfo=Util.getKeyInfo(decl);
    if (keyInfo != null && keyInfo.size() > 0) {
      identify=(XSDIdentityConstraintDefinition)keyInfo.get(0);
      keyPath=(XSDXPathDefinition)keyInfo.get(1);
      identify.getFields().remove(keyPath);
    }
    dialog=new BusinessElementInputDialog(this,page.getSite().getShell(),"String_Node_Str",decl.getName(),ref == null ? null : ref.getQName(),elementDeclarations,selParticle.getMinOccurs(),selParticle.getMaxOccurs(),false);
    dialog.setBlockOnOpen(true);
    int ret=dialog.open();
    if (ret == Window.CANCEL) {
      return Status.CANCEL_STATUS;
    }
    XSDElementDeclaration newRef=null;
    if (!"String_Node_Str".equals(refName.trim())) {
      newRef=Util.findReference(refName,schema);
      if (newRef == null) {
        MessageDialog.openError(this.page.getSite().getShell(),"String_Node_Str","String_Node_Str" + refName + "String_Node_Str");
        return Status.CANCEL_STATUS;
      }
    }
    decl.setName("String_Node_Str".equals(this.elementName) ? null : this.elementName);
    if (keyPath != null) {
      XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
      XSDXPathDefinition field=factory.createXSDXPathDefinition();
      field.setVariety(keyPath.getVariety());
      field.setValue(elementName);
      identify.getFields().add(field);
    }
    if (newRef != null) {
      decl.setResolvedElementDeclaration(newRef);
      decl.setTypeDefinition(null);
      Element elem=decl.getElement();
      if (elem.getAttributes().getNamedItem("String_Node_Str") != null)       elem.getAttributes().removeNamedItem("String_Node_Str");
      decl.updateElement();
    }
 else     if (ref != null) {
      XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
      XSDElementDeclaration newD=(XSDElementDeclaration)factory.createXSDElementDeclaration();
      newD.setName(this.elementName);
      newD.updateElement();
      XSDSimpleTypeDefinition stringType=((XSDTreeContentProvider)page.getTreeViewer().getContentProvider()).getXsdSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001,"String_Node_Str");
      newD.setTypeDefinition(stringType);
      if (selParticle.getContainer() instanceof XSDModelGroup) {
        XSDModelGroup group=((XSDModelGroup)selParticle.getContainer());
        ((XSDModelGroup)selParticle.getContainer()).getContents().remove(selParticle);
        selParticle=factory.createXSDParticle();
        selParticle.setContent(newD);
        group.getContents().add(selParticle);
      }
    }
    selParticle.setMinOccurs(this.minOccurs);
    selParticle.setMaxOccurs(this.maxOccurs);
    selParticle.updateElement();
    page.refresh();
    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;
}