01: /*
02: * Copyright (C) 2004 TiongHiang Lee
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Email: thlee@onemindsoft.org
19: */
20:
21: package org.onemind.swingweb.templaterender.layout;
22:
23: import java.awt.Component;
24: import java.util.Comparator;
25:
26: /**
27: * A wrapper for the component for sorting
28: * @author TiongHiang Lee (thlee@onemindsoft.org)
29: */
30: public class ComponentPositionSorter implements Comparator {
31:
32: public static final ComponentPositionSorter INSTANCE = new ComponentPositionSorter();
33:
34: /**
35: * Constructor
36: */
37: private ComponentPositionSorter() {
38: }
39:
40: public int compare(Object o1, Object o2) {
41: Component com1 = ((ComponentCell) o1).getComponent();
42: Component com2 = ((ComponentCell) o2).getComponent();
43: if (com1.equals(com2)) {
44: return 0;
45: }
46: if (com1.getY() < com2.getY()) {
47: return -1;
48: } else if (com1.getY() > com2.getY()) {
49: return 1;
50: } else { //same Y-axis
51: if (com1.getX() < com2.getX()) {
52: return -1;
53: } else if (com1.getX() > com2.getX()) {
54: return 1;
55: } else {
56: return -1; //if x,y is same, just use any
57: }
58: }
59: }
60: }
|