// Remove pages no longer required and remember the rest in a map
int i = 0;
Map<String,IFormPage> pageCache = new HashMap<String,IFormPage>(requiredPageIds.size());
while (i < getPageCount()) {
IFormPage current = (IFormPage) pages.get(i);
if (!requiredPageIds.contains(current.getId()))
removePage(i);
else {
pageCache.put(current.getId(), current);
i++;
}
}
// Cache new pages
for (String pageId : requiredPageIds) {
if (!pageCache.containsKey(pageId)) {
IFormPage page = SOURCE_PAGE.equals(pageId) ? sourcePage : pageFactories.get(pageId).createPage(this, model, pageId);
pageCache.put(pageId, page);
}
}
// Add pages back in
int requiredPointer = 0;
int existingPointer = 0;
while (requiredPointer < requiredPageIds.size()) {
try {
String requiredId = requiredPageIds.get(requiredPointer);
if (existingPointer >= getPageCount()) {
if (SOURCE_PAGE.equals(requiredId))
addPage(sourcePage, getEditorInput());
else
addPage(pageCache.get(requiredId));
} else {
IFormPage existingPage = (IFormPage) pages.get(existingPointer);
if (!requiredId.equals(existingPage.getId())) {
if (SOURCE_PAGE.equals(requiredId))
addPage(existingPointer, sourcePage, getEditorInput());
else
addPage(existingPointer, pageCache.get(requiredId));
}