-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mmbi-glitch/text-editor
Merge dev branch with main
- Loading branch information
Showing
11 changed files
with
392 additions
and
62 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/main/java/com/c159251/a1/jtexteditor/EditorController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/main/java/com/c159251/a1/jtexteditor/EditorLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/main/java/com/c159251/a1/jtexteditor/HelloApplication.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.