package JabberClient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
public class Progress extends JFrame implements ActionListener {
static ProgressMonitor progressmonitor;
static OutgoingFileTransfer outgoingfiletransfer;
static IncomingFileTransfer incomingfiletransfer;
static ChatWindow chatwindow;
static MainWindow mainwindow;
static int intMax;
boolean outgoing;
ProgressMonitor pm;
Timer timer ;
public Progress(int intMax, ChatWindow chatwindow, OutgoingFileTransfer outgoingfiletransfer) {
System.out.println("Progess KONSTRUKTOR OutgoingFileTransfer!!!!" );
outgoing = true;
this.intMax = intMax;
this.chatwindow = chatwindow;
this.outgoingfiletransfer = outgoingfiletransfer;
UIManager.put("ProgressMonitor.progressText", "Datei-Senden");
UIManager.put("OptionPane.cancelButtonText", "Abbruch senden");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
pm = new ProgressMonitor(chatwindow, "","", 0, intMax);
timer = new Timer(500, this);
timer.start();
}
public Progress(int intMax, IncomingFileTransfer incomingfiletransfer) {
System.out.println("Progess KONSTRUKTOR IncomingFileTransfer!!!!" );
outgoing = false;
this.intMax = intMax;
this.incomingfiletransfer = incomingfiletransfer;
UIManager.put("ProgressMonitor.progressText", "Datei-Empfangen");
UIManager.put("OptionPane.cancelButtonText", " Abbruch");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
pm = new ProgressMonitor(mainwindow, "","", 0, intMax);
timer = new Timer(500, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
if(outgoing){
Thread thread = new Thread (new Runnable() {
public void run() {
try {
if(!outgoingfiletransfer.isDone()){
if (pm.isCanceled()) {
System.out.println("ProgessMonitor canceled" );
pm.setNote("Der Transfer wurde abgebrochen!");
timer.stop();
outgoingfiletransfer.cancel();
pm.close();
}
else{
pm.setProgress((int) outgoingfiletransfer.getAmountWritten());
pm.setNote("Der Transfer ist zu " + NumberFormat.getPercentInstance().format(outgoingfiletransfer.getProgress()) + " abgeschlossen.");
}
}
else{
pm.setNote("Transfer erfolgreich beendet!");
pm.close();
}
}
catch (Exception ex) {}
}
});
thread.start ();
}
else{
Thread thread = new Thread (new Runnable() {
public void run() {
try {
if(!incomingfiletransfer.isDone()){
if (pm.isCanceled()) {
System.out.println("Incoming ProgessMonitor canceled" );
pm.setNote("Der Transfer wurde abgebrochen!");
timer.stop();
incomingfiletransfer.cancel();
pm.close();
}
else{
pm.setProgress((int) incomingfiletransfer.getAmountWritten());
pm.setNote("Der Transfer ist zu " + NumberFormat.getPercentInstance().format(incomingfiletransfer.getProgress()) + " abgeschlossen.");
}}
else{
pm.setNote("Transfer erfolgreich beendet!");
pm.close();
}
}
catch (Exception ex) {}
}
});
thread.start ();
}
}
}