Merge branch 'filetreefix' into 'master'

Filetreefix

See merge request oysteikt/tdt4100-project-2021v!3
This commit is contained in:
Oystein Kristoffer Tveit 2021-03-01 08:21:28 +00:00
commit 35a370626f
11 changed files with 235 additions and 103 deletions

View File

@ -40,24 +40,20 @@ public class MainController implements Initializable {
this.eventBus = new EventBus(); this.eventBus = new EventBus();
this.eventBus.register(this); this.eventBus.register(this);
List.of( List.of(editorController, filetreeController, modelineController, menubarController)
editorController, .forEach(c -> c.setEventBus(this.eventBus));
filetreeController,
modelineController,
menubarController
).forEach(c -> c.setEventBus(this.eventBus));
} }
/** /**
* Get the global eventbus. * Get the global eventbus.
*
* @return The eventbus * @return The eventbus
*/ */
public EventBus getEventBus() { public EventBus getEventBus() {
return this.eventBus; return this.eventBus;
} }
public HostServices getHostServices() { public HostServices getHostServices() {
return hostServices; return hostServices;
} }
@ -67,16 +63,14 @@ public class MainController implements Initializable {
} }
private void setCSSAt(int position, String cssPath) { private void setCSSAt(int position, String cssPath) {
String nextStyleSheet = String nextStyleSheet = getClass().getResource(cssPath).toExternalForm();
getClass()
.getResource(cssPath)
.toExternalForm();
Model.getScene().getStylesheets().set(position, nextStyleSheet); Model.getScene().getStylesheets().set(position, nextStyleSheet);
} }
/** /**
* Change the CSS according to which language is being used. * Change the CSS according to which language is being used.
*
* @param event * @param event
*/ */
@Subscribe @Subscribe
@ -96,6 +90,7 @@ public class MainController implements Initializable {
/** /**
* Handle an exit request for the whole program. * Handle an exit request for the whole program.
*
* @param event * @param event
*/ */
@Subscribe @Subscribe

View File

@ -1,8 +1,11 @@
package app.controllers; package app.controllers;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL; import java.net.URL;
import java.util.Collection; import java.util.Collection;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Scanner;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
@ -23,6 +26,7 @@ import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent; import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent; import app.events.UndoEvent;
import app.events.FileSaveStateChangedEvent; import app.events.FileSaveStateChangedEvent;
import app.events.FileSelectedEvent;
import app.model.Model; import app.model.Model;
import app.service.LanguageOperations; import app.service.LanguageOperations;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -43,9 +47,7 @@ public class EditorController implements Initializable, Controller {
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
editor.setParagraphGraphicFactory(LineNumberFactory.get(editor)); editor.setParagraphGraphicFactory(LineNumberFactory.get(editor));
editor editor.textProperty().addListener((obs, oldV, newV) -> this.editorChanged());
.textProperty()
.addListener((obs, oldV, newV) -> this.editorChanged());
} }
@Override @Override
@ -71,8 +73,8 @@ public class EditorController implements Initializable, Controller {
} }
/** /**
* Uses Model.language to determine whether the current line/selection * Uses Model.language to determine whether the current line/selection is
* is commented or not, and toggles the comment. * commented or not, and toggles the comment.
*/ */
private void toggleComment() { private void toggleComment() {
if (editor.getSelectedText().equals("")) { if (editor.getSelectedText().equals("")) {
@ -84,13 +86,7 @@ public class EditorController implements Initializable, Controller {
else else
newText = Model.getLanguage().commentLine(currentLine); newText = Model.getLanguage().commentLine(currentLine);
editor.replaceText( editor.replaceText(editor.getCurrentParagraph(), 0, editor.getCurrentParagraph(), currentLine.length(), newText);
editor.getCurrentParagraph(),
0,
editor.getCurrentParagraph(),
currentLine.length(),
newText
);
} else { // Comment selection } 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); this.editor.setWrapText(isWrapText);
} }
@ -125,8 +121,24 @@ public class EditorController implements Initializable, Controller {
/** /**
* Refreshes the editor whenever the language is changed. * Refreshes the editor whenever the language is changed.
*
* @param event * @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 @Subscribe
private void handle(LanguageChangedEvent event) { private void handle(LanguageChangedEvent event) {
this.refreshHighlighting(); this.refreshHighlighting();
@ -134,6 +146,7 @@ public class EditorController implements Initializable, Controller {
/** /**
* Toggles a language specific comment of the line/selection * Toggles a language specific comment of the line/selection
*
* @param event * @param event
*/ */
@Subscribe @Subscribe

View File

@ -2,67 +2,43 @@ package app.controllers;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.CheckBoxTreeItem; import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView; import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import jdk.jshell.execution.Util; import javafx.scene.input.MouseEvent;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus; 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 app.model.Model;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
public class FiletreeController implements Initializable, Controller { public class FiletreeController implements Initializable, Controller {
private EventBus eventBus; 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 folder = new Image(getClass().getResourceAsStream("/graphics/folder.png"));
Image md = new Image(getClass().getResourceAsStream("/graphics/md.png")); Image md = new Image(getClass().getResourceAsStream("/graphics/md.png"));
Image java = new Image(getClass().getResourceAsStream("/graphics/java.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<String> parent) { // Creating the variable.
@FXML
if (file.isDirectory()) { private TreeView<String> filetree;
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<String>(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<String> root = new CheckBoxTreeItem<String>(inputChosen);
filetree.setShowRoot(false);
File fileInputChosen = new File(inputChosen);
File fileList[] = fileInputChosen.listFiles();
for (File f : fileList) {
generateTree(f, root);
}
filetree.setRoot(root);
}
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
// TODO: implement //
showTree("C:\\Users\\Oyste\\OneDrive\\Skrivebord\\test");
} }
@Override @Override
@ -72,12 +48,106 @@ public class FiletreeController implements Initializable, Controller {
} }
/** /**
* Links the controller to the global model * The displaying of the fileTree. The inputChosen(the path) is aquired from the
* * eventBus (OpeFileProjectEvent). The root is created as a CheckBoxItems and
* @param model The model to be linked * sends it to generateTree, and after that setting it to the root.
*/ */
public void setModel(Model model) { private void showTree(String inputChosen) {
this.model = model; CheckBoxTreeItem<String> 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<String> parent) {
if (file.isDirectory()) {
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<>(file.getName(), new ImageView(folder));
parent.getChildren().add(element);
List<File> dirList = new ArrayList<>();
List<File> 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<File> dirList, List<File> 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<String> that will add to the parent.
*/
private void checkExtensions(File file, CheckBoxTreeItem<String> 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<String> parent) {
CheckBoxTreeItem<String> element = new CheckBoxTreeItem<>(name, new ImageView(image));
parent.getChildren().add(element);
}
@FXML
private void handleMouseClick(MouseEvent event) {
if (event.getClickCount() == 2) {
TreeItem<String> 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());
} }
} }

View File

@ -10,14 +10,17 @@ import com.google.common.eventbus.Subscribe;
import app.events.CopyEvent; import app.events.CopyEvent;
import app.events.CutEvent; import app.events.CutEvent;
import app.events.ExitApplicationEvent; import app.events.ExitApplicationEvent;
import app.events.FileSelectedEvent;
import app.events.LanguageChangedEvent; import app.events.LanguageChangedEvent;
import app.events.OpenLinkInBrowserEvent; import app.events.OpenLinkInBrowserEvent;
import app.events.OpenProjectEvent;
import app.events.PasteEvent; import app.events.PasteEvent;
import app.events.RedoEvent; import app.events.RedoEvent;
import app.events.ThemeChangedEvent; import app.events.ThemeChangedEvent;
import app.events.ToggleCommentEvent; import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent; import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent; import app.events.UndoEvent;
import app.model.Model;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -51,35 +54,34 @@ public class MenubarController implements Initializable, Controller {
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* FILE */ /* FILE */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
@FXML @FXML
public String handleOpenFile() { public void handleOpenFile() {
FileChooser fc = new FileChooser(); FileChooser fc = new FileChooser();
fc.setTitle("Open File"); fc.setTitle("Open File");
Stage stage = (Stage) menubar.getScene().getWindow(); Stage stage = (Stage) menubar.getScene().getWindow();
File chosenFile = fc.showOpenDialog(stage); File chosenFile = fc.showOpenDialog(stage);
String corFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\"); String correctFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\");
System.out.println(chosenFile.getAbsolutePath());
System.out.println(corFormat);
return corFormat; Model.setActiveFilePath(chosenFile.toPath());
this.eventBus.post(new FileSelectedEvent(correctFormat));
} }
@FXML @FXML
public String handleOpenProject() { private void handleOpenProject() {
DirectoryChooser dc = new DirectoryChooser(); DirectoryChooser dc = new DirectoryChooser();
dc.setTitle("Open Project"); dc.setTitle("Open Project");
Stage stage = (Stage) menubar.getScene().getWindow(); Stage stage = (Stage) menubar.getScene().getWindow();
File chosenDir = dc.showDialog(stage); File chosenDir = dc.showDialog(stage);
String corFormat = chosenDir.getAbsolutePath().replace("\\", "\\\\"); String correctFormat = chosenDir.getAbsolutePath().replace("\\", "\\\\");
System.out.println(chosenDir.getAbsolutePath());
System.out.println(corFormat); Model.setProjectPath(chosenDir.toPath());
this.eventBus.post(new OpenProjectEvent(correctFormat));
return corFormat;
} }
// @FXML // @FXML
@ -98,13 +100,12 @@ public class MenubarController implements Initializable, Controller {
/** /**
* Handles the event where the language was change from the menu. * Handles the event where the language was change from the menu.
*
* @param event * @param event
*/ */
@FXML @FXML
private void handleLanguageChange(ActionEvent event) { private void handleLanguageChange(ActionEvent event) {
this.eventBus.post( this.eventBus.post(new LanguageChangedEvent(((RadioMenuItem) event.getSource()).getText()));
new LanguageChangedEvent(
((RadioMenuItem) event.getSource()).getText()));
} }
@FXML @FXML
@ -115,13 +116,12 @@ public class MenubarController implements Initializable, Controller {
@FXML @FXML
private void handleThemeChange(ActionEvent event) { private void handleThemeChange(ActionEvent event) {
this.eventBus.post( this.eventBus.post(new ThemeChangedEvent(((RadioMenuItem) event.getSource()).getText()));
new ThemeChangedEvent(
((RadioMenuItem) event.getSource()).getText()));
} }
/** /**
* Handles the event where there was an exit request from the menu. * Handles the event where there was an exit request from the menu.
*
* @param event * @param event
*/ */
@FXML @FXML
@ -130,7 +130,7 @@ public class MenubarController implements Initializable, Controller {
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* EDIT */ /* EDIT */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
@FXML @FXML
@ -164,7 +164,7 @@ public class MenubarController implements Initializable, Controller {
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* ABOUT */ /* ABOUT */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
@FXML @FXML
@ -174,23 +174,18 @@ public class MenubarController implements Initializable, Controller {
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* SUBSCRIPTIONS */ /* SUBSCRIPTIONS */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/** /**
* Updates menubuttons whenever the language is changed * Updates menubuttons whenever the language is changed
*
* @param event * @param event
*/ */
@Subscribe @Subscribe
private void handle(LanguageChangedEvent event) { private void handle(LanguageChangedEvent event) {
this.languageToggleGroup this.languageToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast)
.getToggles() .filter(t -> t.getId().equals("toggle" + event.getLanguage())).findFirst().orElseThrow().setSelected(true);
.stream()
.map(RadioMenuItem.class::cast)
.filter(t -> t.getId().equals("toggle" + event.getLanguage()))
.findFirst()
.orElseThrow()
.setSelected(true);
} }
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,28 +1,38 @@
package app.model; package app.model;
import java.nio.file.Path;
import javafx.scene.Scene; import javafx.scene.Scene;
/** /**
* Data model of the application. * Data model of the application.
* *
* Contains a static reference to state that has to be accessed * Contains a static reference to state that has to be accessed by multiple
* by multiple pieces in the application, including the primary scene. * pieces in the application, including the primary scene.
*/ */
public class Model { public class Model {
private static boolean fileIsSaved; private static boolean fileIsSaved;
private static String activeFilePath; private static Path activeFilePath;
private static String currentProjectPath; private static Path currentProjectPath;
private static ProgrammingLanguage currentProgrammingLanguage; private static ProgrammingLanguage currentProgrammingLanguage;
private static Scene scene; private static Scene scene;
public static String getActiveFilePath() { public static Path getActiveFilePath() {
return activeFilePath; return activeFilePath;
} }
public static String getProjectPath() { public static void setActiveFilePath(Path path) {
Model.activeFilePath = path;
}
public static Path getProjectPath() {
return currentProjectPath; return currentProjectPath;
} }
public static void setProjectPath(Path path) {
Model.currentProjectPath = path;
}
public static ProgrammingLanguage getLanguage() { public static ProgrammingLanguage getLanguage() {
return currentProgrammingLanguage; return currentProgrammingLanguage;
} }
@ -47,4 +57,4 @@ public class Model {
Model.fileIsSaved = fileIsSaved; Model.fileIsSaved = fileIsSaved;
} }
} }

View File

@ -0,0 +1,5 @@
package service;
public class FileOperations {
}

View File

@ -11,6 +11,6 @@
fitToHeight="true"> fitToHeight="true">
<content> <content>
<TreeView <TreeView
fx:id="filetree"/> fx:id="filetree" onMouseClicked="#handleMouseClick"/>
</content> </content>
</ScrollPane> </ScrollPane>

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 B