From 610411fc22a1a30fe7edb86d4a23054c4e2e37ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Martinussen?= Date: Mon, 1 Mar 2021 09:21:28 +0100 Subject: [PATCH] Filetreefix --- src/main/java/app/MainController.java | 19 +-- .../app/controllers/EditorController.java | 39 +++-- .../app/controllers/FiletreeController.java | 154 +++++++++++++----- .../app/controllers/MenubarController.java | 51 +++--- .../java/app/events/FileSelectedEvent.java | 15 ++ src/main/java/app/events/OpenFileEvent.java | 15 ++ .../java/app/events/OpenProjectEvent.java | 14 ++ src/main/java/app/model/Model.java | 24 ++- src/main/java/service/FileOperations.java | 5 + .../resources/fxml/components/Filetree.fxml | 2 +- src/main/resources/graphics/placeholder.png | Bin 0 -> 297 bytes 11 files changed, 235 insertions(+), 103 deletions(-) create mode 100644 src/main/java/app/events/FileSelectedEvent.java create mode 100644 src/main/java/app/events/OpenFileEvent.java create mode 100644 src/main/java/app/events/OpenProjectEvent.java create mode 100644 src/main/java/service/FileOperations.java create mode 100644 src/main/resources/graphics/placeholder.png diff --git a/src/main/java/app/MainController.java b/src/main/java/app/MainController.java index 7809375..facfb25 100644 --- a/src/main/java/app/MainController.java +++ b/src/main/java/app/MainController.java @@ -40,24 +40,20 @@ public class MainController implements Initializable { this.eventBus = new EventBus(); this.eventBus.register(this); - List.of( - editorController, - filetreeController, - modelineController, - menubarController - ).forEach(c -> c.setEventBus(this.eventBus)); + List.of(editorController, filetreeController, modelineController, menubarController) + .forEach(c -> c.setEventBus(this.eventBus)); } /** * Get the global eventbus. + * * @return The eventbus */ public EventBus getEventBus() { return this.eventBus; } - public HostServices getHostServices() { return hostServices; } @@ -67,16 +63,14 @@ public class MainController implements Initializable { } private void setCSSAt(int position, String cssPath) { - String nextStyleSheet = - getClass() - .getResource(cssPath) - .toExternalForm(); - + String nextStyleSheet = getClass().getResource(cssPath).toExternalForm(); + Model.getScene().getStylesheets().set(position, nextStyleSheet); } /** * Change the CSS according to which language is being used. + * * @param event */ @Subscribe @@ -96,6 +90,7 @@ public class MainController implements Initializable { /** * Handle an exit request for the whole program. + * * @param event */ @Subscribe diff --git a/src/main/java/app/controllers/EditorController.java b/src/main/java/app/controllers/EditorController.java index 0ab4f7a..073798a 100644 --- a/src/main/java/app/controllers/EditorController.java +++ b/src/main/java/app/controllers/EditorController.java @@ -1,8 +1,11 @@ package app.controllers; +import java.io.File; +import java.io.FileNotFoundException; import java.net.URL; import java.util.Collection; import java.util.ResourceBundle; +import java.util.Scanner; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; @@ -23,6 +26,7 @@ import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; import app.events.UndoEvent; import app.events.FileSaveStateChangedEvent; +import app.events.FileSelectedEvent; import app.model.Model; import app.service.LanguageOperations; import javafx.fxml.FXML; @@ -43,9 +47,7 @@ public class EditorController implements Initializable, Controller { public void initialize(URL url, ResourceBundle resourceBundle) { editor.setParagraphGraphicFactory(LineNumberFactory.get(editor)); - editor - .textProperty() - .addListener((obs, oldV, newV) -> this.editorChanged()); + editor.textProperty().addListener((obs, oldV, newV) -> this.editorChanged()); } @Override @@ -71,8 +73,8 @@ public class EditorController implements Initializable, Controller { } /** - * Uses Model.language to determine whether the current line/selection - * is commented or not, and toggles the comment. + * Uses Model.language to determine whether the current line/selection is + * commented or not, and toggles the comment. */ private void toggleComment() { if (editor.getSelectedText().equals("")) { @@ -84,13 +86,7 @@ public class EditorController implements Initializable, Controller { else newText = Model.getLanguage().commentLine(currentLine); - editor.replaceText( - editor.getCurrentParagraph(), - 0, - editor.getCurrentParagraph(), - currentLine.length(), - newText - ); + editor.replaceText(editor.getCurrentParagraph(), 0, editor.getCurrentParagraph(), currentLine.length(), newText); } else { // Comment selection @@ -105,7 +101,7 @@ public class EditorController implements Initializable, Controller { } } - private void setWrapText(boolean isWrapText) { + private void setWrapText(boolean isWrapText) { this.editor.setWrapText(isWrapText); } @@ -125,8 +121,24 @@ public class EditorController implements Initializable, Controller { /** * Refreshes the editor whenever the language is changed. + * * @param event */ + + @Subscribe + private void handle(FileSelectedEvent event) { + try { + Scanner sc = new Scanner(new File(event.getPath())); + editor.clear(); + while (sc.hasNextLine()) { + editor.appendText(sc.nextLine()); + editor.appendText("\n"); + } + } catch (FileNotFoundException ex) { + System.out.println(event.getPath()); + } + } + @Subscribe private void handle(LanguageChangedEvent event) { this.refreshHighlighting(); @@ -134,6 +146,7 @@ public class EditorController implements Initializable, Controller { /** * Toggles a language specific comment of the line/selection + * * @param event */ @Subscribe diff --git a/src/main/java/app/controllers/FiletreeController.java b/src/main/java/app/controllers/FiletreeController.java index 74b4536..b4aa0a3 100644 --- a/src/main/java/app/controllers/FiletreeController.java +++ b/src/main/java/app/controllers/FiletreeController.java @@ -2,67 +2,43 @@ package app.controllers; import javafx.fxml.FXML; import javafx.scene.control.CheckBoxTreeItem; +import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; -import javafx.scene.control.cell.CheckBoxTreeCell; import javafx.scene.image.Image; import javafx.scene.image.ImageView; -import jdk.jshell.execution.Util; +import javafx.scene.input.MouseEvent; import java.io.File; import java.net.URL; +import java.util.ArrayList; +import java.util.List; import java.util.ResourceBundle; import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import app.events.FileSelectedEvent; +import app.events.OpenProjectEvent; import app.model.Model; import javafx.fxml.Initializable; public class FiletreeController implements Initializable, Controller { private EventBus eventBus; - private Model model; - - @FXML - private TreeView filetree; + // Creating the images for the icons. Image folder = new Image(getClass().getResourceAsStream("/graphics/folder.png")); Image md = new Image(getClass().getResourceAsStream("/graphics/md.png")); Image java = new Image(getClass().getResourceAsStream("/graphics/java.png")); + Image placeholder = new Image(getClass().getResourceAsStream("/graphics/placeholder.png")); - public void generateTree(File file, CheckBoxTreeItem parent) { - - if (file.isDirectory()) { - CheckBoxTreeItem element = new CheckBoxTreeItem(file.getName(), new ImageView(folder)); - parent.getChildren().add(element); - - for (File f : file.listFiles()) { - generateTree(f, element); - } - } else if ("java".equals(file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()))) { - parent.getChildren().add(new CheckBoxTreeItem<>(file.getName(), new ImageView(java))); - } else if ("md".equals(file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()))) { - parent.getChildren().add(new CheckBoxTreeItem<>(file.getName(), new ImageView(md))); - } - } - - public void showTree(String inputChosen) { - CheckBoxTreeItem root = new CheckBoxTreeItem(inputChosen); - filetree.setShowRoot(false); - File fileInputChosen = new File(inputChosen); - File fileList[] = fileInputChosen.listFiles(); - - for (File f : fileList) { - generateTree(f, root); - } - - filetree.setRoot(root); - } + // Creating the variable. + @FXML + private TreeView filetree; @Override public void initialize(URL url, ResourceBundle resourceBundle) { - // TODO: implement - showTree("C:\\Users\\Oyste\\OneDrive\\Skrivebord\\test"); - + // } @Override @@ -72,12 +48,106 @@ public class FiletreeController implements Initializable, Controller { } /** - * Links the controller to the global model - * - * @param model The model to be linked + * The displaying of the fileTree. The inputChosen(the path) is aquired from the + * eventBus (OpeFileProjectEvent). The root is created as a CheckBoxItems and + * sends it to generateTree, and after that setting it to the root. */ - public void setModel(Model model) { - this.model = model; + private void showTree(String inputChosen) { + CheckBoxTreeItem root = new CheckBoxTreeItem<>(inputChosen); + filetree.setShowRoot(false); + File fileInputChosen = new File(inputChosen); + + generateTree(fileInputChosen, root); + + filetree.setRoot(root); + } + + /** + * 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. + */ + private void generateTree(File file, CheckBoxTreeItem parent) { + + if (file.isDirectory()) { + CheckBoxTreeItem element = new CheckBoxTreeItem<>(file.getName(), new ImageView(folder)); + parent.getChildren().add(element); + + List dirList = new ArrayList<>(); + List fileList = new ArrayList<>(); + + sortFiles(dirList, fileList, file); + + for (File f : dirList) { + generateTree(f, element); + } + + } else { + checkExtensions(file, parent); + } + } + + /** + * A helping function to sort the files/folders in the fileTree so that it shows + * in the correct order. + */ + private void sortFiles(List dirList, List fileList, File file) { + for (File f : file.listFiles()) { + if (f.isDirectory()) + dirList.add(f); + else { + fileList.add(f); + } + + } + dirList.addAll(fileList); + + } + + /** + * A help function to check if the extensions match .java or.md to insert the + * spesific icons and sending it to another help funtion createExtension to + * create the new CheckboxTreeItem that will add to the parent. + */ + private void checkExtensions(File file, CheckBoxTreeItem parent) { + String name = file.getName(); + String ext = (name.substring(file.getName().lastIndexOf(".") + 1, file.getName().length())); + + if ("java".equals(ext)) + createExtension(name, java, parent); + else if ("md".equals(ext)) + createExtension(name, md, parent); + else + createExtension(name, placeholder, parent); + } + + private void createExtension(String name, Image image, CheckBoxTreeItem parent) { + CheckBoxTreeItem element = new CheckBoxTreeItem<>(name, new ImageView(image)); + parent.getChildren().add(element); + } + + @FXML + private void handleMouseClick(MouseEvent event) { + if (event.getClickCount() == 2) { + TreeItem item = filetree.getSelectionModel().getSelectedItem(); + + String root = Model.getProjectPath().getFileName().toString(); + String path = ""; + while (!root.equals(item.getValue())) { + path = File.separator + item.getValue() + path; + item = item.getParent(); + } + + path = Model.getProjectPath() + path; + this.eventBus.post(new FileSelectedEvent(path)); + } + } + + @Subscribe + private void handle(OpenProjectEvent event) { + this.showTree(event.getPath()); } } diff --git a/src/main/java/app/controllers/MenubarController.java b/src/main/java/app/controllers/MenubarController.java index 7fa391f..19b45be 100644 --- a/src/main/java/app/controllers/MenubarController.java +++ b/src/main/java/app/controllers/MenubarController.java @@ -10,14 +10,17 @@ import com.google.common.eventbus.Subscribe; import app.events.CopyEvent; import app.events.CutEvent; import app.events.ExitApplicationEvent; +import app.events.FileSelectedEvent; import app.events.LanguageChangedEvent; import app.events.OpenLinkInBrowserEvent; +import app.events.OpenProjectEvent; import app.events.PasteEvent; import app.events.RedoEvent; import app.events.ThemeChangedEvent; import app.events.ToggleCommentEvent; import app.events.ToggleWrapTextEvent; import app.events.UndoEvent; +import app.model.Model; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -51,35 +54,34 @@ public class MenubarController implements Initializable, Controller { } /* ------------------------------------------------------------------------ */ - /* FILE */ + /* FILE */ /* ------------------------------------------------------------------------ */ @FXML - public String handleOpenFile() { + public void handleOpenFile() { FileChooser fc = new FileChooser(); fc.setTitle("Open File"); Stage stage = (Stage) menubar.getScene().getWindow(); File chosenFile = fc.showOpenDialog(stage); - String corFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\"); - System.out.println(chosenFile.getAbsolutePath()); - System.out.println(corFormat); + String correctFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\"); - return corFormat; + Model.setActiveFilePath(chosenFile.toPath()); + this.eventBus.post(new FileSelectedEvent(correctFormat)); } @FXML - public String handleOpenProject() { + private void handleOpenProject() { DirectoryChooser dc = new DirectoryChooser(); dc.setTitle("Open Project"); Stage stage = (Stage) menubar.getScene().getWindow(); File chosenDir = dc.showDialog(stage); - String corFormat = chosenDir.getAbsolutePath().replace("\\", "\\\\"); - System.out.println(chosenDir.getAbsolutePath()); - System.out.println(corFormat); + String correctFormat = chosenDir.getAbsolutePath().replace("\\", "\\\\"); + + Model.setProjectPath(chosenDir.toPath()); + this.eventBus.post(new OpenProjectEvent(correctFormat)); - return corFormat; } // @FXML @@ -98,13 +100,12 @@ public class MenubarController implements Initializable, Controller { /** * Handles the event where the language was change from the menu. + * * @param event */ @FXML private void handleLanguageChange(ActionEvent event) { - this.eventBus.post( - new LanguageChangedEvent( - ((RadioMenuItem) event.getSource()).getText())); + this.eventBus.post(new LanguageChangedEvent(((RadioMenuItem) event.getSource()).getText())); } @FXML @@ -115,13 +116,12 @@ public class MenubarController implements Initializable, Controller { @FXML private void handleThemeChange(ActionEvent event) { - this.eventBus.post( - new ThemeChangedEvent( - ((RadioMenuItem) event.getSource()).getText())); + this.eventBus.post(new ThemeChangedEvent(((RadioMenuItem) event.getSource()).getText())); } /** * Handles the event where there was an exit request from the menu. + * * @param event */ @FXML @@ -130,7 +130,7 @@ public class MenubarController implements Initializable, Controller { } /* ------------------------------------------------------------------------ */ - /* EDIT */ + /* EDIT */ /* ------------------------------------------------------------------------ */ @FXML @@ -164,7 +164,7 @@ public class MenubarController implements Initializable, Controller { } /* ------------------------------------------------------------------------ */ - /* ABOUT */ + /* ABOUT */ /* ------------------------------------------------------------------------ */ @FXML @@ -174,23 +174,18 @@ public class MenubarController implements Initializable, Controller { } /* ------------------------------------------------------------------------ */ - /* SUBSCRIPTIONS */ + /* SUBSCRIPTIONS */ /* ------------------------------------------------------------------------ */ /** * Updates menubuttons whenever the language is changed + * * @param event */ @Subscribe private void handle(LanguageChangedEvent event) { - this.languageToggleGroup - .getToggles() - .stream() - .map(RadioMenuItem.class::cast) - .filter(t -> t.getId().equals("toggle" + event.getLanguage())) - .findFirst() - .orElseThrow() - .setSelected(true); + this.languageToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast) + .filter(t -> t.getId().equals("toggle" + event.getLanguage())).findFirst().orElseThrow().setSelected(true); } } diff --git a/src/main/java/app/events/FileSelectedEvent.java b/src/main/java/app/events/FileSelectedEvent.java new file mode 100644 index 0000000..6328ce3 --- /dev/null +++ b/src/main/java/app/events/FileSelectedEvent.java @@ -0,0 +1,15 @@ +package app.events; + +public class FileSelectedEvent { + + private String path; + + public FileSelectedEvent(String path) { + this.path = path; + } + + public String getPath() { + return this.path; + } + +} diff --git a/src/main/java/app/events/OpenFileEvent.java b/src/main/java/app/events/OpenFileEvent.java new file mode 100644 index 0000000..86983c6 --- /dev/null +++ b/src/main/java/app/events/OpenFileEvent.java @@ -0,0 +1,15 @@ +package app.events; + +public class OpenFileEvent extends Event { + + private String path; + + public OpenFileEvent(String path) { + this.path = path; + } + + public String getPath() { + return this.path; + } + +} diff --git a/src/main/java/app/events/OpenProjectEvent.java b/src/main/java/app/events/OpenProjectEvent.java new file mode 100644 index 0000000..31bbbf7 --- /dev/null +++ b/src/main/java/app/events/OpenProjectEvent.java @@ -0,0 +1,14 @@ +package app.events; + +public class OpenProjectEvent extends Event { + + private String path; + + public OpenProjectEvent(String path) { + this.path = path; + } + + public String getPath() { + return this.path; + } +} diff --git a/src/main/java/app/model/Model.java b/src/main/java/app/model/Model.java index e2fbdc5..1f0a6ce 100644 --- a/src/main/java/app/model/Model.java +++ b/src/main/java/app/model/Model.java @@ -1,28 +1,38 @@ package app.model; +import java.nio.file.Path; + import javafx.scene.Scene; /** * Data model of the application. * - * Contains a static reference to state that has to be accessed - * by multiple pieces in the application, including the primary scene. + * Contains a static reference to state that has to be accessed by multiple + * pieces in the application, including the primary scene. */ public class Model { private static boolean fileIsSaved; - private static String activeFilePath; - private static String currentProjectPath; + private static Path activeFilePath; + private static Path currentProjectPath; private static ProgrammingLanguage currentProgrammingLanguage; private static Scene scene; - public static String getActiveFilePath() { + public static Path getActiveFilePath() { return activeFilePath; } - public static String getProjectPath() { + public static void setActiveFilePath(Path path) { + Model.activeFilePath = path; + } + + public static Path getProjectPath() { return currentProjectPath; } + public static void setProjectPath(Path path) { + Model.currentProjectPath = path; + } + public static ProgrammingLanguage getLanguage() { return currentProgrammingLanguage; } @@ -47,4 +57,4 @@ public class Model { Model.fileIsSaved = fileIsSaved; } -} +} \ No newline at end of file diff --git a/src/main/java/service/FileOperations.java b/src/main/java/service/FileOperations.java new file mode 100644 index 0000000..8472d57 --- /dev/null +++ b/src/main/java/service/FileOperations.java @@ -0,0 +1,5 @@ +package service; + +public class FileOperations { + +} diff --git a/src/main/resources/fxml/components/Filetree.fxml b/src/main/resources/fxml/components/Filetree.fxml index b02c3a5..6ea8001 100644 --- a/src/main/resources/fxml/components/Filetree.fxml +++ b/src/main/resources/fxml/components/Filetree.fxml @@ -11,6 +11,6 @@ fitToHeight="true"> + fx:id="filetree" onMouseClicked="#handleMouseClick"/> \ No newline at end of file diff --git a/src/main/resources/graphics/placeholder.png b/src/main/resources/graphics/placeholder.png new file mode 100644 index 0000000000000000000000000000000000000000..5c0aba0f41545b2c7b9f33be8753e01b66f920f3 GIT binary patch literal 297 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt`lJY5_^ zEPD4|-tWciC~*8Ee_Kddri(PU>%n{W{0X{t%}dw5^hux8zVQR2i%;Tor5oI0SwRsI z5{~P=x@JenSblGr(AYZj{=2&LwwtMr*Ie5?Tc<9JWl6~h@ws-uKX8u7BVU^b(&Z8Q z*87dr4=b)K?C0evXnwr^)GWdCADj5H7G5pvRF9bw@<)45qWK@aeN1e(C6+S0zrj^6 z^DF&GwZt+8-7kmUv!s}9lAEP@fNx&+?8N>X{*13Gj%r>jP<#K^<=+1+E(Z2b7i%Aw s>3u5$=|vhKlUFR%Lin0`WLC(x%1p00i_>zopr0Je~N$N&HU literal 0 HcmV?d00001