Search in sources :

Example 26 with XSDComplexTypeContent

use of org.eclipse.xsd.XSDComplexTypeContent in project tmdm-studio-se by Talend.

the class XSDNewIdentityConstraintAction method doAction.

@Override
public IStatus doAction() {
    try {
        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;
        }
        // $NON-NLS-1$
        childNames = Util.getChildElementNames("", decl);
        // 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);
        // $NON-NLS-1$
        selector.setValue(".");
        icd.setSelector(selector);
        XSDXPathDefinition field = factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        // $NON-NLS-1$
        field.setValue(".");
        // 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;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IdentityConstraintInputDialog(com.amalto.workbench.dialogs.IdentityConstraintInputDialog) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle)

Aggregations

XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)26 XSDParticle (org.eclipse.xsd.XSDParticle)24 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)22 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)20 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)16 ArrayList (java.util.ArrayList)10 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)10 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)9 XSDParticleContent (org.eclipse.xsd.XSDParticleContent)8 XSDTerm (org.eclipse.xsd.XSDTerm)8 EList (org.eclipse.emf.common.util.EList)7 XSDSchema (org.eclipse.xsd.XSDSchema)7 TreeObject (com.amalto.workbench.models.TreeObject)6 Iterator (java.util.Iterator)6 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)6 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)5 List (java.util.List)4 EObject (org.eclipse.emf.ecore.EObject)4 XSDAttributeUse (org.eclipse.xsd.XSDAttributeUse)4 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)4