/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package socius.telas.principal;
import socius.telas.util.MoverArquivoCompartilhar;
import socius.telas.util.ItemListaArquivos;
import socius.telas.util.ItemListaComputadores;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.rmi.RemoteException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Dialogs;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import socius.adapter.Arquivo;
import socius.telas.util.DadosGlobais;
import socius.dispositivos.ComputadorRemoto;
import socius.stream.Download;
import socius.telas.statusdownload.StatusDownloadController;
import socius.util.ConfigSistema;
/**
* Singleton-preciso acessar em servidor
*
* @author Ana
*/
public class PrincipalController implements Initializable {
private static String socius = System.getProperty("user.home")
+ System.getProperty("file.separator")
+ ConfigSistema.getProperty("cliente.pasta_publica");
private ActionEvent iniciar = new ActionEvent();
@FXML
private ListView<ItemListaArquivos> lista_arquivos;
@FXML
private ListView<ItemListaComputadores> lista_computadores;
@FXML
private ListView<ItemListaArquivos> meus_arquivos;
@FXML
private Label boasvindas;
@FXML
private Button download;
@FXML
private Button compartilhar;
@FXML
private Button remover;
@FXML
private TextField stringPesquisa;
@FXML
private Button deslogar;
@FXML
private ProgressIndicator bytesdown;
@FXML
private Label downloadStatus;
public static boolean atualizar = false;
public static boolean atualizardownload = false;
private ObservableList<ItemListaComputadores> listaComputadores = FXCollections.observableArrayList();
private ObservableList<ItemListaArquivos> listaMeusArquivos = FXCollections.observableArrayList();
private ObservableList<ItemListaArquivos> listaArquivosComputador = FXCollections.observableArrayList();
private Timeline atualizaListaWorker;
private ComputadorRemoto computadorSelecionado;
@FXML
public void download(ActionEvent e) throws RemoteException, IOException {
if (lista_arquivos.getSelectionModel().getSelectedItem() != null) {
Arquivo arq = lista_arquivos.getSelectionModel().getSelectedItem().getArquivo();
Download down = new Download(DadosGlobais.getInstance().getComputador().getServidorConectado(),
lista_computadores.getSelectionModel().getSelectedItem().getComputador(), arq, this);
//abrir pegando o controller
FXMLLoader loader = new FXMLLoader(getClass().getResource("/socius/telas/statusdownload/statusdownload.fxml"));
Parent root = (Parent) loader.load();
Stage stage = new Stage();
// Repassa download para a própria tela se atualizar
StatusDownloadController sdc = loader.getController();
sdc.setRequiredData(stage, down, this);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
// Mostra a tela
stage.show();
//inicia download
down.iniciar();
} else {
Dialogs.showInformationDialog(new Stage(), "Selecione um arquivo primeiro", "Download", "Download");
}
}
@FXML
public void tornarPrivado(ActionEvent e) throws IOException {
//peguar remover arquivo da pasta compartilhada
//falta bloquear arquivo quando alguem estiver efetuando download
if (meus_arquivos.getSelectionModel().getSelectedItem() != null) {
Files.deleteIfExists(new File(socius + System.getProperty("file.separator")
+ meus_arquivos.getSelectionModel().getSelectedItem().getArquivo().getFile().getName()).toPath());
Dialogs.showInformationDialog(new Stage(), "O arquivo de nome:"
+ "" + meus_arquivos.getSelectionModel().getSelectedItem().getArquivo().getFile().getName() + "não está mais público");
//atualizar listas
atualizaListaMeusArquivos();
} else {
Dialogs.showInformationDialog(new Stage(), "Selecione um arquivo, nenhum arquivo foi selecionado!");
}
}
@FXML
public void compartilhar(ActionEvent e) throws IOException {
//permite peguar um arquivo
FileChooser fileChooser = new FileChooser();
File source = new File(System.getProperty("user.home"));
fileChooser.setInitialDirectory(source);
fileChooser.setTitle("Compartilhar Arquivos");
File original = fileChooser.showOpenDialog(new Stage());
if (original != null) {
//fazer depois de peguar o arquivo enviar para pasta publica
File copia = new File(socius + System.getProperty("file.separator") + original.getName());
MoverArquivoCompartilhar moverthread = new MoverArquivoCompartilhar(original.toPath(), copia.toPath());
Thread t = new Thread(moverthread);
t.run();
//os dialogs tambem podem receber um novo stage
Dialogs.showInformationDialog(new Stage(), "O arquivo " + original.getName() + " foi publicado"
+ " com sucesso!");
atualizaListaMeusArquivos();
}
}
@FXML
public void deslogar(ActionEvent e) throws RemoteException, Exception {
atualizaListaWorker.stop();
//computador desconectar
//lista sao limpas em desconectar de online
DadosGlobais.getInstance().getComputador().desconectar();
//fechar tela principal
Stage stage = (Stage) deslogar.getScene().getWindow();
stage.close();
}
public void valoresIniciais() throws RemoteException {
stringPesquisa.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue ov, String t, String t1) {
try {
search(t, t1);
} catch (RemoteException ex) {
Logger.getLogger(PrincipalController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
atualizaListaMeusArquivos();
lista_computadores.setItems(listaComputadores);
meus_arquivos.setItems(listaMeusArquivos);
lista_arquivos.setItems(listaArquivosComputador);
}
public void atualizaListaMeusArquivos() {
try {
listaMeusArquivos.clear();
for (Arquivo a : DadosGlobais.getInstance().getComputador().getArquivos()) {
listaMeusArquivos.add(new ItemListaArquivos(a));
}
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
public void atualizaListaComputadorSelecionado(ComputadorRemoto c) {
try {
computadorSelecionado = c;
listaArquivosComputador.clear();
for (Arquivo a : c.getArquivos()) {
listaArquivosComputador.add(new ItemListaArquivos(a));
}
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
public void search(String oldtext, String newText) throws RemoteException {
lista_arquivos.setItems(listaArquivosComputador);
if (!newText.equals("")) {
ObservableList<ItemListaArquivos> searchResult = FXCollections.observableArrayList();
for (ItemListaArquivos entrada : lista_arquivos.getItems()) {
String entrText = entrada.getArquivo().getFile().getName();
if (entrText.toUpperCase().contains(newText.toUpperCase())) {
searchResult.add(entrada);
}
}
lista_arquivos.setItems(searchResult);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
boasvindas.setText(DadosGlobais.getInstance().getComputador().getUsuario().getNome());
valoresIniciais();
} catch (RemoteException ex) {
Logger.getLogger(PrincipalController.class.getName()).log(Level.SEVERE, null, ex);
}
lista_computadores.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
if (lista_computadores.getSelectionModel().getSelectedItem() != null) {
atualizaListaComputadorSelecionado(lista_computadores.getSelectionModel().getSelectedItem().getComputador());
}
}
});
atualizaListaWorker = new Timeline(new KeyFrame(
Duration.seconds(1), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
if (atualizar) {
atualizar = false;
boolean manterListaComputadorAtual = false;
listaComputadores.clear();
for (ComputadorRemoto c : DadosGlobais.getInstance().getComputador().getServidorConectado().getClientes()) {
if (c.getUsuario().getId() != DadosGlobais.getInstance().getComputador().getUsuario().getId()) {
listaComputadores.add(new ItemListaComputadores(c));
if (computadorSelecionado != null) {
if (computadorSelecionado.equals(c)) {
manterListaComputadorAtual = true;
}
}
}
}
if (!manterListaComputadorAtual) {
listaArquivosComputador.clear();
}
}
} catch (RemoteException ex) {
Logger.getLogger(PrincipalController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}));
atualizaListaWorker.setCycleCount(Timeline.INDEFINITE);
atualizaListaWorker.play();
}
}