Program: 149069
Project: 149069
Buggy source:
public IStatus doAction(){
  try {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    XSDSimpleTypeDefinition typedef=(XSDSimpleTypeDefinition)selection.getFirstElement();
    if (schema.getSchemaForSchemaNamespace().equals(typedef.getTargetNamespace()))     return Status.CANCEL_STATUS;
    ArrayList customTypes=new ArrayList();
    for (Iterator iter=schema.getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition)       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(),"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) {
    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();
    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)       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(),"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) {
    e.printStackTrace();
    MessageDialog.openError(page.getSite().getShell(),"String_Node_Str","String_Node_Str" + e.getLocalizedMessage());
    return Status.CANCEL_STATUS;
  }
  return Status.OK_STATUS;
}