This commit is contained in:
Oystein Kristoffer Tveit 2021-04-26 22:07:54 +02:00
parent 9b2d70c876
commit c12e98f9be
3 changed files with 66 additions and 10 deletions

View File

@ -20,7 +20,7 @@ public class FileOperations {
*
* @param stage A JavaFX stage is required to show the dialog
* @return The chosen file
* @throws FileNotFoundException
* @throws FileNotFoundException if the dialog was canceled
*/
public static File openFileWithDialog(Stage stage) throws FileNotFoundException {
File chosenFile = DialogBoxes.showopenFileWithDialog(stage);
@ -33,11 +33,11 @@ public class FileOperations {
}
/**
* A function to get a file through a dialog
* A function to get directory through a dialog
*
* @param stage A JavaFX stage is required to show the dialog
* @return The chosen file
* @throws FileNotFoundException
* @return The chosen directory
* @throws FileNotFoundException if the dialog was canceled
*/
public static File openDirectoryWithDialog(Stage stage) throws FileNotFoundException {
File dir = DialogBoxes.showOpenDirectoryWithDialog(stage);
@ -48,6 +48,14 @@ public class FileOperations {
return dir;
}
/**
* Saves a file a the specified filepath with the specified content.
* Shows an error message to the user if the file was not found at the specified location.
*
* @param filepath The path of the file to save the content into
* @param content The text content to be saved
* @return Whether the file was sucessfully saved
*/
public static boolean saveFile(Path filepath, String content) {
try (PrintWriter writer = new PrintWriter(filepath.toFile())) {
writer.println(content);
@ -59,6 +67,14 @@ public class FileOperations {
}
}
/**
* Lets the user choose a file to save the specified content into
*
* @param stage A JavaFX stage is needed in order to show the file chooser
* @param content The content to be saved
*
* @return Whether the file was sucessfully saved
*/
public static boolean saveFileWithDialog(Stage stage, String content) {
File chosenLocation;
@ -78,6 +94,14 @@ public class FileOperations {
return false;
}
/**
* Tries to read the contents of a file in the specified filepath.
* If it fails, it shows the user a corresponding error message.
*
* @param filePath The path of the file to be read
*
* @return The text content of the file
*/
public static String readFile(Path filePath) {
if (filePath == null)

View File

@ -26,11 +26,11 @@ public class FiletreeOperations {
// TODO: Error check for recursiveness, and files without icons
/**
* The method to generate the fileTree recursively. If it is a directory a
* CheckBoxStringItem is created and the method is called again. It goes through
* all until every directory or file inside the orginal CheckBoxItem is made. If
* the item is a file it sends it to the help function checkExtension which is
* described below.
* Generate a filetree recursively, find icons for every file and append them to
* the root tree node.
*
* @param file The root directory to be at the top of the tree
* @param parent The root tree item to append children to
*/
public static void generateTree(File file, CheckBoxTreeItem<String> parent) {
@ -73,6 +73,10 @@ public class FiletreeOperations {
/**
* A helping function to sort the files/directories in the fileTree so that it shows
* in the correct order.
*
* @param dirList The list of directories to append all directories to
* @param fileList The list of files to append all the files to
* @param file The directory of which children to sort
*/
private static void sortFiles(List<File> dirList, List<File> fileList, File file) {
for (File f : file.listFiles()) {
@ -85,6 +89,14 @@ public class FiletreeOperations {
}
/**
* A function to get an icon for a file based on its mimetype.
* If no such icon is found, the function will return a default icon
*
* @param file The file to probe for a mimetype
* @return An image containing the icon
* @throws IOException if the default icon was not found. This should not happen
*/
private static Image getIconForFile(File file) throws IOException {
Image icon;
@ -113,6 +125,13 @@ public class FiletreeOperations {
return icon;
}
/**
* Determine the absolute path of a file within the filetree.
* @param item The treeitem that represents the file of which path is needed
* @return The absolute path of the file that the treeitem represents
* @throws FileNotFoundException if no path was found within the filetree such that the
* item could be connected with the root tree item. This should not happen
*/
public static Path getPathOfTreeItem(TreeItem<String> item) throws FileNotFoundException {
Path projectPath =
Model

View File

@ -15,6 +15,9 @@ import app.events.LanguageChangedEvent;
import app.events.ThemeChangedEvent;
import app.model.Model;
/**
* A provider for permanent storage of the settings of the application.
*/
public class SettingsProvider implements SettingsProviderI {
private EventBus eventBus;
@ -28,7 +31,7 @@ public class SettingsProvider implements SettingsProviderI {
Arrays.asList("Java", "Markdown", "Monokai", "Solarized Light");
// Only for testing purposes
/** Only for testing purposes */
protected void setSettingsPath(String settingsPath) {
this.settingsPath = settingsPath;
}
@ -86,11 +89,21 @@ public class SettingsProvider implements SettingsProviderI {
}
/**
* Update the settings whenever the theme gets changed
*
* @param event
*/
@Subscribe
private void handle(ThemeChangedEvent event) {
saveSettings();
}
/**
* Update the settings whenever the language gets changed
*
* @param event
*/
@Subscribe
private void handle(LanguageChangedEvent event) {
saveSettings();