String defaultTemplate = PrintingPlugin.getDefault().getPluginPreferences().getString(
PrintingPlugin.PREF_DEFAULT_TEMPLATE);
ListDialog dialog = createTemplateChooserDialog(templateFactories);
TemplateFactory templateFactory = (TemplateFactory) PrintingPlugin.getDefault()
.getTemplateFactories().get(defaultTemplate);
dialog.setInitialSelections(new Object[]{templateFactory});
int result = dialog.open();
if (result == Window.CANCEL || dialog.getResult().length == 0) {
return null;
}
Template template = null;
templateFactory = ((TemplateFactory) dialog.getResult()[0]);
if (templateFactory == null) {
PrintingPlugin.log(Messages.CreatePageAction_error_cannotFindDefaultTemplate, null);
TemplateFactory firstAvailable = (TemplateFactory) templateFactories.values()
.iterator().next();
if (firstAvailable == null) {
PrintingPlugin.log(
"Unable to locate any templates, resorting to hard coded default.", null); //$NON-NLS-1$
template = new BasicTemplateFactory().createTemplate();
} else {
template = firstAvailable.createTemplate();
}
} else {
template = templateFactory.createTemplate();
}
return template;