Program: 148392
Project: 148392
Buggy source:
public IStatus doAction(){
  try {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    XSDSimpleTypeDefinition typedef=(XSDSimpleTypeDefinition)selection.getFirstElement();
    ArrayList customTypes=new ArrayList();
    for (Iterator iter=schema.getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition) {
        if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null)         customTypes.add(type.getName());
      }
    }
    ArrayList builtInTypes=new ArrayList();
    for (Iterator iter=schema.getSchemaForSchema().getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition)       builtInTypes.add(type.getName());
    }
    dialog=new SimpleTypeInputDialog(this,page.getSite().getShell(),schema,"String_Node_Str",customTypes,builtInTypes);
    dialog.setBlockOnOpen(true);
    int ret=dialog.open();
    if (ret == Window.CANCEL) {
      return Status.CANCEL_STATUS;
    }
    XSDTypeDefinition current=typedef.getBaseTypeDefinition();
    if (builtIn) {
      typedef.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),typeName));
    }
 else {
      XSDSimpleTypeDefinition std=schema.resolveSimpleTypeDefinition(typeName);
      if (!schema.getTypeDefinitions().contains(std)) {
        std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
        schema.getContents().add(std);
      }
      typedef.setBaseTypeDefinition(std);
    }
    typedef.getFacetContents().removeAll(typedef.getFacetContents());
    typedef.updateElement();
    if (builtIn) {
      EList<XSDConstrainingFacet> constrainFacts=typedef.getBaseTypeDefinition().getFacetContents();
      for (      XSDConstrainingFacet fact : constrainFacts) {
        if (fact instanceof XSDPatternFacet) {
          XSDPatternFacet newFact=XSDSchemaBuildingTools.getXSDFactory().createXSDPatternFacet();
          newFact.setLexicalValue(((XSDPatternFacet)fact).getLexicalValue());
          typedef.getFacetContents().add(newFact);
        }
      }
      typedef.updateElement();
    }
    page.refresh();
    page.markDirty();
  }
 catch (  Exception e) {
    log.error(e.getStackTrace());
    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();
    XSDSimpleTypeDefinition typedef=(XSDSimpleTypeDefinition)selection.getFirstElement();
    ArrayList customTypes=new ArrayList();
    for (Iterator iter=schema.getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition) {
        if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null)         customTypes.add(type.getName());
      }
    }
    ArrayList builtInTypes=new ArrayList();
    for (Iterator iter=schema.getSchemaForSchema().getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition)       builtInTypes.add(type.getName());
    }
    dialog=new SimpleTypeInputDialog(this,page.getSite().getShell(),schema,"String_Node_Str",customTypes,builtInTypes);
    dialog.setBlockOnOpen(true);
    int ret=dialog.open();
    if (ret == Window.CANCEL) {
      return Status.CANCEL_STATUS;
    }
    XSDTypeDefinition current=typedef.getBaseTypeDefinition();
    if (builtIn) {
      typedef.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),typeName));
    }
 else {
      XSDSimpleTypeDefinition std=schema.resolveSimpleTypeDefinition(typeName);
      if (!schema.getTypeDefinitions().contains(std)) {
        std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
        schema.getContents().add(std);
      }
      typedef.setBaseTypeDefinition(std);
    }
    typedef.getFacetContents().removeAll(typedef.getFacetContents());
    typedef.updateElement();
    if (builtIn) {
      EList<XSDConstrainingFacet> constrainFacts=typedef.getBaseTypeDefinition().getFacetContents();
      for (      XSDConstrainingFacet fact : constrainFacts) {
        if (fact instanceof XSDPatternFacet) {
          XSDPatternFacet newFact=XSDSchemaBuildingTools.getXSDFactory().createXSDPatternFacet();
          newFact.setLexicalValue(((XSDPatternFacet)fact).getLexicalValue());
          typedef.getFacetContents().add(newFact);
        }
      }
      typedef.updateElement();
    }
    page.refresh();
    page.markDirty();
  }
 catch (  Exception e) {
    log.error(e.getMessage(),e);
    MessageDialog.openError(page.getSite().getShell(),"String_Node_Str","String_Node_Str" + e.getLocalizedMessage());
    return Status.CANCEL_STATUS;
  }
  return Status.OK_STATUS;
}