Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.76 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 javafxsocketklient;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStream;
  13. import java.io.PrintWriter;
  14. import java.net.Socket;
  15. import java.net.SocketException;
  16. import java.net.URL;
  17. import java.net.UnknownHostException;
  18. import java.util.ResourceBundle;
  19. import javafx.event.ActionEvent;
  20. import javafx.fxml.FXML;
  21. import javafx.fxml.Initializable;
  22. import javafx.scene.control.Button;
  23. import javafx.scene.control.Label;
  24. import javafx.scene.control.TextField;
  25.  
  26. /**
  27.  *
  28.  * @author Lukasz Laskowski
  29.  */
  30. public class FXMLDocumentController implements Initializable {
  31.     @FXML
  32.     private Button newButton;
  33.    
  34.     @FXML
  35.     private Button delButton;
  36.    
  37.     @FXML
  38.     private Button chButton;
  39.    
  40.     @FXML
  41.     private Button listButton;
  42.    
  43.     @FXML
  44.     private Button dateButton;
  45.    
  46.     @FXML
  47.     private Button returnButton;
  48.    
  49.     @FXML
  50.     private Button confirmButtonAdd;
  51.    
  52.     @FXML
  53.     private Button confirmButtonDelete;
  54.    
  55.     @FXML
  56.     private Button confirmButtonChange;
  57.    
  58.     @FXML
  59.     private Label label;
  60.        
  61.     @FXML
  62.     private TextField name;
  63.    
  64.     @FXML
  65.     private TextField content;
  66.    
  67.     String line;
  68.    
  69.     @FXML
  70.     private void handleButtonAction1(ActionEvent event) { //NOWY PLIK
  71.         newButton.setVisible(false);chButton.setVisible(false);
  72.         delButton.setVisible(false);listButton.setVisible(false);
  73.         dateButton.setVisible(false);
  74.        
  75.         confirmButtonAdd.setVisible(true);returnButton.setVisible(true);
  76.         name.setVisible(true);content.setVisible(true);
  77.         name.setPromptText("Nazwa pliku");content.setPromptText("Zawartość pliku");
  78.         name.setText("");content.setText("");
  79.     }
  80.    
  81.     @FXML
  82.     private void handleButtonAction2(ActionEvent event) { //USUNIĘCIE PLIKU
  83.         newButton.setVisible(false);chButton.setVisible(false);
  84.         delButton.setVisible(false);listButton.setVisible(false);
  85.         dateButton.setVisible(false);
  86.        
  87.         confirmButtonDelete.setVisible(true);returnButton.setVisible(true);
  88.         name.setVisible(true);
  89.         name.setPromptText("Nazwa pliku");name.setText("");
  90.        
  91.     }
  92.    
  93.     @FXML
  94.     private void handleButtonAction3(ActionEvent event) {//ZMIANA NAZWY
  95.         newButton.setVisible(false);chButton.setVisible(false);
  96.         delButton.setVisible(false);listButton.setVisible(false);
  97.         dateButton.setVisible(false);
  98.        
  99.         confirmButtonChange.setVisible(true);returnButton.setVisible(true);
  100.         name.setVisible(true);content.setVisible(true);
  101.         name.setPromptText("Stara nazwa");content.setPromptText("Nowa nazwa");
  102.         name.setText("");content.setText("");    
  103.     }
  104.    
  105.     @FXML
  106.     private void handleButtonList(ActionEvent event) {
  107.         newButton.setVisible(false);chButton.setVisible(false);
  108.         delButton.setVisible(false);listButton.setVisible(false);
  109.         dateButton.setVisible(false);label.setVisible(true);
  110.         returnButton.setVisible(true);
  111.         try {
  112.             String serverHost = "localhost"; // adres IP serwera ("cyfrowo" lub z użyciem DNS)
  113.             int serverPort = 8080; // numer portu na którym nasłuchuje serwer
  114.             Socket socket = new Socket(serverHost, serverPort); // Utworzenie gniazda
  115.  
  116.             OutputStream sockOut = socket.getOutputStream(); //strumień wyjściowy
  117.             InputStream sockIn = socket.getInputStream(); //strumień wejściowy
  118.  
  119.             BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
  120.             PrintWriter out = new PrintWriter(sockOut, true); //wyjście
  121.  
  122.             out.println("LIST"); //wysłanie danych
  123.             line = in.readLine();        
  124.             System.out.println("otrzymałem: " + line);
  125.             line = in.readLine();
  126.             label.setText(line);
  127.             while(line!=null){
  128.                 System.out.println("otrzymałem: " + line);
  129.                 label.setText(label.getText() + "\n" + line);
  130.                 line = in.readLine();
  131.             }
  132.             sockOut.close(); //zamknięcie strumienia
  133.             sockIn.close(); //zamknięcie strumienia
  134.             socket.close(); //zamknięcie gniazada  
  135.         } catch (UnknownHostException e) {
  136.             System.out.println("wyjątek: nieznany host");
  137.         } catch (SocketException e) {
  138.             System.out.println("wyjątek: komunikacja przez gniazdo");
  139.         } catch (IOException e) {
  140.             System.out.println("wyjątek: błąd we/wy");
  141.         }    
  142.     }
  143.    
  144.     @FXML
  145.     private void handleButtonDate(ActionEvent event) {
  146.         newButton.setVisible(false);chButton.setVisible(false);
  147.         delButton.setVisible(false);listButton.setVisible(false);
  148.         label.setVisible(true);returnButton.setVisible(true);
  149.         try {
  150.             String serverHost = "localhost"; // adres IP serwera ("cyfrowo" lub z użyciem DNS)
  151.             int serverPort = 8080; // numer portu na którym nasłuchuje serwer
  152.             Socket socket = new Socket(serverHost, serverPort); // Utworzenie gniazda
  153.  
  154.             OutputStream sockOut = socket.getOutputStream(); //strumień wyjściowy
  155.             InputStream sockIn = socket.getInputStream(); //strumień wejściowy
  156.  
  157.             BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
  158.             PrintWriter out = new PrintWriter(sockOut, true); //wyjście
  159.  
  160.             out.println("DATE"); //wysłanie danych
  161.             line = in.readLine();
  162.             System.out.println("otrzymałem: " + line);
  163.             label.setText(line);
  164.            
  165.             sockOut.close(); //zamknięcie strumienia
  166.             sockIn.close(); //zamknięcie strumienia
  167.             socket.close(); //zamknięcie gniazada
  168.            
  169.         } catch (UnknownHostException e) {
  170.             System.out.println("wyjątek: nieznany host");
  171.         } catch (SocketException e) {
  172.             System.out.println("wyjątek: komunikacja przez gniazdo");
  173.         } catch (IOException e) {
  174.             System.out.println("wyjątek: błąd we/wy");
  175.         }    
  176.     }
  177.    
  178.     @FXML
  179.     private void handleButtonNew(ActionEvent event) {
  180.         try {
  181.             String serverHost = "localhost"; // adres IP serwera ("cyfrowo" lub z użyciem DNS)
  182.             int serverPort = 8080; // numer portu na którym nasłuchuje serwer
  183.             Socket socket = new Socket(serverHost, serverPort); // Utworzenie gniazda
  184.  
  185.             OutputStream sockOut = socket.getOutputStream(); //strumień wyjściowy
  186.             InputStream sockIn = socket.getInputStream(); //strumień wejściowy
  187.  
  188.             BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
  189.             PrintWriter out = new PrintWriter(sockOut, true); //wyjście
  190.  
  191.             out.println("MAKE"); //wysłanie danych
  192.            
  193.             line = in.readLine();
  194.             System.out.println("otrzymałem: " + line);
  195.             out.println(name.getText()); //nazwa pliku
  196.            
  197.             line = in.readLine();
  198.             System.out.println("otrzymałem: " + line);
  199.            
  200.             out.println(content.getText()); //zawartość pliku
  201.             line = in.readLine();
  202.             System.out.println("otrzymałem: " + line);
  203.            
  204.             sockOut.close(); //zamknięcie strumienia
  205.             sockIn.close(); //zamknięcie strumienia
  206.             socket.close(); //zamknięcie gniazada
  207.            
  208.         } catch (UnknownHostException e) {
  209.             System.out.println("wyjątek: nieznany host");
  210.         } catch (SocketException e) {
  211.             System.out.println("wyjątek: komunikacja przez gniazdo");
  212.         } catch (IOException e) {
  213.             System.out.println("wyjątek: błąd we/wy");
  214.         }  
  215.     }
  216.    
  217.     @FXML
  218.     private void handleButtonDelete(ActionEvent event) {
  219.         try {
  220.             String serverHost = "localhost"; // adres IP serwera ("cyfrowo" lub z użyciem DNS)
  221.             int serverPort = 8080; // numer portu na którym nasłuchuje serwer
  222.             Socket socket = new Socket(serverHost, serverPort); // Utworzenie gniazda
  223.  
  224.             OutputStream sockOut = socket.getOutputStream(); //strumień wyjściowy
  225.             InputStream sockIn = socket.getInputStream(); //strumień wejściowy
  226.  
  227.             BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
  228.             PrintWriter out = new PrintWriter(sockOut, true); //wyjście
  229.  
  230.             out.println("DEL"); //wysłanie danych
  231.            
  232.             line = in.readLine();
  233.             System.out.println("otrzymałem: " + line);
  234.             out.println(name.getText()); //nazwa pliku
  235.            
  236.             line = in.readLine();
  237.             System.out.println("otrzymałem: " + line);
  238.            
  239.             sockOut.close(); //zamknięcie strumienia
  240.             sockIn.close(); //zamknięcie strumienia
  241.             socket.close(); //zamknięcie gniazada
  242.            
  243.         } catch (UnknownHostException e) {
  244.             System.out.println("wyjątek: nieznany host");
  245.         } catch (SocketException e) {
  246.             System.out.println("wyjątek: komunikacja przez gniazdo");
  247.         } catch (IOException e) {
  248.             System.out.println("wyjątek: błąd we/wy");
  249.         }  
  250.     }
  251.    
  252.     @FXML
  253.     private void handleButtonChange(ActionEvent event) {
  254.         try {
  255.             String serverHost = "localhost"; // adres IP serwera ("cyfrowo" lub z użyciem DNS)
  256.             int serverPort = 8080; // numer portu na którym nasłuchuje serwer
  257.             Socket socket = new Socket(serverHost, serverPort); // Utworzenie gniazda
  258.  
  259.             OutputStream sockOut = socket.getOutputStream(); //strumień wyjściowy
  260.             InputStream sockIn = socket.getInputStream(); //strumień wejściowy
  261.  
  262.             BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
  263.             PrintWriter out = new PrintWriter(sockOut, true); //wyjście
  264.  
  265.             out.println("CHANGE"); //wysłanie danych
  266.             line = in.readLine();
  267.             System.out.println("otrzymałem: " + line);
  268.             out.println(name.getText()); //nazwa pliku
  269.             line = in.readLine();
  270.             System.out.println("otrzymałem: " + line);
  271.             out.println(content.getText()); //nowa nazwa pliku
  272.             line = in.readLine();
  273.             System.out.println("otrzymałem: " + line);
  274.              
  275.             sockOut.close(); //zamknięcie strumienia
  276.             sockIn.close(); //zamknięcie strumienia
  277.             socket.close(); //zamknięcie gniazada
  278.            
  279.         } catch (UnknownHostException e) {
  280.             System.out.println("wyjątek: nieznany host");
  281.         } catch (SocketException e) {
  282.             System.out.println("wyjątek: komunikacja przez gniazdo");
  283.         } catch (IOException e) {
  284.             System.out.println("wyjątek: błąd we/wy");
  285.         }  
  286.     }
  287.    
  288.     @FXML
  289.     private void handleButtonReturn(ActionEvent event) {
  290.         newButton.setVisible(true);chButton.setVisible(true);
  291.         delButton.setVisible(true);listButton.setVisible(true);
  292.         dateButton.setVisible(true);
  293.         name.setVisible(false);content.setVisible(false);
  294.         confirmButtonAdd.setVisible(false);
  295.         confirmButtonDelete.setVisible(false);
  296.         confirmButtonChange.setVisible(false);
  297.         returnButton.setVisible(false);
  298.         label.setVisible(false);
  299.     }
  300.      
  301.     @Override
  302.     public void initialize(URL url, ResourceBundle rb) {
  303.         // TODO
  304.     }    
  305.    
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement