Friday, September 24, 2010

ExplorerPlus

ExplorerPlus
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
import java.net.*;

class MyExplorer implements TreeSelectionListener
{
ImageIcon def=new ImageIcon(".\\java.jpg");

static JFrame frame;
JLabel label;
JTextField textField;
JPanel upperPanel,lowerPanel,showPanel;
JTable table;
File f;
JTree tree;
JScrollPane sp,sp1;
JEditorPane editorPane;
JScrollPane sp2;
JPanel imagePanel;
JLabel imageLabel;
JPanel mainPanel;
MyExplorer()
{
frame=new JFrame("MyExplorer: ~~~~~~By:Milan~~~~~~");
label=new JLabel("Enter A Valid Directory Path and Press Enter:",JLabel.CENTER);
textField = new JTextField();
textField.addActionListener(new TextFieldListener());

String home = System.getProperty("user.home");
textField.setText(home);


upperPanel = new JPanel(new GridLayout(2,1));
upperPanel.add(label);
upperPanel.add(textField);

lowerPanel=new JPanel(new GridLayout(1,2));
table=new JTable(new DirectoryModel(new File(home)));
f=new File(home);
tree=new JTree(createNodes(null,f));
tree.addTreeSelectionListener(this);
sp=new JScrollPane(tree);
lowerPanel.add(sp,0,0);
sp1=new JScrollPane(table);
lowerPanel.add(sp1,0,1);


showPanel=new JPanel();
showPanel.setLayout(new GridLayout(1,2));
editorPane=new JEditorPane();
//editorPane.setText("ok");
editorPane.setEditable(false);
sp2=new JScrollPane(editorPane);
imagePanel=new JPanel();
imageLabel=new JLabel("",JLabel.CENTER);
imageLabel.setIcon(new ImageIcon(def.getImage().getScaledInstance(500,300,Image.SCALE_DEFAULT)));
imagePanel.setLayout(new BorderLayout());
imagePanel.add(new JScrollPane(imageLabel));
showPanel.add(imagePanel);
showPanel.add(sp2);

mainPanel=new JPanel();
mainPanel.setLayout(new GridLayout(2,2));

//frame.setLayout(new GridLayout(0,1));

mainPanel.add(lowerPanel);
mainPanel.add(showPanel);

frame.add(upperPanel,BorderLayout.NORTH);
frame.add(mainPanel);


//frame.pack();
Toolkit t=Toolkit.getDefaultToolkit();
int height=t.getScreenSize().height;
int width=t.getScreenSize().width;
frame.setSize(width,height-50);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String... s)
{
new MyExplorer();
Date d = new Date();
JPanel datePanel = new JPanel();
JLabel label = new JLabel(d.toLocaleString());
datePanel.add(label);
frame.add(datePanel,"South");
while(true)
{
d=new Date();
label.setText(""+d.toLocaleString());
}

}

class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
File temp=new File(textField.getText());
if(temp.exists())
{
setTree();
setTable();
}
else
JOptionPane.showMessageDialog(null,"file/directory does not exits","Error",JOptionPane.ERROR_MESSAGE);
}
}

public void setTree()
{
f=new File(textField.getText());
tree=new JTree(createNodes(null,f));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
lowerPanel.removeAll();
sp=new JScrollPane(tree);
lowerPanel.add(sp,0,0);
tree.addTreeSelectionListener(this);
}

public void setTable()
{
lowerPanel.remove(sp1);
sp1=new JScrollPane(new JTable(new DirectoryModel(new File(textField.getText()))));
lowerPanel.add(sp1,0,1);
lowerPanel.updateUI();
}


public void setFileTable()
{
lowerPanel.remove(sp1);
sp1=new JScrollPane(new JTable(new FileModel(new File(textField.getText()))));
lowerPanel.add(sp1,0,1);
lowerPanel.updateUI();
}



DefaultMutableTreeNode createNodes(DefaultMutableTreeNode top, File dir)
{
String curPath = dir.getPath();
Vector files = new Vector();
File f;
//File tmpf=new File(curPath);
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (top != null)
{
top.add(curDir);
}
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
{
String path=tmp[i];
String newpath;
if (curPath.equals("."))
newpath = path;
else
newpath = curPath + File.separator + tmp[i];

if ((f = new File(newpath)).isDirectory())
createNodes(curDir, f);
else
files.addElement(curPath+File.separator+path);
}
for (int j = 0; j {
curDir.add(new DefaultMutableTreeNode(files.elementAt(j)));
}
return curDir;
}

public void valueChanged(TreeSelectionEvent e)
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node==null)
{
System.out.println("node is null");
return;
}
Object nodeInfo = node.getUserObject();
File f=new File(nodeInfo.toString());
if(f.isDirectory())
{
textField.setText(f.getPath());
setTable();
editorPane.setText("This is a Directory not a File");
imageLabel.setIcon(new ImageIcon(def.getImage().getScaledInstance(500,300,Image.SCALE_DEFAULT)));
}
else
{
textField.setText(nodeInfo.toString());
setFileTable();
setEditorPane(nodeInfo.toString());
}
}

public void setEditorPane(String path)
{
File file=new File(path);
String filename=file.getName();
if(filename.endsWith(".txt")||filename.endsWith(".java")||filename.endsWith(".c")||filename.endsWith(".cpp")||filename.endsWith(".css"))
{
openFile(file);
}
else if(filename.endsWith(".jpg")||filename.endsWith(".JPG")||filename.endsWith(".JPEG") || filename.endsWith(".jpeg")||
filename.endsWith(".gif")||filename.endsWith(".GIF") || filename.endsWith(".png")||filename.endsWith(".PNG")||
filename.endsWith(".bmp")||filename.endsWith(".BMP"))
{
ImageIcon temp=new ImageIcon(file.getPath());
ImageIcon thumbnail=temp;
if (temp.getIconWidth() >380 || temp.getIconHeight()>300)
{
thumbnail = new ImageIcon(temp.getImage().getScaledInstance(500,300,Image.SCALE_DEFAULT));
}
imageLabel.setText("");
imageLabel.setIcon(thumbnail);
}
else
{
imageLabel.setIcon(new ImageIcon("java.jpeg"));
editorPane.setText("this is not a txt,java,c,cpp,css file");
//imageLabel.setText("not an image");
//ImageIcon t=new ImageIcon("java.jpg");
def=new ImageIcon(def.getImage().getScaledInstance(500,300,Image.SCALE_DEFAULT));
imageLabel.setIcon(def);
imageLabel.repaint();
}
}

public void openFile(File file)
{
String data="";
String temp="";
try
{
BufferedReader br=new BufferedReader(new FileReader(file));
while((data=br.readLine())!=null)
{
data=data+"\n";
temp=temp+data;
}
//System.out.println(data);
editorPane.setText(temp);
editorPane.setCaretPosition(0);
showPanel.updateUI();
}
catch(Exception e)
{
System.out.println("exception on reading file");
}
}


}//end class Explorer


class DirectoryModel extends AbstractTableModel
{
File directory;
String[] members;
int rowCount;

public DirectoryModel(File dir)
{
directory = dir; // Hold the directory
members = dir.list(); // Get the list of files and subdirectories
if (members != null)

// Table rows = No. of entities inside the directory
rowCount = members.length;

else
{
// If the memeber list is null, row count should be zero.
rowCount = 0;
// This can happen if an invalid directory is entered in the text field.
System.out.println("Not a valid directory!");
}
}

//get the number of rows for the table to be prepared.
public int getRowCount()
{
return (members != null? rowCount:0);
}

//get the column count.
public int getColumnCount()
{
return members != null? 6:0;
}

// get each of the table values at the specified row and column.
public Object getValueAt(int row, int column)
{
if (directory == null || members == null)
{
return null;
}

File fileSysEntity = new File(directory, members[row]);
switch(column)
{
case 0: //name
return fileSysEntity.getName();
case 1: //size
if (fileSysEntity.isDirectory())
{
return "...";
}
else
{
return new Long(fileSysEntity.length());
}
case 2:
if(fileSysEntity.canRead())
{
return (new Boolean("true"));
}
else
{
return (new Boolean(false));
}

case 3:
if(fileSysEntity.canRead())
{
return (new Boolean(true));
}
else
{
return (new Boolean(false));
}

case 4:
if(fileSysEntity.isHidden())
{
return (new Boolean(true));
}
else
{
return (new Boolean(false));
}


case 5: //directory or not
return fileSysEntity.isDirectory()? new Boolean(true):new Boolean(false);
default:
return "";
}
}

//get the column names to be used in the table header.
public String getColumnName(int column)
{
switch(column)
{
case 0:
return "Name";
case 1:
return "Size";
case 2:
return "Read";
case 3:
return "Write";
case 4:
return "Hidden";
case 5:
return "Directory";
default:
return "";
}
}

//get the class types of the entries in each of the table columns.
public Class getColumnClass(int column)
{
Class returnClass = Boolean.class;
if (column == 0|| column==1)
returnClass = String.class;
return returnClass;
}
}//end class Directory model





class FileModel extends AbstractTableModel
{
File directory;
int rowCount;

public FileModel(File dir)
{
directory = dir; // Hold the directory
rowCount=1;
}

//get the number of rows for the table to be prepared.
public int getRowCount()
{
return 1;
}

//get the column count.
public int getColumnCount()
{
return 5;
}

//get each of the table values at the specified row and column.
public Object getValueAt(int row, int column)
{
if (directory == null)
{
return null;
}

File fileSysEntity = new File(directory.getPath());
switch(column)
{
case 0: //name
return fileSysEntity.getName();

case 1: //size
return new Long(fileSysEntity.length());

case 2:
if(fileSysEntity.canRead())
{
return (new Boolean("true"));
}
else
{
return (new Boolean(false));
}

case 3:
if(fileSysEntity.canRead())
{
return (new Boolean(true));
}
else
{
return (new Boolean(false));
}

case 4:
if(fileSysEntity.isHidden())
{
return (new Boolean(true));
}
else
{
return (new Boolean(false));
}
default:
return "";
}
}

// get the column names to be used in the table header.
public String getColumnName(int column)
{
switch(column)
{
case 0:
return "Name";
case 1:
return "Size";
case 2:
return "Read";
case 3:
return "Write";
case 4:
return "Hidden";
default:
return "";
}
}

//get the class types of the entries in each of the table columns.
public Class getColumnClass(int column)
{
Class returnClass = Boolean.class;
if (column == 0 || column==1)
returnClass = String.class;
return returnClass;
}
}//end class Filemodel

Download

0 comments:

Post a Comment