2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.Rectangle
;
32 import java
.util
.HashMap
;
35 import jexer
.event
.TResizeEvent
;
38 * StretchLayoutManager repositions child widgets based on their coordinates
39 * when added and the current widget size.
41 public class StretchLayoutManager
implements LayoutManager
{
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
50 private int width
= 0;
55 private int height
= 0;
60 private int originalWidth
= 0;
65 private int originalHeight
= 0;
68 * Map of widget to original dimensions.
70 private HashMap
<TWidget
, Rectangle
> children
= new HashMap
<TWidget
, Rectangle
>();
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
79 * @param width the width of the parent widget
80 * @param height the height of the parent widget
82 public StretchLayoutManager(final int width
, final int height
) {
83 originalWidth
= width
;
84 originalHeight
= height
;
87 // ------------------------------------------------------------------------
88 // LayoutManager ----------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * Process the parent widget's resize event, and resize/reposition child
95 * @param resize resize event
97 public void onResize(final TResizeEvent resize
) {
98 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
99 width
= resize
.getWidth();
100 height
= resize
.getHeight();
106 * Add a child widget to manage.
108 * @param child the widget to manage
110 public void add(final TWidget child
) {
111 Rectangle rect
= new Rectangle(child
.getX(), child
.getY(),
112 child
.getWidth(), child
.getHeight());
113 children
.put(child
, rect
);
117 * Remove a child widget from those managed by this LayoutManager.
119 * @param child the widget to remove
121 public void remove(final TWidget child
) {
122 children
.remove(child
);
125 // ------------------------------------------------------------------------
126 // StretchLayoutManager ---------------------------------------------------
127 // ------------------------------------------------------------------------
130 * Resize/reposition child widgets based on difference between current
131 * dimensions and the original dimensions.
133 public void layoutChildren() {
134 double widthRatio
= (double) width
/ originalWidth
;
135 if (!Double
.isFinite(widthRatio
)) {
138 double heightRatio
= (double) height
/ originalHeight
;
139 if (!Double
.isFinite(heightRatio
)) {
142 for (TWidget child
: children
.keySet()) {
143 Rectangle rect
= children
.get(child
);
144 child
.setDimensions((int) (rect
.getX() * widthRatio
),
145 (int) (rect
.getY() * heightRatio
),
146 (int) (rect
.getWidth() * widthRatio
),
147 (int) (rect
.getHeight() * heightRatio
));