Search in sources :

Example 1 with DOMNamespaceInfoManager

use of org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceInfoManager in project webtools.sourceediting by eclipse.

the class XSDSchemaSection method doHandleEvent.

public void doHandleEvent(Event event) {
    // $NON-NLS-1$
    errorText.setText("");
    String prefixValue = prefixText.getText();
    String tnsValue = targetNamespaceText.getText();
    Element element = xsdSchema.getElement();
    TypesHelper helper = new TypesHelper(xsdSchema);
    String currentPrefix = helper.getPrefix(element.getAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE), false);
    String currentNamespace = xsdSchema.getTargetNamespace();
    if (tnsValue.trim().length() == 0) {
        if (prefixValue.trim().length() > 0) {
            // $NON-NLS-1$
            errorText.setText(XSDEditorPlugin.getXSDString("_ERROR_TARGET_NAMESPACE_AND_PREFIX"));
            int length = errorText.getText().length();
            red = new Color(null, 255, 0, 0);
            StyleRange style = new StyleRange(0, length, red, targetNamespaceText.getBackground());
            errorText.setStyleRange(style);
            return;
        }
    }
    if (event.widget == prefixText) {
        // widget loses focus.
        if (prefixValue.equals(currentPrefix))
            return;
        updateNamespaceInfo(prefixValue, tnsValue);
    } else if (event.widget == targetNamespaceText) {
        // widget loses focus.
        if (tnsValue.equals(currentNamespace))
            return;
        DOMNamespaceInfoManager namespaceInfoManager = new DOMNamespaceInfoManager();
        List namespaceInfoList = namespaceInfoManager.getNamespaceInfoList(xsdSchema.getElement());
        Element xsdSchemaElement = xsdSchema.getElement();
        DocumentImpl doc = (DocumentImpl) xsdSchemaElement.getOwnerDocument();
        try {
            doc.getModel().beginRecording(this, XSDEditorPlugin.getXSDString("_UI_NAMESPACE_CHANGE"));
            if (// remove the targetNamespace attribute
            tnsValue.trim().length() == 0) {
                xsdSchemaElement.removeAttribute(XSDConstants.TARGETNAMESPACE_ATTRIBUTE);
                return;
            }
            // Now replace the namespace for the xmlns entry
            for (Iterator i = namespaceInfoList.iterator(); i.hasNext(); ) {
                NamespaceInfo info = (NamespaceInfo) i.next();
                if (info.uri.equals(currentNamespace)) {
                    info.uri = tnsValue;
                }
            }
            xsdSchema.setIncrementalUpdate(false);
            // set the new xmlns entries
            namespaceInfoManager.removeNamespaceInfo(element);
            namespaceInfoManager.addNamespaceInfo(element, namespaceInfoList, false);
            xsdSchema.setIncrementalUpdate(true);
            // set the targetNamespace attribute
            xsdSchema.setTargetNamespace(tnsValue);
            TargetNamespaceChangeHandler targetNamespaceChangeHandler = new TargetNamespaceChangeHandler(xsdSchema, currentNamespace, tnsValue);
            targetNamespaceChangeHandler.resolve();
        } catch (Exception e) {
        } finally {
            try {
                xsdSchema.update();
            } finally {
                doc.getModel().endRecording(this);
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) TargetNamespaceChangeHandler(org.eclipse.wst.xsd.ui.internal.nsedit.TargetNamespaceChangeHandler) DocumentImpl(org.eclipse.wst.xml.core.internal.document.DocumentImpl) TypesHelper(org.eclipse.wst.xsd.ui.internal.util.TypesHelper) DOMNamespaceInfoManager(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceInfoManager) Iterator(java.util.Iterator) List(java.util.List) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Aggregations

Iterator (java.util.Iterator)1 List (java.util.List)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 Color (org.eclipse.swt.graphics.Color)1 DOMNamespaceInfoManager (org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceInfoManager)1 NamespaceInfo (org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)1 DocumentImpl (org.eclipse.wst.xml.core.internal.document.DocumentImpl)1 TargetNamespaceChangeHandler (org.eclipse.wst.xsd.ui.internal.nsedit.TargetNamespaceChangeHandler)1 TypesHelper (org.eclipse.wst.xsd.ui.internal.util.TypesHelper)1 Element (org.w3c.dom.Element)1