/*
* MyExplorerGUI.java
*
* Created on 21. April 2005, 12:47
*/
package Explorer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.tree.*;
/**
*
* @author alle
*/
public class MyExplorerGUI extends javax.swing.JFrame
{
private DefaultMutableTreeNode dmtn;
private MyTableModel dtm;
private String[] tableHead = {"Name", "Größe Byte", "Typ"};
private MyFile curDir;
private Vector redo;
private Vector undo;
public MyExplorerGUI()
{
try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
}
catch(ClassNotFoundException e) { System.out.println("Fehler im Look&Feel"); }
catch(InstantiationException e) { System.out.println("Fehler im Look&Feel"); }
catch(IllegalAccessException e) { System.out.println("Fehler im Look&Feel"); }
catch(UnsupportedLookAndFeelException e) { System.out.println("Fehler im Look&Feel"); }
JFrame fr = new JFrame("Warten");
JPanel pa = new JPanel();
pa.add(new JLabel("Verzeichnisse werden geladen"));
fr.getContentPane().add(pa);
fr.show();
fr.pack();
dtm = new MyTableModel( tableHead, 1);
dmtn = new DefaultMutableTreeNode("Arbeitsplatz");
redo = new Vector();
undo = new Vector();
laufwerkeLaden();
fr.setVisible(false);
initComponents();
tVerzeichnis.setModel(dtm);
tree.setSelectionRow(0);
}
private void laufwerkeLaden(){
// File[] roots = new File[File.listRoots().length];
// for(int i=0; i<File.listRoots().length; i++) {
// roots[i] = (File)File.listRoots()[i];
// }
MyFile[] roots = MyFile.listRootsInEx();
DefaultMutableTreeNode tmp;
for(int i=0; i<roots.length; i++) {
tmp = new DefaultMutableTreeNode(roots[i]);
dmtn.add(tmp);
addTableRowData(roots[i]);
ebenenLaden(tmp, 0, roots[i]);
}
}
private void addTableRowData(MyFile f) {
Object[] tableInsert = new Object[3];
String out = "Datei";
if(f.isFile()) {
try {
StringTokenizer stk = new StringTokenizer(f.getName(),".");
String s = stk.nextToken();
s = stk.nextToken();
out = s+"-"+out;
}
catch(NoSuchElementException e) {
System.out.println("Keine Extension");
}
}
tableInsert[0] = f;
Long l = new Long(f.length());
tableInsert[1] = l;
if(f.isDirectory())
tableInsert[2] = "Ordner";
else
tableInsert[2] = out;
dtm.addRow(tableInsert);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
pmPopUp = new javax.swing.JPopupMenu();
miDateiLoeschen = new javax.swing.JMenuItem();
miDateiUmbennen = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
mNeu = new javax.swing.JMenu();
miNeuOrdner = new javax.swing.JMenuItem();
miNeuTxt = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
miOeffnenTxt = new javax.swing.JMenuItem();
jSplitPane1 = new javax.swing.JSplitPane();
spTree = new javax.swing.JScrollPane();
tree = new JTree(dmtn);
spVerzeichnis = new javax.swing.JScrollPane();
tVerzeichnis = new javax.swing.JTable();
pSymbolleisten = new javax.swing.JPanel();
pButtons = new javax.swing.JPanel();
btBack = new javax.swing.JButton();
btForward = new javax.swing.JButton();
btDirUp = new javax.swing.JButton();
pAdresse = new javax.swing.JPanel();
lbAdresse = new javax.swing.JLabel();
tfAdresse = new javax.swing.JTextField();
btWechselnZu = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
mDatei = new javax.swing.JMenu();
miBeenden = new javax.swing.JMenuItem();
mTextEditor = new javax.swing.JMenu();
miTextEditor = new javax.swing.JMenuItem();
miDateiLoeschen.setText("Datei l\u00f6schen");
miDateiLoeschen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onDateiLoeschen(evt);
}
});
pmPopUp.add(miDateiLoeschen);
miDateiUmbennen.setText("Datei umbennen");
miDateiUmbennen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onDateiUmbennen(evt);
}
});
pmPopUp.add(miDateiUmbennen);
pmPopUp.add(jSeparator1);
mNeu.setText("Neu");
miNeuOrdner.setText("Ordner");
miNeuOrdner.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onNeuOrdner(evt);
}
});
mNeu.add(miNeuOrdner);
miNeuTxt.setText("Txt-Datei");
miNeuTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onNeuTxt(evt);
}
});
mNeu.add(miNeuTxt);
pmPopUp.add(mNeu);
pmPopUp.add(jSeparator2);
miOeffnenTxt.setText("Datei mit Editor \u00f6ffnen");
miOeffnenTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onOpenWithEd(evt);
}
});
pmPopUp.add(miOeffnenTxt);
setTitle("MyExplorer");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
spTree.setPreferredSize(new java.awt.Dimension(150, 322));
tree.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
}
public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
onExpanded(evt);
}
});
tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
onTree(evt);
}
});
spTree.setViewportView(tree);
jSplitPane1.setLeftComponent(spTree);
spVerzeichnis.setPreferredSize(new java.awt.Dimension(350, 130));
tVerzeichnis.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tVerzeichnis.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
onTVerzeichnisClick(evt);
}
});
spVerzeichnis.setViewportView(tVerzeichnis);
jSplitPane1.setRightComponent(spVerzeichnis);
getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
pSymbolleisten.setLayout(new java.awt.GridLayout(2, 0));
pButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
btBack.setText("<=");
btBack.setToolTipText("");
btBack.setActionCommand("-1");
btBack.setEnabled(false);
btBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onReDo(evt);
}
});
pButtons.add(btBack);
btForward.setText("=>");
btForward.setActionCommand("1");
btForward.setEnabled(false);
btForward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onReDo(evt);
}
});
pButtons.add(btForward);
btDirUp.setText("/\\");
btDirUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onDirUp(evt);
}
});
pButtons.add(btDirUp);
pSymbolleisten.add(pButtons);
pAdresse.setLayout(new java.awt.BorderLayout());
pAdresse.setPreferredSize(new java.awt.Dimension(111, 15));
lbAdresse.setText("Adresse");
pAdresse.add(lbAdresse, java.awt.BorderLayout.WEST);
tfAdresse.setText("Arbeitsplatz");
tfAdresse.setPreferredSize(new java.awt.Dimension(69, 15));
pAdresse.add(tfAdresse, java.awt.BorderLayout.CENTER);
btWechselnZu.setText("=> Wechseln zu");
pAdresse.add(btWechselnZu, java.awt.BorderLayout.EAST);
pSymbolleisten.add(pAdresse);
getContentPane().add(pSymbolleisten, java.awt.BorderLayout.NORTH);
mDatei.setText("Datei");
miBeenden.setText("Ende");
miBeenden.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onEnde(evt);
}
});
mDatei.add(miBeenden);
jMenuBar1.add(mDatei);
mTextEditor.setText("TextEditor");
miTextEditor.setText("Editor \u00f6ffnen");
miTextEditor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
onOpenEditor(evt);
}
});
mTextEditor.add(miTextEditor);
jMenuBar1.add(mTextEditor);
setJMenuBar(jMenuBar1);
pack();
}
private void onTVerzeichnisClick(java.awt.event.MouseEvent evt) {
try {
if(evt.getButton() == MouseEvent.BUTTON1) {
int sel = tVerzeichnis.getSelectedRow();
MyFile tmp = (MyFile)dtm.getValueAt(sel,0);
if (evt.getClickCount() == 2) {
if(tmp.isDirectory()) {
curDir = tmp;
DefaultMutableTreeNode help = (DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent();
int i = 0;
for (i=0; i<help.getChildCount(); i++) {
if ( tmp.equals((MyFile)((DefaultMutableTreeNode)help.getChildAt(i)).getUserObject()) ) {
help = (DefaultMutableTreeNode)help.getChildAt(i);
break;
}
}
if(help.getDepth()<2)
ebenenLaden(help,1,(MyFile)help.getUserObject());
tree.setSelectionPath(new TreePath(help.getPath()));
openFilesInList(tmp);
setTitle(tmp.getAbsolutePath());
}
if(tmp.isFile()) {
StringTokenizer stk = new StringTokenizer(tmp.getName(),".");
String s = stk.nextToken();
s = stk.nextToken();
if(s.equalsIgnoreCase("txt")) {
EditorGUI ed = new EditorGUI(tmp);
ed.load();
ed.show();
}
else
Runtime.getRuntime().exec("rundll32.exe url.dll,FileProtocolHandler "+tmp.getAbsolutePath());
}
}
}
else {
if(tVerzeichnis.getSelectedRow()==-1) {
miDateiLoeschen.setVisible(false);
miDateiUmbennen.setVisible(false);
miOeffnenTxt.setVisible(false);
pmPopUp.show(this, evt.getX(), evt.getY());
}
else {
miDateiLoeschen.setVisible(true);
miDateiUmbennen.setVisible(true);
if( ((MyFile)tVerzeichnis.getValueAt(tVerzeichnis.getSelectedRow(),0)).isFile() ) {
miOeffnenTxt.setVisible(true);
}
pmPopUp.show(this, evt.getX(), evt.getY());
}
}
}
catch(IOException e) {
System.out.println("Fehler beim Ausführen einer Datei");
}
catch(NullPointerException e) {
System.out.println("Kann nicht ausgewählt werden");
}
}
private void onOpenWithEd(java.awt.event.ActionEvent evt) {
int sel = tVerzeichnis.getSelectedRow();
MyFile tmp = (MyFile)dtm.getValueAt(sel,0);
EditorGUI ed = new EditorGUI(tmp);
ed.load();
ed.show();
}
private void onDirUp(java.awt.event.ActionEvent evt) {
if(curDir.getParentFile()!=null) {
tree.setSelectionPath(tree.getSelectionPath().getParentPath());
if( ((DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent()).getParent() == null) {
curDir = null;
btDirUp.setEnabled(false);
int i=dtm.getRowCount()-1;
while(i>=0) {
dtm.removeRow(i);
i--;
}
MyFile[] files = MyFile.listRootsInEx();
for(i=0; i<files.length; i++) {
addTableRowData(files[i]);
}
//Redo hinzufürgen
if(redo.size() == 0 || (TreePath)redo.elementAt(redo.size()-1)!= tree.getSelectionPath()) {
redo.add(tree.getSelectionPath());
btBack.setEnabled(true);
}
}
else {
curDir = curDir.getParentFileInEx();
openFilesInList(curDir);
}
}
}
private void onReDo(java.awt.event.ActionEvent evt) {
if(Integer.parseInt(evt.getActionCommand()) < 0) {
if( ((DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent()).getParent() == null) {
curDir = null;
btDirUp.setEnabled(false);
int i=dtm.getRowCount()-1;
while(i>=0) {
dtm.removeRow(i);
i--;
}
MyFile[] files = MyFile.listRootsInEx();
for(i=0; i<files.length; i++) {
addTableRowData(files[i]);
}
tree.setSelectionPath((TreePath)redo.elementAt(redo.size()-1));
}
else {
tree.setSelectionPath((TreePath)redo.elementAt(redo.size()-1));
openFilesInList(curDir);
}
undo.add((TreePath)redo.remove(redo.size()-1));
btForward.setEnabled(true);
if(redo.size()==0)
btBack.setEnabled(false);
}
if(Integer.parseInt(evt.getActionCommand()) > 0) {
tree.setSelectionPath((TreePath)undo.elementAt(undo.size()-1));
redo.add((TreePath)undo.remove(undo.size()-1));
btBack.setEnabled(true);
if(undo.size()==0)
btForward.setEnabled(false);
}
}
private void onNeuTxt(java.awt.event.ActionEvent evt) {
try{
MyFile newTxt = new MyFile(curDir.getPath(), JOptionPane.showInputDialog(this, "Name eingeben")+".txt");
FileWriter fw = new FileWriter(newTxt);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("");
bw.close();
}
catch(IOException e) {
System.out.println("Fehler beim Erzeugen");
}
openFilesInList(curDir);
}
private void onNeuOrdner(java.awt.event.ActionEvent evt) {
MyFile newDir = new MyFile(curDir.getPath(), JOptionPane.showInputDialog(this, "Name eingeben"));
newDir.mkdir();
((DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent()).add(new DefaultMutableTreeNode(newDir));
openFilesInList(curDir);
}
private void onDateiUmbennen(java.awt.event.ActionEvent evt) {
int sel = tVerzeichnis.getSelectedRow();
MyFile tmp = (MyFile)dtm.getValueAt(sel,0);
tmp.renameTo(new MyFile(tmp.getParent(),JOptionPane.showInputDialog(this, "Neuen Namen eingeben:")));
openFilesInList(tmp.getParentFileInEx());
}
private void onDateiLoeschen(java.awt.event.ActionEvent evt) {
if(JOptionPane.showConfirmDialog(this, "Wirklich löschen", "Abfrage", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
int sel = tVerzeichnis.getSelectedRow();
MyFile tmp = (MyFile)dtm.getValueAt(sel,0);
tmp.delete();
openFilesInList(tmp.getParentFileInEx());
}
}
private void onExpanded(javax.swing.event.TreeExpansionEvent evt) {
try {
DefaultMutableTreeNode help = (DefaultMutableTreeNode)evt.getPath().getLastPathComponent();
MyFile f = (MyFile)help.getUserObject();
curDir = f;
openFilesInList(f);
if(help.getDepth()<2) {
ebenenLaden(help,1,f);
setTitle(f.getAbsolutePath());
}
}
catch(ClassCastException e) {
System.out.println("Kein File");
}
catch(NullPointerException e) {
System.out.println("Konnte Datei nicht laden");
}
}
private void ebenenLaden(DefaultMutableTreeNode dmtn, int ebene, MyFile dir) {
if(ebene<2) {
if(dir.isDirectory()) {
MyFile[] files = dir.listFilesInEx();
DefaultMutableTreeNode tmp;
int zweiteE = ebene++;
for(int i=0; i<files.length; i++) {
if (files[i].isDirectory()) {
try {
tmp = new DefaultMutableTreeNode(files[i]);
dmtn.add(tmp);
ebenenLaden(tmp, zweiteE, files[i]);
}
catch(NullPointerException e) {
System.out.println("Konnte "+ files[i].toString() +" nicht laden");
}
}
}
}
}
}
private void openFilesInList(MyFile f) {
int i=dtm.getRowCount()-1;
while(i>=0) {
dtm.removeRow(i);
i--;
}
MyFile[] files = f.listFilesInEx();
for(i=0; i<files.length; i++) {
addTableRowData(files[i]);
}
//Redo hinzufürgen
if(redo.size() == 0 || (TreePath)redo.elementAt(redo.size()-1)!= tree.getSelectionPath()) {
redo.add(tree.getSelectionPath());
btBack.setEnabled(true);
}
}
private void onTree(javax.swing.event.TreeSelectionEvent evt) {
try {
DefaultMutableTreeNode help = (DefaultMutableTreeNode)evt.getPath().getLastPathComponent();
MyFile f = (MyFile)help.getUserObject();
curDir = f;
//Files anzeigen
openFilesInList(f);
setTitle(f.getAbsolutePath());
if(help.getDepth()<2) {
//Weitere Ebenen laden
ebenenLaden(help,1,f);
}
}
catch(ClassCastException e) {
System.out.println("Kein File");
}
catch(NullPointerException e) {
System.out.println("Konnte Datei nicht laden");
}
}
private void onOpenEditor(java.awt.event.ActionEvent evt)
{
EditorGUI ed = new EditorGUI(null);
ed.show();
}
private void onEnde(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt)
{
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
new MyExplorerGUI().show();
}
// Variables declaration - do not modify
private javax.swing.JButton btBack;
private javax.swing.JButton btDirUp;
private javax.swing.JButton btForward;
private javax.swing.JButton btWechselnZu;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JLabel lbAdresse;
private javax.swing.JMenu mDatei;
private javax.swing.JMenu mNeu;
private javax.swing.JMenu mTextEditor;
private javax.swing.JMenuItem miBeenden;
private javax.swing.JMenuItem miDateiLoeschen;
private javax.swing.JMenuItem miDateiUmbennen;
private javax.swing.JMenuItem miNeuOrdner;
private javax.swing.JMenuItem miNeuTxt;
private javax.swing.JMenuItem miOeffnenTxt;
private javax.swing.JMenuItem miTextEditor;
private javax.swing.JPanel pAdresse;
private javax.swing.JPanel pButtons;
private javax.swing.JPanel pSymbolleisten;
private javax.swing.JPopupMenu pmPopUp;
private javax.swing.JScrollPane spTree;
private javax.swing.JScrollPane spVerzeichnis;
private javax.swing.JTable tVerzeichnis;
private javax.swing.JTextField tfAdresse;
private javax.swing.JTree tree;
// End of variables declaration
}