Created
July 26, 2015 16:43
-
-
Save neojou/c4db70b2fa49636caa7a to your computer and use it in GitHub Desktop.
Java jpegviewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package jpegviewer; | |
import java.io.*; | |
import java.util.*; | |
import javax.swing.tree.*; | |
import javax.swing.event.*; | |
/** | |
* | |
* @author neojou | |
*/ | |
class FileTreeModel implements TreeModel { | |
private File root; | |
private Vector listeners = new Vector(); | |
private HashMap<String, String[]> childListMap; | |
static private String[] EmptyStringArray = new String[0]; | |
private String[] getChildList(File parent) { | |
String filename = parent.getAbsolutePath(); | |
String[] filenameList = childListMap.get(filename); | |
if ( filenameList != null ) return filenameList; | |
List<String> childList = new ArrayList<String>(); | |
String[] children = parent.list(); | |
for (int i=0; i<children.length; i++) { | |
File child_file = new File(parent, children[i]); | |
if ( child_file.isDirectory() ) { | |
//String[] aList = getChildList(child_file); | |
//if ( aList==null || aList.length == 0 ) continue; | |
} else if ( child_file.isFile()) { | |
if ( !child_file.getName().endsWith(".jpg") && | |
!child_file.getName().endsWith(".png") ) { | |
continue; | |
} | |
} else { | |
continue; | |
} | |
childList.add(children[i]); | |
} | |
if ( childList.size() > 0 ) { | |
String[] result = new String[childList.size()]; | |
result = childList.toArray(result); | |
childListMap.put(filename, result); | |
return result; | |
} else { | |
childListMap.put(filename, EmptyStringArray); | |
return EmptyStringArray; | |
} | |
} | |
public FileTreeModel(File rootDirectory) { | |
root = rootDirectory; | |
childListMap = new HashMap(); | |
} | |
public Object getRoot() { | |
return root; | |
} | |
public Object getChild(Object parent, int index) { | |
File directory = (File) parent; | |
String[] children = getChildList(directory); | |
return new TreeFile(directory, children[index]); | |
} | |
public int getChildCount(Object parent) { | |
File file = (File) parent; | |
if (file.isDirectory()) { | |
String[] fileList = getChildList(file); | |
if (fileList != null && fileList != EmptyStringArray ) | |
return fileList.length; | |
} | |
return 0; | |
} | |
public boolean isLeaf(Object node) { | |
File file = (File) node; | |
return file.isFile(); | |
} | |
public int getIndexOfChild(Object parent, Object child) { | |
File directory = (File) parent; | |
File file = (File) child; | |
String[] children = getChildList(directory); | |
for (int i = 0; i < children.length; i++) { | |
if (file.getName().equals(children[i])) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
public void valueForPathChanged(TreePath path, Object value) { | |
File oldFile = (File) path.getLastPathComponent(); | |
String fileParentPath = oldFile.getParent(); | |
String newFileName = (String) value; | |
File targetFile = new File(fileParentPath, newFileName); | |
oldFile.renameTo(targetFile); | |
File parent = new File(fileParentPath); | |
int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) }; | |
Object[] changedChildren = { targetFile }; | |
fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren); | |
} | |
private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) { | |
TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children); | |
Iterator iterator = listeners.iterator(); | |
TreeModelListener listener = null; | |
while (iterator.hasNext()) { | |
listener = (TreeModelListener) iterator.next(); | |
listener.treeNodesChanged(event); | |
} | |
} | |
public void addTreeModelListener(TreeModelListener listener) { | |
listeners.add(listener); | |
} | |
public void removeTreeModelListener(TreeModelListener listener) { | |
listeners.remove(listener); | |
} | |
private class TreeFile extends File { | |
public TreeFile(File parent, String child) { | |
super(parent, child); | |
} | |
public String toString() { | |
return getName(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package jpegviewer; | |
import java.awt.*; | |
import java.awt.image.*; | |
import java.io.*; | |
import javax.imageio.*; | |
import javax.swing.*; | |
/** | |
* | |
* @author neojou | |
*/ | |
public class JpegViewer implements Runnable { | |
private File image_file; | |
private JFrame show_frame; | |
private BufferedImage image; | |
public JpegViewer(File file) { | |
image_file = file; | |
show_frame = new JFrame(file.getName()); | |
show_frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | |
show_frame.setVisible(false); | |
image = null; | |
} | |
public void run() | |
{ | |
try | |
{ | |
image = ImageIO.read(image_file); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
ImageIcon imageIcon = new ImageIcon(image); | |
JLabel jLabel = new JLabel(); | |
jLabel.setIcon(imageIcon); | |
show_frame.getContentPane().add(jLabel, BorderLayout.CENTER); | |
show_frame.pack(); | |
show_frame.setLocationRelativeTo(null); | |
show_frame.setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package jpegviewer; | |
import java.io.*; | |
import java.awt.*; | |
import javax.swing.*; | |
import javax.swing.event.*; | |
import javax.swing.tree.*; | |
/** | |
* | |
* @author neojou | |
*/ | |
public class TreeApp extends JFrame { | |
private JTree tree; | |
// | |
private FileTreeModel fileTreeModel; | |
private JTextArea fileDetailsTextArea; | |
public TreeApp(String directory) | |
{ | |
fileDetailsTextArea = new JTextArea(); | |
fileDetailsTextArea.setEditable(false); | |
fileTreeModel = new FileTreeModel(new File(directory)); | |
tree = new JTree(fileTreeModel); | |
tree.setEditable(true); | |
tree.addTreeSelectionListener(new TreeSelectionListener() { | |
public void valueChanged(TreeSelectionEvent event) { | |
try { | |
File file = (File) tree.getLastSelectedPathComponent(); | |
new Thread(new JpegViewer(file)).start(); | |
} catch ( Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
setLayout(new BorderLayout()); | |
add(new JScrollPane(tree), BorderLayout.CENTER); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setSize(800, 600); | |
setVisible(true); | |
} | |
public static void main(String[] args) throws Exception { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
TreeApp app = new TreeApp("/Users/neojou"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment