diff --git a/.idea/sonarlint/issuestore/5/d/5d837c4ed85ecaaf932c506e80ff5d7b9f3d590d b/.idea/sonarlint/issuestore/5/d/5d837c4ed85ecaaf932c506e80ff5d7b9f3d590d new file mode 100644 index 0000000..e69de29 diff --git a/.idea/sonarlint/issuestore/a/5/a5cc2925ca8258af241be7e5b0381edf30266302 b/.idea/sonarlint/issuestore/a/5/a5cc2925ca8258af241be7e5b0381edf30266302 new file mode 100644 index 0000000..e69de29 diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3121671..3b5411c 100644 --- a/pom.xml +++ b/pom.xml @@ -11,14 +11,24 @@ UTF-8 - 5.8.2 + 5.9.0 + + org.kordamp.ikonli + ikonli-typicons-pack + 12.3.1 + + + org.kordamp.ikonli + ikonli-javafx + 12.3.1 + org.openjfx javafx-controls - 17.0.1 + 18.0.2 org.openjfx @@ -30,11 +40,6 @@ controlsfx 11.1.1 - - org.kordamp.ikonli - ikonli-javafx - 12.3.0 - org.junit.jupiter junit-jupiter-api @@ -69,7 +74,7 @@ default-cli - com.c159251.a1.jtexteditor/com.c159251.a1.jtexteditor.HelloApplication + com.c159251.a1.jtexteditor/com.c159251.a1.jtexteditor.EditorLauncher app app diff --git a/src/main/java/com/c159251/a1/jtexteditor/EditorController.java b/src/main/java/com/c159251/a1/jtexteditor/EditorController.java new file mode 100644 index 0000000..74fdf9a --- /dev/null +++ b/src/main/java/com/c159251/a1/jtexteditor/EditorController.java @@ -0,0 +1,151 @@ +package com.c159251.a1.jtexteditor; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.MenuItem; +import javafx.scene.control.TextArea; +import javafx.scene.input.Clipboard; +import javafx.scene.input.ClipboardContent; +import javafx.scene.layout.AnchorPane; +import javafx.stage.FileChooser; +import java.text.SimpleDateFormat; +import java.util.Date; + +import java.io.*; + +/** This class is connected with the fxml config file and is responsible for the main program logic. **/ + +public class EditorController { + + public static File selectedFile; + public static Clipboard systemClipboard; + public MenuItem closeFile; + public MenuItem openFile; + public MenuItem saveFile; + public MenuItem saveFileAs; + public TextArea textPane; + public Button cutBtn; + public Button copyBtn; + public Button pasteBtn; + public Button selectBtn; + public int selectFrom; + public int selectTo; + public String copiedText; + public String cutText; + public Label dateTimeLabel; + public AnchorPane topAnchor; + + @FXML + public void initialize() { + systemClipboard = Clipboard.getSystemClipboard(); + //set date and time in menu + SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm"); + AnchorPane.setRightAnchor(dateTimeLabel,5.0); + dateTimeLabel.setText(formatter.format(new Date())); + + } + + // close file on 'close' button press + @FXML + protected void onFileClose(ActionEvent actionEvent) { + System.exit(0); + } + + // open txt file on 'open' button press + @FXML + public void onFileOpen(ActionEvent actionEvent) { + FileChooser fileChooser = new FileChooser(); + fileChooser.getExtensionFilters().add( + new FileChooser.ExtensionFilter("Text files (*.odt, *.txt)", "*.txt", "*.odt") + ); + fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + selectedFile = fileChooser.showOpenDialog(null); + if (selectedFile != null) { + loadTextFromFile(selectedFile); + } + } + + protected void loadTextFromFile(File fileToLoad) { + StringBuilder fileToText; + // load text in file using a buffered file reader + try (BufferedReader fileReader = new BufferedReader(new FileReader(fileToLoad))) { + // Load all lines of text, one by one, into a StringBuilder + String line; + fileToText = new StringBuilder(); + while ((line = fileReader.readLine()) != null) { + fileToText.append(line).append("\n"); + } + // if successfully loaded, populate textPane with file text + if (!fileToText.isEmpty()) { + textPane.setText(fileToText.toString()); + } + + } + catch (IOException e) { + e.printStackTrace(); + } + + } + + public void copyText() { + ClipboardContent content = new ClipboardContent(); + content.putString(textPane.getSelectedText()); + systemClipboard.setContent(content); + } + + public void cutText() { + ClipboardContent content = new ClipboardContent(); + String text = textPane.getSelectedText(); + selectFrom = textPane.getCaretPosition() - text.length(); + selectTo = textPane.getCaretPosition(); + textPane.deleteText(selectFrom, selectTo); + content.putString(text); + systemClipboard.setContent(content); + } + + public void pasteText() { + if (!systemClipboard.getString().isBlank()) { + textPane.insertText(textPane.getCaretPosition(), systemClipboard.getString()); + } + } + @FXML + protected void onFileSave() { + //if save is triggered with no stored file, then it should try as a 'save as' + if (selectedFile == null) { + onFileSaveAs(); + return; + } + saveTextToFile(selectedFile); + } + + @FXML + protected void onFileSaveAs() { + FileChooser fileChooser = new FileChooser(); + fileChooser.getExtensionFilters().add( + new FileChooser.ExtensionFilter("Text files (*.txt)", "*.txt") + ); + // if saved file is null, set to default directory + fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + // otherwise, set to parent directory of saved file + if (selectedFile != null) { + fileChooser.setInitialDirectory(selectedFile.getParentFile()); + } + selectedFile = fileChooser.showSaveDialog(null); + if (selectedFile != null) { + saveTextToFile(selectedFile); + } + } + + public void saveTextToFile(File fileToSave) { + //if the text is not blank, then write text to file + if (!textPane.getText().isBlank()) { + try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(fileToSave))) { + fileWriter.write(textPane.getText()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/c159251/a1/jtexteditor/EditorLauncher.java b/src/main/java/com/c159251/a1/jtexteditor/EditorLauncher.java new file mode 100644 index 0000000..2be536e --- /dev/null +++ b/src/main/java/com/c159251/a1/jtexteditor/EditorLauncher.java @@ -0,0 +1,32 @@ +package com.c159251.a1.jtexteditor; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.io.File; + +/** This class is solely responsible for launching the Application window. **/ + +public class EditorLauncher extends Application { + + @Override + public void start(Stage primaryStage) throws Exception { + FXMLLoader fxmlLoader = new FXMLLoader(EditorLauncher.class.getResource("jtexteditor-layout.fxml")); + Parent root = fxmlLoader.load(); + EditorController editorController = fxmlLoader.getController(); + editorController.initialize(); + Scene primaryScene = new Scene(root, 720, 480); + primaryStage.setTitle("Simple Text Editor"); + primaryStage.setScene(primaryScene); + primaryStage.show(); + } + + public static void main(String[] args) { + launch(); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/c159251/a1/jtexteditor/HelloApplication.java b/src/main/java/com/c159251/a1/jtexteditor/HelloApplication.java deleted file mode 100644 index 1739a95..0000000 --- a/src/main/java/com/c159251/a1/jtexteditor/HelloApplication.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.c159251.a1.jtexteditor; - -import javafx.application.Application; -import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; -import javafx.stage.Stage; - -import java.io.IOException; - -public class HelloApplication extends Application { - @Override - public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Hello!"); - stage.setScene(scene); - stage.show(); - } - - public static void main(String[] args) { - launch(); - } -} \ No newline at end of file diff --git a/src/main/java/com/c159251/a1/jtexteditor/HelloController.java b/src/main/java/com/c159251/a1/jtexteditor/HelloController.java deleted file mode 100644 index c0536f2..0000000 --- a/src/main/java/com/c159251/a1/jtexteditor/HelloController.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.c159251.a1.jtexteditor; - -import javafx.fxml.FXML; -import javafx.scene.control.Label; - -public class HelloController { - @FXML - private Label welcomeText; - - @FXML - protected void onHelloButtonClick() { - welcomeText.setText("Welcome to JavaFX Application!"); - } -} \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index d8cef45..cfe92a7 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -4,7 +4,7 @@ requires org.controlsfx.controls; requires org.kordamp.ikonli.javafx; - + requires org.kordamp.ikonli.typicons; opens com.c159251.a1.jtexteditor to javafx.fxml; exports com.c159251.a1.jtexteditor; } \ No newline at end of file diff --git a/src/main/resources/com/c159251/a1/jtexteditor/hello-view.fxml b/src/main/resources/com/c159251/a1/jtexteditor/hello-view.fxml deleted file mode 100644 index d34a567..0000000 --- a/src/main/resources/com/c159251/a1/jtexteditor/hello-view.fxml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - + +
+ + +