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.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

View File

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

View File

@ -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<String> parent) {
if (file.isDirectory()) {
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);
}
// Creating the variable.
@FXML
private TreeView<String> 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<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.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;
@ -55,31 +58,30 @@ public class MenubarController implements Initializable, Controller {
/* ------------------------------------------------------------------------ */
@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
@ -179,18 +179,13 @@ public class MenubarController implements Initializable, Controller {
/**
* 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);
}
}

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

View File

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

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 B