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
;
89 // ------------------------------------------------------------------------
90 // LayoutManager ----------------------------------------------------------
91 // ------------------------------------------------------------------------
94 * Process the parent widget's resize event, and resize/reposition child
97 * @param resize resize event
99 public void onResize(final TResizeEvent resize
) {
100 if (resize
.getType() == TResizeEvent
.Type
.WIDGET
) {
101 width
= resize
.getWidth();
102 height
= resize
.getHeight();
108 * Add a child widget to manage.
110 * @param child the widget to manage
112 public void add(final TWidget child
) {
113 Rectangle rect
= new Rectangle(child
.getX(), child
.getY(),
114 child
.getWidth(), child
.getHeight());
115 children
.put(child
, rect
);
120 * Remove a child widget from those managed by this LayoutManager.
122 * @param child the widget to remove
124 public void remove(final TWidget child
) {
125 children
.remove(child
);
129 // ------------------------------------------------------------------------
130 // StretchLayoutManager ---------------------------------------------------
131 // ------------------------------------------------------------------------
134 * Resize/reposition child widgets based on difference between current
135 * dimensions and the original dimensions.
137 public void layoutChildren() {
138 double widthRatio
= (double) width
/ originalWidth
;
139 if (!Double
.isFinite(widthRatio
)) {
142 double heightRatio
= (double) height
/ originalHeight
;
143 if (!Double
.isFinite(heightRatio
)) {
146 for (TWidget child
: children
.keySet()) {
147 Rectangle rect
= children
.get(child
);
148 child
.setDimensions((int) (rect
.getX() * widthRatio
),
149 (int) (rect
.getY() * heightRatio
),
150 (int) (rect
.getWidth() * widthRatio
),
151 (int) (rect
.getHeight() * heightRatio
));