Repository navigation
Expand file tree
/
Copy pathNoteApp.java
More file actions
347 lines (299 loc) · 13.4 KB
/
Copy pathNoteApp.java
File metadata and controls
347 lines (299 loc) · 13.4 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
public class NoteApp {
private JFrame frame;
private JTextArea textArea;
private JTextField titleField;
private JPanel sidebar, imageDisplayPanel, titlePanel, imagePanel, buttonsPanel;//notes-area
private JButton saveButton, deleteButton, newNoteButton, addImageButton, logOut, sketchButton;
private DefaultListModel<Note> noteListModel;//sidebar
private JList<Note> noteList;//for the previous notes
private String currentUser;
private User currUser;
private Note currentNote;
public NoteApp(User user) {
this.currentNote = new Note();
start();
this.currUser = user;
loadUserNotes();
}
public NoteApp() {
currentNote = new Note();
start();
selectUser();
}
private void start() {
Color backgroundColor = new Color(255, 255, 255, 255);
Color titleBarColor = new Color(40, 42, 54);
Color buttonColor = new Color(76, 176, 80);
Color buttonTextColor = new Color(0, 0, 0);
Color text = new Color(0, 0, 0);
frame = new JFrame("Note App");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.getContentPane().setBackground(backgroundColor);
ImageIcon appIcon = new ImageIcon("D:/Faculty/Second Year/1st Semester/OOP/Note Taking App/icon.jpg");
frame.setIconImage(appIcon.getImage());
sidebar = new JPanel(new BorderLayout());
sidebar.setBackground(titleBarColor);
sidebar.setPreferredSize(new Dimension(120, frame.getHeight()));
noteListModel = new DefaultListModel<>();
noteList = new JList<>(noteListModel);
noteList.setBackground(backgroundColor);
noteList.setForeground(text);
noteList.setFont(new Font("Arial", Font.BOLD, 14));
sidebar.add(new JScrollPane(noteList), BorderLayout.CENTER);
noteList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int index = noteList.locationToIndex(e.getPoint());
if (index == -1) {
return;
}
noteList.setSelectedIndex(index);
Note selectedNote = noteList.getSelectedValue();
if (e.getClickCount() == 2) {
openNoteForEditing(selectedNote);
} else if (SwingUtilities.isRightMouseButton(e)) {
showNotePopupMenu(e);
}
}
});
frame.add(sidebar, BorderLayout.WEST);
// Title Panel
titlePanel = new JPanel(new BorderLayout());
titleField = new JTextField();
titlePanel.add(titleField, BorderLayout.CENTER);
titleField.setPreferredSize(new Dimension(frame.getWidth(), 30));
titleField.setText("Title");
titleField.setFont(new Font("Arial", Font.BOLD, 18));
titleField.setBackground(backgroundColor);
titleField.setForeground(text);
titleField.setCaretColor(text);
frame.add(titlePanel, BorderLayout.NORTH);
// Text Area
textArea = new JTextArea();
textArea.setBackground(backgroundColor);
textArea.setText("Content");
textArea.setFont(new Font("Arial", Font.BOLD, 16));
textArea.setForeground(text);
textArea.setCaretColor(text);
JScrollPane textScrollPane = new JScrollPane(textArea);
frame.add(textScrollPane, BorderLayout.CENTER);
// Image Display Panel
imagePanel = new JPanel(new BorderLayout());
imagePanel.setBackground(backgroundColor);
imageDisplayPanel = new JPanel();
imageDisplayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
imageDisplayPanel.setPreferredSize(new Dimension(280, frame.getHeight()));
imageDisplayPanel.setBackground(backgroundColor);
imagePanel.add(new JScrollPane(imageDisplayPanel), BorderLayout.CENTER);
frame.add(imagePanel, BorderLayout.EAST);
// Buttons Panel
buttonsPanel = new JPanel();
buttonsPanel.setBackground(backgroundColor);
saveButton = new JButton("Save");
deleteButton = new JButton("Delete");
newNoteButton = new JButton("New Note");
addImageButton = new JButton("Attach Photo");
sketchButton = new JButton("Add Sketch");
logOut = new JButton("Log Out");
styleButton(saveButton, buttonColor, buttonTextColor);
styleButton(deleteButton, buttonColor, buttonTextColor);
styleButton(newNoteButton, buttonColor, buttonTextColor);
styleButton(addImageButton, buttonColor, buttonTextColor);
styleButton(sketchButton, buttonColor, buttonTextColor);
styleButton(logOut, buttonColor, buttonTextColor);
buttonsPanel.add(saveButton);
buttonsPanel.add(deleteButton);
buttonsPanel.add(newNoteButton);
buttonsPanel.add(addImageButton);
buttonsPanel.add(sketchButton);
buttonsPanel.add(logOut);
frame.add(buttonsPanel, BorderLayout.SOUTH);
// Add Listeners
addImageButton.addActionListener(e -> chooseImage());
saveButton.addActionListener(e -> saveNote());
deleteButton.addActionListener(e -> deleteNote());
newNoteButton.addActionListener(e -> createNewNote());
sketchButton.addActionListener(e -> {
try {
writeSketch();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
logOut.addActionListener(e -> goToLoginScreen());
frame.setVisible(true);
}
private void styleButton(JButton button, Color background, Color foreground) {
button.setBackground(background);
button.setForeground(foreground);
button.setFocusPainted(false);
button.setFont(new Font("Arial", Font.BOLD, 14));
}
private void goToLoginScreen() {
frame.dispose();
new LoginScreen();
}
private void chooseImage() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
String imagePath = selectedFile.getAbsolutePath();
currentNote.addImagePath(imagePath);
addImageToPanel(imagePath);
}
}
private void writeSketch() throws IOException {
Runtime.getRuntime().exec("mspaint");
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
String imagePath = selectedFile.getAbsolutePath();
currentNote.addImagePath(imagePath);
addImageToPanel(imagePath);
}
}
private void addImageToPanel(String imagePath) {
ImageIcon imageIcon = new ImageIcon(imagePath);
Image scaledImage = imageIcon.getImage().getScaledInstance(250, 200, Image.SCALE_SMOOTH);
imagePanel = new JPanel();
imagePanel.setLayout(new BorderLayout());
JLabel imageLabel = new JLabel(new ImageIcon(scaledImage));
imagePanel.add(imageLabel, BorderLayout.CENTER);
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem deleteMenuItem = new JMenuItem("Delete Image");
popupMenu.add(deleteMenuItem);
deleteMenuItem.addActionListener(e -> {
imageDisplayPanel.remove(imagePanel);
currentNote.getImagePaths().remove(imagePath);
imageDisplayPanel.revalidate();
imageDisplayPanel.repaint();
});
imageLabel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) popupMenu.show(imageLabel, e.getX(), e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) popupMenu.show(imageLabel, e.getX(), e.getY());
}
});
imageDisplayPanel.add(imagePanel);
imageDisplayPanel.revalidate();
imageDisplayPanel.repaint();
}
private void saveNote() {
currentNote.setTitle(titleField.getText());
currentNote.setText(textArea.getText());
try {
FileManager.saveNoteToFile(currentNote, currUser);
// Check if the note already exists in the list
if (!noteListModel.contains(currentNote)) {
noteListModel.addElement(currentNote);
}
JOptionPane.showMessageDialog(frame, "Note saved successfully!");
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Failed to save note: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void deleteNote() {
Note selectedNote = noteList.getSelectedValue();
if (selectedNote == null) {
JOptionPane.showMessageDialog(frame, "No note selected.", "Error", JOptionPane.WARNING_MESSAGE);
return;
}
int confirm = JOptionPane.showConfirmDialog(frame, "Are you sure you want to delete this note?");
if (confirm == JOptionPane.YES_OPTION) {
FileManager.deleteNote(currUser, selectedNote);
noteListModel.removeElement(selectedNote);
if (currentNote.equals(selectedNote)) {
createNewNote();
}
}
}
private void createNewNote() {
String[] options = {"Standard Note", "Secure Note"};
int choice = JOptionPane.showOptionDialog(frame, "What type of note do you want to create?", "New Note",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if (choice == 0) {
currentNote = new Note();
} else if (choice == 1) {
JPasswordField passwordField = new JPasswordField();
Object[] message = {"Enter password for the secure note:", passwordField};
int option = JOptionPane.showConfirmDialog(frame, message, "Secure Note Password", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
String password = new String(passwordField.getPassword());
if (!password.trim().isEmpty()) currentNote = new SecureNote("", password);
else
JOptionPane.showMessageDialog(frame, "Password cannot be empty for a secure note.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
titleField.setText("Title");
textArea.setText("Content");
imageDisplayPanel.removeAll();
imageDisplayPanel.revalidate();
imageDisplayPanel.repaint();
}
private void openNoteForEditing(Note note) {
try {
String noteTitle = note.getTitle();
File noteFile = new File(FileManager.main_folder + currUser.getUsername() + "/" + noteTitle + ".txt");
Note notee = FileManager.loadNoteFromFile(noteFile);
if (notee instanceof SecureNote) {
JPasswordField passwordField = new JPasswordField();
Object[] message = {"Enter The Password", passwordField};
int option = JOptionPane.showConfirmDialog(frame, message, "Secure Note Password", JOptionPane.OK_CANCEL_OPTION);
String inputPassword = new String(passwordField.getPassword());
if (option == JOptionPane.OK_OPTION && ((SecureNote) notee).checkPassword(inputPassword)) {
loadNoteIntoEditor(notee);
} else {
JOptionPane.showMessageDialog(frame, "Incorrect password.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
loadNoteIntoEditor(notee);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "Failed to load note: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void loadNoteIntoEditor(Note note) {
currentNote = note;
titleField.setText(note.getTitle());
textArea.setText(note.getText());
imageDisplayPanel.removeAll();
for (String imagePath : note.getImagePaths()) addImageToPanel(imagePath);
imageDisplayPanel.revalidate();
imageDisplayPanel.repaint();
}
private void showNotePopupMenu(MouseEvent e) {
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem deleteMenuItem = new JMenuItem("Delete Note");
deleteMenuItem.addActionListener(actionEvent -> deleteNote());
popupMenu.add(deleteMenuItem);
popupMenu.show(noteList, e.getX(), e.getY());
}
private void selectUser() {
String username = JOptionPane.showInputDialog(frame, "Enter your username:");
while (username == null || username.trim().isEmpty()) {
JOptionPane.showMessageDialog(frame, "Username cannot be empty.", "Error", JOptionPane.ERROR_MESSAGE);
username = JOptionPane.showInputDialog(frame, "Enter your username:");
}
currentUser = username.trim();
loadUserNotes();
}
private void loadUserNotes() {
noteListModel.clear();
var userNotes1 = FileManager.getNotesList(currUser);
for (Note n : userNotes1) {
noteListModel.addElement(n);
}
}
}