/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package socius.telas.statusdownload;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Dialogs;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
import socius.stream.Download;
import socius.telas.principal.PrincipalController;
/**
* FXML Controller class
*
* @author Ana
*/
public class StatusDownloadController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML
private ProgressIndicator indicador;
@FXML
private Label lblProgresso;
@FXML
private Label lblNomeArquivo;
private Timeline atualizaProgress;
private Download download;
private Stage stage;
private PrincipalController pcl;
@Override
public void initialize(URL url, ResourceBundle rb) {
atualizaProgress = new Timeline(new KeyFrame(
Duration.seconds(0.5), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
float transferidos = download.getBytesTransferidos();
float total = download.getBystesTotais();
float porcentagem = transferidos / total;
StringBuilder progresso = new StringBuilder();
progresso.append(String.format("%.2f", transferidos / 1024 / 1024, transferidos));
progresso.append(" MB de ");
progresso.append(String.format("%.2f", total / 1024 / 1024, transferidos));
progresso.append(" MB transferidos.");
lblProgresso.setText(progresso.toString());
lblNomeArquivo.setText(download.getNomeArquivo());
indicador.setProgress(porcentagem);
if(porcentagem >= 1 || transferidos >= total) {
// Retira confirmação para fechar tela
stage.setOnCloseRequest(null);
// Interrompe atualização da tela
atualizaProgress.stop();
pcl.atualizaListaMeusArquivos();
}
} catch (InterruptedException ex) {
Logger.getLogger(StatusDownloadController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}));
atualizaProgress.setCycleCount(Timeline.INDEFINITE);
atualizaProgress.play();
}
public void setRequiredData(Stage stg, Download dw, PrincipalController pctl) {
this.stage = stg;
this.download = dw;
this.pcl = pctl;
// Confirmação para fechar tela
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
Dialogs.DialogResponse res = Dialogs.showConfirmDialog(
stage, "Tem certeza de que deseja cancelar o download?",
"Download", "Download");
if (res.equals(Dialogs.DialogResponse.YES)) {
download.interromper();
pcl.atualizaListaMeusArquivos();
stage.close();
} else {
// Cancela fechamento da janela
t.consume();
}
}
});
}
}