package pack;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
public class Suche extends JFrame{
private static final long serialVersionUID = 1L;
Pan p = new Pan();
Suche() {
super();
setTitle("Suche");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initLaF();
setLayout(new BorderLayout());
getContentPane().add(p, BorderLayout.CENTER);
}
public class Pan extends JPanel {
private static final long serialVersionUID = 1L;
JLabel lblDir = new JLabel("Verzeichnis:");
JTextField txtDir = new JTextField();
JButton btnDir = new JButton("...");
JLabel lblSuch = new JLabel("Suche:");
JTextField txtSuch = new JTextField();
JLabel lblRep = new JLabel("Ersetze:");
JTextField txtRep = new JTextField();
JButton btnGo = new JButton("go !");
JTextArea taHtml = new JTextArea(10,40);
JScrollPane scroll = new JScrollPane();
Pan() {
super();
setLayout(null);
lblDir.setBounds(10,10,60,20);
add(lblDir);
txtDir.setBounds(80,10,560,20);
add(txtDir);
btnDir.setBounds(650,10,30,20);
add(btnDir);
btnDir.addActionListener(new GetDir());
lblSuch.setBounds(10,50,60,20);
add(lblSuch);
txtSuch.setBounds(80,50,560,20);
add(txtSuch);
lblRep.setBounds(10,80,60,20);
add(lblRep);
txtRep.setBounds(80,80,560,20);
add(txtRep);
btnGo.setBounds(20,110,100,30);
add(btnGo);
btnGo.addActionListener(new ButGo());
scroll.setBounds(10,160,670,300);
scroll.setViewportView(taHtml);
add(scroll);
}
}
class GetDir implements ActionListener{
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = fc.showOpenDialog(p);
if(option == JFileChooser.APPROVE_OPTION) {
String pfad = fc.getSelectedFile().getAbsolutePath();
p.txtDir.setText(pfad);
}
}
}
class ButGo implements ActionListener {
public void actionPerformed(ActionEvent ae) {
String x = p.txtDir.getText();
Pattern pat = Pattern.compile("\\\\");
Matcher mat = pat.matcher(x);
if(mat.find() == true) {
String resPfad = mat.replaceAll("\\\\\\\\");
File f = new File(resPfad);
listen(f);
System.out.println("###############################\nFERTIG!\n################################");
}
}
}
class DateiFilter implements FileFilter {
private String ende1, ende2, ende3, ende4;
public DateiFilter(String endung1, String endung2, String endung3, String endung4) {
ende1 = endung1;
ende2 = endung2;
ende3 = endung3;
ende4 = endung4;
}
public boolean accept(File dat) {
if(dat.isDirectory() == true) {
return true;
}
else {
if(dat.getName().endsWith(ende1) == true ||
dat.getName().endsWith(ende2) == true ||
dat.getName().endsWith(ende3) == true ||
dat.getName().endsWith(ende4) == true) {
return true;
}
else {
return false;
}
}
}
}
int cnt = 0;
int cntG = 0;
void listen(File dir) {
DateiFilter filter = new DateiFilter(".html",".htm",".HTML", ".HTM");
File[] dateien = dir.listFiles(filter);
if(dateien != null) {
for(int i=0; i<dateien.length; i++) {
if(dateien[i].isDirectory()) {
listen(dateien[i]);
}
if(dateien[i].isFile() && dateien[i].length() < 1000000) {
cntG++;
try {
BufferedReader rein = new BufferedReader(new FileReader(dateien[i]));
StringBuffer puffer = new StringBuffer();
String zeile;
while((zeile = rein.readLine()) != null) {
puffer.append(zeile);
puffer.append('\n');
}
rein.close();
String inhalt = new String(puffer);
p.taHtml.setText("");
p.taHtml.setText(inhalt);
p.taHtml.setCaretPosition(0);
p.taHtml.selectAll();
String regMus = p.txtSuch.getText();
String repText = p.txtRep.getText();
Pattern pa = Pattern.compile(regMus, Pattern.CASE_INSENSITIVE);
Matcher ma = pa.matcher(p.taHtml.getSelectedText());
if(ma.find() == true) {
cnt++;
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
System.out.println("Dateien gefunden: " + cnt + " - Dateien gesamt: " + cntG);
}
public void initLaF() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
SwingUtilities.updateComponentTreeUI(p);
}
catch(Exception c){
}
}
public static void main(String[] args) {
Suche fm = new Suche();
fm.setSize(700,500);
fm.setLocation(270,140);
fm.setVisible(true);
fm.setResizable(false);
}
}