Advertisement
lyogadi

Controller file

Oct 5th, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.application.Platform;
  4. import javafx.fxml.FXML;
  5. import javafx.fxml.Initializable;
  6. import javafx.scene.control.ComboBox;
  7. import javafx.scene.control.ListView;
  8. import javafx.scene.control.TextField;
  9.  
  10. import java.io.*;
  11. import java.net.URL;
  12. import java.nio.charset.Charset;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.ResourceBundle;
  16.  
  17. public class Controller implements Initializable {
  18.     //private static final Charset UTF_8 = Charset.forName("UTF-8");
  19.     //private static final Charset ISO = Charset.forName("ISO-8859-1");
  20.  
  21.     private String[][] arrayCongtyDienluc = new String[0][];
  22.     private String[][] arrayDienluc = new String[0][];
  23.  
  24.     private String fileSaveList = "data.dat";
  25.  
  26.     @FXML
  27.     public ComboBox<String> Combo_congtydienluc;
  28.     public ComboBox<String> Combo_dienluc;
  29.     public TextField TF_maKH;
  30.     public TextField TF_diachicuthe;
  31.     public ListView<String> LV_saveList;
  32.  
  33.     @Override
  34.     public void initialize(URL location, ResourceBundle resources) {
  35.         try {
  36.             arrayCongtyDienluc = RequestDienluc.requestCongtyDienluc();
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         }
  40.  
  41.         for (String[] strings : arrayCongtyDienluc) {
  42.             Combo_congtydienluc.getItems().add(strings[0]);
  43.         }
  44.         Combo_congtydienluc.getSelectionModel().select(0);
  45.  
  46.         try {
  47.             getDienluc();
  48.         } catch (IOException e) {
  49.             LyoLibs.print("Không tải được thông tin cắt điện");
  50. //            e.printStackTrace();
  51.         }
  52.  
  53.         try {
  54.             LV_saveList.getItems().addAll(loadSaveList());
  55.         } catch (IOException e) {
  56.             LyoLibs.print("data.dat chưa có!");
  57. //            e.printStackTrace();
  58.         }
  59.     }
  60.  
  61.     public void getDienluc() throws IOException {
  62.         String congtydienluc = Combo_congtydienluc.getValue();
  63.         System.out.println(congtydienluc);
  64.         for (String[] array1 : arrayCongtyDienluc) {
  65.             if (array1[0] == congtydienluc) {
  66.                 arrayDienluc = RequestDienluc.requestDienluc(array1[1]);
  67.                 Combo_dienluc.getItems().clear();
  68.                 for (String[] array2 : arrayDienluc) {
  69.                     Combo_dienluc.getItems().add(array2[0]);
  70.                 }
  71.                 Combo_dienluc.getSelectionModel().select(0);
  72.                 break;
  73.             }
  74.         }
  75.     }
  76.  
  77.     public void searchButton() throws IOException {
  78.         String id = TF_maKH.getText();
  79.  
  80.         if (id.isEmpty()) {
  81.             String dienluc = Combo_dienluc.getValue().toString();
  82.             for (String[] strings : arrayDienluc) {
  83.                 if (strings[0].equals(dienluc)) {
  84.                     id = strings[1];
  85.                     break;
  86.                 }
  87.             }
  88.         }
  89.  
  90.         String[] result = RequestDienluc.request(id, TF_diachicuthe.getText());
  91.         LyoLibs.arrayPrint(result);
  92.         ArrayDisplay.display(result);
  93.     }
  94.  
  95.     public void addButton() throws IOException {
  96.         String maKH = TF_maKH.getText();
  97.         if (!maKH.isEmpty()) {
  98.             LV_saveList.getItems().add(maKH);
  99.         } else {
  100.             String dienluc = Combo_dienluc.getValue();
  101.             String madonvi = "";
  102.  
  103.             for (String[] item : arrayDienluc) {
  104.                 if (item[0].equals(dienluc)) {
  105.                     madonvi = item[1];
  106.                     break;
  107.                 }
  108.             }
  109.             String result = madonvi + "|" + TF_diachicuthe.getText() + "|" + Combo_dienluc.getValue() + "|" + Combo_congtydienluc.getValue();
  110.             LV_saveList.getItems().add(result);
  111.         }
  112.         saveSaveList();
  113.     }
  114.  
  115.     public void closeButton() {
  116.         Platform.exit();
  117.     }
  118.  
  119.     //TODO: Xem thông tin đã chọn
  120.     public void showSaveListButton() throws IOException {
  121.         String diachicuthe = "";
  122.         String id = LV_saveList.getSelectionModel().getSelectedItem();
  123.         if (id.contains("|")) {
  124.             String[] items = id.split("\\|");
  125.             id = items[0];
  126.             diachicuthe = items[1];
  127.         }
  128.         ArrayDisplay.display(RequestDienluc.request(id, diachicuthe));
  129.     }
  130.  
  131.     public void deleteSaveListButton() throws IOException {
  132.         LV_saveList.getItems().remove(LV_saveList.getSelectionModel().getSelectedIndex());
  133.         saveSaveList();
  134.     }
  135.  
  136.     private void saveSaveList() throws IOException {
  137.         String str = "";
  138.         for (int i = 0; i < LV_saveList.getItems().size(); i++) {
  139.             str += LV_saveList.getItems().get(i) + "\n";
  140.         }
  141.         BufferedWriter writer = new BufferedWriter(new FileWriter(fileSaveList));
  142.         writer.write(str);
  143.         writer.close();
  144.         System.out.println("Đã lưu dữ liệu data.dat");
  145.     }
  146.  
  147.     private List<String> loadSaveList() throws IOException {
  148.         BufferedReader fileOpen = new BufferedReader(new FileReader(fileSaveList));
  149.         List<String> lines = new ArrayList<>();
  150.  
  151.         String line = null;
  152.         while ((line = fileOpen.readLine()) != null) {
  153.             lines.add(line);
  154.         }
  155.         fileOpen.close();
  156.         return lines;
  157.     }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement