Program: 148832
Project: 148832
Buggy source:
public IStatus doAction(){
  try {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    isConcept=false;
    if (selection.getFirstElement() instanceof XSDModelGroup) {
      TreePath tPath=((TreeSelection)selection).getPaths()[0];
      for (int i=0; i < tPath.getSegmentCount(); i++) {
        if (tPath.getSegment(i) instanceof XSDElementDeclaration)         decl=(XSDElementDeclaration)tPath.getSegment(i);
 else         if (tPath.getSegment(i) instanceof XSDParticle)         decl=(XSDElementDeclaration)((XSDParticle)tPath.getSegment(i)).getTerm();
      }
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDElementDeclaration || declNew != null) {
      decl=(XSDElementDeclaration)selection.getFirstElement();
      if (declNew != null) {
        decl=declNew;
      }
      checkConcept();
    }
 else {
      if (selection.getFirstElement() != null) {
        decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getTerm();
      }
    }
    if (showDlg) {
      dialog=new ComplexTypeInputDialog(this,page.getSite().getShell(),schema,Util.getComplexTypes(decl.getSchema()),isXSDModelGroup);
      dialog.setBlockOnOpen(true);
      int ret=dialog.open();
      if (ret == Dialog.CANCEL) {
        return Status.CANCEL_STATUS;
      }
    }
    if (!showDlg && !validateType()) {
      return Status.CANCEL_STATUS;
    }
    XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
    boolean anonymous=(typeName == null) || ("String_Node_Str".equals(typeName));
    boolean alreadyExists=false;
    XSDComplexTypeDefinition complexType=null;
    XSDParticle subParticle=null;
    XSDParticle groupParticle=null;
    XSDElementDeclaration subElement=null;
    if (!anonymous) {
      EList list=schema.getTypeDefinitions();
      String ns="String_Node_Str";
      if (typeName.lastIndexOf("String_Node_Str") != -1) {
        ns=typeName.substring(typeName.lastIndexOf("String_Node_Str") + 3);
        typeName=typeName.substring(0,typeName.lastIndexOf("String_Node_Str"));
      }
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDTypeDefinition td=(XSDTypeDefinition)iter.next();
        if ((td.getName().equals(typeName) && (td instanceof XSDComplexTypeDefinition))) {
          alreadyExists=true;
          complexType=(XSDComplexTypeDefinition)td;
          break;
        }
      }
    }
    if (!alreadyExists) {
      subElement=factory.createXSDElementDeclaration();
      subElement.setName("String_Node_Str");
      subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
      subParticle=factory.createXSDParticle();
      subParticle.setMinOccurs(1);
      subParticle.setMaxOccurs(1);
      subParticle.setContent(subElement);
      subParticle.updateElement();
      XSDModelGroup group=factory.createXSDModelGroup();
      if (isChoice)       group.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll)       group.setCompositor(XSDCompositor.ALL_LITERAL);
 else       group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
      group.getContents().add(0,subParticle);
      group.updateElement();
      complexType=factory.createXSDComplexTypeDefinition();
      if (!anonymous) {
        complexType.setName(typeName);
        schema.getContents().add(complexType);
      }
      complexType.updateElement();
      groupParticle=factory.createXSDParticle();
      groupParticle.setMinOccurs(1);
      groupParticle.setMaxOccurs(1);
      groupParticle.setContent(group);
      groupParticle.updateElement();
      complexType.setContent(groupParticle);
      complexType.updateElement();
    }
    if (anonymous)     decl.setAnonymousTypeDefinition(complexType);
 else {
      decl.setTypeDefinition(complexType);
    }
    if (isConcept) {
      ArrayList keys=new ArrayList();
      EList list=decl.getIdentityConstraintDefinitions();
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDIdentityConstraintDefinition icd=(XSDIdentityConstraintDefinition)iter.next();
        if (icd.getIdentityConstraintCategory().equals(XSDIdentityConstraintCategory.UNIQUE_LITERAL))         keys.add(icd);
      }
      decl.getIdentityConstraintDefinitions().removeAll(keys);
      XSDElementDeclaration firstDecl=null;
      if (complexType.getContent() instanceof XSDParticle) {
        if (((XSDParticle)complexType.getContent()).getTerm() instanceof XSDModelGroup) {
          XSDModelGroup group=(XSDModelGroup)((XSDParticle)complexType.getContent()).getTerm();
          EList gpl=group.getContents();
          for (Iterator iter=gpl.iterator(); iter.hasNext(); ) {
            XSDParticle part=(XSDParticle)iter.next();
            if (part.getTerm() instanceof XSDElementDeclaration) {
              firstDecl=(XSDElementDeclaration)part.getTerm();
              break;
            }
          }
        }
      }
      if (firstDecl != null) {
        XSDIdentityConstraintDefinition uniqueKey=factory.createXSDIdentityConstraintDefinition();
        uniqueKey.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
        uniqueKey.setName(decl.getName());
        XSDXPathDefinition selector=factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        selector.setValue("String_Node_Str");
        uniqueKey.setSelector(selector);
        XSDXPathDefinition field=factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        field.setValue(firstDecl.getAliasName());
        uniqueKey.getFields().add(field);
        decl.getIdentityConstraintDefinitions().add(uniqueKey);
      }
    }
    decl.updateElement();
    page.refresh();
    declNew=null;
    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();
    isConcept=false;
    if (selection.getFirstElement() instanceof XSDModelGroup) {
      TreePath tPath=((TreeSelection)selection).getPaths()[0];
      for (int i=0; i < tPath.getSegmentCount(); i++) {
        if (tPath.getSegment(i) instanceof XSDElementDeclaration)         decl=(XSDElementDeclaration)tPath.getSegment(i);
 else         if (tPath.getSegment(i) instanceof XSDParticle)         decl=(XSDElementDeclaration)((XSDParticle)tPath.getSegment(i)).getTerm();
      }
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDElementDeclaration || declNew != null) {
      decl=(XSDElementDeclaration)selection.getFirstElement();
      if (declNew != null) {
        decl=declNew;
      }
      checkConcept();
    }
 else {
      if (selection.getFirstElement() != null) {
        decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getTerm();
      }
    }
    if (showDlg) {
      dialog=new ComplexTypeInputDialog(this,page.getSite().getShell(),schema,decl.getTypeDefinition(),Util.getComplexTypes(decl.getSchema()),isXSDModelGroup);
      dialog.setBlockOnOpen(true);
      int ret=dialog.open();
      if (ret == Dialog.CANCEL) {
        return Status.CANCEL_STATUS;
      }
    }
    if (!showDlg && !validateType()) {
      return Status.CANCEL_STATUS;
    }
    XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
    boolean anonymous=(typeName == null) || ("String_Node_Str".equals(typeName));
    boolean alreadyExists=false;
    XSDComplexTypeDefinition complexType=null;
    XSDParticle subParticle=null;
    XSDParticle groupParticle=null;
    XSDElementDeclaration subElement=null;
    XSDElementDeclaration parent=(XSDElementDeclaration)Util.getParent(decl);
    if (!anonymous) {
      EList list=schema.getTypeDefinitions();
      String ns="String_Node_Str";
      if (typeName.lastIndexOf("String_Node_Str") != -1) {
        ns=typeName.substring(typeName.lastIndexOf("String_Node_Str") + 3);
        typeName=typeName.substring(0,typeName.lastIndexOf("String_Node_Str"));
      }
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDTypeDefinition td=(XSDTypeDefinition)iter.next();
        if ((td.getName().equals(typeName) && (td instanceof XSDComplexTypeDefinition))) {
          alreadyExists=true;
          complexType=(XSDComplexTypeDefinition)td;
          break;
        }
      }
    }
 else {
      complexType=(XSDComplexTypeDefinition)parent.getTypeDefinition();
      if (complexType.getName() == null) {
        alreadyExists=true;
      }
    }
    if (complexType != null) {
      XSDParticleImpl partCnt=(XSDParticleImpl)complexType.getContent();
      XSDModelGroupImpl mdlGrp=(XSDModelGroupImpl)partCnt.getTerm();
      if (isChoice)       mdlGrp.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll) {
        mdlGrp.setCompositor(XSDCompositor.ALL_LITERAL);
        partCnt.setMaxOccurs(1);
      }
 else {
        mdlGrp.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        partCnt.getElement().getAttributeNode("String_Node_Str").setNodeValue("String_Node_Str");
      }
      partCnt.setMinOccurs(1);
      parent.updateElement();
    }
    if (decl.getTypeDefinition() instanceof XSDSimpleTypeDefinition)     alreadyExists=false;
    if (!alreadyExists) {
      subElement=factory.createXSDElementDeclaration();
      subElement.setName("String_Node_Str");
      subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
      subParticle=factory.createXSDParticle();
      subParticle.setMinOccurs(1);
      subParticle.setMaxOccurs(1);
      subParticle.setContent(subElement);
      subParticle.updateElement();
      XSDModelGroup group=factory.createXSDModelGroup();
      if (isChoice)       group.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll)       group.setCompositor(XSDCompositor.ALL_LITERAL);
 else       group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
      group.getContents().add(0,subParticle);
      group.updateElement();
      complexType=factory.createXSDComplexTypeDefinition();
      if (!anonymous) {
        complexType.setName(typeName);
        schema.getContents().add(complexType);
      }
      complexType.updateElement();
      groupParticle=factory.createXSDParticle();
      groupParticle.setMinOccurs(1);
      groupParticle.setMaxOccurs(1);
      groupParticle.setContent(group);
      groupParticle.updateElement();
      complexType.setContent(groupParticle);
      complexType.updateElement();
    }
    if (anonymous)     decl.setAnonymousTypeDefinition(complexType);
 else {
      decl.setTypeDefinition(complexType);
    }
    if (isConcept) {
      ArrayList keys=new ArrayList();
      EList list=decl.getIdentityConstraintDefinitions();
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDIdentityConstraintDefinition icd=(XSDIdentityConstraintDefinition)iter.next();
        if (icd.getIdentityConstraintCategory().equals(XSDIdentityConstraintCategory.UNIQUE_LITERAL))         keys.add(icd);
      }
      decl.getIdentityConstraintDefinitions().removeAll(keys);
      XSDElementDeclaration firstDecl=null;
      if (complexType.getContent() instanceof XSDParticle) {
        if (((XSDParticle)complexType.getContent()).getTerm() instanceof XSDModelGroup) {
          XSDModelGroup group=(XSDModelGroup)((XSDParticle)complexType.getContent()).getTerm();
          EList gpl=group.getContents();
          for (Iterator iter=gpl.iterator(); iter.hasNext(); ) {
            XSDParticle part=(XSDParticle)iter.next();
            if (part.getTerm() instanceof XSDElementDeclaration) {
              firstDecl=(XSDElementDeclaration)part.getTerm();
              break;
            }
          }
        }
      }
      if (firstDecl != null) {
        XSDIdentityConstraintDefinition uniqueKey=factory.createXSDIdentityConstraintDefinition();
        uniqueKey.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
        uniqueKey.setName(decl.getName());
        XSDXPathDefinition selector=factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        selector.setValue("String_Node_Str");
        uniqueKey.setSelector(selector);
        XSDXPathDefinition field=factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        field.setValue(firstDecl.getAliasName());
        uniqueKey.getFields().add(field);
        decl.getIdentityConstraintDefinitions().add(uniqueKey);
      }
    }
    decl.updateElement();
    page.refresh();
    declNew=null;
    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;
}