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