Search in sources :

Example 1 with SimpleTypeInputDialog

use of com.amalto.workbench.dialogs.SimpleTypeInputDialog in project tmdm-studio-se by Talend.

the class XSDChangeBaseTypeAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        XSDSimpleTypeDefinition typedef = (XSDSimpleTypeDefinition) selection.getFirstElement();
        // Cannot change the simple type definition of built in type
        // if (schema.getSchemaForSchemaNamespace().equals(typedef.getTargetNamespace())) return
        // Status.CANCEL_STATUS;
        // build list of custom types and built in types
        ArrayList customTypes = new ArrayList();
        for (Object element : schema.getTypeDefinitions()) {
            XSDTypeDefinition type = (XSDTypeDefinition) element;
            if (type instanceof XSDSimpleTypeDefinition) {
                if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null) {
                    customTypes.add(type.getName());
                }
            }
        }
        List builtInTypes = XSDUtil.getBuiltInTypes();
        // can't change builtin's base type
        if (builtInTypes.contains(typedef.getName())) {
            return Status.CANCEL_STATUS;
        }
        dialog = new SimpleTypeInputDialog(this, page.getSite().getShell(), schema, Messages.XSDChangeBaseTypeAction_DialogTitle, customTypes, builtInTypes, typedef.getBaseTypeDefinition().getName());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        // backup current Base Type
        XSDTypeDefinition current = typedef.getBaseTypeDefinition();
        // set new one
        if (builtIn) {
            typedef.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), typeName));
        } else {
            // check if simple type definition already exists
            XSDSimpleTypeDefinition std = schema.resolveSimpleTypeDefinition(typeName);
            if (!schema.getTypeDefinitions().contains(std)) {
                // $NON-NLS-1$
                std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
                schema.getContents().add(std);
            }
            typedef.setBaseTypeDefinition(std);
        }
        // remove current facets
        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();
        }
        // remove current if no more in use
        // if ( (current.getName()!=null) && //anonymous type
        // (!schema.getSchemaForSchemaNamespace().equals(current.getTargetNamespace()))
        // ){
        // List eut =Util.findElementsUsingType(schema, current.getTargetNamespace(), current.getName());
        // if (eut.size()==0)
        // schema.getContents().remove(current);
        // }
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDChangeBaseTypeAction_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDPatternFacet(org.eclipse.xsd.XSDPatternFacet) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) XSDConstrainingFacet(org.eclipse.xsd.XSDConstrainingFacet) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SimpleTypeInputDialog(com.amalto.workbench.dialogs.SimpleTypeInputDialog) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Example 2 with SimpleTypeInputDialog

use of com.amalto.workbench.dialogs.SimpleTypeInputDialog in project tmdm-studio-se by Talend.

the class XSDChangeToSimpleTypeAction method doAction.

@Override
public IStatus doAction() {
    try {
        XSDElementDeclaration decl = null;
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        // declNew is the new created one not the selected one in tree vew
        if (declNew != null) {
            decl = declNew;
        } else if (selection.getFirstElement() instanceof XSDElementDeclaration) {
            isConcept = true;
            decl = (XSDElementDeclaration) selection.getFirstElement();
        } else {
            isConcept = false;
            if (selection.getFirstElement() != null) {
                decl = (XSDElementDeclaration) ((XSDParticle) selection.getFirstElement()).getTerm();
            }
        }
        // build list of custom types and built in types
        List<String> customTypes = new ArrayList<String>();
        for (XSDTypeDefinition type : schema.getTypeDefinitions()) {
            if (type instanceof XSDSimpleTypeDefinition) {
                if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null) {
                    customTypes.add(type.getName());
                }
            }
        }
        List<String> builtInTypes = XSDUtil.getBuiltInTypes();
        if (showDlg) {
            String name = decl.getTypeDefinition().getName();
            if (decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                name = null;
                boolean confirm = MessageDialog.openConfirm(page.getSite().getShell(), Messages.Warning, Messages.XSDChangeToCXX_ChangeToAnotherTypeWarning);
                if (!confirm) {
                    return Status.CANCEL_STATUS;
                }
            }
            dialog = new SimpleTypeInputDialog(this, page.getSite().getShell(), schema, Messages.XSDChangeToXX_DialogTitle, customTypes, builtInTypes, name);
            dialog.setBlockOnOpen(true);
            int ret = dialog.open();
            if (ret == Window.CANCEL) {
                return Status.CANCEL_STATUS;
            }
        }
        // remove all unique keys and make new one
        if (isConcept) {
            // remove exisitng unique key(s)
            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);
            // add new unique Key
            XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
            XSDIdentityConstraintDefinition uniqueKey = factory.createXSDIdentityConstraintDefinition();
            uniqueKey.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
            uniqueKey.setName(decl.getName());
            XSDXPathDefinition selector = factory.createXSDXPathDefinition();
            selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
            // $NON-NLS-1$
            selector.setValue(".");
            uniqueKey.setSelector(selector);
            XSDXPathDefinition field = factory.createXSDXPathDefinition();
            field.setVariety(XSDXPathVariety.FIELD_LITERAL);
            // $NON-NLS-1$
            field.setValue(".");
            uniqueKey.getFields().add(field);
            decl.getIdentityConstraintDefinitions().add(uniqueKey);
        }
        // Save current type definition
        XSDTypeDefinition current = decl.getTypeDefinition();
        // set new one
        if (builtIn) {
            decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), typeName));
        } else {
            // check if concept already exists
            if (typeName != null && typeName.length() > 0) {
                XSDSimpleTypeDefinition std = null;
                // $NON-NLS-1$
                String ns = "";
                if (typeName.lastIndexOf(" : ") != -1) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    ns = typeName.substring(typeName.lastIndexOf(" : ") + 3);
                    // $NON-NLS-1$
                    typeName = typeName.substring(0, typeName.lastIndexOf(" : "));
                }
                for (XSDTypeDefinition typeDef : schema.getTypeDefinitions()) {
                    if (typeDef instanceof XSDSimpleTypeDefinition) {
                        if (typeDef.getName().equals(typeName)) {
                            std = (XSDSimpleTypeDefinition) typeDef;
                            break;
                        }
                    }
                }
                if (std == null) {
                    std = schema.resolveSimpleTypeDefinition(typeName);
                    std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), // $NON-NLS-1$
                    "string"));
                    if (typeName.equals(EUUIDCustomType.MULTI_LINGUAL.getName())) {
                        XSDPatternFacet f = XSDSchemaBuildingTools.getXSDFactory().createXSDPatternFacet();
                        // $NON-NLS-1$
                        f.setLexicalValue("(\\[\\w+\\:[^\\[\\]]*\\]){0,}");
                        std.getFacetContents().add(f);
                    }
                    schema.getContents().add(std);
                }
                decl.setTypeDefinition(std);
            } else {
                XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
                simpleType = factory.createXSDSimpleTypeDefinition();
                simpleType.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), // $NON-NLS-1$
                "string"));
                decl.setAnonymousTypeDefinition(simpleType);
            }
        }
        decl.updateElement();
        // remove current if no more in use
        // if (current != null) {
        // if ( (current.getName()!=null) && //anonymous type
        // (!schema.getSchemaForSchemaNamespace().equals(current.getTargetNamespace()))
        // ){
        // List eut =Util.findElementsUsingType(schema, current.getTargetNamespace(), current.getName());
        // if (eut.size()==0)
        // schema.getContents().remove(current);
        // }
        // }
        declNew = null;
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDChangeToXX_ErrorMsg1, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDPatternFacet(org.eclipse.xsd.XSDPatternFacet) XSDFactory(org.eclipse.xsd.XSDFactory) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) EList(org.eclipse.emf.common.util.EList) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) SimpleTypeInputDialog(com.amalto.workbench.dialogs.SimpleTypeInputDialog) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition)

Example 3 with SimpleTypeInputDialog

use of com.amalto.workbench.dialogs.SimpleTypeInputDialog in project tmdm-studio-se by Talend.

the class XSDNewSimpleTypeDefinition method doAction.

@Override
public IStatus doAction() {
    List<String> customTypes = new ArrayList<String>();
    List<String> builtInTypes = XSDUtil.getBuiltInTypes();
    dialog = new SimpleTypeInputDialog(this, page.getSite().getShell(), schema, Messages.XSDNewSimpleTypeDefinition_NewSimpleType, customTypes, builtInTypes, null);
    dialog.setBlockOnOpen(true);
    int ret = dialog.open();
    if (ret == Dialog.CANCEL) {
        return Status.CANCEL_STATUS;
    }
    XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
    XSDSimpleTypeDefinition typedef = factory.createXSDSimpleTypeDefinition();
    // set new one
    if (!builtIn) {
        // check if simple type definition already exists
        XSDSimpleTypeDefinition std = schema.resolveSimpleTypeDefinition(typeName);
        if (!schema.getTypeDefinitions().contains(std)) {
            // $NON-NLS-1$
            std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), "string"));
            schema.getContents().add(std);
        }
        typedef.setBaseTypeDefinition(std);
    }
    // remove current facets
    typedef.getFacetContents().removeAll(typedef.getFacetContents());
    typedef.updateElement();
    page.refresh();
    page.markDirty();
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) ArrayList(java.util.ArrayList) SimpleTypeInputDialog(com.amalto.workbench.dialogs.SimpleTypeInputDialog)

Aggregations

SimpleTypeInputDialog (com.amalto.workbench.dialogs.SimpleTypeInputDialog)3 ArrayList (java.util.ArrayList)3 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)3 EList (org.eclipse.emf.common.util.EList)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 XSDFactory (org.eclipse.xsd.XSDFactory)2 XSDPatternFacet (org.eclipse.xsd.XSDPatternFacet)2 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)2 Iterator (java.util.Iterator)1 List (java.util.List)1 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)1 XSDConstrainingFacet (org.eclipse.xsd.XSDConstrainingFacet)1 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)1 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)1 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)1