}
private Map buildContainerNameToPositionMap(final Format rootFormat) {
final Map fragmentToCounter = new HashMap();
FormatVisitorAdapter visitor = new FormatVisitorAdapter() {
public boolean visitFormat(final Format format, final Object object) {
if (format instanceof Pane || format instanceof Region) {
final Fragment fragment = format.getEnclosingFragment();
if (fragment != null) {
final String containerName = format.getName();
Counter counter =
(Counter) fragmentToCounter.get(fragment);
if (counter == null) {
counter = new Counter();
fragmentToCounter.put(fragment, counter);
}
final Map map = (Map) object;
List fragments = (List) map.get(containerName);
if (fragments == null) {
fragments = new LinkedList();
map.put(containerName, fragments);
}
fragments.add(new ContainerPosition(fragment,
counter.getCount()));
counter.increment();
}
}
return false;
}
};
final Map containerNameToFragments = new HashMap();
try {
if (rootFormat != null) {
visitor.visitFormatChildren(rootFormat, containerNameToFragments);
}
} catch (FormatVisitorException e) {
e.printStackTrace();
throw new IllegalStateException("Cannot traverse format hierarchy");
}