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
);
130 * Reset a child widget's original/preferred size.
132 * @param child the widget to manage
134 public void resetSize(final TWidget child
) {
135 // For this layout, adding is the same as replacing.
139 // ------------------------------------------------------------------------
140 // StretchLayoutManager ---------------------------------------------------
141 // ------------------------------------------------------------------------
144 * Resize/reposition child widgets based on difference between current
145 * dimensions and the original dimensions.
147 private void layoutChildren() {
148 double widthRatio
= (double) width
/ originalWidth
;
149 if (Math
.abs(widthRatio
) > Double
.MAX_VALUE
) {
152 double heightRatio
= (double) height
/ originalHeight
;
153 if (Math
.abs(heightRatio
) > Double
.MAX_VALUE
) {
156 for (TWidget child
: children
.keySet()) {
157 Rectangle rect
= children
.get(child
);
158 child
.setDimensions((int) (rect
.getX() * widthRatio
),
159 (int) (rect
.getY() * heightRatio
),
160 (int) (rect
.getWidth() * widthRatio
),
161 (int) (rect
.getHeight() * heightRatio
));