ZelekWafelek

javaGUI

Dec 8th, 2021
724
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMLController.java to edit this template
  4.  */
  5. package pl.polsl.organizer_kamilwierzbicki_gui;
  6.  
  7. import Model.Date;
  8. import Model.Event;
  9. import Model.EventWithIndex;
  10. import Model.InvalidDateFormatException;
  11. import Model.Model;
  12. import java.io.DataInputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.net.URL;
  19. import java.util.List;
  20. import java.util.ResourceBundle;
  21. import java.util.Scanner;
  22. import javafx.beans.value.ChangeListener;
  23. import javafx.beans.value.ObservableValue;
  24. import javafx.event.ActionEvent;
  25. import javafx.fxml.FXML;
  26. import javafx.fxml.Initializable;
  27.  
  28. import javafx.scene.control.Button;
  29. import javafx.scene.control.Label;
  30. import javafx.scene.control.ListView;
  31. import javafx.scene.control.TextArea;
  32. import javafx.scene.control.TextField;
  33. import javafx.stage.FileChooser;
  34. /**
  35.  * FXML Controller class
  36.  *
  37.  * @author zelek_f3uufcy
  38.  */
  39.  
  40. public class OrganizerController implements Initializable {
  41.    
  42.     final int maxCharactersInLine = 30;
  43.     final int maxCharactersToFindNewLine = 7;
  44.    
  45.     @FXML
  46.     private Button newEvent_Button;
  47.     @FXML
  48.     private ListView<String> events_list;
  49.     @FXML
  50.     private Button remove_button;
  51.     @FXML
  52.     private TextField date_textField;
  53.     @FXML
  54.     private TextField title_textField;
  55.     @FXML
  56.     private TextArea desc_textField;
  57.     @FXML
  58.     private Label desc_label;
  59.     /**
  60.      * Initializes the controller class.
  61.      */
  62.     private Model model;
  63.     @Override
  64.     public void initialize(URL url, ResourceBundle rb) {
  65.         model = new Model();
  66.     events_list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
  67.  
  68.     @Override
  69.     public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
  70.         // Your action here
  71.         int indx = events_list.getSelectionModel().getSelectedIndex();
  72.         if(indx >= 0){
  73.             List<EventWithIndex> events = model.getEvents();
  74.            
  75.             setDescLabel(events.get(indx).event.getDescription());
  76.         }
  77.     }
  78. });
  79.        
  80.     }    
  81.    
  82.     @FXML
  83.     private void newEvent_ButtonAction(ActionEvent event) {
  84.         try {
  85.             model.addEvent(new Event(date_textField.getText(), title_textField.getText(), desc_textField.getText()));
  86.         } catch (InvalidDateFormatException e) {
  87.  
  88.         }
  89.         updateList();
  90.        
  91.  
  92.     }  
  93.    
  94.     private void updateList(){
  95.         events_list.getItems().clear();
  96.         List<EventWithIndex> events = model.getEvents();
  97.         for(EventWithIndex e : events){
  98.             Date currentDate = e.event.getDate();
  99.             String currentEventString = String.valueOf(e.index) + " : " +
  100.                     Integer.toString( currentDate.getDay()) +
  101.                     "/" + Integer.toString(currentDate.getMonth()) +
  102.                     "/" + Integer.toString(currentDate.getYear()) +
  103.                     ": " + e.event.getTitle() + "\n";
  104.             events_list.getItems().add(currentEventString);
  105.         }
  106.     }
  107.  
  108.     @FXML
  109.     private void remove_ButtonAction(ActionEvent event) {
  110.         String selected = events_list.getSelectionModel().getSelectedItem();
  111.         if(selected == null)return;
  112.         String parsed = selected.split("[:]+")[0];
  113.         int index = Integer.parseInt(parsed.replaceAll("[\\D]", ""));
  114.         model.rmEvent(index);
  115.         updateList();
  116.     }
  117.    
  118.     @FXML
  119.     private void saveToFile_buttonAction(ActionEvent event){
  120.         FileChooser fileChooser = new FileChooser();
  121.         File file = fileChooser.showSaveDialog(null);
  122.         try{
  123.             DataOutputStream fileStream = new DataOutputStream(new FileOutputStream(file));
  124.             model.saveToJson(fileStream);
  125.         }
  126.         catch(Exception e){
  127.             System.out.println("Cannot save file: ");
  128.             System.out.println(e);
  129.         }
  130.     }
  131.    
  132.     @FXML
  133.     private void openFile_buttonAction(ActionEvent event){
  134.         FileChooser fileChooser = new FileChooser();
  135.         File file = fileChooser.showOpenDialog(null);
  136.         try{
  137.             DataInputStream fileStream = new DataInputStream(new FileInputStream(file));
  138.             model.readFromJson(fileStream);
  139.             updateList();
  140.         }
  141.         catch(IOException e){
  142.            
  143.         }
  144.     }
  145.    
  146.     private void setDescLabel(String desc){
  147.         String splitted = splitStringByMaxLineLength(desc);
  148.         desc_label.setText(splitted);
  149.     }
  150.     private String splitStringByMaxLineLength(String in) {
  151.         Scanner scanner = new Scanner(in);
  152.         String dividedDesc = new String();
  153.  
  154.         String ret = new String();
  155.         while (scanner.hasNextLine()) {
  156.             String line = scanner.nextLine();
  157.             if (line.length() <= maxCharactersInLine) {
  158.                 ret += line + '\n';
  159.             } else {
  160.                 boolean newLineWasSet = false;
  161.                 char lineChars[] = line.toCharArray();
  162.                 for (int i = 1;
  163.                         i < maxCharactersToFindNewLine && !newLineWasSet;
  164.                         i++) {
  165.                     if (line.charAt(maxCharactersInLine - i) == ' ') {
  166.                         lineChars[maxCharactersInLine - i] = '\n';
  167.                         line = new String(lineChars);
  168.                         newLineWasSet = true;
  169.                     }
  170.                 }
  171.                 if (!newLineWasSet){
  172.                        line = putChar(line, '\n', maxCharactersInLine);
  173.                 }
  174.                 ret += splitStringByMaxLineLength(line);
  175.             }
  176.         }
  177.         scanner.close();
  178.         return ret;
  179.     }
  180.     private String putChar(String in, char character, int pos) {
  181.         String ret;
  182.         char[] updatedArr = new char[in.length() + 1];
  183.         in.getChars(0, maxCharactersInLine, updatedArr, 0);
  184.         updatedArr[pos] = character;
  185.         in.getChars(pos, in.length(), updatedArr, pos + 1);
  186.         ret = new String(updatedArr);
  187.         return ret;
  188.     }
  189.  
  190. }
  191.  
RAW Paste Data