* @return An ordered list of JavaScriptContainers.
* @throws OrderException If some of the containers could not be ordered.
*/
private List getOrderedContainers() throws OrderException {
// Create an instance of topological sorter.
TopologicalSorter sorter = new TopologicalSorter();
// Build a map of producers: widgetId -> JavaScriptContainer
// and add single node for each producer to the graph.
Map producersMap = new HashMap();
for (int containerIndex = 0; containerIndex < containers.size(); containerIndex++) {
DefaultJavaScriptContainer container =
(DefaultJavaScriptContainer) containers.get(containerIndex);
List producedIds = container.getCreatedWidgetIds();
for (int producedIdIndex = 0; producedIdIndex < producedIds.size(); producedIdIndex++) {
String producedId = (String) producedIds.get(producedIdIndex);
if (producersMap.containsKey(producedId)) {
throw new IllegalStateException("Widget ID produced twice.");
}
producersMap.put(producedId, container);
}
sorter.addNode(container);
}
// Fill graph with producer-consumer edges.
for (int containerIndex = 0; containerIndex < containers.size(); containerIndex++) {
DefaultJavaScriptContainer consumer =
(DefaultJavaScriptContainer) containers.get(containerIndex);
List consumedIds = consumer.getUsedWidgetIds();
for (int consumedIdIndex = 0; consumedIdIndex < consumedIds.size(); consumedIdIndex++) {
String consumedId = (String) consumedIds.get(consumedIdIndex);
Object producer = producersMap.get(consumedId);
if (producer == null) {
throw new IllegalStateException("Unspecified widget ID: " + consumedId);
}
sorter.addEdge(consumer, producer);
}
}
// Sort the graph topologically.
List orderedContainers;
try {
orderedContainers = sorter.sort();
} catch (SortException e) {
throw new OrderException("Could not order some of the containers.", e.getUnsortedNodes());
}
// Reverse the list of containers, so that all dependencies are placed