-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainApp.java
More file actions
288 lines (220 loc) · 8.39 KB
/
MainApp.java
File metadata and controls
288 lines (220 loc) · 8.39 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package application;
import java.io.IOException;
import java.util.List;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
// called at start of application
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("TextProApp");
try {
// Load root layout from fxml
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
// min height and width calculated from components in TextAppLayout
primaryStage.setMinHeight(430);
primaryStage.setMinWidth(334);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
showTextProApp();
}
/**
* Shows the main TextApplication scene
*/
public void showTextProApp() {
try {
// Load the fxml file and set into the center of the main layout
FXMLLoader loader = new FXMLLoader(getClass().getResource("view/TextAppLayout.fxml"));
HBox textProPage = (HBox) loader.load();
rootLayout.setCenter(textProPage);
// Connect controller and main app
TextProController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
// SHOW NEW STAGE METHODS
/**
* Shows dialog for user input error
*
* @param inErr - message to dispaly
*/
public void showInputErrorDialog(String inErr) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Input Error");
alert.setContentText(inErr);
alert.showAndWait();
}
/**
* Displays dialog that allows user to select local text file to display in TextArea
*
* @param ta - reference to TextArea to display loaded text file
*
*/
public void showLoadFileDialog(AutoSpellingTextArea ta) {
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/LoadFileLayout.fxml"));
VBox page = (VBox) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Load File");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set reference to stage in controller
LoadFileDialogController controller = loader.getController();
controller.setDialogStage(dialogStage);
// give controller reference to text area to load file into
controller.setTextArea(ta);
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
public void showEditDistanceDialog(String selectedText) {
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/EditDistanceLayout.fxml"));
VBox page = (VBox) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Calculate Edit Distance");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set reference to stage in controller
EditDistanceDialogController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setMainApp(this);
controller.setField(selectedText);
// give controller reference to scene (cursor)
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
public void showEDResult(List<String> path) {
// intialize alert/dialog to display edit distance result
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Result");
alert.setHeaderText("Word Path : ");
alert.initModality(Modality.NONE);
alert.setResizable(true);
// create layout for content
VBox box = new VBox();
HBox midBox = new HBox();
box.setPadding(new Insets(35,0,35,0));
box.setSpacing(35);
midBox.setSpacing(15);
Label pathLabel = new Label();
Label numStepsLabel = new Label("Number of steps : ");
Label numSteps = new Label();
Font font = new Font(14);
pathLabel.setFont(font);
numStepsLabel.setFont(font);
numSteps.setFont(Font.font(font.getFamily(), FontWeight.BOLD, 14));
midBox.getChildren().add(numStepsLabel);
midBox.getChildren().add(numSteps);
midBox.setAlignment(Pos.CENTER);
box.getChildren().add(pathLabel);
box.getChildren().add(midBox);
box.setAlignment(Pos.CENTER);
alert.getDialogPane().setPrefWidth(300);
// check for path
if(path != null) {
numSteps.setText(Integer.toString(path.size()-1));
pathLabel.setText(String.join(" -> ", path));
Text text = new Text(pathLabel.getText());
text.setFont(font);
if(text.getLayoutBounds().getWidth() > 200) {
alert.getDialogPane().setPrefWidth(text.getLayoutBounds().getWidth()+100);
}
}
// no path found
else {
pathLabel.setText("No Path Found.");
numSteps.setText("N/A");
}
// set content and styling
alert.getDialogPane().setContent(box);
alert.getDialogPane().getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
alert.getDialogPane().getStyleClass().add("myDialog");
alert.showAndWait();
}
public void showMarkovDialog(textgen.MarkovTextGenerator mtg) {
try {
// Load the fxml file and create a new stage for the popup
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/MarkovLayout.fxml"));
BorderPane page = (BorderPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Markov Text Generator");
//dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set reference to stage in controller
//BUG -- when first displayed results don't show up until resize window
MarkovController controller = loader.getController();
//controller.setDialogStage(dialogStage);
controller.setMainApp(this);
controller.setMTG(mtg);
// Show the dialog and wait until the user closes it
dialogStage.showAndWait();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
public void showLoadStage(Stage loadStage, String text) {
loadStage.initModality(Modality.APPLICATION_MODAL);
loadStage.initOwner(primaryStage);
VBox loadVBox = new VBox(20);
loadVBox.setAlignment(Pos.CENTER);
Text tNode = new Text(text);
tNode.setFont(new Font(16));
loadVBox.getChildren().add(new HBox());
loadVBox.getChildren().add(tNode);
loadVBox.getChildren().add(new HBox());
Scene loadScene = new Scene(loadVBox, 300, 200);
loadStage.setScene(loadScene);
loadStage.show();
}
// MAIN
public static void main(String[] args) {
launch(args);
}
public Stage getStage() {
return this.primaryStage;
}
}