package gui;
import java.io.File;
import javax.swing.JFileChooser;
import sync.*;
public class MainGUI extends javax.swing.JFrame {
/** Creates new form MainGUI */
public MainGUI(){
initComponents();
}
private void btnSyncActionPerformed(java.awt.event.ActionEvent evt) {
if (flSource != null && flTarget != null) {
Syncmaster sm = new Syncmaster(flSource, flTarget,[B]pbFull[/B]);
[B] pbRun.setIndeterminate(true);[/B]
sm.start();
[B] pbRun.setIndeterminate(false);[/B]
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainGUI().setVisible(true);
}
});
}
package sync;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JProgressBar;
/**
*
* @author Conan
*/
public class Syncmaster {
private File dirSource = null;
private File dirTarget = null;
private String dirSourceString = null;
private String dirTargetString = null;
private FileContainer target = null;
private FileContainer source = null;
private ArrayList<File> targetlist = null;
private ArrayList<File> sourcelist = null;
private JProgressBar pbFull = null;
public Syncmaster(File flSource, File flTarget, JProgressBar pbFull) {
this.dirSource = flSource;
this.source = new FileContainer(flSource);
if ((flSource.getPath() + "\\").contains("\\\\")) {
dirSourceString = (flSource.getPath() + "\\").replace("\\\\", "");
} else {
dirSourceString = flSource.getPath();
}
this.dirTarget = flTarget;
this.target = new FileContainer(flTarget);
if ((flTarget.getPath() + "\\").contains("\\\\")) {
dirTargetString = (flTarget.getPath() + "\\").replace("\\\\", "");
} else {
dirTargetString = flTarget.getPath();
}
this.pbFull = pbFull;
}
public void start() {
source.analys();
sourcelist = source.getList();
show(sourcelist);
target.analys();
targetlist = target.getList();
// show(targetlist);
// compare();
System.out.print("\n!!!!!!!!!!!!!!!!!!!!!!Backward!!!!!!!!!!!!!!!!!!!!!\n");
String temp = dirSourceString;
sourcelist = target.getList();
dirSourceString = dirTargetString;
targetlist = source.getList();
dirTargetString = temp;
// compare();
System.out.println("\nFinish!!!");
}
public void compare() {
System.out.println(dirSourceString+"\\");
System.out.println(dirTargetString+"\\");
for (int i = 0; i < sourcelist.size(); i++) {
File a = sourcelist.get(i);
[B] pbFull.setValue(calcProcessBar(i));[/B]
if (a.isDirectory()) {
System.out.print("mkdir: " + a.getPath() + "\\");
new File(a.getPath().replace(dirSourceString, dirTargetString)).mkdirs();
System.out.print(" end\n");
// System.out.println("!!!!!!!!!!!!!!!!!!"+a.getPath());
// System.out.println("!!!!!!!!!!!!!!!!!!"+a.getPath().replace(flSource.getPath(), flTarget.getPath()));
} else {
if (targetlist.isEmpty()) {
File temp = new File(a.getPath().replace(dirSourceString, dirTargetString).replace(a.getName(), ""));
System.out.print("mkdir: " + temp.getPath() + "\\");
temp.mkdirs();
System.out.print(" end\n");
copy(a, new File(a.getPath().replace(dirSourceString, dirTargetString)));
} else {
for (File b : targetlist) {
// System.out.println("!!!!!!!!!!!!!!!!!!OK");
if (a.getPath().replace(dirSourceString, "").equals(b.getPath().replace(dirTargetString, ""))) {
// System.out.println("!!!!!!!!!!!!!!!!!!OK2");
checkDate(a, b);
break;
} else {
if (b.equals(targetlist.get(targetlist.size() - 1))) {
// System.out.println("!!!!!!!!!!!!!!!!!!OK3");
new File(a.getPath().replace(dirSourceString, dirTargetString).replace(a.getName(), "")).mkdirs();
copy(a, new File(a.getPath().replace(dirSourceString, dirTargetString)));
break;
}
}
}
}
}
}
}
private void checkDate(File a, File b) {
if (a.lastModified() > b.lastModified()) {
copy(a, b);
}
}
private void copy(File source, File target) {
System.out.print("copy: " + source.getPath());
FileInputStream read = null;
FileOutputStream write = null;
try {
read = new FileInputStream(source);
write = new FileOutputStream(target);
byte[] buffer = new byte[4096];
int length;
while ((length = read.read(buffer)) > 0) {
System.out.print(".");
write.write(buffer, 0, length);
}
System.out.print(" end\n");
} catch (IOException ex) {
Logger.getLogger(Syncmaster.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
read.close();
write.close();
} catch (IOException ex) {
Logger.getLogger(Syncmaster.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
[B] private int calcProcessBar(int a) {
double pm = 1000. / (double) targetlist.size() * (double) a;
if (pm * 10 % 10 > 5) {
pm = pm + 0.5;
}
return (int) pm;
}[/B]
private void show(ArrayList<File> filelist) {
System.out.println(dirSource);
System.out.println(dirTarget);
for (File file : filelist) {
if (file.isDirectory()) {
System.out.println(file.getPath() + "\\");
} else {
System.out.println(file.getPath());
}
}
System.out.println("Nächster Ordner!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
Thread t = new Thread(new Runnable() {
public void run() {
Syncmaster sm = new Syncmaster(flSource, flTarget, pbFull);
pbRun.setIndeterminate(true);
sm.start();
pbRun.setIndeterminate(false);
}
});
t.start();
Thread t = new Thread(new Runnable() {
public void run() {
Syncmaster sm = new Syncmaster(flSource, flTarget, pbFull);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
pbRun.setIndeterminate(true);
}
});
sm.start();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
pbRun.setIndeterminate(false);
}
});
}
});
t.start();
package gui;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import sync.*;
/**
*
* @author barracden
*/
public class MainGUI extends javax.swing.JFrame {
private File flSource = null;
private File flTarget = null;
private Syncmaster sm = null;
private About About = null;
private int[] option={0};
/** Creates new form MainGUI */
public MainGUI() {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
initComponents();
}
private void btnSourceActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser search = new JFileChooser();
search.setDialogType(JFileChooser.OPEN_DIALOG);
search.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = search.showDialog(this, "Open");
if (option == JFileChooser.APPROVE_OPTION) {
flSource = search.getSelectedFile();
txtSource.setText((flSource.getPath() + "\\").replace("\\\\", "\\"));
}
}
private void btnTargetActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser search = new JFileChooser();
search.setDialogType(JFileChooser.OPEN_DIALOG);
search.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = search.showDialog(this,"Öffnen");
if (option == JFileChooser.APPROVE_OPTION) {
flTarget = search.getSelectedFile();
txtTarget.setText((flTarget.getPath() + "\\").replace("\\\\", "\\"));
}
}
private void btnScActionPerformed(java.awt.event.ActionEvent evt) {
if (btnSc.getText().equals("Sync")) {
if (flSource != null && flTarget != null) {
sm = new Syncmaster(flSource, flTarget, pbFull, pbRun,btnSc,option);
btnSc.setText("Cancel");
pbRun.setIndeterminate(true);
sm.start();
}
}else{
sm.stop();
btnSc.setText("Sync");
pbRun.setIndeterminate(false);
}
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
About=new About(this,true);
About.setVisible(true);
}
private void rbmQZZQActionPerformed(java.awt.event.ActionEvent evt) {
option[0]=0;
}
private void rbmQZActionPerformed(java.awt.event.ActionEvent evt) {
option[0]=1;
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnSc;
private javax.swing.JButton btnSource;
private javax.swing.JButton btnTarget;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar pbFull;
private javax.swing.JProgressBar pbRun;
private javax.swing.ButtonGroup rbgRichtung;
private javax.swing.JRadioButtonMenuItem rbmQZ;
private javax.swing.JRadioButtonMenuItem rbmQZZQ;
private javax.swing.JTextField txtSource;
private javax.swing.JTextField txtTarget;
// End of variables declaration
}
package sync;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JProgressBar;
/**
*
* @author Conan
*/
public class Syncmaster extends Thread {
private File dirSource = null;
private File dirTarget = null;
private String dirSourceString = null;
private String dirTargetString = null;
private FileContainer target = null;
private FileContainer source = null;
private ArrayList<File> targetlist = null;
private ArrayList<File> sourcelist = null;
private JProgressBar pbFull = null;
private JProgressBar pbRun = null;
private JButton sc;
private int[] option;
public Syncmaster(File flSource, File flTarget, JProgressBar pbFull, JProgressBar pbAction, JButton sc, int[] option) {
this.dirSource = flSource;
this.source = new FileContainer(flSource);
if ((flSource.getPath() + "\\").contains("\\\\")) {
dirSourceString = (flSource.getPath() + "\\").replace("\\\\", "");
} else {
dirSourceString = flSource.getPath();
}
this.dirTarget = flTarget;
this.target = new FileContainer(flTarget);
if ((flTarget.getPath() + "\\").contains("\\\\")) {
dirTargetString = (flTarget.getPath() + "\\").replace("\\\\", "");
} else {
dirTargetString = flTarget.getPath();
}
this.pbFull = pbFull;
this.pbRun = pbAction;
this.sc = sc;
this.option = option;
}
@Override
public void run() {
source.analys();
sourcelist = source.getList();
// show(sourcelist);
target.analys();
targetlist = target.getList();
// show(targetlist);
compare();
pbFull.setValue(1000);
if (option[0] == 0) {
System.out.print("\n!!!!!!!!!!!!!!!!!!!!!!Backward!!!!!!!!!!!!!!!!!!!!!\n");
String temp = dirSourceString;
sourcelist = target.getList();
dirSourceString = dirTargetString;
targetlist = source.getList();
dirTargetString = temp;
compare();
pbFull.setValue(1000);
}
System.out.println("\nFinish!!!");
pbRun.setIndeterminate(false);
sc.setText("Sync");
}
public void compare() {
System.out.println(dirSourceString + "\\");
System.out.println(dirTargetString + "\\");
for (int i = 0; i < sourcelist.size(); i++) {
File a = sourcelist.get(i);
pbFull.setValue(calcProcessBar(i));
if (a.isDirectory()) {
System.out.print("mkdir: " + a.getPath() + "\\");
new File(a.getPath().replace(dirSourceString, dirTargetString)).mkdirs();
System.out.print(" end\n");
} else {
if (targetlist.isEmpty()) {
File temp = new File(removeLast(a.getPath().replace(dirSourceString, dirTargetString), a.getName()));
System.out.print("mkdir: " + temp.getPath() + "\\");
temp.mkdirs();
System.out.print(" end\n");
copy(a, new File(a.getPath().replace(dirSourceString, dirTargetString)));
} else {
for (File b : targetlist) {
if (a.getPath().replace(dirSourceString, "").equals(b.getPath().replace(dirTargetString, ""))) {
checkDate(a, b);
break;
} else {
if (b.equals(targetlist.get(targetlist.size() - 1))) {
new File(removeLast(a.getPath().replace(dirSourceString, dirTargetString), a.getName())).mkdirs();
copy(a, new File(a.getPath().replace(dirSourceString, dirTargetString)));
break;
}
}
}
}
}
}
}
private void checkDate(File a, File b) {
if (a.lastModified() > b.lastModified()) {
copy(a, b);
}
}
private void copy(File source, File target) {
System.out.print("copy: " + source.getPath());
FileInputStream read = null;
FileOutputStream write = null;
try {
read = new FileInputStream(source);
write = new FileOutputStream(target);
byte[] buffer = new byte[4096];
int length;
while ((length = read.read(buffer)) > 0) {
System.out.print(".");
write.write(buffer, 0, length);
}
System.out.print(" end\n");
} catch (IOException ex) {
System.out.println("Abbruch duch benutzer oder Fehler!!");
} finally {
try {
read.close();
write.close();
} catch (IOException ex) {
Logger.getLogger(Syncmaster.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private int calcProcessBar(int a) {
double pm = 1000. / sourcelist.size();
pm = pm * (double) a;
if (pm * 10 % 10 > 5) {
pm = pm + 0.5;
}
return (int) pm;
}
private void show(ArrayList<File> filelist) {
System.out.println(dirSource);
System.out.println(dirTarget);
for (File file : filelist) {
if (file.isDirectory()) {
System.out.println(file.getPath() + "\\");
} else {
System.out.println(file.getPath());
}
}
System.out.println("Nächster Ordner!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
public static String removeLast(String s, String n) {
String output = "";
char[] vs = s.toCharArray();
for (int i = 0; i < s.length() - n.length(); i++) {
output = output + vs[i];
}
return output;
}
}
pbRun.setIndeterminate(true);
new Runnable() {
@Override
public void run() {
pbRun.setIndeterminate(true);
}
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
pbRun.setIndeterminate(true);
}
});
Da hat Dein Lehrer recht.Mein Lehrer hat auch gesagt das es auch nicht gut sei alle swing Komponente im Konstruktor zu übergeben. Aber wie kann ich sonst die Werte ämdern, da ich von einen non-static Klasse nicht auf eine Static Klasse zugreifen darf/kann?
import java.util.EventObject;
public class ProgressEvent extends EventObject {
private final int minValue;
private final int maxValue;
private final int currentValue;
private final boolean finished;
private final boolean canceled;
public ProgressEvent(Object source,
int minValue,
int maxValue,
int currentValue,
boolean finished,
boolean canceled) {
this.minValue = minValue;
this.maxValue = maxValue;
this.currentValue = currentValue;
this.finished = finished;
this.canceled = canceled;
}
public int getMinValue() { return minValue; }
public int getMaxValue() { return maxValue; }
public int getCurrentValue() { return currentValue; }
public boolean isFinished() { return finished; }
public boolean isCanceled() { return canceled; }
// TODO: Implement toString()
}
import java.util.EventListener;
public interface ProgressListener extends EventListener {
void progressChanged(ProgressEvent e);
}
import java.util.List;
...
private final List<ProgressListener> listeners = new LinkedList<ProgressListener>();
public void addProgressListener(ProgressListener l) {
listeners.add(l);
}
public void removeProgressListener(ProgressListener l) {
listeners.remove(l);
}
/* protected methods for sending events to our listeners */
protected void fireProgressChanged(int minValue, int maxValue, int currentValue) {
ProgressEvent event = null; // create only once if listeners available
for (ProgressListener l : listeners) {
if (event == null) {
event = new ProgressEvent(this, minValue, maxValue, currentValue, false, false);
}
l.progressChanged(event);
}
}
protected void fireProgressCanceled(int minValue, int maxValue, int currentValue) {
ProgressEvent event = null; // create only once if listeners available
for (ProgressListener l : listeners) {
if (event == null) {
event = new ProgressEvent(this, minValue, maxValue, currentValue, false, true);
}
l.progressChanged(event);
}
}
protected void fireProgressFinished(int minValue, int maxValue) {
ProgressEvent event = null; // create only once if listeners available
for (ProgressListener l : listeners) {
if (event == null) {
event = new ProgressEvent(this, minValue, maxValue, maxValue, true, false);
}
l.progressChanged(event);
}
}