use of org.eclipse.wst.xsd.ui.internal.wizards.XSDSelectIncludeFileWizard in project webtools.sourceediting by eclipse.
the class XSDDirectivesSchemaLocationUpdater method updateSchemaLocation.
/**
* Modifies the schema location by opening the schema location dialog and
* processing the results. This method refactors the code in
* XSDImportSection$widgetSelected and SchemaLocationSection$widgetSelected
* and the processing in handleSchemaLocationChange()
*/
public static void updateSchemaLocation(XSDSchema xsdSchema, Object selection, boolean isInclude) {
Shell shell = Display.getCurrent().getActiveShell();
IFile currentIFile = null;
IEditorInput editorInput = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput();
ViewerFilter filter;
if (editorInput instanceof IFileEditorInput) {
currentIFile = ((IFileEditorInput) editorInput).getFile();
filter = new // $NON-NLS-1$
ResourceFilter(// $NON-NLS-1$
new String[] { ".xsd" }, new IFile[] { currentIFile }, null);
} else {
filter = new // $NON-NLS-1$
ResourceFilter(// $NON-NLS-1$
new String[] { ".xsd" }, null, null);
}
XSDSelectIncludeFileWizard fileSelectWizard = new // $NON-NLS-1$
XSDSelectIncludeFileWizard(// $NON-NLS-1$
xsdSchema, // $NON-NLS-1$
isInclude, // $NON-NLS-1$
XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_SCHEMA"), // $NON-NLS-1$
XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_DESC"), filter, new StructuredSelection(selection));
WizardDialog wizardDialog = new WizardDialog(shell, fileSelectWizard);
wizardDialog.create();
wizardDialog.setBlockOnOpen(true);
int result = wizardDialog.open();
if (result == Window.OK) {
IFile selectedIFile = fileSelectWizard.getResultFile();
String schemaFileString;
if (selectedIFile != null && currentIFile != null) {
schemaFileString = URIHelper.getRelativeURI(selectedIFile.getLocation(), currentIFile.getLocation());
} else if (selectedIFile != null && currentIFile == null) {
schemaFileString = selectedIFile.getLocationURI().toString();
} else {
schemaFileString = fileSelectWizard.getURL();
}
// $NON-NLS-1$
String attributeSchemaLocation = "schemaLocation";
if (selection instanceof XSDImport) {
XSDImport xsdImport = (XSDImport) selection;
// $NON-NLS-1$
xsdImport.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
String namespace = fileSelectWizard.getNamespace();
if (namespace == null)
// $NON-NLS-1$
namespace = "";
XSDSchema externalSchema = fileSelectWizard.getExternalSchema();
java.util.Map map = xsdSchema.getQNamePrefixToNamespaceMap();
Element schemaElement = xsdSchema.getElement();
// update the xmlns in the schema element first, and then update the
// import element next so that the last change will be in the import element. This keeps the
// selection on the import element
TypesHelper helper = new TypesHelper(externalSchema);
String prefix = helper.getPrefix(namespace, false);
if (map.containsKey(prefix)) {
prefix = null;
}
if (prefix == null || (prefix != null && prefix.length() == 0)) {
// $NON-NLS-1$
StringBuffer newPrefix = new StringBuffer("pref");
int prefixExtension = 1;
while (map.containsKey(newPrefix.toString()) && prefixExtension < 100) {
// $NON-NLS-1$
newPrefix = new StringBuffer("pref" + String.valueOf(prefixExtension));
prefixExtension++;
}
prefix = newPrefix.toString();
}
// $NON-NLS-1$
String attributeNamespace = "namespace";
if (namespace.length() > 0) {
// if ns already in map, use its corresponding prefix
if (map.containsValue(namespace)) {
TypesHelper typesHelper = new TypesHelper(xsdSchema);
prefix = typesHelper.getPrefix(namespace, false);
} else // otherwise add to the map
{
// $NON-NLS-1$
schemaElement.setAttribute("xmlns:" + prefix, namespace);
}
// prefixText.setText(prefix);
xsdImport.getElement().setAttribute(attributeNamespace, namespace);
}
// into the resource set
if (// redundant
selection instanceof XSDImportImpl) {
XSDImportImpl xsdImportImpl = (XSDImportImpl) selection;
xsdImportImpl.importSchema();
}
} else if (selection instanceof XSDInclude) {
XSDInclude xsdInclude = (XSDInclude) selection;
xsdInclude.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
} else if (selection instanceof XSDRedefine) {
XSDRedefine xsdRedefine = (XSDRedefine) selection;
xsdRedefine.getElement().setAttribute(attributeSchemaLocation, schemaFileString);
}
}
}
Aggregations