Example usage for org.eclipse.jface.dialogs MessageDialog openError

List of usage examples for org.eclipse.jface.dialogs MessageDialog openError

Introduction

In this page you can find the example usage for org.eclipse.jface.dialogs MessageDialog openError.

Prototype

public static void openError(Shell parent, String title, String message) 

Source Link

Document

Convenience method to open a standard error dialog.

Usage

From source file:com.amalto.workbench.actions.XSDEditParticleAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {//from w w  w .  j  av  a  2s  .  c  o m
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();

        String originalXpath = getOriginalXpath();
        String entity = originalXpath.substring(0, originalXpath.indexOf("/")); //$NON-NLS-1$

        selParticle = (XSDParticle) selection.getFirstElement();

        if (!(selParticle.getTerm() instanceof XSDElementDeclaration)) {
            return Status.CANCEL_STATUS;
        }

        XSDElementDeclaration decl = (XSDElementDeclaration) selParticle.getContent();

        XSDElementDeclaration ref = null;
        if (decl.isElementDeclarationReference()) {
            // it is a ref
            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();
            if (d.getTargetNamespace() != null
                    && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE)) {
                continue;
            }

            if (!d.getQName().equals(entity)) {
                elementDeclarations.add(
                        d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : "")); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        elementDeclarations.add(""); //$NON-NLS-1$

        XSDIdentityConstraintDefinition identify = null;

        XSDXPathDefinition keyPath = null;
        List<Object> keyInfo = Util.getKeyInfo(decl);
        boolean isPK = false;
        if (keyInfo != null && keyInfo.size() > 0) {
            identify = (XSDIdentityConstraintDefinition) keyInfo.get(0);
            keyPath = (XSDXPathDefinition) keyInfo.get(1);
            isPK = true;
        }
        initEleName = decl.getName();
        dialog = new BusinessElementInputDialog(this, page.getSite().getShell(),
                Messages.XSDEditParticleAction_InputDialogTitle, decl.getName(),
                ref == null ? null : ref.getQName(), elementDeclarations, selParticle.getMinOccurs(),
                selParticle.getMaxOccurs(), false, isPK);
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        if (keyPath != null) {
            identify.getFields().remove(keyPath);
        }
        // find reference
        XSDElementDeclaration newRef = null;
        if (!"".equals(refName.trim())) { //$NON-NLS-1$
            newRef = Util.findReference(refName, schema);
            if (newRef == null) {
                MessageDialog.openError(this.page.getSite().getShell(), Messages._Error,
                        Messages.bind(Messages.XSDEditParticleAction_ErrorMsg, refName));
                return Status.CANCEL_STATUS;
            }
        } // ref

        // update validation rule
        Set<String> paths = new HashSet<String>();
        Util.collectElementPaths((IStructuredContentProvider) page.getElementsViewer().getContentProvider(),
                page.getSite(), selParticle, paths, null);
        //

        decl.setName("".equals(this.elementName) ? null : this.elementName); //$NON-NLS-1$
        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("type") != null) {
                elem.getAttributes().removeNamedItem("type");//$NON-NLS-1$
            }
            decl.updateElement();
        } else if (ref != null) {
            // fliu
            // no more element declarations --> we create a new Declaration with String simpleType definition
            // instead
            // FIXME: dereferecning and element is buggy

            XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
            XSDElementDeclaration newD = factory.createXSDElementDeclaration();
            newD.setName(this.elementName);
            newD.updateElement();
            XSDSimpleTypeDefinition stringType = ((SchemaTreeContentProvider) page.getTreeViewer()
                    .getContentProvider()).getXsdSchema().getSchemaForSchema()
                            .resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, "string"); //$NON-NLS-1$

            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);
            }
        }

        if (Util.changeElementTypeToSequence(decl, maxOccurs) == Status.CANCEL_STATUS) {
            return Status.CANCEL_STATUS;
        }

        selParticle.setMinOccurs(this.minOccurs);
        if (maxOccurs > -1) {
            selParticle.setMaxOccurs(this.maxOccurs);
        } else {
            if (selParticle.getElement().getAttributeNode("maxOccurs") != null) {
                selParticle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");//$NON-NLS-1$//$NON-NLS-2$
            } else {
                selParticle.getElement().setAttribute("maxOccurs", "unbounded");//$NON-NLS-1$//$NON-NLS-2$
            }
        }

        selParticle.updateElement();

        updateReference(originalXpath);

        if (elementExAdapter != null) {
            elementExAdapter.renameElement(decl.getSchema(), paths, decl.getName());
        }
        if (mapinfoExAdapter != null) {
            mapinfoExAdapter.renameElementMapinfo(paths, decl.getName());
        }

        page.refresh();
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDEditParticleAction_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDEditParticleAction.java

License:Open Source License

public void widgetSelected(SelectionEvent e) {
    if (dialog.getReturnCode() == -1) {
        return; // there was a validation error
    }//from  w  ww. j a va 2 s . c om
    elementName = dialog.getElementName();
    refName = dialog.getRefName();
    minOccurs = dialog.getMinOccurs();
    maxOccurs = dialog.getMaxOccurs();

    // check that this element does not already exist
    XSDModelGroup group = (XSDModelGroup) selParticle.getContainer();
    // get position of the selected particle in the container
    for (Object element : group.getContents()) {
        XSDParticle p = (XSDParticle) element;
        if (p.getTerm() instanceof XSDElementDeclaration) {
            XSDElementDeclaration thisDecl = (XSDElementDeclaration) p.getTerm();
            if (thisDecl.getName().equals(elementName) && initEleName != null
                    && !initEleName.equalsIgnoreCase(elementName)) {
                MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                        Messages.bind(Messages.XSDEditParticleAction_ErrorMsg2, elementName));
                return;
            }
        }
    } // for
    dialog.close();
}

From source file:com.amalto.workbench.actions.XSDEditXPathAction.java

License:Open Source License

public IStatus doAction() {
    try {//from ww  w. j  a v  a  2 s.  co  m
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        XSDXPathDefinition xpath = (XSDXPathDefinition) selection.getFirstElement();
        icd = (XSDIdentityConstraintDefinition) xpath.getContainer();

        // InputDialog id = new InputDialog(
        // page.getSite().getShell(),
        // "Edit XPath",
        // "Enter a new XPath for the "+((xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))?"field":"selector"),
        // xpath.getValue(),
        // new IInputValidator() {
        // public String isValid(String newText) {
        // if ((newText==null) || "".equals(newText)) return "The XPath cannot be empty";
        // return null;
        // };
        // }
        // );
        // List<String> childNames = Util.getChildElementNames("", (XSDElementDeclaration) icd.getContainer());
        List<String> childNames = new ArrayList<String>();
        childNames.add("."); //$NON-NLS-1$
        SelectFieldDialog id = new SelectFieldDialog(page.getSite().getShell(),
                Messages.XSDEditXPathAction_DialogTitle, childNames, xpath.getValue());
        id.create();
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        String field = id.getField();
        if (field.length() == 0)
            return Status.CANCEL_STATUS;
        XSDXPathDefinition newXpath = XSDSchemaBuildingTools.getXSDFactory().createXSDXPathDefinition();
        newXpath.setValue(field);
        if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL)) {
            int index = icd.getFields().indexOf(xpath);
            newXpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
            icd.getFields().set(index, newXpath);
        } else {
            newXpath.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
            icd.setSelector(newXpath);
        }

        icd.updateElement();
        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(newXpath), true);
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDEditXPathAction_ErrorMsg, e.getLocalizedMessage()));

        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDGetXPathAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {//ww  w  .  j a v  a 2 s .  c o  m

        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        XSDParticle particle = (XSDParticle) selection.getFirstElement();
        XSDTerm term = particle.getTerm();

        if (!(term instanceof XSDElementDeclaration))
            return Status.CANCEL_STATUS;

        Clipboard clipboard = Util.getClipboard();

        String path = ""; //$NON-NLS-1$
        TreeItem item = page.getTreeViewer().getTree().getSelection()[0];
        do {
            XSDConcreteComponent component = (XSDConcreteComponent) item.getData();
            if (component instanceof XSDParticle) {
                if (((XSDParticle) component).getTerm() instanceof XSDElementDeclaration)
                    path = "/" + ((XSDElementDeclaration) ((XSDParticle) component).getTerm()).getName() + path; //$NON-NLS-1$
            } else if (component instanceof XSDElementDeclaration) {
                path = ((XSDElementDeclaration) component).getName() + path;
            }
            // System.out.println("          "+path+ "             $$"+component.toString()+"$$");
            item = item.getParentItem();
        } while (item != null);

        xpath = path;

        clipboard.setContents(new Object[] { path }, new Transfer[] { TextTransfer.getInstance() });
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDGetXPathAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewComplexTypeDefinition.java

License:Open Source License

private boolean validateType() {
    if (!"".equals(typeName)) {//$NON-NLS-1$
        EList<XSDTypeDefinition> list = schema.getTypeDefinitions();
        for (Iterator<XSDTypeDefinition> iter = list.iterator(); iter.hasNext();) {
            XSDTypeDefinition td = (XSDTypeDefinition) iter.next();
            if (td.getName().equals(typeName)) {
                if (td instanceof XSDComplexTypeDefinition) {
                    MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                            Messages.bind(Messages._ThisTypeHead, typeName));
                    return false;
                }//from ww  w.  j a  va2s  . c  o m
            }
        } // for
    } else {
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages._PleaseEnterCTypeName);
        return false;
    }
    return true;
}

From source file:com.amalto.workbench.actions.XSDNewConceptAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {/* ww w.  j  a v a 2s.  co m*/
        List<String> customTypes = new ArrayList<String>();
        for (Iterator<XSDTypeDefinition> iter = schema.getTypeDefinitions().iterator(); iter.hasNext();) {
            XSDTypeDefinition type = iter.next();
            if (type instanceof XSDSimpleTypeDefinition) {
                customTypes.add(type.getName()
                        + (type.getTargetNamespace() != null ? " : " + type.getTargetNamespace() : "")); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        List<String> builtInTypes = new ArrayList<String>();
        for (Iterator<XSDTypeDefinition> iter = schema.getSchemaForSchema().getTypeDefinitions()
                .iterator(); iter.hasNext();) {
            XSDTypeDefinition type = iter.next();
            if (type instanceof XSDSimpleTypeDefinition) {
                builtInTypes.add(type.getName());
            }
        }

        NewConceptOrElementDialog id = new NewConceptOrElementDialog(this, page.getSite().getShell(), schema,
                Messages.XSDNewConceptAction_NewEntity, customTypes, builtInTypes);

        id.setBlockOnOpen(true);
        id.open();

        if (id.getReturnCode() == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDNewConceptAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewElementAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {//  w w w .  j  a v a  2s  . c  o m
        List<String> customTypes = new ArrayList<String>();
        for (Iterator<XSDTypeDefinition> iter = schema.getTypeDefinitions().iterator(); iter.hasNext();) {
            XSDTypeDefinition type = iter.next();
            if (type instanceof XSDSimpleTypeDefinition) {
                customTypes.add(type.getName()
                        + (type.getTargetNamespace() != null ? " : " + type.getTargetNamespace() : ""));//$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        List<String> builtInTypes = new ArrayList<String>();
        for (Iterator<XSDTypeDefinition> iter = schema.getSchemaForSchema().getTypeDefinitions()
                .iterator(); iter.hasNext();) {
            XSDTypeDefinition type = iter.next();
            if (type instanceof XSDSimpleTypeDefinition) {
                builtInTypes.add(type.getName());
            }
        }

        NewConceptOrElementDialog id = new NewConceptOrElementDialog(this, page.getSite().getShell(), schema,
                Messages.XSDNewElementAction_Text, customTypes, builtInTypes);

        id.setBlockOnOpen(true);
        id.open();
        if (id.getReturnCode() == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDNewElementAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewGroupFromParticleAction.java

License:Open Source License

public IStatus doAction() {
    try {// w  w w .ja  va2 s . com
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        selParticle = (XSDParticle) selection.getFirstElement();

        if (!(selParticle.getContainer() instanceof XSDModelGroup))
            return Status.CANCEL_STATUS;

        XSDModelGroup group = (XSDModelGroup) selParticle.getContainer();
        // get position of the selected particle in the container
        int index = 0;
        int i = 0;
        for (Iterator iter = group.getContents().iterator(); iter.hasNext();) {
            XSDParticle p = (XSDParticle) iter.next();
            if (p.equals(selParticle)) {
                index = i;
                break;
            }
            i++;
        }

        dialog = new NewGroupDialog(this, page.getSite().getShell());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();

        // add an element declaration
        XSDElementDeclaration subElement = factory.createXSDElementDeclaration();
        subElement.setName("subelement");//$NON-NLS-1$
        subElement.setTypeDefinition(
                schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));//$NON-NLS-1$

        XSDParticle subParticle = factory.createXSDParticle();
        subParticle.setMinOccurs(1);
        subParticle.setMaxOccurs(1);
        subParticle.setContent(subElement);
        subParticle.updateElement();

        XSDModelGroup newGroup = factory.createXSDModelGroup();
        if (isChoice)
            newGroup.setCompositor(XSDCompositor.CHOICE_LITERAL);
        else if (isAll)
            newGroup.setCompositor(XSDCompositor.ALL_LITERAL);
        else
            newGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        newGroup.getContents().add(0, subParticle);
        newGroup.updateElement();

        XSDParticle particle = factory.createXSDParticle();
        particle.setContent(newGroup);
        particle.setMinOccurs(this.minOccurs);
        particle.setMaxOccurs(this.maxOccurs);

        group.getContents().add(index + 1, particle);
        group.updateElement();

        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(particle), true);

        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDNewGroupFromParticleAction_ErrorMsg, e.getLocalizedMessage()));

        return Status.CANCEL_STATUS;
    }

    return Status.CANCEL_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewGroupFromTypeAction.java

License:Open Source License

public IStatus doAction() {
    try {/* w  ww. j  ava  2 s.  c  o  m*/

        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) {
            XSDComplexTypeDefinition ctd = (XSDComplexTypeDefinition) selection.getFirstElement();
            if (!(ctd.getContent() instanceof XSDParticle))
                return Status.CANCEL_STATUS;
            if (!(((XSDParticle) ctd.getContent()).getTerm() instanceof XSDModelGroup))
                return Status.CANCEL_STATUS;
            group = (XSDModelGroup) ((XSDParticle) ctd.getContent()).getTerm();
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            group = (XSDModelGroup) ((XSDParticle) selection.getFirstElement()).getTerm();
        } else {
            return Status.CANCEL_STATUS;
        }

        dialog = new NewGroupDialog(this, page.getSite().getShell());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();

        // add an element declaration
        XSDElementDeclaration subElement = factory.createXSDElementDeclaration();
        subElement.setName("subelement");//$NON-NLS-1$
        subElement.setTypeDefinition(
                schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));//$NON-NLS-1$

        XSDParticle subParticle = factory.createXSDParticle();
        subParticle.setMinOccurs(1);
        subParticle.setMaxOccurs(1);
        subParticle.setContent(subElement);
        subParticle.updateElement();

        XSDModelGroup newGroup = factory.createXSDModelGroup();
        if (isChoice)
            newGroup.setCompositor(XSDCompositor.CHOICE_LITERAL);
        else if (isAll)
            newGroup.setCompositor(XSDCompositor.ALL_LITERAL);
        else
            newGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
        newGroup.getContents().add(0, subParticle);
        newGroup.updateElement();

        XSDParticle particle = factory.createXSDParticle();
        particle.setContent(newGroup);
        particle.setMinOccurs(this.minOccurs);
        particle.setMaxOccurs(this.maxOccurs);

        group.getContents().add(0, particle);
        group.updateElement();

        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(particle), true);
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDNewGroupFromTypeAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewIdentityConstraintAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {/*from w ww  .jav  a2  s  .c  o m*/

        int index = -1;
        // EList list = schema.getIdentityConstraintDefinitions();

        // schema.getElement();
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        List<String> childNames = new ArrayList<String>();
        if (selection.getFirstElement() instanceof XSDElementDeclaration) {
            decl = (XSDElementDeclaration) selection.getFirstElement();
            // childNames = Util.getChildElementNames(decl.getElement());
        }

        else if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
            XSDIdentityConstraintDefinition selIcd = (XSDIdentityConstraintDefinition) selection
                    .getFirstElement();
            decl = (XSDElementDeclaration) (selIcd.getContainer());

            // get position of the selected Identity Constraint in the container (Element Declaration)
            int i = 0;
            for (Iterator iter = decl.getIdentityConstraintDefinitions().iterator(); iter.hasNext();) {
                XSDIdentityConstraintDefinition ic = (XSDIdentityConstraintDefinition) iter.next();
                if (ic.equals(selIcd)) {
                    index = i;
                    break;
                }
                i++;
            }

        } else if (selection.getFirstElement() instanceof XSDParticle) {
            XSDParticle selParticle = (XSDParticle) selection.getFirstElement();
            if (!(selParticle.getTerm() instanceof XSDElementDeclaration))
                return Status.CANCEL_STATUS;
            decl = (XSDElementDeclaration) selParticle.getTerm();
            // childNames.add(decl.getName());

        } else {
            MessageDialog.openError(this.page.getSite().getShell(), Messages._Error,
                    Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg,
                            selection.getFirstElement().getClass().getName()));
            return Status.CANCEL_STATUS;
        }
        childNames = Util.getChildElementNames("", decl); //$NON-NLS-1$
        // filter the non top level fields
        List<String> topChilds = new ArrayList<String>();
        for (String child : childNames) {
            if (child.indexOf('/') == -1) {
                topChilds.add(child);
            }
        }
        dialog = new IdentityConstraintInputDialog(decl, page.getSite().getShell(),
                Messages.XSDNewIdentityConstraintAction_AddANewKey, topChilds, decl.getName());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        keyName = dialog.getKeyName();
        fieldName = dialog.getFieldName();
        type = dialog.getType();

        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();

        XSDIdentityConstraintDefinition icd = factory.createXSDIdentityConstraintDefinition();
        icd.setName(keyName);
        icd.setIdentityConstraintCategory(type);
        XSDXPathDefinition selector = factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        selector.setValue("."); //$NON-NLS-1$
        icd.setSelector(selector);
        XSDXPathDefinition field = factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        field.setValue("."); //$NON-NLS-1$
        // if complex content set name of first field
        if (fieldName == null || fieldName.trim().length() == 0) {
            if (decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                XSDComplexTypeContent ctc = ((XSDComplexTypeDefinition) decl.getTypeDefinition()).getContent();
                if (ctc instanceof XSDParticle) {
                    if (((XSDParticle) ctc).getTerm() instanceof XSDModelGroup) {
                        XSDModelGroup mg = (XSDModelGroup) ((XSDParticle) ctc).getTerm();
                        if (mg.getContents().size() > 0)
                            if (mg.getContents().get(0).getTerm() instanceof XSDElementDeclaration)
                                field.setValue(((XSDElementDeclaration) (mg.getContents().get(0).getTerm()))
                                        .getName());
                    }
                }
            }
        } else {
            field.setValue(fieldName);
        }
        icd.getFields().add(field);

        decl.getIdentityConstraintDefinitions().add(index + 1, icd);
        decl.updateElement();

        updateElementForAddedfield(icd, fieldName);

        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(icd), true);
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg2, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}