-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJexerImageViewer.java
More file actions
315 lines (276 loc) · 9.95 KB
/
JexerImageViewer.java
File metadata and controls
315 lines (276 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import jexer.TAction;
import jexer.TApplication;
import jexer.TDesktop;
import jexer.TDirectoryList;
import jexer.TImage;
import jexer.backend.SwingTerminal;
import jexer.bits.CellAttributes;
import jexer.bits.GraphicsChars;
import jexer.event.TKeypressEvent;
import jexer.event.TResizeEvent;
import jexer.menu.TMenu;
import jexer.ttree.TDirectoryTreeItem;
import jexer.ttree.TTreeItem;
import jexer.ttree.TTreeViewWidget;
import static jexer.TKeypress.*;
/**
* Implements a simple image thumbnail file viewer. Much of this code was
* stripped down from TFileOpenBox.
*/
public class JexerImageViewer extends TApplication {
/**
* Main entry point.
*/
public static void main(String [] args) throws Exception {
JexerImageViewer app = new JexerImageViewer();
(new Thread(app)).start();
}
/**
* Public constructor chooses the ECMA-48 / Xterm backend.
*/
public JexerImageViewer() throws Exception {
super(BackendType.XTERM);
// The stock tool menu has items for redrawing the screen, opening
// images, and (when using the Swing backend) setting the font.
addToolMenu();
// We will have one menu containing a mix of new and stock commands
TMenu fileMenu = addMenu("&File");
// Stock commands: a new shell, exit program.
fileMenu.addDefaultItem(TMenu.MID_SHELL);
fileMenu.addSeparator();
fileMenu.addDefaultItem(TMenu.MID_EXIT);
// Filter the files list to support image suffixes only.
List<String> filters = new ArrayList<String>();
filters.add("^.*\\.[Jj][Pp][Gg]$");
filters.add("^.*\\.[Jj][Pp][Ee][Gg]$");
filters.add("^.*\\.[Pp][Nn][Gg]$");
filters.add("^.*\\.[Gg][Ii][Ff]$");
filters.add("^.*\\.[Bb][Mm][Pp]$");
setDesktop(new ImageViewerDesktop(this, ".", filters));
}
}
/**
* The desktop contains a tree view on the left, list of files on the top
* right, and image view on the bottom right.
*/
class ImageViewerDesktop extends TDesktop {
/**
* The left-side tree view pane.
*/
private TTreeViewWidget treeView;
/**
* The data behind treeView.
*/
private TDirectoryTreeItem treeViewRoot;
/**
* The top-right-side directory list pane.
*/
private TDirectoryList directoryList;
/**
* The bottom-right-side image pane.
*/
private TImage imageWidget;
/**
* Public constructor.
*
* @param application the TApplication that manages this window
* @param path path of selected file
* @param filters a list of strings that files must match to be displayed
* @throws IOException of a java.io operation throws
*/
public ImageViewerDesktop(final TApplication application, final String path,
final List<String> filters) throws IOException {
super(application);
setActive(true);
// Add directory treeView
treeView = addTreeViewWidget(0, 0, getWidth() / 2, getHeight(),
new TAction() {
public void DO() {
TTreeItem item = treeView.getSelected();
File selectedDir = ((TDirectoryTreeItem) item).getFile();
try {
directoryList.setPath(selectedDir.getCanonicalPath());
if (directoryList.getList().size() > 0) {
setThumbnail(directoryList.getPath());
} else {
if (imageWidget != null) {
getChildren().remove(imageWidget);
}
imageWidget = null;
}
activate(treeView);
} catch (IOException e) {
// If the backend is Swing, we can emit the stack
// trace to stderr. Otherwise, just squash it.
if (getScreen() instanceof SwingTerminal) {
e.printStackTrace();
}
}
}
}
);
treeViewRoot = new TDirectoryTreeItem(treeView, path, true);
// Add directory files list
directoryList = addDirectoryList(path, getWidth() / 2 + 1, 0,
getWidth() / 2 - 1, getHeight() / 2,
new TAction() {
public void DO() {
setThumbnail(directoryList.getPath());
}
},
new TAction() {
public void DO() {
setThumbnail(directoryList.getPath());
}
},
filters);
if (directoryList.getList().size() > 0) {
activate(directoryList);
setThumbnail(directoryList.getPath());
} else {
activate(treeView);
}
}
/**
* Handle window/screen resize events.
*
* @param event resize event
*/
@Override
public void onResize(final TResizeEvent event) {
// Resize the tree and list
treeView.setY(1);
treeView.setWidth(getWidth() / 2);
treeView.setHeight(getHeight() - 1);
treeView.onResize(new TResizeEvent(event.getBackend(),
TResizeEvent.Type.WIDGET,
treeView.getWidth(),
treeView.getHeight()));
treeView.getTreeView().onResize(new TResizeEvent(event.getBackend(),
TResizeEvent.Type.WIDGET,
treeView.getWidth() - 1,
treeView.getHeight() - 1));
directoryList.setX(getWidth() / 2 + 1);
directoryList.setY(1);
directoryList.setWidth(getWidth() / 2 - 1);
directoryList.setHeight(getHeight() / 2 - 1);
directoryList.onResize(new TResizeEvent(event.getBackend(),
TResizeEvent.Type.WIDGET,
directoryList.getWidth(),
directoryList.getHeight()));
// Recreate the image
if (imageWidget != null) {
getChildren().remove(imageWidget);
}
imageWidget = null;
if (directoryList.getList().size() > 0) {
activate(directoryList);
setThumbnail(directoryList.getPath());
} else {
activate(treeView);
}
}
/**
* Handle keystrokes.
*
* @param keypress keystroke event
*/
@Override
public void onKeypress(final TKeypressEvent keypress) {
if (treeView.isActive() || directoryList.isActive()) {
if ((keypress.equals(kbEnter))
|| (keypress.equals(kbUp))
|| (keypress.equals(kbDown))
|| (keypress.equals(kbPgUp))
|| (keypress.equals(kbPgDn))
|| (keypress.equals(kbHome))
|| (keypress.equals(kbEnd))
) {
// Tree view will be changing, update the directory list.
super.onKeypress(keypress);
// This is the same action as treeView's enter.
TTreeItem item = treeView.getSelected();
File selectedDir = ((TDirectoryTreeItem) item).getFile();
try {
if (treeView.isActive()) {
directoryList.setPath(selectedDir.getCanonicalPath());
}
if (directoryList.getList().size() > 0) {
activate(directoryList);
setThumbnail(directoryList.getPath());
} else {
if (imageWidget != null) {
getChildren().remove(imageWidget);
}
imageWidget = null;
activate(treeView);
}
} catch (IOException e) {
// If the backend is Swing, we can emit the stack trace
// to stderr. Otherwise, just squash it.
if (getScreen() instanceof SwingTerminal) {
e.printStackTrace();
}
}
return;
}
}
// Pass to my parent
super.onKeypress(keypress);
}
/**
* Draw me on screen.
*/
@Override
public void draw() {
CellAttributes background = getTheme().getColor("tdesktop.background");
putAll(' ', background);
vLineXY(getWidth() / 2, 0, getHeight(),
GraphicsChars.WINDOW_SIDE, getBackground());
hLineXY(getWidth() / 2, getHeight() / 2, (getWidth() + 1) / 2,
GraphicsChars.WINDOW_TOP, getBackground());
putCharXY(getWidth() / 2, getHeight() / 2,
GraphicsChars.WINDOW_LEFT_TEE, getBackground());
}
/**
* Set the image thumbnail.
*
* @param file the image file
*/
private void setThumbnail(final File file) {
if (file == null) {
return;
}
if (!file.exists() || !file.isFile()) {
return;
}
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
// If the backend is Swing, we can emit the stack trace to
// stderr. Otherwise, just squash it.
if (getScreen() instanceof SwingTerminal) {
e.printStackTrace();
}
return;
}
if (imageWidget != null) {
getChildren().remove(imageWidget);
}
int width = getWidth() / 2 - 1;
int height = getHeight() / 2 - 1;
imageWidget = new TImage(this, getWidth() - width,
getHeight() - height, width, height, image, 0, 0, null);
// Resize the image to fit within the pane.
imageWidget.setScaleType(TImage.Scale.SCALE);
imageWidget.setActive(false);
activate(directoryList);
}
}