View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.util;
20  
21  import java.awt.Component;
22  import java.awt.Container;
23  import java.awt.Point;
24  import java.io.Serializable;
25  import java.util.Comparator;
26  
27  import javax.swing.SwingUtilities;
28  
29  /***
30  * A Comparator that will sort components in a common container
31  * based first on their y-coordinate and then on their x-coordinate,
32  * producing a list sorted from top to bottom and left to right.
33  * If all components are not in the same container, the resulting
34  * sort is undefined.
35  *
36  * @author michael@mpowers.net
37  * @author $Author: cgruber $
38  * @version $Revision: 904 $
39  */
40  public class PositionComparator implements Comparator, Serializable
41  {
42      private Container rootContainer;
43      private transient Component c1, c2;
44      private transient Point p1, p2;
45  
46  /***
47  * Standard constructor to configure the comparator.
48  * @param aContainer The common container for all the objects to be compared.
49  */
50      public PositionComparator( Container aContainer )
51      {
52          rootContainer = aContainer;
53      }
54  
55      // interface Comparable
56  
57      public int compare(Object o1, Object o2)
58      {
59          c1 = (Component) o1;
60          c2 = (Component) o2;
61  
62          p1 = SwingUtilities.convertPoint( c1.getParent(), c1.getLocation(), rootContainer );
63          p2 = SwingUtilities.convertPoint( c2.getParent(), c2.getLocation(), rootContainer );
64  
65          if ( p1.y != p2.y )
66          {
67              return p1.y - p2.y;
68          }
69          return p1.x - p2.x;
70      }
71  }
72  
73  /*
74   * $Log$
75   * Revision 1.2  2006/02/18 23:19:05  cgruber
76   * Update imports and maven dependencies.
77   *
78   * Revision 1.1  2006/02/16 13:22:22  cgruber
79   * Check in all sources in eclipse-friendly maven-enabled packages.
80   *
81   * Revision 1.1.1.1  2000/12/21 15:51:27  mpowers
82   * Contributing wotonomy.
83   *
84   * Revision 1.2  2000/12/20 16:25:45  michael
85   * Added log to all files.
86   *
87   *
88   */
89