Advertisement
Guest User

AIDS

a guest
Apr 26th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.19 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.beans.property.SimpleStringProperty;
  4. import javafx.beans.value.ChangeListener;
  5. import javafx.beans.value.ObservableValue;
  6. import javafx.collections.FXCollections;
  7. import javafx.collections.ObservableList;
  8. import javafx.event.ActionEvent;
  9. import javafx.event.EventHandler;
  10. import javafx.fxml.FXML;
  11. import javafx.fxml.FXMLLoader;
  12. import javafx.scene.control.*;
  13. import javafx.scene.control.cell.TextFieldTableCell;
  14. import javafx.scene.input.MouseButton;
  15. import javafx.scene.input.MouseEvent;
  16. import javafx.scene.layout.BorderPane;
  17. import javafx.util.Callback;
  18. import jdk.nashorn.internal.runtime.ECMAException;
  19. import org.postgresql.ds.PGPoolingDataSource;
  20. import sample.inserts.*;
  21. import java.io.IOException;
  22. import java.sql.Connection;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Statement;
  26. import java.util.Optional;
  27.  
  28. public class Controller {
  29.     private static String url;
  30.     private static String user;
  31.     private static String password;
  32.     protected PGPoolingDataSource source;
  33.     private Connection con;
  34.     private ObservableList<ObservableList> flights;
  35.     private ObservableList<ObservableList> passengers;
  36.     private ObservableList<ObservableList> boardingPasses;
  37.     private ObservableList<ObservableList> airTickets;
  38.     private ObservableList<ObservableList> aircrafts;
  39.     private ObservableList<ObservableList> luggageList;
  40.     private ObservableList<ObservableList> cargoList;
  41.     private ObservableList<ObservableList> techOps;
  42.  
  43.     @FXML
  44.     private BorderPane mainPane;
  45.  
  46.     @FXML
  47.     private TableView flight;
  48.  
  49.     @FXML
  50.     private TableView passenger;
  51.  
  52.     @FXML
  53.     private TableView boarding_pass;
  54.  
  55.     @FXML
  56.     private TableView air_ticket;
  57.  
  58.     @FXML
  59.     private TableView aircraft;
  60.  
  61.     @FXML
  62.     private TableView luggage;
  63.  
  64.     @FXML
  65.     private TableView cargo;
  66.  
  67.     @FXML
  68.     private TableView technical_operation;
  69.  
  70.     @FXML
  71.     private TextArea consoleTextArea;
  72.  
  73.     @FXML
  74.     private TextArea detailTextArea;
  75.  
  76.     @FXML
  77.     public void setDatabaseBtnClicked(ActionEvent actionEvent) throws Exception {
  78.         Dialog<ButtonType> dialog = new Dialog<>();
  79.         dialog.initOwner(mainPane.getScene().getWindow());
  80.         dialog.setTitle("Set database login info");
  81.         FXMLLoader fxmlLoader = new FXMLLoader();
  82.         fxmlLoader.setLocation(getClass().getResource("setDatabaseDialog.fxml"));
  83.         try {
  84.             dialog.getDialogPane().setContent(fxmlLoader.load());
  85.         } catch (IOException e) {
  86.             System.out.println("Couldn't load the dialog");
  87.             e.printStackTrace();
  88.             return;
  89.         }
  90.  
  91.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  92.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  93.  
  94.         Optional<ButtonType> result = dialog.showAndWait();
  95.  
  96.         if (result.isPresent() && result.get() == ButtonType.OK) {
  97.             setDatabaseDialogController controller = fxmlLoader.getController();
  98.             url = controller.getUrl();
  99.             user = controller.getUser();
  100.             password = controller.getPassword();
  101.             connectToDbs();
  102.         }
  103.     }
  104.  
  105.     @FXML
  106.     public void addAirTicket() throws Exception {
  107.         String from = "", to = "", created = "", class1 = "", passenger_id = "", flight_id = "";
  108.  
  109.         Dialog<ButtonType> dialog = new Dialog<>();
  110.         dialog.initOwner(mainPane.getScene().getWindow());
  111.         dialog.setTitle("New Air ticket");
  112.         FXMLLoader fxmlLoader = new FXMLLoader();
  113.         fxmlLoader.setLocation(getClass().getResource("inserts/insertAirTicket.fxml"));
  114.         try {
  115.             dialog.getDialogPane().setContent(fxmlLoader.load());
  116.         } catch (IOException e) {
  117.             System.out.println("Couldn't load the dialog");
  118.             e.printStackTrace();
  119.             return;
  120.         }
  121.  
  122.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  123.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  124.  
  125.         Optional<ButtonType> result = dialog.showAndWait();
  126.  
  127.         if (result.isPresent() && result.get() == ButtonType.OK) {
  128.             insertAirTicketController controller = fxmlLoader.getController();
  129.             from = controller.getFrom();
  130.             to = controller.getTo();
  131.             created = controller.getCreated();
  132.             class1 = controller.getClass1();
  133.             passenger_id = controller.getPassenger_id();
  134.             flight_id = controller.getFlight_id();
  135.  
  136.             try {
  137.                 Statement stmt = con.createStatement();
  138.                 String sql = "INSERT INTO AIR_TICKET (\"from\", \"to\", created, class, passenger_id, flight_id) " +
  139.                         "VALUES ('" + from + "','" + to + "','" + created + "'," + class1 + "," + passenger_id + "," + flight_id + ")";
  140.                 System.out.println(sql);
  141.                 stmt.executeUpdate(sql);
  142.             } catch (SQLException sqle) {
  143.                 System.out.println(sqle.getMessage());
  144.             }
  145.         }
  146.     }
  147.  
  148.     @FXML
  149.     private void addAircraft() throws Exception {
  150.         String model = "", capacity = "", dateOfBuilt = "", maxLoad = "", registration = "";
  151.  
  152.         Dialog<ButtonType> dialog = new Dialog<>();
  153.         dialog.initOwner(mainPane.getScene().getWindow());
  154.         dialog.setTitle("New Aircraft");
  155.         FXMLLoader fxmlLoader = new FXMLLoader();
  156.         fxmlLoader.setLocation(getClass().getResource("inserts/insertAircraft.fxml"));
  157.         try {
  158.             dialog.getDialogPane().setContent(fxmlLoader.load());
  159.         } catch (IOException e) {
  160.             System.out.println("Couldn't load the dialog");
  161.             e.printStackTrace();
  162.             return;
  163.         }
  164.  
  165.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  166.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  167.  
  168.         Optional<ButtonType> result = dialog.showAndWait();
  169.  
  170.         if (result.isPresent() && result.get() == ButtonType.OK) {
  171.             insertAircraftController controller = fxmlLoader.getController();
  172.             model = controller.getModel();
  173.             capacity = controller.getCapacity();
  174.             dateOfBuilt = controller.getDateOfBuilt();
  175.             maxLoad = controller.getMaxLoad();
  176.             registration = controller.getRegistration();
  177.         }
  178.  
  179.         try {
  180.             Statement stmt = con.createStatement();
  181.             String sql = "INSERT INTO AIRCRAFT (model, capacity, date_of_built, max_load, registration) " +
  182.                     "VALUES ('" + model + "','" + capacity + "','" + dateOfBuilt + "'," + maxLoad + ",'" + registration + "')";
  183.             System.out.println(sql);
  184.             stmt.executeUpdate(sql);
  185.         } catch (SQLException sqle) {
  186.             System.out.println(sqle.getMessage());
  187.         }
  188.     }
  189.  
  190.     @FXML
  191.     private void addBoardingPass() {
  192.         String class1 = "", created = "", flight_id = "", air_ticket_id = "", seat = "";
  193.  
  194.         Dialog<ButtonType> dialog = new Dialog<>();
  195.         dialog.initOwner(mainPane.getScene().getWindow());
  196.         dialog.setTitle("New Boarding pass");
  197.         FXMLLoader fxmlLoader = new FXMLLoader();
  198.         fxmlLoader.setLocation(getClass().getResource("inserts/insertBoardingPass.fxml"));
  199.         try {
  200.             dialog.getDialogPane().setContent(fxmlLoader.load());
  201.         } catch (IOException e) {
  202.             System.out.println("Couldn't load the dialog");
  203.             e.printStackTrace();
  204.             return;
  205.         }
  206.  
  207.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  208.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  209.  
  210.         Optional<ButtonType> result = dialog.showAndWait();
  211.  
  212.         if (result.isPresent() && result.get() == ButtonType.OK) {
  213.             insertBoardingPassController controller = fxmlLoader.getController();
  214.             class1 = controller.getClass1();
  215.             created = controller.getCreated();
  216.             flight_id = controller.getFlight_id();
  217.             air_ticket_id = controller.getAir_ticket_id();
  218.             seat = controller.getSeat();
  219.         }
  220.  
  221.         try {
  222.             Statement stmt = con.createStatement();
  223.             String sql = "INSERT INTO BOARDING_PASS (class, created, flight_id, air_ticket_id, seat) " +
  224.                     "VALUES (" + class1 + ",'" + created + "'," + flight_id + "," + air_ticket_id + "," + seat + ")";
  225.             stmt.executeUpdate(sql);
  226.         } catch (SQLException sqle) {
  227.             System.out.println(sqle.getMessage());
  228.         }
  229.     }
  230.  
  231.     @FXML
  232.     private void addCargo() {
  233.         String weight = "", flight_id = "";
  234.  
  235.         Dialog<ButtonType> dialog = new Dialog<>();
  236.         dialog.initOwner(mainPane.getScene().getWindow());
  237.         dialog.setTitle("New Cargo");
  238.         FXMLLoader fxmlLoader = new FXMLLoader();
  239.         fxmlLoader.setLocation(getClass().getResource("inserts/insertCargo.fxml"));
  240.         try {
  241.             dialog.getDialogPane().setContent(fxmlLoader.load());
  242.         } catch (IOException e) {
  243.             System.out.println("Couldn't load the dialog");
  244.             e.printStackTrace();
  245.             return;
  246.         }
  247.  
  248.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  249.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  250.  
  251.         Optional<ButtonType> result = dialog.showAndWait();
  252.  
  253.         if (result.isPresent() && result.get() == ButtonType.OK) {
  254.             insertCargoController controller = fxmlLoader.getController();
  255.             weight = controller.getWeight();
  256.             flight_id = controller.getFlight_id();
  257.         }
  258.  
  259.         try {
  260.             Statement stmt = con.createStatement();
  261.             String sql = "INSERT INTO CARGO (weight, flight_id) " +
  262.                     "VALUES (" + weight + "," + flight_id + ")";
  263.             stmt.executeUpdate(sql);
  264.         } catch (SQLException sqle) {
  265.             System.out.println(sqle.getMessage());
  266.         }
  267.     }
  268.  
  269.     @FXML
  270.     private void addFlight() {
  271.         String from = "", to = "", gate = "", departure = "", arrival = "", status = "", flight_code = "", aircraft_id = "";
  272.  
  273.         Dialog<ButtonType> dialog = new Dialog<>();
  274.         dialog.initOwner(mainPane.getScene().getWindow());
  275.         dialog.setTitle("New Flight");
  276.         FXMLLoader fxmlLoader = new FXMLLoader();
  277.         fxmlLoader.setLocation(getClass().getResource("inserts/insertFlight.fxml"));
  278.         try {
  279.             dialog.getDialogPane().setContent(fxmlLoader.load());
  280.         } catch (IOException e) {
  281.             System.out.println("Couldn't load the dialog");
  282.             e.printStackTrace();
  283.             return;
  284.         }
  285.  
  286.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  287.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  288.  
  289.         Optional<ButtonType> result = dialog.showAndWait();
  290.  
  291.         if (result.isPresent() && result.get() == ButtonType.OK) {
  292.             insertFlightController controller = fxmlLoader.getController();
  293.             from = controller.getFrom();
  294.             to = controller.getTo();
  295.             gate = controller.getGate();
  296.             departure = controller.getDeparture();
  297.             arrival = controller.getArrival();
  298.             status = controller.getStatus();
  299.             flight_code = controller.getFlightCode();
  300.             aircraft_id = controller.getAircraftID();
  301.         }
  302.  
  303.         try {
  304.             Statement stmt = con.createStatement();
  305.             String sql = "INSERT INTO AIRCRAFT (\"from\", \"to\", gate, departure, arrival, status, flight_code, aircraft_id) " +
  306.                     "VALUES ('" + from + "','" + to + "','" + gate + "','" + departure + "','" + arrival + "','" + status + "','" + flight_code + "'," + aircraft_id + ")";
  307.             System.out.println(sql);
  308.             stmt.executeUpdate(sql);
  309.         } catch (SQLException sqle) {
  310.             System.out.println(sqle.getMessage());
  311.         }
  312.     }
  313.  
  314.     @FXML
  315.     private void addLuggage() {
  316.         String weight = "", boarding_pass_id = "";
  317.  
  318.         Dialog<ButtonType> dialog = new Dialog<>();
  319.         dialog.initOwner(mainPane.getScene().getWindow());
  320.         dialog.setTitle("New Luggage");
  321.         FXMLLoader fxmlLoader = new FXMLLoader();
  322.         fxmlLoader.setLocation(getClass().getResource("inserts/insertLuggage.fxml"));
  323.         try {
  324.             dialog.getDialogPane().setContent(fxmlLoader.load());
  325.         } catch (IOException e) {
  326.             System.out.println("Couldn't load the dialog");
  327.             e.printStackTrace();
  328.             return;
  329.         }
  330.  
  331.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  332.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  333.  
  334.         Optional<ButtonType> result = dialog.showAndWait();
  335.  
  336.         if (result.isPresent() && result.get() == ButtonType.OK) {
  337.             insertLuggageController controller = fxmlLoader.getController();
  338.             weight = controller.getWeight();
  339.             boarding_pass_id = controller.getBoardingPassID();
  340.         }
  341.  
  342.         try {
  343.             Statement stmt = con.createStatement();
  344.             String sql = "INSERT INTO LUGGAGE (weight, boarding_pass_id) " +
  345.                     "VALUES (" + weight + "," + boarding_pass_id + ")";
  346.             stmt.executeUpdate(sql);
  347.         } catch (SQLException sqle) {
  348.             System.out.println(sqle.getMessage());
  349.         }
  350.     }
  351.  
  352.     @FXML
  353.     private void addPassenger() {
  354.         String dateOfBirth = "", phoneNo = "", status = "", firstName = "", lastName = "", email = "", sex = "";
  355.  
  356.         Dialog<ButtonType> dialog = new Dialog<>();
  357.         dialog.initOwner(mainPane.getScene().getWindow());
  358.         dialog.setTitle("New Passenger");
  359.         FXMLLoader fxmlLoader = new FXMLLoader();
  360.         fxmlLoader.setLocation(getClass().getResource("inserts/insertPassenger.fxml"));
  361.         try {
  362.             dialog.getDialogPane().setContent(fxmlLoader.load());
  363.         } catch (IOException e) {
  364.             System.out.println("Couldn't load the dialog");
  365.             e.printStackTrace();
  366.             return;
  367.         }
  368.  
  369.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  370.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  371.  
  372.         Optional<ButtonType> result = dialog.showAndWait();
  373.  
  374.         if (result.isPresent() && result.get() == ButtonType.OK) {
  375.             insertPassengerController controller = fxmlLoader.getController();
  376.             dateOfBirth = controller.getDateOfBirth();
  377.             phoneNo = controller.getPhoneNo();
  378.             status = controller.getStatus();
  379.             firstName = controller.getFirstName();
  380.             lastName = controller.getLastName();
  381.             email = controller.getEmail();
  382.             sex = controller.getSex();
  383.         }
  384.  
  385.         try {
  386.             Statement stmt = con.createStatement();
  387.             String sql = "INSERT INTO PASSENGER (date_of_birth, phone_no, status, first_name, last_name, email, sex) " +
  388.                     "VALUES ('" + dateOfBirth + "','" + phoneNo + "','" + status + "','" + firstName + "','" + lastName + "','" + email + "','" + sex + "'')";
  389.             System.out.println(sql);
  390.             stmt.executeUpdate(sql);
  391.         } catch (SQLException sqle) {
  392.             System.out.println(sqle.getMessage());
  393.         }
  394.     }
  395.  
  396.     @FXML
  397.     private void addTechnicalOperation() {
  398.         String end = "", aircraft_id = "", firs_name = "", last_name = "", info = "";
  399.  
  400.         Dialog<ButtonType> dialog = new Dialog<>();
  401.         dialog.initOwner(mainPane.getScene().getWindow());
  402.         dialog.setTitle("New Technical operation");
  403.         FXMLLoader fxmlLoader = new FXMLLoader();
  404.         fxmlLoader.setLocation(getClass().getResource("inserts/insertTechnicalOperation.fxml"));
  405.         try {
  406.             dialog.getDialogPane().setContent(fxmlLoader.load());
  407.         } catch (IOException e) {
  408.             System.out.println("Couldn't load the dialog");
  409.             e.printStackTrace();
  410.             return;
  411.         }
  412.  
  413.         dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
  414.         dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
  415.  
  416.         Optional<ButtonType> result = dialog.showAndWait();
  417.  
  418.         if (result.isPresent() && result.get() == ButtonType.OK) {
  419.             insertTechnicalOperationController controller = fxmlLoader.getController();
  420.             end = controller.getEnd();
  421.             aircraft_id = controller.getAircraftID();
  422.             firs_name = controller.getFirstName();
  423.             last_name = controller.getLastName();
  424.             info = controller.getInfo();
  425.         }
  426.  
  427.         try {
  428.             Statement stmt = con.createStatement();
  429.             String sql = "INSERT INTO TECHNICAL_OPERATION (end, aircraft_id, first_name, last_name, info) " +
  430.                     "VALUES ('" + end + "'," + aircraft_id + ",'" + firs_name + "','" + last_name + "','" + info + "')";
  431.             stmt.executeUpdate(sql);
  432.         } catch (SQLException sqle) {
  433.             System.out.println(sqle.getMessage());
  434.         }
  435.     }
  436.  
  437.     @FXML
  438.     public void executeConsole() {
  439.         try {
  440.             source = dbs_conn.getSource(url.split("/")[0], url.split("/")[1], user, password);
  441.             Connection conn = source.getConnection();
  442.             try {
  443.                 conn.createStatement().execute(consoleTextArea.getText());
  444.             } catch (Exception e) {
  445.  
  446.             } finally {
  447.                 try {
  448.                     if (conn != null) conn.close();
  449.                 } catch (Exception e) {
  450.                     detailTextArea.setText(e.getMessage());
  451.                 }
  452.             }
  453.         } catch (Exception e) {
  454.             detailTextArea.setText(e.getMessage());
  455.         } finally {
  456.             refreshTables();
  457.         }
  458.     }
  459.  
  460.     @FXML
  461.     private void refreshTables() {
  462.         try {
  463.             flight.getItems().clear();
  464.             flight.getColumns().clear();
  465.             flights = FXCollections.observableArrayList();
  466.             getData(flight, "flight", flights);
  467.  
  468.             passenger.getItems().clear();
  469.             passenger.getColumns().clear();
  470.             passengers = FXCollections.observableArrayList();
  471.             getData(passenger, "passenger", passengers);
  472.  
  473.             boarding_pass.getItems().clear();
  474.             boarding_pass.getColumns().clear();
  475.             boardingPasses = FXCollections.observableArrayList();
  476.             getData(boarding_pass, "boarding_pass", boardingPasses);
  477.  
  478.             air_ticket.getItems().clear();
  479.             air_ticket.getColumns().clear();
  480.             airTickets = FXCollections.observableArrayList();
  481.             getData(air_ticket, "air_ticket", airTickets);
  482.  
  483.             aircraft.getItems().clear();
  484.             aircraft.getColumns().clear();
  485.             aircrafts = FXCollections.observableArrayList();
  486.             getData(aircraft, "aircraft", aircrafts);
  487.  
  488.             luggage.getItems().clear();
  489.             luggage.getColumns().clear();
  490.             luggageList = FXCollections.observableArrayList();
  491.             getData(luggage, "luggage", luggageList);
  492.  
  493.             cargo.getItems().clear();
  494.             cargo.getColumns().clear();
  495.             cargoList = FXCollections.observableArrayList();
  496.             getData(cargo, "cargo", cargoList);
  497.  
  498.             technical_operation.getItems().clear();
  499.             technical_operation.getColumns().clear();
  500.             techOps = FXCollections.observableArrayList();
  501.             getData(technical_operation, "technical_operation", techOps);
  502.  
  503.         } catch (Exception e) {
  504.  
  505.         }
  506.     }
  507.  
  508.     @FXML
  509.     public void clearConsole() {
  510.         consoleTextArea.clear();
  511.         detailTextArea.clear();
  512.     }
  513.  
  514.     public void getData(TableView table, String _class, ObservableList<ObservableList> rows) throws SQLException {
  515.         /*
  516.         Pripoji sa na databazu
  517.          */
  518.         source = dbs_conn.getSource(url.split("/")[0], url.split("/")[1], user, password);
  519.         Connection conn = source.getConnection();
  520.         try {
  521.             /*
  522.             Vyselectuje prvych 100 rows z databazy aby to netrvalo prilis dlho
  523.              */
  524.             String SQL = "SELECT * FROM " + _class + " LIMIT 100";
  525.             ResultSet rs = conn.createStatement().executeQuery(SQL);
  526.  
  527.             /*
  528.             Dynamicky vytvori columns v TableView podla toho kolko columns sa nachadza v danej tabulke v databaze
  529.              */
  530.             for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
  531.                 final int j = i;
  532.                 TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
  533.                 col.setCellFactory(TextFieldTableCell.forTableColumn());
  534.                 col.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
  535.                     @Override
  536.                     public void handle(TableColumn.CellEditEvent cellEditEvent) {
  537.                         editCell(cellEditEvent);
  538.                     }
  539.                 });
  540.                 col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
  541.                     public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
  542.                         return new SimpleStringProperty(param.getValue().get(j).toString());
  543.                     }
  544.                 });
  545.  
  546.                 table.getColumns().addAll(col);
  547.             }
  548.  
  549.             /*
  550.             Naplni ObservableList datami
  551.              */
  552.             while (rs.next()) {
  553.                 ObservableList<String> row = FXCollections.observableArrayList();
  554.                 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
  555.                     if (rs.getObject(i) == null) {
  556.                         row.add("No data");
  557.                     } else {
  558.                         row.add(rs.getString(i));
  559.                     }
  560.                 }
  561.                 rows.add(row);
  562.             }
  563.  
  564.             table.setItems(rows);
  565.             ContextMenu cm = new ContextMenu();
  566.             MenuItem mi = new MenuItem("Delete");
  567.             StringBuilder row = new StringBuilder();
  568.  
  569.             table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
  570.                 @Override
  571.                 public void changed(ObservableValue observableValue, Object o, Object t1) {
  572.                     // TOTO TREBA OPRAVIT!!!
  573.                     if (t1 != null) {
  574.                         row.append(t1.toString());
  575.                     }
  576.                 }
  577.             });
  578.             mi.setOnAction(new EventHandler<ActionEvent>() {
  579.                 @Override
  580.                 public void handle(ActionEvent actionEvent) {
  581.                     String[] rowParts = row.toString().substring(1, row.length() - 1).split(",");
  582.                     int id = 0;
  583.                     for (int i = 0; i < table.getColumns().size(); i++) {
  584.                         if (((TableColumn)table.getColumns().get(i)).getText().matches("id")) {
  585.                             id += Integer.parseInt(rowParts[i].substring(1));
  586.                         }
  587.                     }
  588.                     try {
  589.                         source = dbs_conn.getSource(url.split("/")[0], url.split("/")[1], user, password);
  590.                         Connection conn = source.getConnection();
  591.                         String sql = "DELETE FROM " + table.getId() + " WHERE id = " + String.valueOf(id) + ";";
  592.                         detailTextArea.setText(sql);
  593.                         conn.createStatement().execute(sql);
  594.                     } catch (Exception e) {
  595.                         detailTextArea.setText(e.getMessage());
  596.                     }
  597.                     table.getItems().remove(table.getSelectionModel().getSelectedIndex());
  598.                     table.refresh();
  599.                 }
  600.             });
  601.             cm.getItems().add(mi);
  602.             table.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
  603.                 @Override
  604.                 public void handle(MouseEvent mouseEvent) {
  605.                     if (mouseEvent.getButton() == MouseButton.SECONDARY) {
  606.                         cm.show(table, mouseEvent.getScreenX(), mouseEvent.getScreenY());
  607.                     }
  608.                 }
  609.             });
  610.  
  611.         } catch (SQLException e) {
  612.             detailTextArea.textProperty().setValue(e.getMessage());
  613.         } finally {
  614.             try {
  615.                 if (conn != null) conn.close();
  616.             } catch (Exception e) {
  617.                 detailTextArea.textProperty().setValue(e.getMessage());
  618.             }
  619.         }
  620.     }
  621.  
  622.     private void editCell(TableColumn.CellEditEvent cellEditEvent) {
  623.         /*
  624.         Zistim novu hodnotu bunky, index stlpca a zvysne hodnoty v riadku
  625.          */
  626.         String newValue = cellEditEvent.getNewValue().toString();
  627.         int columnIndex = cellEditEvent.getTablePosition().getColumn();
  628.         String rawRow = cellEditEvent.getRowValue().toString();
  629.         String[] rowData = rawRow.substring(1, rawRow.length() - 1).split(",");
  630.  
  631.         /*
  632.         Vytvorim novy riadok s aktualizovanymi hodnotami
  633.          */
  634.         rowData[columnIndex] = newValue;
  635.  
  636.         try {
  637.             source = dbs_conn.getSource(url.split("/")[0], url.split("/")[1], user, password);
  638.             Connection conn = source.getConnection();
  639.             int id = 0;
  640.             for (int i = 0; i < cellEditEvent.getTableView().getColumns().size(); i++) {
  641.                 if (((TableColumn)cellEditEvent.getTableView().getColumns().get(i)).getText().matches("id")) {
  642.                     System.out.println(((TableColumn)cellEditEvent.getTableView().getColumns().get(i)).getText());
  643.                     id += Integer.parseInt(rowData[i].substring(1));
  644.                 }
  645.             }
  646.             String sql = "UPDATE " + cellEditEvent.getTableView().getId() + " SET \"" + cellEditEvent.getTableColumn().getText() + "\" = '" + newValue + "' WHERE id = " + id + ";";
  647.             detailTextArea.setText(sql);
  648.             conn.createStatement().execute(sql);
  649.         } catch (Exception e) {
  650.             detailTextArea.setText(e.getMessage());
  651.         }
  652.     }
  653.  
  654.     private void connectToDbs() {
  655.         Statement stmt;
  656.  
  657.         dbs_conn conn = new dbs_conn(url, user, password);
  658.         con = conn.connect();
  659.  
  660.         refreshTables();
  661.     }
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement