Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.14 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package gestionclinica;
  7.  
  8. import DBAccess.ClinicDBAccess;
  9. import java.io.IOException;
  10. import java.net.URL;
  11. import java.util.ArrayList;
  12. import java.util.ResourceBundle;
  13. import javafx.beans.property.SimpleObjectProperty;
  14. import javafx.beans.value.ObservableValue;
  15. import javafx.collections.FXCollections;
  16. import javafx.collections.ObservableList;
  17. import javafx.event.ActionEvent;
  18. import javafx.fxml.FXML;
  19. import javafx.fxml.FXMLLoader;
  20. import javafx.fxml.Initializable;
  21. import javafx.scene.Scene;
  22. import javafx.scene.control.ChoiceBox;
  23. import javafx.scene.control.TableView;
  24. import javafx.scene.image.Image;
  25. import javafx.scene.layout.AnchorPane;
  26. import javafx.stage.Modality;
  27. import javafx.stage.Stage;
  28. import javafx.beans.value.ChangeListener ;
  29. import javafx.scene.control.Alert;
  30. import javafx.scene.control.Button;
  31. import javafx.scene.control.ComboBox;
  32. import javafx.scene.control.ListView;
  33. import javafx.scene.control.TableCell;
  34. import javafx.scene.control.TableColumn;
  35. import javafx.scene.image.ImageView;
  36. import javafx.scene.layout.BorderPane;
  37. import javafx.scene.text.Text;
  38. import model.Patient;
  39. import org.controlsfx.control.textfield.CustomTextField;
  40.  
  41. /**
  42. * FXML Controller class
  43. *
  44. * @author Carlos
  45. */
  46. public class FXMLPacientesMainController implements Initializable {
  47.  
  48. TableView<Patient> lPatients;
  49. Image avatar;
  50. @FXML
  51. private ComboBox tipoBusqueda;
  52.  
  53. @FXML
  54. private CustomTextField barraBusqueda;
  55.  
  56. @FXML
  57. private BorderPane borderPane;
  58. @FXML
  59. private AnchorPane scene;
  60.  
  61. private static final Image imgLupa = new Image("loupe.png");
  62. static boolean editable;
  63. @FXML
  64. private TableView<Patient> tablaPacientes;
  65. @FXML
  66. private TableColumn<Patient, Image> columnaFoto;
  67. @FXML
  68. private TableColumn<Patient, String> columnaNombre;
  69. @FXML
  70. private TableColumn<Patient, String> columnaApellidos;
  71. @FXML
  72. private TableColumn<Patient, String> columnaDNI;
  73.  
  74. ArrayList<Patient> pacientes;
  75. static Patient current;
  76. @FXML
  77. private Button delButton;
  78. @FXML
  79. private Button showButton;
  80.  
  81. ClinicDBAccess clinicDBAccess = ClinicDBAccess.getSingletonClinicDBAccess();
  82. ObservableList<Patient> patientsObservableList;
  83. @FXML
  84. private Button buscarButton;
  85. ArrayList<Patient> introducirPacientes = new ArrayList<Patient>();
  86.  
  87.  
  88. /**
  89. * Initializes the controller class.
  90. */
  91. @Override
  92. public void initialize(URL url, ResourceBundle rb) {
  93. // TODO
  94. pacientes = clinicDBAccess.getPatients();
  95. patientsObservableList = FXCollections.observableList(clinicDBAccess.getPatients());
  96. String [] options = {"DNI", "Nombre", "Apellido"};
  97.  
  98. tipoBusqueda.setItems(FXCollections.observableArrayList(options));
  99.  
  100. tipoBusqueda.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>(){
  101. public void changed(ObservableValue ov, Number value, Number new_value){
  102. barraBusqueda.setPromptText("Introduce aquí el "+ (options[new_value.intValue()]).toLowerCase());
  103. }
  104. });
  105. barraBusqueda.setLeft(new ImageView(imgLupa));
  106.  
  107. borderPane.prefHeightProperty().bind(scene.heightProperty());
  108. borderPane.prefWidthProperty().bind(scene.widthProperty());
  109.  
  110. inicializarTabla(pacientes);
  111.  
  112. delButton.disableProperty().bind(tablaPacientes.getSelectionModel().selectedIndexProperty().isEqualTo(-1));
  113. showButton.disableProperty().bind(tablaPacientes.getSelectionModel().selectedIndexProperty().isEqualTo(-1));
  114. }
  115.  
  116. @FXML
  117. private void addAct(ActionEvent event) throws IOException {
  118. editable = true;
  119.  
  120.  
  121. FXMLLoader miCargador = new FXMLLoader(getClass().getResource("/Vista/FXMLPacientesAdd.fxml"));
  122. AnchorPane root = (AnchorPane) miCargador.load();
  123.  
  124.  
  125.  
  126. Scene scene = new Scene(root);
  127. Stage stage = new Stage();
  128. stage.setScene(scene);
  129. stage.setTitle("Añadir paciente");
  130. stage.initModality(Modality.APPLICATION_MODAL);
  131. stage.showAndWait();
  132. if(FXMLPacientesAddController.guardadoP == true){
  133. tablaPacientes.setItems(patientsObservableList);
  134. }
  135.  
  136. }
  137.  
  138. @FXML
  139. private void delAct(ActionEvent event) {
  140. current = tablaPacientes.getSelectionModel().getSelectedItem();
  141. if(ClinicDBAccess.getSingletonClinicDBAccess().hasAppointments(current)){
  142. Alert alertAmazon = new Alert(Alert.AlertType.INFORMATION);
  143. alertAmazon.setTitle("Error");
  144. alertAmazon.setHeaderText("No se puede borrar este paciente");
  145. alertAmazon.setContentText("Por favor, compruebe que el paciente no tiene ninguna cita concertada");
  146. alertAmazon.showAndWait();
  147. } else {
  148. patientsObservableList.remove(current);
  149. tablaPacientes.setItems(patientsObservableList);
  150.  
  151. }
  152. }
  153.  
  154. @FXML
  155. private void showAct(ActionEvent event) throws IOException {
  156. editable = false;
  157. current = tablaPacientes.getSelectionModel().getSelectedItem();
  158. FXMLLoader miCargador = new FXMLLoader(getClass().getResource("/Vista/FXMLPacientesAdd.fxml"));
  159. AnchorPane root = (AnchorPane) miCargador.load();
  160.  
  161. Scene scene = new Scene(root);
  162. Stage stage = new Stage();
  163. stage.setScene(scene);
  164. stage.setTitle("Mostrar detalles paciente");
  165. stage.initModality(Modality.APPLICATION_MODAL);
  166. stage.showAndWait();
  167.  
  168. }
  169.  
  170. private void inicializarTabla(ArrayList<Patient> pacientes1){
  171.  
  172. tablaPacientes.setItems(FXCollections.observableList(pacientes1));
  173. columnaFoto.setCellValueFactory(celda1 -> new SimpleObjectProperty<Image>(celda1.getValue().getPhoto()));
  174. columnaFoto.setCellFactory(columna -> {return new TableCell<Patient, Image>(){
  175. private ImageView view = new ImageView();
  176. @Override
  177. protected void updateItem(Image item, boolean empty){
  178. if(item == null || empty) setGraphic(null);
  179. else{
  180. view.setImage(item);
  181. view.setFitHeight(60);
  182. view.setFitWidth(60);
  183. view.setPreserveRatio(true);
  184. setGraphic(view);
  185. }
  186. }
  187. };
  188. });
  189. columnaNombre.setCellValueFactory((celda2 -> new SimpleObjectProperty<String>(celda2.getValue().getName())));
  190. columnaNombre.setCellFactory(columna -> {return new TableCell<Patient, String>(){
  191. private Text view = new Text();
  192. @Override
  193. protected void updateItem(String item, boolean empty){
  194. if(item == null || empty){
  195. setGraphic(null);
  196. }else{
  197. view.setText(item);
  198. setGraphic(view);
  199. }
  200. }
  201.  
  202. };
  203. });
  204. columnaApellidos.setCellValueFactory((celda3 -> new SimpleObjectProperty<String>(celda3.getValue().getSurname())));
  205. columnaApellidos.setCellFactory(columna -> {return new TableCell<Patient, String>(){
  206. private Text view = new Text();
  207. @Override
  208. protected void updateItem(String item, boolean empty){
  209. if(item == null || empty){
  210. setGraphic(null);
  211. }else{
  212. view.setText(item);
  213. setGraphic(view);
  214. }
  215. }
  216.  
  217. };
  218. });
  219. columnaDNI.setCellValueFactory((celda4 -> new SimpleObjectProperty<String>(celda4.getValue().getIdentifier())));
  220. columnaDNI.setCellFactory(columna -> {return new TableCell<Patient, String>(){
  221. private Text view = new Text();
  222. @Override
  223. protected void updateItem(String item, boolean empty){
  224. if(item == null || empty){
  225. setGraphic(null);
  226. }else{
  227. view.setText(item);
  228. setGraphic(view);
  229. }
  230. }
  231.  
  232. };
  233. });
  234. }
  235.  
  236. @FXML
  237. private void buscarAct(ActionEvent event) {
  238. String bus = barraBusqueda.getText().toLowerCase();
  239.  
  240. ObservableList<Patient> pat = FXCollections.observableList(introducirPacientes);
  241.  
  242. switch(tipoBusqueda.getSelectionModel().getSelectedIndex()){
  243.  
  244. case 0:
  245. for (int i = 0; i < pacientes.size(); i++) {
  246. String patient = pacientes.get(i).getIdentifier().toLowerCase();
  247. if(patient.equals(bus) || patient.startsWith(bus)){
  248. introducirPacientes.add(pacientes.get(i));
  249. }
  250. }
  251. inicializarTabla(introducirPacientes);
  252. break;
  253.  
  254. case 1:
  255. for (int i = 0; i < pacientes.size(); i++) {
  256. String patient = pacientes.get(i).getName().toLowerCase();
  257. if(patient.equals(bus) || patient.startsWith(bus)){
  258. introducirPacientes.add(pacientes.get(i));
  259. }
  260. }
  261. inicializarTabla(introducirPacientes);
  262. break;
  263.  
  264. case 2:
  265. for (int i = 0; i < pacientes.size(); i++) {
  266. String patient = pacientes.get(i).getSurname().toLowerCase();
  267. if(patient.equals(bus) || patient.startsWith(bus)){
  268. introducirPacientes.add(pacientes.get(i));
  269. }
  270. }
  271. inicializarTabla(introducirPacientes);
  272. break;
  273.  
  274. }
  275. }
  276.  
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement