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
.image
.BufferedImage
;
33 import java
.io
.IOException
;
34 import javax
.imageio
.ImageIO
;
36 import jexer
.event
.TKeypressEvent
;
37 import jexer
.event
.TMouseEvent
;
38 import jexer
.event
.TResizeEvent
;
39 import static jexer
.TKeypress
.*;
42 * TImageWindow shows an image with scrollbars.
44 public class TImageWindow
extends TScrollableWindow
{
46 // ------------------------------------------------------------------------
47 // Constants --------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * The number of lines to scroll on mouse wheel up/down.
53 private static final int wheelScrollSize
= 3;
55 // ------------------------------------------------------------------------
56 // Variables --------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * Hang onto the TImage so I can resize it with the window.
62 private TImage imageField
;
64 // ------------------------------------------------------------------------
65 // Constructors -----------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Public constructor opens a file.
71 * @param parent the main application
72 * @param file the file to open
73 * @throws IOException if a java.io operation throws
75 public TImageWindow(final TApplication parent
,
76 final File file
) throws IOException
{
78 this(parent
, file
, 0, 0, parent
.getScreen().getWidth(),
79 parent
.getScreen().getHeight() - 2);
83 * Public constructor opens a file.
85 * @param parent the main application
86 * @param file the file to open
87 * @param x column relative to parent
88 * @param y row relative to parent
89 * @param width width of window
90 * @param height height of window
91 * @throws IOException if a java.io operation throws
93 public TImageWindow(final TApplication parent
, final File file
,
94 final int x
, final int y
, final int width
,
95 final int height
) throws IOException
{
97 super(parent
, file
.getName(), x
, y
, width
, height
, RESIZABLE
);
99 BufferedImage image
= ImageIO
.read(file
);
101 imageField
= new TImage(this, 0, 0, getWidth() - 2, getHeight() - 2,
103 setTitle(file
.getName());
109 * Setup other fields after the image is created.
111 private void setupAfterImage() {
112 if (imageField
.getRows() < getHeight() - 2) {
113 imageField
.setHeight(imageField
.getRows());
114 setHeight(imageField
.getRows() + 2);
116 if (imageField
.getColumns() < getWidth() - 2) {
117 imageField
.setWidth(imageField
.getColumns());
118 setWidth(imageField
.getColumns() + 2);
121 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
122 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
124 setBottomValue(imageField
.getRows() - imageField
.getHeight());
126 setRightValue(imageField
.getColumns() - imageField
.getWidth());
129 // ------------------------------------------------------------------------
130 // Event handlers ---------------------------------------------------------
131 // ------------------------------------------------------------------------
134 * Handle mouse press events.
136 * @param mouse mouse button press event
139 public void onMouseDown(final TMouseEvent mouse
) {
140 // Use TWidget's code to pass the event to the children.
141 super.onMouseDown(mouse
);
143 if (mouse
.isMouseWheelUp()) {
144 imageField
.setTop(imageField
.getTop() - wheelScrollSize
);
145 } else if (mouse
.isMouseWheelDown()) {
146 imageField
.setTop(imageField
.getTop() + wheelScrollSize
);
148 setVerticalValue(imageField
.getTop());
152 * Handle mouse release events.
154 * @param mouse mouse button release event
157 public void onMouseUp(final TMouseEvent mouse
) {
158 // Use TWidget's code to pass the event to the children.
159 super.onMouseUp(mouse
);
161 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
162 // Clicked/dragged on vertical scrollbar
163 imageField
.setTop(getVerticalValue());
165 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
166 // Clicked/dragged on horizontal scrollbar
167 imageField
.setLeft(getHorizontalValue());
172 * Method that subclasses can override to handle mouse movements.
174 * @param mouse mouse motion event
177 public void onMouseMotion(final TMouseEvent mouse
) {
178 // Use TWidget's code to pass the event to the children.
179 super.onMouseMotion(mouse
);
181 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
182 // Clicked/dragged on vertical scrollbar
183 imageField
.setTop(getVerticalValue());
185 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
186 // Clicked/dragged on horizontal scrollbar
187 imageField
.setLeft(getHorizontalValue());
192 * Handle window/screen resize events.
194 * @param event resize event
197 public void onResize(final TResizeEvent event
) {
198 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
199 // Resize the image field
200 TResizeEvent imageSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
201 event
.getWidth() - 2, event
.getHeight() - 2);
202 imageField
.onResize(imageSize
);
204 // Have TScrollableWindow handle the scrollbars
205 super.onResize(event
);
209 // Pass to children instead
210 for (TWidget widget
: getChildren()) {
211 widget
.onResize(event
);
218 * @param keypress keystroke event
221 public void onKeypress(final TKeypressEvent keypress
) {
222 if (keypress
.equals(kbUp
)) {
224 imageField
.setTop(getVerticalValue());
227 if (keypress
.equals(kbDown
)) {
229 imageField
.setTop(getVerticalValue());
232 if (keypress
.equals(kbPgUp
)) {
233 bigVerticalDecrement();
234 imageField
.setTop(getVerticalValue());
237 if (keypress
.equals(kbPgDn
)) {
238 bigVerticalIncrement();
239 imageField
.setTop(getVerticalValue());
242 if (keypress
.equals(kbRight
)) {
243 horizontalIncrement();
244 imageField
.setLeft(getHorizontalValue());
247 if (keypress
.equals(kbLeft
)) {
248 horizontalDecrement();
249 imageField
.setLeft(getHorizontalValue());
253 // We did not take it, let the TImage instance see it.
254 super.onKeypress(keypress
);
256 setVerticalValue(imageField
.getTop());
257 setBottomValue(imageField
.getRows() - imageField
.getHeight());
258 setHorizontalValue(imageField
.getLeft());
259 setRightValue(imageField
.getColumns() - imageField
.getWidth());
262 // ------------------------------------------------------------------------
263 // TWindow ----------------------------------------------------------------
264 // ------------------------------------------------------------------------
274 // We have to get the scrollbar values after we have let the image
276 setBottomValue(imageField
.getRows() - imageField
.getHeight());
277 setRightValue(imageField
.getColumns() - imageField
.getWidth());