Skip to content

Commit

Permalink
Merge pull request #1 from mmbi-glitch/text-editor
Browse files Browse the repository at this point in the history
Merge dev branch with main
  • Loading branch information
robsimpson authored Aug 15, 2022
2 parents 9633a0a + 06189fd commit bedaa76
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 62 deletions.
Empty file.
Empty file.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.8.2</junit.version>
<junit.version>5.9.0</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-typicons-pack</artifactId>
<version>12.3.1</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>12.3.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
<version>18.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
Expand All @@ -30,11 +40,6 @@
<artifactId>controlsfx</artifactId>
<version>11.1.1</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>12.3.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -69,7 +74,7 @@
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.c159251.a1.jtexteditor/com.c159251.a1.jtexteditor.HelloApplication
<mainClass>com.c159251.a1.jtexteditor/com.c159251.a1.jtexteditor.EditorLauncher
</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
Expand Down
151 changes: 151 additions & 0 deletions src/main/java/com/c159251/a1/jtexteditor/EditorController.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/c159251/a1/jtexteditor/EditorLauncher.java
Original file line number Diff line number Diff line change
@@ -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();
}


}
23 changes: 0 additions & 23 deletions src/main/java/com/c159251/a1/jtexteditor/HelloApplication.java

This file was deleted.

Loading

0 comments on commit bedaa76

Please sign in to comment.