-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkLunaGUI.java
More file actions
486 lines (391 loc) · 20.7 KB
/
WorkLunaGUI.java
File metadata and controls
486 lines (391 loc) · 20.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//Timestamp feature
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.web.WebView;
import javafx.scene.web.WebEngine;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.*;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javafx.scene.paint.Color;
import javafx.scene.control.ColorPicker;
import javax.swing.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javafx.scene.text.Text;
public class WorkLunaGUI extends Application {
private String currentFilePath = null;
private TabPane tabPane;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Eccentricity");
tabPane = new TabPane();
Tab firstTab = createTab("Tab 1");
tabPane.getTabs().add(firstTab);
// Create the "New", "Open", and "Save" buttons with icons
HBox buttonPanel = new HBox(10);
buttonPanel.setPadding(new javafx.geometry.Insets(10));
buttonPanel.setAlignment(Pos.CENTER_RIGHT);
Button addButton = createIconButton("New");
Button openButton = createIconButton("Open");
Button saveButton = createIconButton("Save");
Text path = new Text("Directory: "+currentFilePath);
final ColorPicker colorPicker = new ColorPicker();
colorPicker.setValue(Color.CORAL);
addButton.setOnAction(e -> addNewTab());
openButton.setOnAction(e -> openFile());
saveButton.setOnAction(e -> {
if (currentFilePath == null) {
currentFilePath = saveFile(getTextContentOfCurrentTab(), firstTab);
} else {
saveContentToFile(getTextContentOfCurrentTab(), currentFilePath);
}
});
buttonPanel.getChildren().addAll(path, colorPicker, addButton, openButton, saveButton);
// Set up the main layout with padding and alignment
BorderPane root = new BorderPane();
root.setCenter(tabPane);
root.setTop(buttonPanel);
root.setPadding(new javafx.geometry.Insets(10));
// Set up the scene with a background color
Scene scene = new Scene(root, 800, 600);
scene.setFill(javafx.scene.paint.Color.LIGHTGRAY);
// Add shortcut for saving the content
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.S && event.isControlDown()) {
if (currentFilePath == null) {
currentFilePath = saveFile(getTextContentOfCurrentTab(), firstTab);
} else {
saveContentToFile(getTextContentOfCurrentTab(), currentFilePath);
}
event.consume();
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
private Tab createTab(String tabName) {
Tab tab = new Tab(tabName);
// Use a simple approach to store currentFilePath in the tab's user data
tab.setUserData(new String[1]); // Array to store file path
//tab.setUserData(new String[] { null, path }); // Store file path and the Text element
VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER);
// Create the TextArea for user input with a border and padding
TextArea textArea = new TextArea("Enter text here...");
textArea.setFont(Font.font("Arial", 14));
textArea.setWrapText(true);
textArea.setStyle("-fx-border-color: #CCCCCC; -fx-border-width: 2; -fx-padding: 10; -fx-font-size: 22px;");
// Make TextArea grow to fill the available vertical space
VBox.setVgrow(textArea, Priority.ALWAYS);
// Create WebView to display HTML with some padding and alignment
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
File htmlFile = new File("HTML/computer.html"); // Provide the correct path
webEngine.load(htmlFile.toURI().toString());
// Make WebView grow to fill the available vertical space
VBox.setVgrow(webView, Priority.ALWAYS);
// Update the WebView dynamically based on TextArea content
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
String htmlContent = encoder(newValue);
webEngine.loadContent("<pre>" + htmlContent + "</pre>");
webEngine.getLoadWorker().stateProperty().addListener((_observable, oldState, newState) -> {
if (newState == javafx.concurrent.Worker.State.SUCCEEDED) {
// Execute JavaScript to scroll to the bottom
// Added a timeout to ensure content is fully rendered
webEngine.executeScript("setTimeout(function() { window.scrollTo(0, document.documentElement.scrollHeight); }, 0);");
}
});
});
// Create SplitPane with the TextArea and WebView
SplitPane splitPane = new SplitPane();
splitPane.getItems().addAll(textArea, webView);
// Set the divider position (this can be adjusted dynamically)
splitPane.setDividerPositions(0.5); // This splits the area equally
// Make the SplitPane grow to fill the available vertical space
VBox.setVgrow(splitPane, Priority.ALWAYS);
// Add the SplitPane to the tab content
vbox.getChildren().add(splitPane);
tab.setContent(vbox);
// Add close button to tab
tab.setClosable(true);
return tab;
}
public static String lightenColor(String colorInput, double factor) {
// Ensure the factor is between 0 and 1
factor = Math.min(1, Math.max(0, factor));
// If colorInput is null or empty, print an error message and return
if (colorInput == null || colorInput.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, " Color input cannot be null or empty.", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
Color color;
// Check if the input starts with '#', indicating it's a hex code
if (colorInput.startsWith("#")) {
// Remove '#' and check if it's a valid 6-character hex code
colorInput = colorInput.substring(1); // Remove '#'
if (colorInput.length() != 6) {
JOptionPane.showMessageDialog(null, " Invalid hex color format. It should be in the format #RRGGBB.", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
try {
// Parse the hex color code into RGB components
int r = Integer.parseInt(colorInput.substring(0, 2), 16);
int g = Integer.parseInt(colorInput.substring(2, 4), 16);
int b = Integer.parseInt(colorInput.substring(4, 6), 16);
color = Color.rgb(r, g, b);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, " Invalid hex color format. Could not parse the color components.", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} else {
// Treat input as a named color and convert it to a Color object
try {
color = Color.web(colorInput); // This handles named colors like "green", "red", etc.
} catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog(null, " Invalid color name: '" + colorInput + "'. Please provide a valid color name or hex code.", "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
// Get the RGB components from the Color object (normalized to 0-255)
double r = color.getRed() * 255;
double g = color.getGreen() * 255;
double b = color.getBlue() * 255;
// Lighten the color by increasing RGB components towards 255
r = Math.min(255, r + (255 - r) * factor);
g = Math.min(255, g + (255 - g) * factor);
b = Math.min(255, b + (255 - b) * factor);
// Convert the new RGB values back to a hex color string
return String.format("#%02X%02X%02X", (int) r, (int) g, (int) b);
}
private void addNewTab() {
String tabName = "Tab " + (tabPane.getTabs().size() + 1);
tabPane.getTabs().add(createTab(tabName));
}
private void openFile() {
FileChooser fileChooser = new FileChooser();
//fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt"));
//fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("HTML Files", "*.html", "*.htm"));
//fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown Files", "*.md"));
//fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Eccentric Files", "*.ecc"));
File file = fileChooser.showOpenDialog(null);
if (file != null) {
// Get the currently selected tab (active tab)
Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
if (selectedTab != null) {
// Update the tab name to the file name
selectedTab.setText(file.getName());
// Store the file path in the tab's user data
selectedTab.setUserData(file.getAbsolutePath());
// Load the file content into the tab
loadContentFromFile(file.getAbsolutePath(), selectedTab);
}
}
}
private void loadContentFromFile(String filePath, Tab tab) {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
content.append(line).append("\n");
}
if (tab.getContent() instanceof VBox) {
VBox contentBox = (VBox) tab.getContent();
if (contentBox.getChildren().size() > 0 && contentBox.getChildren().get(0) instanceof SplitPane) {
SplitPane splitPane = (SplitPane) contentBox.getChildren().get(0);
if (splitPane.getItems().size() > 0 && splitPane.getItems().get(0) instanceof TextArea) {
TextArea textArea = (TextArea) splitPane.getItems().get(0);
textArea.setText(content.toString());
}
}
}
} catch (IOException e) {
showError("Error opening file.");
}
}
// Method to save content to the selected file
private String saveFile(String content, Tab tab) {
JFileChooser fileChooser = new JFileChooser();
// Set the dialog title
fileChooser.setDialogTitle("Save As");
// Show the "Save As" dialog
int userSelection = fileChooser.showSaveDialog(null);
File file = null;
// Check if the user selected a file or canceled
if (userSelection == JFileChooser.APPROVE_OPTION) {
// Get the selected file
file = fileChooser.getSelectedFile();
String fileName = file.getName();
// Set the tab's title to the file name
tab.setText(fileName);
// Save the file path to the tab's user data
tab.setUserData(new String[] { file.getAbsolutePath() });
// Ensure file has the correct extension (if missing)
if (!file.getName().endsWith(".ecc")) {
file = new File(file.getAbsolutePath() + ".ecc");
}
} else {
JOptionPane.showMessageDialog(null, " Save operation was canceled.", "Error", JOptionPane.ERROR_MESSAGE);
return null; // If the user cancels, return null (no file saved)
}
try (FileWriter writer = new FileWriter(file)) {
writer.write(content);
showMessage("File saved successfully: " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
//private void saveContentToFile(String content) { }
private void saveContentToFile(String content, String filePath) {
File file = new File(filePath);
// Create a BufferedWriter to write the content to the file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
// Write content to the file
writer.write(content);
showMessage("Content written to file successfully.");
} catch (IOException e) {
showError("An error occurred while writing to the file: " + e.getMessage());
}
}
private String getTextContentOfCurrentTab() {
Tab currentTab = tabPane.getSelectionModel().getSelectedItem();
if (currentTab != null) {
// Check if the content of the current tab is a VBox
if (currentTab.getContent() instanceof VBox) {
VBox contentBox = (VBox) currentTab.getContent();
// Check if the first child of VBox is a SplitPane and it has children
if (!contentBox.getChildren().isEmpty() && contentBox.getChildren().get(0) instanceof SplitPane) {
SplitPane splitPane = (SplitPane) contentBox.getChildren().get(0);
// Check if the SplitPane has at least one child and the first child is a TextArea
if (!splitPane.getItems().isEmpty() && splitPane.getItems().get(0) instanceof TextArea) {
TextArea textArea = (TextArea) splitPane.getItems().get(0);
return textArea.getText(); // Return the text from the TextArea
} else {
showError("First item in SplitPane is not a TextArea or SplitPane has no items");
}
} else {
showError("First child of VBox is not a SplitPane or VBox has no children");
}
} else {
showError("Current tab's content is not a VBox");
}
}
return ""; // Return an empty string if none of the conditions are met
}
private String encoder(String textContent) {
Pattern pattern = Pattern.compile("<([a-zA-Z0-9#]+)=([a-zA-Z0-9#]+)>");
HashMap<String, String> variables = new HashMap<String, String>();
// Create a Matcher object
Matcher matcher = pattern.matcher(textContent);
// Find all matches
while (matcher.find()) {
// Access the first matching group (before the "=")
String key = matcher.group(1);
// Access the second matching group (after the "=")
String value = matcher.group(2);
// Output the matched key and value
System.out.println("Key: " + key + ", Value: " + value);
variables.put(key,value);
}
pattern = Pattern.compile("\\{([a-zA-Z0-9#]+)\\}");
matcher = pattern.matcher(textContent);
// Find all matches
while (matcher.find()) {
String key = matcher.group(1); // Get the matched key
if (variables.containsKey(key)) { // Check if the key exists in the variables map
// Replace the key with the corresponding value
textContent = textContent.replaceAll("\\{" + key + "\\}", variables.get(key));
}
}
textContent = "<head><link href='https://cdn.jsdelivr.net/npm/prismjs@1.25.0/themes/prism-tomorrow.css' rel='stylesheet' />" +
"<script src='https://cdn.jsdelivr.net/npm/prismjs@1.25.0/prism.js'></script>" +
"<script src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'></script>"+
"<style>*{margin:0;padding:0;}</style>"+ textContent;
textContent = textContent.replaceAll("<align:(\\w+)>", "<div style='text-align:$1;'>");
textContent = textContent.replaceAll("</align>", "</div>");
//textContent = textContent.replaceAll("<define:([a-zA-Z0-9#]+)>([a-zA-Z0-9#]+)</define>", "");
//textContent = textContent.replaceAll("</define>", "");
textContent = textContent.replaceAll("<color:([a-zA-Z0-9#]+)>", "<span style='color:$1;'>");
textContent = textContent.replaceAll("</color>", "</span>");
textContent = textContent.replaceAll("<font:(\\w+)>", "<span style='font-family:$1;'>");
textContent = textContent.replaceAll("</font>", "</span>");
textContent = textContent.replaceAll("<size:(\\w+)>", "<span style='font-size:$1;'>");
textContent = textContent.replaceAll("</size>", "</span>");
textContent = textContent.replaceAll("<margin:(\\w+)>", "<div style='margin:$1;'>");
textContent = textContent.replaceAll("</margin>", "</div>");
textContent = textContent.replaceAll("<height:(\\w+)>", "<div style='height:$1;'>");
textContent = textContent.replaceAll("</height>", "</div>");
textContent = textContent.replaceAll("<margin-(\\w+):(\\w+)>", "<div style='margin-$1:$2;'>");
textContent = textContent.replaceAll("</margin>", "</div>");
textContent = textContent.replaceAll("<highlight:([a-zA-Z0-9#]+)>", "<span style='padding: 5px; background-color: $1'>");
textContent = textContent.replaceAll("</highlight>", "</span>");
textContent = textContent.replaceAll("<padding:(\\w+)>", "<span style='padding:$1;'>");
textContent = textContent.replaceAll("</padding>", "</span>");
textContent = textContent.replaceAll("<section:([a-zA-Z0-9#]+)>", "<section style='width:100%;background-color:$1;padding:10px;'>");
textContent = textContent.replaceAll("<b>", "<strong>");
textContent = textContent.replaceAll("</b>", "</strong>");
textContent = textContent.replaceAll("<i>", "<em>");
textContent = textContent.replaceAll("</i>", "</em>");
textContent = textContent.replaceAll("<ul>", "<ul class='custom-list'>");
textContent = textContent.replaceAll("<ol>", "<ol class='custom-ordered-list'>");
//textContent = textContent.replaceAll("<code:(\\w+)>","<pre class='language-$1'><code><xmp>");
//textContent = textContent.replaceAll("</code>","</xmp></code></pre>");
textContent = textContent.replaceAll("<code:(\\w+)>",
"<script src='https://cdn.jsdelivr.net/npm/prismjs@1.25.0/components/prism-$1.min.js'></script></head><pre class='language-$1'><code><xmp>");
textContent = textContent.replaceAll("</code>","</xmp></code></pre>");
pattern = Pattern.compile("<sticky-note:([a-zA-Z0-9#]+)>");
matcher = pattern.matcher(textContent);
// Replace all matches of the sticky-note pattern
while (matcher.find()) {
String originalColor = matcher.group(1); // The captured color from the regex
String lightenedColor = lightenColor(originalColor, 0.5); // Lighten the color by 50%
// Replace the sticky-note tag with the corresponding <span> tag and the lightened border color
textContent = textContent.replace(matcher.group(),
"<span style='border-left:" + originalColor + " solid 4px; padding: 5px; background-color: " + lightenedColor + "'>");
}
textContent = textContent.replaceAll("</sticky-note>","</span>");
textContent += "<br/><br/>";
System.out.println(textContent+"\n");
return textContent;
}
private void showMessage(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
private void showError(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
// Helper method to create a button with an icon
private Button createIconButton(String text) {
Button button = new Button(text);
//Image icon = new Image("file:" + iconPath);
//ImageView imageView = new ImageView(icon);
//imageView.setFitHeight(20);
//imageView.setFitWidth(20);
//button.setGraphic(imageView);
button.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-padding: 5px 10px; -fx-margin-bottom: 20px; -fx-border-radius:50px;");
return button;
}
}