PR2WordCloud/src/main/java/domain/FileLoader.java

78 lines
2.5 KiB
Java

package domain;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;
public class FileLoader {
private File filePath;
private File stopwordsPath;
private String fileFormat;
private String stopwordFormat;
public FileLoader() {
this.filePath = null;
this.stopwordsPath = null;
this.fileFormat = "";
this.stopwordFormat = "";
}
//AI Method with swing FileChooser with changes to run in this program
public void loadFileGUI() {
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Text Files", "txt")); //selbst hinzugefügt
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Word Documents", "docx")); //selbst hinzugefügt
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PowerPoint Presentations", "pptx")); //selbst hinzugefügt
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
if (filePath == null) {
filePath = fileChooser.getSelectedFile();
fileFormat = getFileFormat(filePath);
} else {
stopwordsPath = fileChooser.getSelectedFile();
stopwordFormat = getFileFormat(stopwordsPath);
}
}
} catch (HeadlessException e) {
throw new RuntimeException(e);
}
}
//detect format from file for further processing
public String getFileFormat(File path) {
String fileName = path.getName();
String fileFormat = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";
switch (fileFormat.toLowerCase()) {
case "pdf":
return "pdf";
case "txt":
return "txt";
case "docx":
return "docx";
case "pptx":
return "pptx";
default:
return "File format not supported";
}
}
public File getFilePath() {
return filePath;
}
public File getStopwordsPath() {
return stopwordsPath;
}
public String getFileFormat() {
return fileFormat;
}
public String getStopwordFormat() {
return stopwordFormat;
}
}