private void addToRowList(EntityLayout entity, ArrayList rowsList) { double layoutY = entity.getLocation().y; for (Iterator iterator = rowsList.iterator(); iterator.hasNext(); ) { List currentRow = (List) iterator.next(); EntityLayout currentRowEntity = (EntityLayout) currentRow.get(0); double currentRowY = currentRowEntity.getLocation().y; if (layoutY >= currentRowY - DELTA && layoutY <= currentRowY + DELTA) { currentRow.add(entity); return; } } List newRow = new ArrayList(); newRow.add(entity); rowsList.add(newRow); }
public void applyLayout(boolean clean) { if (!clean) return; ArrayList rowsList = new ArrayList(); EntityLayout[] entities = context.getEntities(); for (int i = 0; i < entities.length; i++) { addToRowList(entities[i], rowsList); } Collections.sort( rowsList, new Comparator() { public int compare(Object o1, Object o2) { List a0 = (List) o1; List a1 = (List) o2; EntityLayout entity0 = (EntityLayout) a0.get(0); EntityLayout entity1 = (EntityLayout) a1.get(0); return (int) (entity0.getLocation().y - entity1.getLocation().y); } }); Comparator entityComparator = new Comparator() { public int compare(Object o1, Object o2) { return (int) (((EntityLayout) o1).getLocation().y - ((EntityLayout) o2).getLocation().y); } }; DisplayIndependentRectangle bounds = context.getBounds(); int heightSoFar = 0; for (Iterator iterator = rowsList.iterator(); iterator.hasNext(); ) { List currentRow = (List) iterator.next(); Collections.sort(currentRow, entityComparator); int i = 0; int width = (int) (bounds.width / 2 - currentRow.size() * 75); heightSoFar += ((EntityLayout) currentRow.get(0)).getSize().height + VSPACING; for (Iterator iterator2 = currentRow.iterator(); iterator2.hasNext(); ) { EntityLayout entity = (EntityLayout) iterator2.next(); DisplayIndependentDimension size = entity.getSize(); entity.setLocation(width + 10 * ++i + size.width / 2, heightSoFar + size.height / 2); width += size.width; } } }