-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
400 lines (307 loc) · 12.7 KB
/
Main.java
File metadata and controls
400 lines (307 loc) · 12.7 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import simpleUi.Editor;
import simpleUi.SimpleEditor;
import java.nio.file.Files;
import arraylist.ArrayGenericList;
import arraylist.ListQueue;
import arraylist.ListStack;
import arraylist.Queue;
import arraylist.Stack;
import bst.BST;
import searchsort.SearchSort;
import genericlist.StepCounter;
import linkedlist.LinkedGenericList;
public class Main {
private static BST<String> wordTree = new BST<>();
public static void main(String[] args) {
Editor ui = new SimpleEditor("CS2 Text Editor");
Stack<String> undoStack =
new ListStack<String>(new ArrayGenericList<String>(new StepCounter()));
Stack<String> redoStack =
new ListStack<String>(new ArrayGenericList<String>(new StepCounter()));
Queue<String> printQueue =
new ListQueue<>(new ArrayGenericList<String>(new StepCounter()));
Queue<Long> printTimes =
new ListQueue<>(new ArrayGenericList<Long>(new StepCounter()));
// You will be adding your code within each one of these button actions.
// The "-> { ... }" syntax defines a Runnable (a block of code to run later).
// The block of code to be run is your implementation for that button.
// ANALYZE BUTTON
// Purpose: build data structures from current text and report analysis.
ui.addButton("Analyze", () -> {
wordTree = new BST<>();
StepCounter bstCounter = new StepCounter();
String text = ui.getText();
String[] words = text.split("\\s+");
for (String w : words) {
if (w == null || w.isEmpty()) continue;
w = w.toLowerCase();
w = w.replaceAll("^[^a-zA-Z]+", "");
w = w.replaceAll("[^a-zA-Z]+$", "");
if (w.isEmpty()) continue;
wordTree.insert(w, bstCounter);
}
int bstSize = wordTree.size();
int bstInsertSteps = bstCounter.get();
ui.alert(
"BST built successfully!\n\n" +
"Unique words in BST: " + bstSize + "\n" +
"BST insertion comparisons: " + bstInsertSteps
);
});
// REPLACE BUTTON
// Purpose: perform replacement and record steps for the operation.
ui.addButton("Replace (Case sensitive)", () -> {
String find = ui.prompt("Text to find:");
String replace = ui.prompt("Replace with:");
String oldText = ui.getText();
// Save undo before change
undoStack.push(oldText);
// Clear redo stack
while (!redoStack.isEmpty()) redoStack.pop();
String newText = oldText.replace(find, replace);
ui.setText(newText);
});
// CLEAR BUTTON
// Purpose: clear editor text and record previous state for undo once
// implemented.
ui.addButton("Clear", () -> {
String current = ui.getText();
// Save undo
undoStack.push(current);
// Clear redo stack
while (!redoStack.isEmpty()) redoStack.pop();
// Clear editor
ui.setText("");
});
// UNDO BUTTON
// Purpose: restore previous state using undo/redo stacks.
ui.addButton("Undo", () -> {
if (undoStack.isEmpty()) return;
String current = ui.getText();
// Move current to redo
redoStack.push(current);
// Restore last state
String prev = undoStack.pop();
ui.setText(prev);
});
// REDO BUTTON
// Purpose: reapply last undone change.
ui.addButton("Redo", () -> {
if (redoStack.isEmpty()) return;
String current = ui.getText();
// Save current to undo
undoStack.push(current);
// Apply redo
String restored = redoStack.pop();
ui.setText(restored);
});
// SEARCH - ArrayList
// Purpose: Use ArrayList and perform a linear search while counting steps
ui.addButton("Search ArrayList", () -> {
StepCounter counter = new StepCounter();
ArrayGenericList<String> list = new ArrayGenericList<>(counter);
String[] words = ui.getText().split("\\s+");
for (String w : words) {
if (w == null) continue;
w = w.toLowerCase();
w = w.replaceAll("^[^a-zA-Z]+", "");
w = w.replaceAll("[^a-zA-Z]+$", "");
if (w.isEmpty()) continue;
list.add(w);
}
String target = ui.prompt("Search for:");
if (target == null) return;
counter.reset();
int index = SearchSort.linearSearch(list, target, counter);
ui.alert("ArrayList Search\nFound at: " + index +
"\nSteps: " + counter.get());
});
// SEARCH - LinkedList
// Purpose: Use LinkedList and perform a linear search while counting steps
ui.addButton("Search LinkedList", () -> {
StepCounter counter = new StepCounter();
LinkedGenericList<String> list = new LinkedGenericList<>(counter);
// Build normalized list
String[] words = ui.getText().split("\\s+");
for (String w : words) {
if (w == null) continue;
w = w.toLowerCase();
w = w.replaceAll("^[^a-zA-Z]+", "");
w = w.replaceAll("[^a-zA-Z]+$", "");
if (w.isEmpty()) continue;
list.add(w);
}
// Normalize the search target
String target = ui.prompt("Search for:");
if (target == null) return;
target = target.toLowerCase();
target = target.replaceAll("^[^a-zA-Z]+", "");
target = target.replaceAll("[^a-zA-Z]+$", "");
if (target.isEmpty()) {
ui.alert("Search becomes empty after cleaning.");
return;
}
// Search
counter.reset();
int index = SearchSort.linearSearch(list, target, counter);
ui.alert("LinkedList Search\nFound at: " + index +
"\nSteps: " + counter.get());
});
// SEARCH - BST
// Purpose: USe BST and perform search while counting comparison steps.
ui.addButton("Search BST", () -> {
if (wordTree == null || wordTree.isEmpty()) {
ui.alert("BST is empty.\nRun Analyze first.");
return;
}
// get search query
String query = ui.prompt("Search word for BST:");
if (query == null) return;
// normalize like Analyze
query = query.toLowerCase();
query = query.replaceAll("^[^a-zA-Z]+", "");
query = query.replaceAll("[^a-zA-Z]+$", "");
if (query.isEmpty()) {
ui.alert("Search word becomes empty after cleaning.");
return;
}
// build fresh ArrayList + LinkedList from editor text
StepCounter arrayCounter = new StepCounter();
StepCounter linkedCounter = new StepCounter();
ArrayGenericList<String> arrayList = new ArrayGenericList<>(arrayCounter);
LinkedGenericList<String> linkedList = new LinkedGenericList<>(linkedCounter);
String[] textWords = ui.getText().split("\\s+");
for (String w : textWords) {
if (w == null) continue;
w = w.toLowerCase();
w = w.replaceAll("^[^a-zA-Z]+", "");
w = w.replaceAll("[^a-zA-Z]+$", "");
if (w.isEmpty()) continue;
arrayList.add(w);
linkedList.add(w);
}
// search arraylist
arrayCounter.reset();
int arrayIndex = SearchSort.linearSearch(arrayList, query, arrayCounter);
// search linkedlist
linkedCounter.reset();
int linkedIndex = SearchSort.linearSearch(linkedList, query, linkedCounter);
// search bst
StepCounter bstCounter = new StepCounter();
boolean bstFound = wordTree.contains(query, bstCounter);
ui.alert(
"Searching for: \"" + query + "\"\n\n" +
"ArrayList → index = " + arrayIndex +
" , comparisons = " + arrayCounter.get() + "\n" +
"LinkedList → index = " + linkedIndex +
" , comparisons = " + linkedCounter.get() + "\n" +
"BST → found = " + bstFound +
" , comparisons = " + bstCounter.get()
);
});
ui.addButton("Search and Highlight", () -> {
String term = ui.prompt("Highlight what word/phrase?");
if (term == null) return;
term = term.trim();
if (term.isEmpty()) {
ui.alert("Nothing to highlight.");
return;
}
ui.highlight(term);
});
// LOAD FILE BUTTON
// Purpose: load text from a file into the editor.
ui.addButton("Load File", () -> {
//add file explorer
javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
//sets window dialogue
chooser.setDialogTitle("Open .txt file (or java tbd)");
//set to files only
chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override public boolean accept(java.io.File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
//limits to only .txt files
}
@Override public String getDescription() {
return "Text files (*.txt)";
}
});
int result = chooser.showOpenDialog(null);
if (result != javax.swing.JFileChooser.APPROVE_OPTION) return;
java.io.File file = chooser.getSelectedFile();
if (file == null) return;
// Save undo before change
String oldText = ui.getText();
undoStack.push(oldText);
while (!redoStack.isEmpty()) redoStack.pop();
try {
String fileContent = java.nio.file.Files.readString(file.toPath());
//sets new text
ui.setText(fileContent);
} catch (Exception e) {
//changed catch since it doesnt need to be in project folder now
ui.alert("Error loading file: \n" + e.getMessage());
undoStack.pop(); // undo push because no change happened
}
});
ui.addButton("Save File", () -> {
javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
chooser.setDialogTitle("Save .txt file");
//set to files only
chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override public boolean accept(java.io.File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
}
@Override public String getDescription() {
return "Text files (*.txt)";
}
});
int result = chooser.showSaveDialog(null);
if (result != javax.swing.JFileChooser.APPROVE_OPTION) return;
java.io.File file = chooser.getSelectedFile();
if (file == null) return;
// Force .txt extension
String path = file.getAbsolutePath();
if (!path.toLowerCase().endsWith(".txt")) {
file = new java.io.File(path + ".txt");
//normalize and ensure file is saved as .txt
}
try {
java.nio.file.Files.writeString(file.toPath(), ui.getText());
ui.alert("File saved: \n" + file.getAbsolutePath());
//shows file directory
} catch (Exception e) {
ui.alert("Error saving file:\n" + e.getMessage());
}
});
ui.addButton("Compile", () -> {
ui.alert("Error: Compile not available yet.");
});
// Load demo text for testing.
ui.addButton("Load Demo", () -> ui.setText(
"If you’re going to try, go all the way.\n" +
"Otherwise, don’t even start.\n" +
"If you're going to try, go all the way.\n" +
"This could mean losing girlfriends, wives, relatives, jobs and maybe even your mind.\n" +
"It could mean not eating for three or four days.\n" +
"It could mean freezing on a park bench.\n" +
"It could mean jail.\n" +
"It could mean derision, mockery, isolation.\n" +
"Isolation is the gift.\n" +
"All the others are a test of your endurance, of how much you really want to do it.\n" +
"And, you’ll do it, despite rejection and the worst odds.\n" +
"And it will be better than anything else you can imagine.\n" +
"If you’re going to try, go all the way.\n" +
"There is no other feeling like that.\n\n" +
"You will be alone with the gods, and the nights will flame with fire.\n" +
"DO IT. DO IT. DO IT. All the way\n" +
"You will ride life straight to perfect laughter. It’s the only good fight there is.\n\n" +
"– Charles Bukowski"
));
ui.show();
}
}