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
54 changes: 48 additions & 6 deletions src/editor/FileManager.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
package editor;

import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class FileManager {
public static void openFile(TextEditor textEditor, JTextArea textArea) {
private static int number = 10000;

public static void openFile(TextEditor textEditor, JTextArea textArea){
textArea.setText("");
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(textEditor) == JFileChooser.APPROVE_OPTION) {
//File file = fileChooser.getSelectedFile();
//TODO
File file = fileChooser.getSelectedFile();
textEditor.currentFile = file;
try {
Scanner scanner = new Scanner(textEditor.currentFile);
String line;
while ((line = scanner.nextLine()) != null){
textArea.append(line + "\n");
}
scanner.close();
} catch (FileNotFoundException e) {
e.getStackTrace();
}
}
}

public static void saveFile(TextEditor textEditor, JTextArea textArea) {
if (textEditor.currentFile == null){
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(textEditor) == JFileChooser.APPROVE_OPTION) {
//TODO
try {
PrintStream printStream = new PrintStream(fileChooser.getSelectedFile());
printStream.print(textArea.getText());
printStream.close();
} catch (FileNotFoundException e) {
e.getStackTrace();
}
}
}
else{
//TODO
try {
PrintStream printStream = new PrintStream(textEditor.currentFile);
printStream.print(textArea.getText());
printStream.close();
} catch (FileNotFoundException e) {
e.getStackTrace();
}
}
}

public static void newFile(TextEditor textEditor, JTextArea textArea) {
//TODO
File file = new File("newfile" + number + ".txt");
number++;
try {
if(file.createNewFile()){
textEditor.currentFile = file;
System.out.println("file created: " + file.getName());
textArea.setText("");
}else{
System.out.println("file already exists.");
}
} catch (IOException e) {
e.getStackTrace();
}
}
}