|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Component; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.GraphicsConfiguration; |
| 27 | +import java.awt.Insets; |
| 28 | +import java.awt.Point; |
| 29 | +import java.awt.Window; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment; |
| 33 | +import static java.awt.Toolkit.getDefaultToolkit; |
| 34 | + |
| 35 | +/** |
| 36 | + * A utility class which provides standard window layouts for multi-window |
| 37 | + * manual tests using the {@link PassFailJFrame} framework. |
| 38 | + * The layout methods {@code right-} and {@code bottom-} implement the |
| 39 | + * {@link PassFailJFrame.PositionWindows PositionWindows} interface and |
| 40 | + * can be used directly or via builder methods. |
| 41 | + * <p> |
| 42 | + * There are several helper methods, such as |
| 43 | + * {@link #getScreenCenter() getScreenCenter}, which could help you |
| 44 | + * implement customized windows layouts. |
| 45 | + */ |
| 46 | +public final class WindowLayouts { |
| 47 | + |
| 48 | + /** Private constructor to prevent instantiating the utility class. */ |
| 49 | + private WindowLayouts() { |
| 50 | + } |
| 51 | + |
| 52 | + /** A gap between windows. (Local copy makes expressions shorter.) */ |
| 53 | + private static final int WINDOW_GAP = PassFailJFrame.WINDOW_GAP; |
| 54 | + |
| 55 | + /** |
| 56 | + * Lays out the window list in one row to the right of |
| 57 | + * the instruction frame. The top of the windows is aligned to |
| 58 | + * that of the instruction frame. |
| 59 | + * |
| 60 | + * @param windows the list of windows to lay out |
| 61 | + * @param instructionUI information about the instruction frame |
| 62 | + */ |
| 63 | + public static void rightOneRow(final List<Window> windows, |
| 64 | + final PassFailJFrame.InstructionUI instructionUI) { |
| 65 | + layoutRow(instructionUI.getLocation().x |
| 66 | + + instructionUI.getSize().width |
| 67 | + + WINDOW_GAP, |
| 68 | + instructionUI.getLocation().y, |
| 69 | + windows); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Lays out the window list in one column to the right of |
| 74 | + * the instruction frame. The top of the first window is aligned to |
| 75 | + * that of the instruction frame. |
| 76 | + * |
| 77 | + * @param windows the list of windows to lay out |
| 78 | + * @param instructionUI information about the instruction frame |
| 79 | + */ |
| 80 | + public static void rightOneColumn(final List<Window> windows, |
| 81 | + final PassFailJFrame.InstructionUI instructionUI) { |
| 82 | + layoutColumn(instructionUI.getLocation().x |
| 83 | + + instructionUI.getSize().width |
| 84 | + + WINDOW_GAP, |
| 85 | + instructionUI.getLocation().y, |
| 86 | + windows); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Lays out the window list in one column to the right of |
| 91 | + * the instruction frame centering the stack of the windows. |
| 92 | + * |
| 93 | + * @param windows the list of windows to lay out |
| 94 | + * @param instructionUI information about the instruction frame |
| 95 | + */ |
| 96 | + public static void rightOneColumnCentered(final List<Window> windows, |
| 97 | + final PassFailJFrame.InstructionUI instructionUI) { |
| 98 | + layoutColumn(instructionUI.getLocation().x |
| 99 | + + instructionUI.getSize().width |
| 100 | + + WINDOW_GAP, |
| 101 | + getScreenCenter().y |
| 102 | + - getWindowListHeight(windows) / 2, |
| 103 | + windows); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Lays out the window list in one row to the bottom of |
| 109 | + * the instruction frame. The left of the first window is aligned to |
| 110 | + * that of the instruction frame. |
| 111 | + * |
| 112 | + * @param windows the list of windows to lay out |
| 113 | + * @param instructionUI information about the instruction frame |
| 114 | + */ |
| 115 | + public static void bottomOneRow(final List<Window> windows, |
| 116 | + final PassFailJFrame.InstructionUI instructionUI) { |
| 117 | + layoutRow(instructionUI.getLocation().x, |
| 118 | + instructionUI.getLocation().y |
| 119 | + + instructionUI.getSize().height |
| 120 | + + WINDOW_GAP, |
| 121 | + windows); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Lays out the window list in one row to the bottom of |
| 126 | + * the instruction frame centering the row of the windows. |
| 127 | + * |
| 128 | + * @param windows the list of windows to lay out |
| 129 | + * @param instructionUI information about the instruction frame |
| 130 | + */ |
| 131 | + public static void bottomOneRowCentered(final List<Window> windows, |
| 132 | + final PassFailJFrame.InstructionUI instructionUI) { |
| 133 | + layoutRow(getScreenCenter().x |
| 134 | + - getWindowListWidth(windows) / 2, |
| 135 | + instructionUI.getLocation().y |
| 136 | + + instructionUI.getSize().height |
| 137 | + + WINDOW_GAP, |
| 138 | + windows); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Lays out the window list in one column to the bottom of |
| 143 | + * the instruction frame. The left of the first window is aligned to |
| 144 | + * that of the instruction frame. |
| 145 | + * |
| 146 | + * @param windows the list of windows to lay out |
| 147 | + * @param instructionUI information about the instruction frame |
| 148 | + */ |
| 149 | + public static void bottomOneColumn(final List<Window> windows, |
| 150 | + final PassFailJFrame.InstructionUI instructionUI) { |
| 151 | + layoutColumn(instructionUI.getLocation().x, |
| 152 | + instructionUI.getLocation().y |
| 153 | + + instructionUI.getSize().height |
| 154 | + + WINDOW_GAP, |
| 155 | + windows); |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + /** |
| 160 | + * Lays out the window list in one row starting at |
| 161 | + * ({@code x0}, {@code y}). |
| 162 | + * |
| 163 | + * @param x0 the starting <var>x</var> coordinate of the windows |
| 164 | + * @param y the <var>y</var> coordinate of the windows |
| 165 | + * @param windows the list of windows to lay out |
| 166 | + */ |
| 167 | + public static void layoutRow(final int x0, |
| 168 | + final int y, |
| 169 | + final List<Window> windows) { |
| 170 | + int x = x0; |
| 171 | + for (Window w : windows) { |
| 172 | + w.setLocation(x, y); |
| 173 | + x += w.getWidth() + WINDOW_GAP; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Lays out the window list in one column starting at |
| 179 | + * ({@code x}, {@code y0}). |
| 180 | + * |
| 181 | + * @param x the <var>x</var> coordinate of the windows |
| 182 | + * @param y0 the starting <var>y</var> coordinate of the windows |
| 183 | + * @param windows the list of windows to lay out |
| 184 | + */ |
| 185 | + public static void layoutColumn(final int x, |
| 186 | + final int y0, |
| 187 | + final List<Window> windows) { |
| 188 | + int y = y0; |
| 189 | + for (Window w : windows) { |
| 190 | + w.setLocation(x, y); |
| 191 | + y += w.getHeight() + WINDOW_GAP; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + /** |
| 197 | + * {@return the center point of the main screen} |
| 198 | + */ |
| 199 | + public static Point getScreenCenter() { |
| 200 | + GraphicsConfiguration gc = getLocalGraphicsEnvironment() |
| 201 | + .getDefaultScreenDevice() |
| 202 | + .getDefaultConfiguration(); |
| 203 | + Dimension size = gc.getBounds() |
| 204 | + .getSize(); |
| 205 | + Insets insets = getDefaultToolkit() |
| 206 | + .getScreenInsets(gc); |
| 207 | + |
| 208 | + return new Point((size.width - insets.left - insets.right) / 2, |
| 209 | + (size.height - insets.top - insets.bottom) / 2); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * {@return width of the windows in the list, taking into account |
| 214 | + * the gap between windows} |
| 215 | + * |
| 216 | + * @param windows the list of windows to get the width of |
| 217 | + */ |
| 218 | + public static int getWindowListWidth(final List<Window> windows) { |
| 219 | + return windows.stream() |
| 220 | + .mapToInt(Component::getWidth) |
| 221 | + .sum() |
| 222 | + + WINDOW_GAP * (windows.size() - 1); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * {@return height of the windows in the list, taking into account |
| 227 | + * the gap between windows} |
| 228 | + * |
| 229 | + * @param windows the list of windows to get the height of |
| 230 | + */ |
| 231 | + public static int getWindowListHeight(final List<Window> windows) { |
| 232 | + return windows.stream() |
| 233 | + .mapToInt(Component::getHeight) |
| 234 | + .sum() |
| 235 | + + WINDOW_GAP * (windows.size() - 1); |
| 236 | + } |
| 237 | +} |
0 commit comments