for (int i= 0; i < array.length; i++) {
RulerColumnDescriptor desc= (RulerColumnDescriptor) array[i];
descriptorsById.put(desc.getId(), desc);
}
DAG dag= new DAG();
for (int i= 0; i < array.length; i++) {
RulerColumnDescriptor desc= (RulerColumnDescriptor) array[i];
dag.addVertex(desc);
Set before= desc.getPlacement().getConstraints();
for (Iterator it= before.iterator(); it.hasNext();) {
RulerColumnPlacementConstraint constraint= (RulerColumnPlacementConstraint) it.next();
String id= constraint.getId();
RulerColumnDescriptor target= (RulerColumnDescriptor) descriptorsById.get(id);
if (target == null) {
noteUnknownTarget(desc, id);
} else {
boolean success;
if (constraint.isBefore())
success= dag.addEdge(desc, target);
else
success= dag.addEdge(target, desc);
if (!success)
noteCycle(desc, target);
}
}
}
Comparator gravityComp= new Comparator() {
public int compare(Object o1, Object o2) {
float diff= ((RulerColumnDescriptor) o1).getPlacement().getGravity() - ((RulerColumnDescriptor) o2).getPlacement().getGravity();
if (diff == 0)
return 0;
if (diff < 0)
return -1;
return 1;
}
};
/* Topological sort - always select the source with the least gravity */
Set toProcess= dag.getSources();
int index= 0;
while (!toProcess.isEmpty()) {
Object next= Collections.min(toProcess, gravityComp);
array[index]= next;
index++;
dag.removeVertex(next);
toProcess= dag.getSources();
}
Assert.isTrue(index == array.length);
ListIterator it= descriptors.listIterator();
for (int i= 0; i < index; i++) {