/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Michael Powers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.ui.swing.util; import java.awt.Component; import java.awt.Container; import java.awt.Point; import java.io.Serializable; import java.util.Comparator; import javax.swing.SwingUtilities; /** * A Comparator that will sort components in a common container based first on * their y-coordinate and then on their x-coordinate, producing a list sorted * from top to bottom and left to right. If all components are not in the same * container, the resulting sort is undefined. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 904 $ */ public class PositionComparator implements Comparator, Serializable { private Container rootContainer; private transient Component c1, c2; private transient Point p1, p2; /** * Standard constructor to configure the comparator. * * @param aContainer The common container for all the objects to be compared. */ public PositionComparator(Container aContainer) { rootContainer = aContainer; } // interface Comparable public int compare(Object o1, Object o2) { c1 = (Component) o1; c2 = (Component) o2; p1 = SwingUtilities.convertPoint(c1.getParent(), c1.getLocation(), rootContainer); p2 = SwingUtilities.convertPoint(c2.getParent(), c2.getLocation(), rootContainer); if (p1.y != p2.y) { return p1.y - p2.y; } return p1.x - p2.x; } } /* * $Log$ Revision 1.2 2006/02/18 23:19:05 cgruber Update imports and maven * dependencies. * * Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in * eclipse-friendly maven-enabled packages. * * Revision 1.1.1.1 2000/12/21 15:51:27 mpowers Contributing wotonomy. * * Revision 1.2 2000/12/20 16:25:45 michael Added log to all files. * * */