diff --git a/src/editor/FileManager.java b/src/editor/FileManager.java index ee77200..171f3b4 100644 --- a/src/editor/FileManager.java +++ b/src/editor/FileManager.java @@ -1,29 +1,65 @@ package editor; - +import java.awt.*; +import java.io.*; +import java.util.Scanner; import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; public class FileManager { - public static void openFile(TextEditor textEditor, JTextArea textArea) { + private JTextArea textEditor; + private File currentFile; + + public FileManager (JTextArea textEditor){ + this.textEditor = textEditor; + } + + public void openFile (){ JFileChooser fileChooser = new JFileChooser(); - if (fileChooser.showOpenDialog(textEditor) == JFileChooser.APPROVE_OPTION) { - //File file = fileChooser.getSelectedFile(); - //TODO + fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt")); + int result = fileChooser.showOpenDialog(null); + + if(result == JFileChooser.APPROVE_OPTION){ + File selectFile = fileChooser.getSelectedFile(); + + try { + Scanner scannerFile = new Scanner(selectFile); + + while ( scannerFile.hasNextLine()){ + System.out.println(scannerFile.nextLine()); + } + } catch (FileNotFoundException e) { + throw new RuntimeException(e.getMessage()); + } + } + else{ + System.out.println("File not choose"); } } - - public static void saveFile(TextEditor textEditor, JTextArea textArea) { - if (textEditor.currentFile == null){ + // Method to save the current content to a file + public void saveFile(){ + if(currentFile == null){ JFileChooser fileChooser = new JFileChooser(); - if (fileChooser.showSaveDialog(textEditor) == JFileChooser.APPROVE_OPTION) { - //TODO + fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt")); + int result = fileChooser.showOpenDialog(null); + + if(result == JFileChooser.APPROVE_OPTION){ + currentFile = fileChooser.getSelectedFile(); } } - else{ - //TODO + + try { + PrintStream printFile = new PrintStream(currentFile); + printFile.println(textEditor.getText()); + } catch (FileNotFoundException e) { + throw new RuntimeException(e.getMessage()); } } - public static void newFile(TextEditor textEditor, JTextArea textArea) { - //TODO + public void newFile (){ + textEditor.setText(""); // Clear the editor's content + currentFile = null; // Reset the current file reference } + + + }