Display images
emmanue1
4 years ago
+71
-0
0 | /* | |
1 | * Copyright (c) 2008-2019 Emmanuel Dupuy. | |
2 | * This project is distributed under the GPLv3 license. | |
3 | * This is a Copyleft license that gives the user the right to use, | |
4 | * copy and modify the code freely for non-commercial purposes. | |
5 | */ | |
6 | ||
7 | package org.jd.gui.service.treenode; | |
8 | ||
9 | import org.jd.gui.api.API; | |
10 | import org.jd.gui.api.feature.*; | |
11 | import org.jd.gui.api.model.Container; | |
12 | import org.jd.gui.util.exception.ExceptionUtil; | |
13 | import org.jd.gui.view.data.TreeNodeBean; | |
14 | ||
15 | import javax.imageio.ImageIO; | |
16 | import javax.swing.*; | |
17 | import javax.swing.tree.DefaultMutableTreeNode; | |
18 | import java.awt.*; | |
19 | import java.io.IOException; | |
20 | import java.io.InputStream; | |
21 | import java.net.URI; | |
22 | ||
23 | public class ImageFileTreeNodeFactoryProvider extends FileTreeNodeFactoryProvider { | |
24 | protected static final ImageIcon ICON = new ImageIcon(ImageFileTreeNodeFactoryProvider.class.getClassLoader().getResource("org/jd/gui/images/file-image.gif")); | |
25 | ||
26 | @Override public String[] getSelectors() { return appendSelectors("*:file:*.gif", "*:file:*.jpg", "*:file:*.png"); } | |
27 | ||
28 | @Override | |
29 | @SuppressWarnings("unchecked") | |
30 | public <T extends DefaultMutableTreeNode & ContainerEntryGettable & UriGettable> T make(API api, Container.Entry entry) { | |
31 | int lastSlashIndex = entry.getPath().lastIndexOf("/"); | |
32 | String name = entry.getPath().substring(lastSlashIndex+1); | |
33 | return (T)new TreeNode(entry, new TreeNodeBean(name, "Location: " + entry.getUri().getPath(), ICON)); | |
34 | } | |
35 | ||
36 | protected static class TreeNode extends FileTreeNodeFactoryProvider.TreeNode implements PageCreator { | |
37 | public TreeNode(Container.Entry entry, Object userObject) { super(entry, userObject); } | |
38 | ||
39 | // --- PageCreator --- // | |
40 | @Override | |
41 | @SuppressWarnings("unchecked") | |
42 | public <T extends JComponent & UriGettable> T createPage(API api) { | |
43 | return (T)new ImagePage(entry); | |
44 | } | |
45 | } | |
46 | ||
47 | protected static class ImagePage extends JPanel implements UriGettable { | |
48 | protected Container.Entry entry; | |
49 | ||
50 | public ImagePage(Container.Entry entry) { | |
51 | super(new BorderLayout()); | |
52 | ||
53 | this.entry = entry; | |
54 | ||
55 | try (InputStream is = entry.getInputStream()) { | |
56 | JScrollPane scrollPane = new JScrollPane(new JLabel(new ImageIcon(ImageIO.read(is)))); | |
57 | ||
58 | scrollPane.getHorizontalScrollBar().setUnitIncrement(16); | |
59 | scrollPane.getVerticalScrollBar().setUnitIncrement(16); | |
60 | ||
61 | add(scrollPane, BorderLayout.CENTER); | |
62 | } catch (IOException e) { | |
63 | assert ExceptionUtil.printStackTrace(e); | |
64 | } | |
65 | } | |
66 | ||
67 | // --- UriGettable --- // | |
68 | @Override public URI getUri() { return entry.getUri(); } | |
69 | } | |
70 | }⏎ |
25 | 25 | org.jd.gui.service.treenode.XmlBasedFileTreeNodeFactoryProvider |
26 | 26 | org.jd.gui.service.treenode.XmlFileTreeNodeFactoryProvider |
27 | 27 | org.jd.gui.service.treenode.ZipFileTreeNodeFactoryProvider |
28 | org.jd.gui.service.treenode.ImageFileTreeNodeFactoryProvider |
Binary diff not shown