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 java
.util
.ResourceBundle
;
35 import javax
.imageio
.ImageIO
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import jexer
.event
.TResizeEvent
;
40 import static jexer
.TKeypress
.*;
43 * TImageWindow shows an image with scrollbars.
45 public class TImageWindow
extends TScrollableWindow
{
50 private static final ResourceBundle i18n
= ResourceBundle
.getBundle(TImageWindow
.class.getName());
52 // ------------------------------------------------------------------------
53 // Constants --------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * The number of lines to scroll on mouse wheel up/down.
59 private static final int wheelScrollSize
= 3;
61 // ------------------------------------------------------------------------
62 // Variables --------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Hang onto the TImage so I can resize it with the window.
68 private TImage imageField
;
70 // ------------------------------------------------------------------------
71 // Constructors -----------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Public constructor opens a file.
77 * @param parent the main application
78 * @param file the file to open
79 * @throws IOException if a java.io operation throws
81 public TImageWindow(final TApplication parent
,
82 final File file
) throws IOException
{
84 this(parent
, file
, 0, 0, parent
.getScreen().getWidth(),
85 parent
.getScreen().getHeight() - 2);
89 * Public constructor opens a file.
91 * @param parent the main application
92 * @param file the file to open
93 * @param x column relative to parent
94 * @param y row relative to parent
95 * @param width width of window
96 * @param height height of window
97 * @throws IOException if a java.io operation throws
99 public TImageWindow(final TApplication parent
, final File file
,
100 final int x
, final int y
, final int width
,
101 final int height
) throws IOException
{
103 super(parent
, file
.getName(), x
, y
, width
, height
, RESIZABLE
);
105 BufferedImage image
= ImageIO
.read(file
);
107 imageField
= new TImage(this, 0, 0, getWidth() - 2, getHeight() - 2,
109 setTitle(file
.getName());
115 * Setup other fields after the image is created.
117 private void setupAfterImage() {
118 if (imageField
.getRows() < getHeight() - 2) {
119 imageField
.setHeight(imageField
.getRows());
120 setHeight(imageField
.getRows() + 2);
122 if (imageField
.getColumns() < getWidth() - 2) {
123 imageField
.setWidth(imageField
.getColumns());
124 setWidth(imageField
.getColumns() + 2);
127 hScroller
= new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
128 vScroller
= new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
130 setBottomValue(imageField
.getRows() - imageField
.getHeight());
132 setRightValue(imageField
.getColumns() - imageField
.getWidth());
134 statusBar
= newStatusBar(i18n
.getString("statusBar"));
137 // ------------------------------------------------------------------------
138 // Event handlers ---------------------------------------------------------
139 // ------------------------------------------------------------------------
142 * Handle mouse press events.
144 * @param mouse mouse button press event
147 public void onMouseDown(final TMouseEvent mouse
) {
148 // Use TWidget's code to pass the event to the children.
149 super.onMouseDown(mouse
);
151 if (mouse
.isMouseWheelUp()) {
152 imageField
.setTop(imageField
.getTop() - wheelScrollSize
);
153 } else if (mouse
.isMouseWheelDown()) {
154 imageField
.setTop(imageField
.getTop() + wheelScrollSize
);
156 setVerticalValue(imageField
.getTop());
160 * Handle mouse release events.
162 * @param mouse mouse button release event
165 public void onMouseUp(final TMouseEvent mouse
) {
166 // Use TWidget's code to pass the event to the children.
167 super.onMouseUp(mouse
);
169 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
170 // Clicked/dragged on vertical scrollbar
171 imageField
.setTop(getVerticalValue());
173 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
174 // Clicked/dragged on horizontal scrollbar
175 imageField
.setLeft(getHorizontalValue());
180 * Method that subclasses can override to handle mouse movements.
182 * @param mouse mouse motion event
185 public void onMouseMotion(final TMouseEvent mouse
) {
186 // Use TWidget's code to pass the event to the children.
187 super.onMouseMotion(mouse
);
189 if (mouse
.isMouse1() && mouseOnVerticalScroller(mouse
)) {
190 // Clicked/dragged on vertical scrollbar
191 imageField
.setTop(getVerticalValue());
193 if (mouse
.isMouse1() && mouseOnHorizontalScroller(mouse
)) {
194 // Clicked/dragged on horizontal scrollbar
195 imageField
.setLeft(getHorizontalValue());
200 * Handle window/screen resize events.
202 * @param event resize event
205 public void onResize(final TResizeEvent event
) {
206 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
207 // Resize the image field
208 TResizeEvent imageSize
= new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
209 event
.getWidth() - 2, event
.getHeight() - 2);
210 imageField
.onResize(imageSize
);
212 // Have TScrollableWindow handle the scrollbars
213 super.onResize(event
);
217 // Pass to children instead
218 for (TWidget widget
: getChildren()) {
219 widget
.onResize(event
);
226 * @param keypress keystroke event
229 public void onKeypress(final TKeypressEvent keypress
) {
230 if (keypress
.equals(kbUp
)) {
232 imageField
.setTop(getVerticalValue());
235 if (keypress
.equals(kbDown
)) {
237 imageField
.setTop(getVerticalValue());
240 if (keypress
.equals(kbPgUp
)) {
241 bigVerticalDecrement();
242 imageField
.setTop(getVerticalValue());
245 if (keypress
.equals(kbPgDn
)) {
246 bigVerticalIncrement();
247 imageField
.setTop(getVerticalValue());
250 if (keypress
.equals(kbRight
)) {
251 horizontalIncrement();
252 imageField
.setLeft(getHorizontalValue());
255 if (keypress
.equals(kbLeft
)) {
256 horizontalDecrement();
257 imageField
.setLeft(getHorizontalValue());
261 // We did not take it, let the TImage instance see it.
262 super.onKeypress(keypress
);
264 setVerticalValue(imageField
.getTop());
265 setBottomValue(imageField
.getRows() - imageField
.getHeight());
266 setHorizontalValue(imageField
.getLeft());
267 setRightValue(imageField
.getColumns() - imageField
.getWidth());
270 // ------------------------------------------------------------------------
271 // TWindow ----------------------------------------------------------------
272 // ------------------------------------------------------------------------
282 // We have to get the scrollbar values after we have let the image
284 setBottomValue(imageField
.getRows() - imageField
.getHeight());
285 setRightValue(imageField
.getColumns() - imageField
.getWidth());