-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskNoteManager.java
More file actions
714 lines (596 loc) · 26.8 KB
/
TaskNoteManager.java
File metadata and controls
714 lines (596 loc) · 26.8 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Custom JTextField that limits display length but allows longer input
class LimitedDisplayTextField extends JTextField {
private int displayLength;
public LimitedDisplayTextField(String text, int displayLength) {
super(text, displayLength);
this.displayLength = displayLength;
}
@Override
protected Document createDefaultModel() {
AbstractDocument doc = (AbstractDocument) super.createDefaultModel();
doc.setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
Document d = fb.getDocument();
int currentLength = d.getLength();
if (currentLength + text.length() <= 100) { // Allow up to 100 characters input
super.insertString(fb, offset, text, attr);
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException {
Document d = fb.getDocument();
int currentLength = d.getLength();
if (currentLength - length + text.length() <= 100) { // Allow up to 100 characters input
super.replace(fb, offset, length, text, attr);
}
}
});
return doc;
}
}
public class TaskNoteManager extends JFrame {
private static final int COMPONENT_HEIGHT = 28;
private static final Color BG_COLOR = new Color(250, 250, 250);
private static final Color ACCENT_COLOR = new Color(100, 100, 100);
private static final Color HEADER_COLOR = new Color(80, 80, 80);
private static final Color TEXT_COLOR = new Color(60, 60, 60);
private static final Color WHITE = new Color(255, 255, 255);
private static final Color BORDER_COLOR = new Color(220, 220, 220);
private static final Color HOVER_COLOR = new Color(70, 70, 70);
private File sageHubDir;
private File taskNoteDir;
private File descFile;
private Properties descriptions;
private int totalFileCount;
private ExecutorService executorService;
private JLabel titleLabel;
private JLabel countLabel;
private JPanel filesPanel;
private JScrollPane scrollPane;
private LimitedDisplayTextField newNoteNameField;
public TaskNoteManager() {
initializeConfig();
initializeUI();
startBackgroundCounter();
refreshFileList();
}
private void initializeConfig() {
try {
Properties config = new Properties();
File configFile = new File("sage.properties");
if (configFile.exists()) {
try (FileInputStream fis = new FileInputStream(configFile)) {
config.load(fis);
}
}
String sageHubPath = config.getProperty("SageHub", "./SageHub");
sageHubDir = new File(sageHubPath);
if (!sageHubDir.exists()) {
sageHubDir.mkdirs();
}
taskNoteDir = new File(sageHubDir, "TaskNote");
if (!taskNoteDir.exists()) {
taskNoteDir.mkdirs();
}
descFile = new File(taskNoteDir, "descriptions.properties");
descriptions = new Properties();
if (descFile.exists()) {
try (FileInputStream fis = new FileInputStream(descFile)) {
descriptions.load(fis);
}
}
totalFileCount = countFiles();
executorService = Executors.newSingleThreadExecutor();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Configuration initialization failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
private int countFiles() {
return countFiles(taskNoteDir);
}
private int countFiles(File dir) {
if (!dir.exists() || !dir.isDirectory()) {
return 0;
}
int count = 0;
File[] files = dir.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
count += countFiles(f);
} else if (f.getName().endsWith(".md")) {
count++;
}
}
}
return count;
}
private void startBackgroundCounter() {
javax.swing.Timer timer = new javax.swing.Timer(2000, e -> {
int newCount = countFiles();
if (newCount != totalFileCount) {
totalFileCount = newCount;
SwingUtilities.invokeLater(() -> {
countLabel.setText("Total Task Notes: " + totalFileCount);
});
}
});
timer.start();
}
private void initializeUI() {
setTitle("SageHub - Task Note Manager");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(900, 650));
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBackground(BG_COLOR);
mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
// Header panel
JPanel headerPanel = new JPanel(new BorderLayout(0, 5));
headerPanel.setBackground(BG_COLOR);
headerPanel.setBorder(new EmptyBorder(10, 5, 10, 5));
// Title row
titleLabel = new JLabel("Task Note");
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 18));
titleLabel.setForeground(TEXT_COLOR);
countLabel = new JLabel("Total Task Notes: " + totalFileCount);
countLabel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
countLabel.setForeground(new Color(120, 120, 120));
JPanel titleRow = new JPanel(new BorderLayout(0, 0));
titleRow.setBackground(BG_COLOR);
titleRow.add(titleLabel, BorderLayout.WEST);
titleRow.add(countLabel, BorderLayout.EAST);
// Second row: Total Task Notes and New Task Note
JLabel newTaskNoteLabel = new JLabel("New Task Note:");
newTaskNoteLabel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
newTaskNoteLabel.setForeground(new Color(100, 100, 100));
newNoteNameField = new LimitedDisplayTextField("", 20);
newNoteNameField.setFont(new Font("Times New Roman", Font.PLAIN, 12));
newNoteNameField.setToolTipText("Enter custom name or leave empty for auto-generated name");
newNoteNameField.setPreferredSize(new Dimension(100, 28));
newNoteNameField.setMaximumSize(new Dimension(100, 28));
newNoteNameField.setMinimumSize(new Dimension(100, 28));
JButton createButton = createStyledButton("Create", new Color(100, 100, 100), new Color(70, 70, 70), COMPONENT_HEIGHT);
createButton.addActionListener(e -> createNewTaskNote());
JButton refreshButton = createStyledButton("Refresh", new Color(100, 100, 100), new Color(70, 70, 70), COMPONENT_HEIGHT);
refreshButton.addActionListener(e -> refreshFileList());
JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
rightPanel.setBackground(BG_COLOR);
rightPanel.add(newTaskNoteLabel);
rightPanel.add(newNoteNameField);
rightPanel.add(createButton);
rightPanel.add(refreshButton);
JPanel secondRow = new JPanel(new BorderLayout(0, 0));
secondRow.setBackground(BG_COLOR);
secondRow.add(rightPanel, BorderLayout.EAST);
// Main header panel with vertical layout to align left edges
JPanel headerContent = new JPanel();
headerContent.setLayout(new BoxLayout(headerContent, BoxLayout.Y_AXIS));
headerContent.setBackground(BG_COLOR);
headerContent.add(titleRow);
headerContent.add(Box.createVerticalStrut(5));
headerContent.add(secondRow);
headerPanel.add(headerContent, BorderLayout.CENTER);
// Files panel
filesPanel = new JPanel();
filesPanel.setLayout(new BoxLayout(filesPanel, BoxLayout.Y_AXIS));
filesPanel.setBackground(BG_COLOR);
scrollPane = new JScrollPane(filesPanel);
scrollPane.setBackground(BG_COLOR);
scrollPane.getViewport().setBackground(BG_COLOR);
scrollPane.setBorder(new LineBorder(BORDER_COLOR, 1));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
mainPanel.add(headerPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
add(mainPanel);
setLocationRelativeTo(null);
}
private JButton createStyledButton(String text, Color accent, Color hover, int height) {
JButton button = new JButton(text);
button.setBackground(accent);
button.setForeground(WHITE);
button.setFont(new Font("Times New Roman", Font.PLAIN, 12));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
int padding = (height - 14) / 2;
button.setBorder(new EmptyBorder(padding, 12, padding, 12));
button.setPreferredSize(new Dimension(button.getPreferredSize().width, height));
button.setMaximumSize(new Dimension(button.getPreferredSize().width, height));
button.setMinimumSize(new Dimension(button.getPreferredSize().width, height));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
button.setBackground(hover);
}
@Override
public void mouseExited(MouseEvent e) {
button.setBackground(accent);
}
});
return button;
}
private void createNewTaskNote() {
String customName = newNoteNameField.getText().trim();
SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyMM");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
String yearMonth = yearMonthFormat.format(new Date());
String dateStr = dateFormat.format(new Date());
String timeStr = timeFormat.format(new Date());
File monthDir = new File(taskNoteDir, yearMonth);
if (!monthDir.exists()) {
monthDir.mkdirs();
}
File dateDir = new File(monthDir, dateStr);
if (!dateDir.exists()) {
dateDir.mkdirs();
}
String fileName;
if (customName.isEmpty()) {
fileName = String.format("TaskNote%s_%s.md", dateStr, timeStr);
File noteFile = new File(dateDir, fileName);
int count = 1;
while (noteFile.exists()) {
fileName = String.format("TaskNote%s_%06d.md", dateStr, count);
noteFile = new File(dateDir, fileName);
count++;
}
} else {
fileName = customName.endsWith(".md") ? customName : customName + ".md";
}
File noteFile = new File(dateDir, fileName);
try {
noteFile.createNewFile();
totalFileCount = countFiles();
countLabel.setText("Total Task Notes: " + totalFileCount);
newNoteNameField.setText("");
SwingUtilities.invokeLater(() -> refreshFileList());
JOptionPane.showMessageDialog(this, "Successfully created task note: " + fileName, "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Creation failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void refreshFileList() {
filesPanel.removeAll();
filesPanel.revalidate();
filesPanel.repaint();
List<File> allFiles = findAllTaskNoteFiles(taskNoteDir);
allFiles.sort((f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified()));
List<File> recentFiles = allFiles.stream().limit(7).collect(Collectors.toList());
if (recentFiles.isEmpty()) {
JLabel emptyLabel = new JLabel("No task note files found");
emptyLabel.setFont(new Font("Times New Roman", Font.PLAIN, 14));
emptyLabel.setForeground(new Color(150, 150, 150));
emptyLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
emptyLabel.setBorder(new EmptyBorder(50, 0, 50, 0));
filesPanel.add(emptyLabel);
} else {
Map<String, List<File>> groupedByDate = new LinkedHashMap<>();
for (File file : recentFiles) {
String dateStr = file.getParentFile().getName();
groupedByDate.computeIfAbsent(dateStr, k -> new ArrayList<>()).add(file);
}
for (Map.Entry<String, List<File>> entry : groupedByDate.entrySet()) {
String dateStr = entry.getKey();
List<File> filesInDate = entry.getValue();
JPanel dateHeader = createDateHeader(dateStr, filesInDate.get(0).getParentFile());
filesPanel.add(dateHeader);
for (File file : filesInDate) {
JPanel filePanel = createFilePanel(file);
filesPanel.add(filePanel);
filesPanel.add(Box.createVerticalStrut(8));
}
}
}
filesPanel.revalidate();
filesPanel.repaint();
}
private JPanel createDateHeader(String dateStr, File dateDir) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(BG_COLOR);
panel.setBorder(new EmptyBorder(8, 15, 8, 15));
panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
JLabel dateLabel = new JLabel(dateStr);
dateLabel.setFont(new Font("Times New Roman", Font.BOLD, 12));
dateLabel.setForeground(TEXT_COLOR);
JLabel fileCountLabel = new JLabel(countFiles(dateDir) + " files");
fileCountLabel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
fileCountLabel.setForeground(new Color(120, 120, 120));
JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
rightPanel.setBackground(BG_COLOR);
rightPanel.add(fileCountLabel);
panel.add(dateLabel, BorderLayout.WEST);
panel.add(rightPanel, BorderLayout.EAST);
return panel;
}
private JPanel createFilePanel(File file) {
String fileContent = readFileContent(file);
String previewText = fileContent.length() > 20 ? fileContent.substring(0, 20) + "..." : fileContent;
JPanel panel = new JPanel(new BorderLayout(10, 8));
panel.setBackground(WHITE);
panel.setBorder(new EmptyBorder(10, 15, 10, 15));
panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
leftPanel.setBackground(WHITE);
JLabel nameLabel = new JLabel(file.getName());
nameLabel.setFont(new Font("Times New Roman", Font.BOLD, 13));
nameLabel.setForeground(TEXT_COLOR);
nameLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
nameLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
// Mouse hover preview for file name
JToolTip previewTip = new JToolTip();
previewTip.setTipText(fileContent);
nameLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
previewTip.setTipText(fileContent);
JWindow popup = new JWindow();
popup.add(new JLabel("<html><body style='width:300px;padding:10px;background:#f5f5f5;border:1px solid #ccc;border-radius:5px;'>" +
fileContent.replace("\n", "<br>") + "</body></html>"));
popup.pack();
popup.setLocation(e.getLocationOnScreen().x + 10, e.getLocationOnScreen().y + 20);
popup.setVisible(true);
nameLabel.putClientProperty("previewPopup", popup);
}
@Override
public void mouseExited(MouseEvent e) {
JWindow popup = (JWindow) nameLabel.getClientProperty("previewPopup");
if (popup != null) {
popup.setVisible(false);
popup.dispose();
nameLabel.putClientProperty("previewPopup", null);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
openFile(file);
}
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.setBackground(WHITE);
JButton openButton = createSmallButton("Open", new Color(100, 140, 180));
openButton.addActionListener(e -> openFile(file));
openButton.setAlignmentY(Component.CENTER_ALIGNMENT);
JButton deleteButton = createSmallButton("Delete", new Color(180, 100, 100));
deleteButton.addActionListener(e -> deleteFile(file));
deleteButton.setAlignmentY(Component.CENTER_ALIGNMENT);
buttonPanel.add(openButton);
buttonPanel.add(Box.createHorizontalStrut(5));
buttonPanel.add(deleteButton);
leftPanel.add(nameLabel);
leftPanel.add(Box.createHorizontalStrut(10));
leftPanel.add(buttonPanel);
String descKey = file.getName();
String desc = descriptions.getProperty(descKey, "");
// Horizontal panel for description components
JPanel descPanel = new JPanel();
descPanel.setLayout(new BoxLayout(descPanel, BoxLayout.X_AXIS));
descPanel.setBackground(WHITE);
JLabel descLabel = new JLabel("Description:");
descLabel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
descLabel.setForeground(new Color(100, 100, 100));
LimitedDisplayTextField descField = new LimitedDisplayTextField(desc, 20);
descField.setFont(new Font("Times New Roman", Font.PLAIN, 12));
descField.setBackground(WHITE);
descField.setBorder(new LineBorder(BORDER_COLOR, 1));
descField.setOpaque(true);
descField.setMaximumSize(new Dimension(100, COMPONENT_HEIGHT));
descField.setPreferredSize(new Dimension(100, COMPONENT_HEIGHT));
descField.setMinimumSize(new Dimension(100, COMPONENT_HEIGHT));
descField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
saveDescription(file.getName(), descField.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
saveDescription(file.getName(), descField.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
saveDescription(file.getName(), descField.getText());
}
});
JLabel contentPreview = new JLabel(previewText);
contentPreview.setFont(new Font("Times New Roman", Font.PLAIN, 12));
contentPreview.setForeground(new Color(140, 140, 140));
// Fixed width 100px for descField with proper layout
descPanel.add(descLabel);
descPanel.add(Box.createHorizontalStrut(5));
descPanel.add(descField);
descPanel.add(Box.createHorizontalStrut(10));
descPanel.add(contentPreview);
descPanel.add(Box.createHorizontalGlue());
panel.add(leftPanel, BorderLayout.WEST);
panel.add(descPanel, BorderLayout.CENTER);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
showContextMenu(e, file);
}
}
});
for (Component comp : panel.getComponents()) {
comp.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
showContextMenu(e, file);
}
}
});
}
return panel;
}
private String readFileContent(File file) {
try {
String content = new String(Files.readAllBytes(file.toPath()));
return content.isEmpty() ? "" : content;
} catch (Exception e) {
return "";
}
}
private String getRelativePath(File file) {
try {
String basePath = sageHubDir.getCanonicalPath();
String filePath = file.getCanonicalPath();
if (filePath.startsWith(basePath)) {
return filePath.substring(basePath.length());
}
} catch (Exception e) {
// ignore
}
return file.getParentFile().getName();
}
private JButton createSmallButton(String text, Color color) {
JButton button = new JButton(text);
button.setBackground(color);
button.setForeground(WHITE);
button.setFont(new Font("Times New Roman", Font.PLAIN, 12));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
int padding = (COMPONENT_HEIGHT - 14) / 2;
button.setBorder(new EmptyBorder(padding, 12, padding, 12));
button.setPreferredSize(new Dimension(button.getPreferredSize().width, COMPONENT_HEIGHT));
button.setMaximumSize(new Dimension(button.getPreferredSize().width, COMPONENT_HEIGHT));
button.setMinimumSize(new Dimension(button.getPreferredSize().width, COMPONENT_HEIGHT));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
button.setBackground(color.darker());
}
@Override
public void mouseExited(MouseEvent e) {
button.setBackground(color);
}
});
return button;
}
private void deleteFile(File file) {
int confirm = JOptionPane.showConfirmDialog(
this,
"Are you sure you want to delete: " + file.getName() + "?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
);
if (confirm == JOptionPane.YES_OPTION) {
try {
Files.deleteIfExists(file.toPath());
descriptions.remove(file.getName());
saveDescriptions();
totalFileCount = countFiles();
countLabel.setText("Total Task Notes: " + totalFileCount);
SwingUtilities.invokeLater(() -> refreshFileList());
JOptionPane.showMessageDialog(this, "Successfully deleted: " + file.getName(), "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Deletion failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void showContextMenu(MouseEvent e, File file) {
JPopupMenu popup = new JPopupMenu();
JMenuItem event1 = new JMenuItem("Event 1");
event1.addActionListener(ev -> {
JOptionPane.showMessageDialog(this, "Event 1 triggered: " + file.getName(), "Event 1", JOptionPane.INFORMATION_MESSAGE);
});
JMenuItem event2 = new JMenuItem("Event 2");
event2.addActionListener(ev -> {
JOptionPane.showMessageDialog(this, "Event 2 triggered: " + file.getName(), "Event 2", JOptionPane.INFORMATION_MESSAGE);
});
popup.add(event1);
popup.add(event2);
popup.show(e.getComponent(), e.getX(), e.getY());
}
private void saveDescription(String fileName, String description) {
descriptions.setProperty(fileName, description);
executorService.submit(() -> {
saveDescriptions();
});
}
private void saveDescriptions() {
try (FileOutputStream fos = new FileOutputStream(descFile)) {
descriptions.store(fos, "Task Note Descriptions");
} catch (Exception e) {
e.printStackTrace();
}
}
private void openFile(File file) {
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
Desktop.getDesktop().open(file);
} else {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("mac")) {
Runtime.getRuntime().exec(new String[]{"open", file.getAbsolutePath()});
} else if (os.contains("win")) {
Runtime.getRuntime().exec(new String[]{"cmd", "/c", "start", "", file.getAbsolutePath()});
} else {
Runtime.getRuntime().exec(new String[]{"xdg-open", file.getAbsolutePath()});
}
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Open failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private List<File> findAllTaskNoteFiles(File dir) {
List<File> files = new ArrayList<>();
if (!dir.exists() || !dir.isDirectory()) {
return files;
}
File[] list = dir.listFiles();
if (list != null) {
for (File f : list) {
if (f.isDirectory()) {
files.addAll(findAllTaskNoteFiles(f));
} else if (f.getName().endsWith(".md")) {
files.add(f);
}
}
}
return files;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
TaskNoteManager manager = new TaskNoteManager();
manager.setVisible(true);
});
}
}