Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions src/editor/FileManager.java
Original file line number Diff line number Diff line change
@@ -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
}



}