| 58 | /// @author shannah |
| 59 | /// |
| 60 | public class TextSelection { |
| 61 | |
| 62 | /// Comparator used for ordering components in left-to-right mode. |
| 63 | private static final Comparator<Component> LTRComparator = new Comparator<Component>() { |
| 64 | |
| 65 | /// We can't just use component's AbsoluteY coordinates for ordering because of scrolling, |
| 66 | /// so we create a scaled coorindate that will order components properly. |
| 67 | /// |
| 68 | /// #### Parameters |
| 69 | /// |
| 70 | /// - `cmp`: the component |
| 71 | /// |
| 72 | /// #### Returns |
| 73 | /// |
| 74 | /// the Y scale |
| 75 | private double getScaledY(Component cmp) { |
| 76 | double y = 0; |
| 77 | while (cmp != null) { |
| 78 | double ratio = cmp.getHeight() / (double) Math.max(cmp.getScrollDimension().getHeight(), cmp.getHeight()); |
| 79 | y = ratio * y; |
| 80 | y += cmp.getY() + cmp.getScrollY(); |
| 81 | cmp = cmp.getParent(); |
| 82 | } |
| 83 | return y; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int compare(Component o1, Component o2) { |
| 88 | int x1 = o1.getAbsoluteX(); |
| 89 | int x2 = o2.getAbsoluteX(); |
| 90 | double y1 = getScaledY(o1); |
| 91 | double y2 = getScaledY(o2); |
| 92 | |
| 93 | int compareY = Double.compare(y1, y2); |
| 94 | if (compareY != 0) { |
| 95 | return compareY; |
| 96 | } |
| 97 | if (x1 < x2) { |
| 98 | return -1; |
| 99 | } else if (x1 > x2) { |
| 100 | return 1; |
| 101 | } |
| 102 | int w1 = o1.getWidth(); |
| 103 | int w2 = o2.getWidth(); |
| 104 | int h1 = o1.getHeight(); |
| 105 | int h2 = o2.getHeight(); |
| 106 | |
| 107 | if (h1 != h2) { |
| 108 | return h1 > h2 ? -1 : 1; |
| 109 | } |
| 110 | if (w1 != w2) { |
| 111 | return w1 > w2 ? -1 : 1; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | }; |
| 117 | /// Comparator used for ordering components in left-to-right mode. |
nothing calls this directly
no test coverage detected