-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.java
More file actions
180 lines (167 loc) · 7.5 KB
/
Copy pathTextEditor.java
File metadata and controls
180 lines (167 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.example.texteditor;
import javafx.collections.ObservableList;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Stack;
/*
* File: Main.java
* Description: A text editor with style change,
* undo and redo functions using stacks.
* Authors:
* - Pavel Sushkov
* Copyright: (c) 2024 Pavel Sushkov
* License: This file is licensed under the MIT License.
*/
public class TextEditor {
/** caves the content of the given TextArea to a file selected by the user.
* @param textArea the textArea containing the text to be saved. This represents the text field of the editor.
* @param stage the stage object representing the current window or dialog. This is used to display the file chooser dialog.
*/
protected void onSave(TextArea textArea, Stage stage) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Text File");
//restriction on saving in text format only
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt"));
File file = fileChooser.showSaveDialog(stage);
if (file != null) {
// reading from a text field to a file
try (FileWriter writer = new FileWriter(file)) {
writer.write(textArea.getText());
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void onClose() {
System.exit(0);
}
/**
* clears the content of the given TextArea.
* @param textArea the textArea to clear. This represents the text field of the editor.
*/
protected void onNew(TextArea textArea) { textArea.clear(); }
/**
* clearing the text field
* @param textArea to read the written text
* @param myStack the elements are written character by character
*/
protected void onUndoClick(TextArea textArea, Stack myStack) {
if (!textArea.getText().isEmpty()) {
//look through the text,adding each character to the stack
for (int i = 0; i < textArea.getText().length(); i++) {
myStack.push(textArea.getText().charAt(i));
}
//clearing the field at the end
textArea.clear();
}
}
/**
* canceling the cleaning operation
* @param myStack stack with saved data from the undo method
* @param redoResult to write to the text field
* @param textArea The TextArea where the result of the redo operation will be displayed. This represents the text field of the editor.
*/
protected void onRedoClick(Stack myStack,String redoResult, TextArea textArea) {
StringBuilder sbRedo = new StringBuilder();
if(!myStack.isEmpty()) {
while (!myStack.isEmpty()) {
sbRedo.append(myStack.pop());
}
//writing the stack elements to a string in reverse
redoResult = sbRedo.reverse().toString();
//add result
textArea.setText(redoResult);
}
}
/**
* initialization colors
* @param myColor choice box
* @param colors lots of colors
* @param textArea where the text is displayed. The selected font color from the choice box will be applied to this text area.
*/
protected void initColors(ChoiceBox <String> myColor, ObservableList <String> colors,TextArea textArea) {
myColor.setValue("Black");
myColor.setItems(colors);
myColor.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
onChangeFontColor(newValue, textArea);
});
}
/**
* initialization fonts
* @param myFontStyle choice box
* @param fonts lots of fonts
* @param textArea where the text is displayed. The selected font style from the choice box will be applied to this text area.
*/
protected void initFonts(ChoiceBox <String> myFontStyle, ObservableList <String> fonts,TextArea textArea) {
myFontStyle.setValue("System");
myFontStyle.setItems(fonts);
myFontStyle.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
onChangeFontStyle(newValue, textArea);
});
}
/**
* initialization sizes
* @param myFontSize choice box
* @param sizes lots of sizes
* @param textArea where the text is displayed. The selected font size from the choice box will be applied to this text area.
*/
protected void initSize(ChoiceBox <String> myFontSize, ObservableList <String> sizes,TextArea textArea) {
myFontSize.setValue("12");
myFontSize.setItems(sizes);
myFontSize.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
onChangeFontSize(newValue, textArea);
});
}
/**
* change color font
* @param myColor color from choice box
* @param textArea where the text is displayed. The font color will be changed according to the selected color.
*/
protected void onChangeFontColor(String myColor, TextArea textArea) {
switch (myColor) {
case "Yellow" -> textArea.setStyle("-fx-text-fill: yellow;");
case "Red" -> textArea.setStyle("-fx-text-fill: red;");
case "Green" -> textArea.setStyle("-fx-text-fill: green;");
case "Blue" -> textArea.setStyle("-fx-text-fill: blue;");
default -> textArea.setStyle("-fx-text-fill: black;");
}
}
/**
* change font
* @param myFontStyle fonts from choice box
* @param textArea where the text is displayed. The font style will be changed according to the selected font.
*/
protected void onChangeFontStyle (String myFontStyle, TextArea textArea) {
switch (myFontStyle) {
case "Calibri" -> textArea.setStyle("-fx-font-family: \"Calibri\";");
case "Century" -> textArea.setStyle("-fx-font-family: \"Century\";");
case "Montserrat" -> textArea.setStyle("-fx-font-family: \"Montserrat\";");
case "Georgia" -> textArea.setStyle("-fx-font-family: \"Georgia\";");
case "Lucida console" -> textArea.setStyle("-fx-font-family: \"Lucida console\";");
default -> textArea.setStyle("-fx-font-family: \"System\";");
}
}
/**
* change font size
* @param myFontSize size of fonts from choice box
* @param textArea where the text is displayed. The font size will be changed according to the selected size.
*/
protected void onChangeFontSize(String myFontSize, TextArea textArea) {
switch (myFontSize) {
case "8" -> textArea.setStyle("-fx-font-size: 8px");
case "12" -> textArea.setStyle("-fx-font-size: 12px");
case "20" -> textArea.setStyle("-fx-font-size: 20px");
case "24" -> textArea.setStyle("-fx-font-size: 24px");
case "36" -> textArea.setStyle("-fx-font-size: 36px");
case "48" -> textArea.setStyle("-fx-font-size: 48px");
case "64" -> textArea.setStyle("-fx-font-size: 64px");
case "128" -> textArea.setStyle("-fx-font-size: 128px");
default -> textArea.setStyle("-fx-font-size: 16px");
}
}
}