Search in sources :

Example 16 with XSDComponent

use of org.eclipse.xsd.XSDComponent in project webtools.sourceediting by eclipse.

the class UpdateComponentReferenceAndManageDirectivesCommand method computeComponent.

protected XSDComponent computeComponent() {
    XSDComponent result = null;
    XSDSchema schema = concreteComponent.getSchema();
    XSDSchemaDirective directive = null;
    // lets see if the element is already visible to our schema
    result = getDefinedComponent(schema, componentName, componentNamespace);
    if (result == null) {
        // includes/imports to get to it
        if (componentNamespace != null && componentNamespace.equals(schema.getTargetNamespace())) {
            // we need to add an include
            // if the component's namespace is not null and matches the schema's
            // target namespace
            directive = XSDFactory.eINSTANCE.createXSDInclude();
        } else if (componentNamespace == null) {
            // we need to add an include
            // if the component's namespace is null, then we can just add it
            // only if the current namespace is not null
            directive = XSDFactory.eINSTANCE.createXSDInclude();
            // we have to ensure the schema for schema prefix is NOT null
            if (schema.getSchemaForSchemaQNamePrefix() == null) {
                String targetNS = schema.getTargetNamespace();
                if (targetNS == null)
                    targetNS = "";
                // this will just update the schema for schema prefix to be, say, xsd
                UpdateNamespaceInformationCommand command = new UpdateNamespaceInformationCommand("", schema, "", targetNS);
                command.execute();
            }
        } else {
            // we need to add an import
            XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
            xsdImport.setNamespace(componentNamespace);
            directive = xsdImport;
        }
        String location = computeNiceLocation(schema.getSchemaLocation(), file);
        directive.setSchemaLocation(location);
        // TODO (cs) we should at the directive 'next' in the list of directives
        // for now I'm just adding as the first thing in the schema :-(
        // 
        schema.getContents().add(0, directive);
        XSDSchema resolvedSchema = directive.getResolvedSchema();
        if (resolvedSchema == null) {
            String platformLocation = "platform:/resource" + file.getFullPath();
            Resource resource = concreteComponent.eResource().getResourceSet().createResource(URI.createURI(platformLocation));
            if (resource instanceof XSDResourceImpl) {
                try {
                    resource.load(null);
                    XSDResourceImpl resourceImpl = (XSDResourceImpl) resource;
                    resolvedSchema = resourceImpl.getSchema();
                    if (resolvedSchema != null) {
                        directive.setResolvedSchema(resolvedSchema);
                    }
                } catch (Exception e) {
                }
            }
        }
        if (resolvedSchema != null) {
            result = getDefinedComponent(resolvedSchema, componentName, componentNamespace);
        } else {
        // TODO (cs) consider setting some error state so that the client can
        // provide a pop-dialog error
        // we should also remove the import/include so save from cluttering
        // the file with bogus directives
        }
    }
    return result;
}
Also used : XSDSchemaDirective(org.eclipse.xsd.XSDSchemaDirective) Resource(org.eclipse.emf.ecore.resource.Resource) XSDResourceImpl(org.eclipse.xsd.util.XSDResourceImpl) XSDImport(org.eclipse.xsd.XSDImport) XSDComponent(org.eclipse.xsd.XSDComponent) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 17 with XSDComponent

use of org.eclipse.xsd.XSDComponent in project webtools.sourceediting by eclipse.

the class UpdateElementReferenceAndManageDirectivesCommand method execute.

public void execute() {
    try {
        beginRecording(concreteComponent.getElement());
        XSDComponent elementDef = computeComponent();
        if (elementDef != null) {
            UpdateElementReferenceCommand command = new UpdateElementReferenceCommand(Messages._UI_ACTION_UPDATE_ELEMENT_REFERENCE, (XSDElementDeclaration) concreteComponent, (XSDElementDeclaration) elementDef);
            command.execute();
            XSDDirectivesManager.removeUnusedXSDImports(concreteComponent.getSchema());
        }
    } catch (Exception e) {
    } finally {
        endRecording();
    }
}
Also used : XSDComponent(org.eclipse.xsd.XSDComponent)

Example 18 with XSDComponent

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

the class XSDSetAnnotationDescriptionsAction method doAction.

public IStatus doAction() {
    try {
        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation)
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
            }
        } else
            xSDCom = (XSDComponent) selection.getFirstElement();
        XSDAnnotationsStructure struc = new XSDAnnotationsStructure(xSDCom);
        if (struc.getAnnotation() == null) {
            throw new RuntimeException(Messages.bind(Messages.XSDSetAnnotationXX_ExceptionInfo2, xSDCom.getClass().getName()));
        }
        AnnotationLanguageLabelsDialog dlg = new AnnotationLanguageLabelsDialog(struc.getDescriptions(), new AnnotationLabelDialogSelectionListener(page), page.getEditorSite().getShell(), Messages.XSDSetAnnotationXX_SetDescOfThisItem);
        dlg.setBlockOnOpen(true);
        dlg.open();
        if (dlg.getReturnCode() == Window.OK) {
            // remove existing annotations with labels
            struc.removeAllDescriptions();
            // add the new ones
            LinkedHashMap<String, String> descriptions = dlg.getDescriptionsMap();
            Set<String> isoCodes = descriptions.keySet();
            for (Iterator iter = isoCodes.iterator(); iter.hasNext(); ) {
                String isoCode = (String) iter.next();
                struc.setDescription(isoCode, descriptions.get(isoCode));
            }
        } else {
            return Status.CANCEL_STATUS;
        }
        if (struc.hasChanged()) {
            page.markDirty();
            page.refresh();
            page.getTreeViewer().expandToLevel(xSDCom, 2);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDSetAnnotationXX_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDAnnotationsStructure(com.amalto.workbench.utils.XSDAnnotationsStructure) Element(org.w3c.dom.Element) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDComponent(org.eclipse.xsd.XSDComponent) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) Iterator(java.util.Iterator) AnnotationLanguageLabelsDialog(com.amalto.workbench.dialogs.AnnotationLanguageLabelsDialog) XSDAnnotation(org.eclipse.xsd.XSDAnnotation)

Example 19 with XSDComponent

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

the class XSDSetAnnotationFKFilterAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        String conceptName = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation) {
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
                }
            }
        } else {
            xSDCom = (XSDComponent) selection.getFirstElement();
        }
        if (xSDCom instanceof XSDElementDeclaration) {
            // $NON-NLS-1$
            conceptName = xSDCom.getElement().getAttributes().getNamedItem("name").getNodeValue();
        }
        if (xSDCom instanceof XSDParticle) {
        }
        XSDAnnotationsStructure struc = null;
        if (xSDCom != null) {
            struc = new XSDAnnotationsStructure(xSDCom);
        }
        if (struc == null || struc.getAnnotation() == null) {
            throw new RuntimeException(Messages.bind(Messages.UnableEditAnnotationType, xSDCom.getClass().getName()));
        }
        fkd = getNewFKFilterDialog(page.getSite().getShell(), struc.getFKFilter(), page, conceptName);
        fkd.setDataModel(dataModelName);
        fkd.setLock(true);
        fkd.setBlockOnOpen(true);
        int ret = fkd.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        String fkfilter = fkd.getFilter();
        struc.setFKFilter(fkfilter);
        if (struc.hasChanged()) {
            page.refresh();
            page.getTreeViewer().expandToLevel(xSDCom, 2);
            page.markDirty();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.ErrorFKFilter, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDAnnotationsStructure(com.amalto.workbench.utils.XSDAnnotationsStructure) Element(org.w3c.dom.Element) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDComponent(org.eclipse.xsd.XSDComponent) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 20 with XSDComponent

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

the class XSDSetAnnotationForeignKeyInfoAction method doAction.

@Override
public IStatus doAction() {
    try {
        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation) {
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
                }
            }
        } else {
            xSDCom = (XSDComponent) selection.getFirstElement();
        }
        XSDAnnotationsStructure struc = new XSDAnnotationsStructure(xSDCom);
        if (struc.getAnnotation() == null) {
            throw new RuntimeException(Messages.bind(Messages.UnableEditAnnotationType, xSDCom.getClass().getName()));
        }
        dlg = getNewAnnotaionOrderedListsDialog(struc.getForeignKeyInfos().values());
        dlg.setLock(true);
        dlg.setRetrieveFKinfos(struc.getRetrieveFKinfos());
        dlg.setFormatFKInfo(struc.getFormatForeignKeyInfo());
        dlg.setBlockOnOpen(true);
        int ret = dlg.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        struc.setForeignKeyInfos(dlg.getXPaths());
        struc.setRetrieveFKinfos(dlg.isRetrieveFKinfos());
        struc.setFormatForeignKeyInfo(dlg.getFormatFKInfo());
        if (struc.hasChanged()) {
            page.refresh();
            page.getTreeViewer().expandToLevel(xSDCom, 2);
            page.markDirty();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.ErrorForeignKey, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) XSDAnnotationsStructure(com.amalto.workbench.utils.XSDAnnotationsStructure) Element(org.w3c.dom.Element) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) XSDComponent(org.eclipse.xsd.XSDComponent)

Aggregations

XSDComponent (org.eclipse.xsd.XSDComponent)21 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)14 TreePath (org.eclipse.jface.viewers.TreePath)14 XSDAnnotationsStructure (com.amalto.workbench.utils.XSDAnnotationsStructure)13 TreeSelection (org.eclipse.jface.viewers.TreeSelection)13 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)13 Element (org.w3c.dom.Element)13 ArrayList (java.util.ArrayList)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 SelectionListener (org.eclipse.swt.events.SelectionListener)5 AnnotationLanguageLabelsDialog (com.amalto.workbench.dialogs.AnnotationLanguageLabelsDialog)4 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)4 AnnotationOrderedListsDialog (com.amalto.workbench.dialogs.AnnotationOrderedListsDialog)3 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)3 Iterator (java.util.Iterator)2 XSDParticle (org.eclipse.xsd.XSDParticle)2 XSDSchema (org.eclipse.xsd.XSDSchema)2 DataModelMainPage (com.amalto.workbench.editors.DataModelMainPage)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 IInputValidator (org.eclipse.jface.dialogs.IInputValidator)1