Advertisement
Guest User

Sample table

a guest
Jul 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 KB | None | 0 0
  1. package userinterface.scenes.receptionist;
  2.  
  3. import core.model.Gender;
  4. import core.model.Patient;
  5. import core.service.PatientService;
  6. import javafx.beans.binding.Bindings;
  7. import javafx.beans.property.ReadOnlyDoubleProperty;
  8. import javafx.beans.property.SimpleIntegerProperty;
  9. import javafx.beans.property.SimpleStringProperty;
  10. import javafx.beans.value.ObservableValue;
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.event.ActionEvent;
  14. import javafx.fxml.FXML;
  15. import javafx.fxml.Initializable;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.TableColumn;
  18. import javafx.scene.control.TableView;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.control.cell.PropertyValueFactory;
  21. import javafx.scene.input.KeyEvent;
  22. import javafx.scene.layout.Pane;
  23. import javafx.util.Callback;
  24. import userinterface.URLSource;
  25. import userinterface.customviewelement.ButtonTableCell;
  26. import userinterface.navigation.UserNavigation;
  27. import userinterface.scenes.BaseController;
  28.  
  29. import java.net.URL;
  30. import java.util.*;
  31. import java.util.concurrent.Callable;
  32. import java.util.function.Function;
  33.  
  34. public class PatientsListController extends BaseController implements Initializable {
  35.  
  36.     @FXML Pane defaultContainer;
  37.  
  38.     @FXML TableView<Patient> tbPatients;
  39.  
  40.     @FXML TableColumn<Patient, Integer> idColumn;
  41.     @FXML TableColumn<Patient, String> nameColumn;
  42.     @FXML TableColumn<Patient, Integer> ageColumn;
  43.     @FXML TableColumn<Patient, Gender> genderColumn;
  44.     @FXML TableColumn<Patient, String> contactNumberColumn;
  45.     @FXML TableColumn<Patient, Button> viewColumn;
  46.  
  47.     @FXML TextField searchField;
  48.  
  49.     PatientService patientService = new PatientService();
  50.  
  51.     ObservableList<Patient> tableSource;
  52.  
  53.     AbstractList<Patient> patients;
  54.  
  55.     @Override public void initialize(URL location, ResourceBundle resources) {
  56.         ReadOnlyDoubleProperty property = getMainView().getDisplayerWidthProperty();
  57.         defaultContainer.prefWidthProperty().bind(property);
  58.         initializeViewComponents();
  59.     }
  60.  
  61.     private void initializeViewComponents() {
  62.         initializeTableBindings();
  63.     }
  64.  
  65.     private void initializeTableBindings() {
  66.  
  67.         idColumn.setCellValueFactory(new PropertyValueFactory<Patient, Integer>("id"));
  68.  
  69.         nameColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Patient, String>, ObservableValue<String>>() {
  70.             @Override
  71.             public ObservableValue<String> call(TableColumn.CellDataFeatures<Patient, String > param) {
  72.                 SimpleStringProperty prop = new SimpleStringProperty(param.getValue().getFullname());
  73.                 prop.bind(Bindings.createStringBinding(new Callable<String>() {
  74.                     @Override
  75.                     public String call() throws Exception {
  76.                         return param.getValue().getFullname();
  77.                     }
  78.                 }));
  79.                 return prop;
  80.             }
  81.         });
  82.         ageColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Patient, Integer>, ObservableValue<Integer>>() {
  83.             @Override
  84.             public ObservableValue<Integer> call(TableColumn.CellDataFeatures<Patient, Integer> param) {
  85.                 SimpleIntegerProperty prop = new SimpleIntegerProperty(param.getValue().getAge());
  86.                 prop.bind(Bindings.createIntegerBinding(new Callable<Integer>() {
  87.                     @Override
  88.                     public Integer call() throws Exception {
  89.                         return param.getValue().getAge();
  90.                     }
  91.                 }));
  92.                 return prop.asObject();
  93.             }
  94.         });
  95.         genderColumn.setCellValueFactory(new PropertyValueFactory<Patient, Gender>("gender"));
  96.         contactNumberColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("contactNumber"));
  97.  
  98.         viewColumn.setCellFactory(ButtonTableCell.<Patient>forTableColumn("View", new Function<Patient, Patient>() {
  99.             @Override
  100.             public Patient apply(Patient patient) {
  101.                 Map<String, Object> passedParammeters = new Hashtable<>();
  102.                 passedParammeters.put(PatientDetailsController.PATIENT_PARAMETER_NAME, patient);
  103.                 URL url = URLSource.getURL("scenes/receptionist/patientdetails.fxml");
  104.                 getMainView().getUserNavigation().navigate(new UserNavigation.NavigationItem(url, false, passedParammeters));
  105.                 return patient;
  106.             }
  107.         }));
  108.  
  109.         tbPatients.setItems(tableSource = FXCollections.observableArrayList(patients = patientService.getAllPatients()));
  110.     }
  111.  
  112.     @FXML private void textChange(KeyEvent event){
  113.         String query = searchField.getText();
  114.  
  115.         if(query.length() == 0){
  116.             tableSource.setAll(patients);
  117.             return;
  118.         }
  119.  
  120.         List<Patient> temp = new ArrayList<>();
  121.         for (Patient patient : patients) {
  122.             if(patient.getFullname().toUpperCase().contains(query.toUpperCase())){
  123.                 temp.add(patient);
  124.             }
  125.         }
  126.         tableSource.setAll(temp);
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement