Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.application.Platform;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.ComboBox;
- import javafx.scene.control.ListView;
- import javafx.scene.control.TextField;
- import java.io.*;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.ResourceBundle;
- public class Controller implements Initializable {
- //private static final Charset UTF_8 = Charset.forName("UTF-8");
- //private static final Charset ISO = Charset.forName("ISO-8859-1");
- private String[][] arrayCongtyDienluc = new String[0][];
- private String[][] arrayDienluc = new String[0][];
- private String fileSaveList = "data.dat";
- @FXML
- public ComboBox<String> Combo_congtydienluc;
- public ComboBox<String> Combo_dienluc;
- public TextField TF_maKH;
- public TextField TF_diachicuthe;
- public ListView<String> LV_saveList;
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- try {
- arrayCongtyDienluc = RequestDienluc.requestCongtyDienluc();
- } catch (IOException e) {
- e.printStackTrace();
- }
- for (String[] strings : arrayCongtyDienluc) {
- Combo_congtydienluc.getItems().add(strings[0]);
- }
- Combo_congtydienluc.getSelectionModel().select(0);
- try {
- getDienluc();
- } catch (IOException e) {
- LyoLibs.print("Không tải được thông tin cắt điện");
- // e.printStackTrace();
- }
- try {
- LV_saveList.getItems().addAll(loadSaveList());
- } catch (IOException e) {
- LyoLibs.print("data.dat chưa có!");
- // e.printStackTrace();
- }
- }
- public void getDienluc() throws IOException {
- String congtydienluc = Combo_congtydienluc.getValue();
- System.out.println(congtydienluc);
- for (String[] array1 : arrayCongtyDienluc) {
- if (array1[0] == congtydienluc) {
- arrayDienluc = RequestDienluc.requestDienluc(array1[1]);
- Combo_dienluc.getItems().clear();
- for (String[] array2 : arrayDienluc) {
- Combo_dienluc.getItems().add(array2[0]);
- }
- Combo_dienluc.getSelectionModel().select(0);
- break;
- }
- }
- }
- public void searchButton() throws IOException {
- String id = TF_maKH.getText();
- if (id.isEmpty()) {
- String dienluc = Combo_dienluc.getValue().toString();
- for (String[] strings : arrayDienluc) {
- if (strings[0].equals(dienluc)) {
- id = strings[1];
- break;
- }
- }
- }
- String[] result = RequestDienluc.request(id, TF_diachicuthe.getText());
- LyoLibs.arrayPrint(result);
- ArrayDisplay.display(result);
- }
- public void addButton() throws IOException {
- String maKH = TF_maKH.getText();
- if (!maKH.isEmpty()) {
- LV_saveList.getItems().add(maKH);
- } else {
- String dienluc = Combo_dienluc.getValue();
- String madonvi = "";
- for (String[] item : arrayDienluc) {
- if (item[0].equals(dienluc)) {
- madonvi = item[1];
- break;
- }
- }
- String result = madonvi + "|" + TF_diachicuthe.getText() + "|" + Combo_dienluc.getValue() + "|" + Combo_congtydienluc.getValue();
- LV_saveList.getItems().add(result);
- }
- saveSaveList();
- }
- public void closeButton() {
- Platform.exit();
- }
- //TODO: Xem thông tin đã chọn
- public void showSaveListButton() throws IOException {
- String diachicuthe = "";
- String id = LV_saveList.getSelectionModel().getSelectedItem();
- if (id.contains("|")) {
- String[] items = id.split("\\|");
- id = items[0];
- diachicuthe = items[1];
- }
- ArrayDisplay.display(RequestDienluc.request(id, diachicuthe));
- }
- public void deleteSaveListButton() throws IOException {
- LV_saveList.getItems().remove(LV_saveList.getSelectionModel().getSelectedIndex());
- saveSaveList();
- }
- private void saveSaveList() throws IOException {
- String str = "";
- for (int i = 0; i < LV_saveList.getItems().size(); i++) {
- str += LV_saveList.getItems().get(i) + "\n";
- }
- BufferedWriter writer = new BufferedWriter(new FileWriter(fileSaveList));
- writer.write(str);
- writer.close();
- System.out.println("Đã lưu dữ liệu data.dat");
- }
- private List<String> loadSaveList() throws IOException {
- BufferedReader fileOpen = new BufferedReader(new FileReader(fileSaveList));
- List<String> lines = new ArrayList<>();
- String line = null;
- while ((line = fileOpen.readLine()) != null) {
- lines.add(line);
- }
- fileOpen.close();
- return lines;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement