This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notes.java
379 lines (301 loc) · 10.6 KB
/
Notes.java
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
@SuppressWarnings("serial")
public class Notes extends JFrame implements ActionListener, ListSelectionListener, TextChangeTemp, KeyListener {
Color sColor = Color.getHSBColor(80, 40, 30); // blue
Color dColor = Color.getHSBColor(252, 195, 2); // purple
Color eColor = Color.getHSBColor(255, 239, 175);
static Color lpr = new Color(254, 217, 183);
static Color blu = new Color(0, 129, 167);
private JButton nFrameBtn;
String title = "Sticky Notes";
public static DefaultListModel<String> listModal;
JFrame x;
boolean recycleMod = false;
public static HashMap<Integer, String> textHMap = new HashMap<Integer, String>(); // texts
public static HashMap<Integer, String> titleHMap = new HashMap<Integer, String>(); // titles
public static HashMap<Integer, String> titleSHMap = new HashMap<Integer, String>(); // search hashmap
public static HashMap<Integer, String> titleSHMapDEL = new HashMap<Integer, String>(); // deleted hashmap
public static HashMap<Integer, String> textSHMapDEL = new HashMap<Integer, String>(); // deleted hashmap
static JList<String> list;
//Bottom Frame
private JPanel btmFrame; // Panel to hold extra items
private JLabel notesCounter; // the notes count label
private JLabel logUpdater; // log uPdater
static DB dataBase = new DB();
private JTextField searchField;
JButton trashBtn;
JPanel bottom;
int counter = dataBase.getNoRows();
Notes() {
x = new JFrame(title);
x.setSize(300, 600);
x.setLocation(50, 60);
x.setLayout(new BorderLayout());
// setBorder(new EmptyBorder(0, 0, 0, 0));
nFrameBtn = new JButton("New Note"); // Main frame title
nFrameBtn.setBackground(dColor); // sets the background
nFrameBtn.setForeground(blu);
nFrameBtn.setFocusable(false);
nFrameBtn.setFont(new Font("sans", Font.BOLD, 25)); // setting the fonts
nFrameBtn.addActionListener(this); // adding action listener to new note button
// List Model
listModal = new DefaultListModel<>();
// J List
list = new JList<>(listModal);
list.setFont(new Font("Sans", Font.BOLD, 17));
list.setBackground(blu);
list.setFixedCellHeight(35);
list.setForeground(Color.WHITE);
list.setSelectionBackground(Color.white);
list.setSelectionForeground(blu);
list.addListSelectionListener(this);
list.setLayoutOrientation(JList.VERTICAL);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportView(list);
// btm Frame
btmFrame = new JPanel();
btmFrame.setLayout(new BorderLayout());
btmFrame.setBorder(new EmptyBorder(1, 3, 3, 3));
// MENU FRAME
JPanel menuFrame = new JPanel();
menuFrame.setLayout(new GridLayout(1, 5));
menuFrame.setBorder(new EmptyBorder(1, 1, 1, 1));
trashBtn = new JButton("Recycle");
trashBtn.addActionListener(this);
JButton sortBtn = new JButton("a-z");
JButton resentBtn = new JButton("L-s");
JButton oldestBtn = new JButton("oldest");
menuFrame.add(trashBtn);
menuFrame.add(sortBtn);
menuFrame.add(resentBtn);
menuFrame.add(oldestBtn);
JPanel top = new JPanel();
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
searchField = new JTextField();
searchField.setFont(new Font("Sans", Font.ITALIC, 12));
searchField.addKeyListener(this);
center.add(searchField, BorderLayout.CENTER);
// 3rd ROW
bottom = new JPanel();
bottom.setLayout(new GridLayout(1, 2));
bottom.setMaximumSize(new Dimension(5, 5));
bottom.setOpaque(true);
notesCounter = new JLabel("Notes Count : " + counter);
notesCounter.setFont(new Font("Sans", Font.ITALIC, 12));
logUpdater = new JLabel("Good Day ?");
logUpdater.setFont(new Font("Sans", Font.ITALIC, 12));
bottom.add(notesCounter);
bottom.add(logUpdater);
// END OF 3rd ROW
btmFrame.add(menuFrame, BorderLayout.NORTH);
btmFrame.add(center, BorderLayout.CENTER);
btmFrame.add(bottom, BorderLayout.SOUTH);
// Adding the list
x.add(nFrameBtn, BorderLayout.NORTH);
x.add(scrollPane, BorderLayout.CENTER);
x.add(btmFrame, BorderLayout.SOUTH);
x.setVisible(true);
x.setResizable(false);
x.setDefaultCloseOperation(EXIT_ON_CLOSE);
dataBase.loadDataFromDB();
}
/*
* Creates a new instance of sticky note, creates a unique ID, fileName ,
* default text adds the ID to all the Hash maps adds the temporary title to the
* hash map and List Modal
*
* @param idName is a unique id with counter
*
* @param newFileName is a temporary title
*
* @param defaultText is the text for the Text area currently its empty
*/
private void newNote() {
int id = counter;
// System.out.println(counter);
String newFileName = "";
String defaultText = "";
String counterLabel = "";
// Title Generation for new note
if (counter <= 9) // checking if the counter is <= 9 then the counter will be 09 , 08, 01 ..etc
{
newFileName = "My Note 0" + counter;
counterLabel = "0" + counter;
} else // if higher than 9 then the counter will be 10,11,12...etc
{
newFileName = "My Note " + counter;
counterLabel = "" + counter;
}
Leafs newNote = new Leafs(true, id, newFileName, defaultText, this);
Notes.dataBase.insertNewNoteDB(newFileName, newFileName);
newNote.leafFrame.setVisible(true);
upDateCounter();
notesCounter.setText("Notes Count : " + counter); // Note Counter Label
successMgs("Created Successfully!"); // btm panel color change
}
private void openNote(int ID, String title, String text) {
// creating anonymous new note here with the parameters
Leafs openNote = new Leafs(false, ID, title, text, this);
openNote.leafFrame.setVisible(true);
successMgs("Opened Successfully!");
}
private void searchAndUpdateList(String currString) {
boolean isAvailable = false;
int searchCounter = 0;
titleSHMap.clear();
for (Integer title : titleHMap.keySet()) {
// System.out.println(title+ " "+titleHMap.get(title).substring(0,
// currString.length()));
if (titleHMap.get(title).substring(0, currString.length()).equalsIgnoreCase(currString)) {
titleSHMap.put(title, titleHMap.get(title));
// System.out.println(title+ " "+ titleHMap.get(title));
searchCounter++;
isAvailable = true;
} else {
}
}
if (isAvailable) {
listModal.removeAllElements();
listModal.addAll(titleSHMap.values());
successMgs(searchCounter + " results matched");
} else {
failureMgs("None Found!");
}
}
static void updateListModal() {
listModal.removeAllElements();
listModal.addAll(titleHMap.values());
list.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nFrameBtn) //
{
// calling the method to create a new note with custom parameters
newNote();
} else if (e.getSource() == searchField) {
//System.out.println(e.getSource());
} else if (e.getSource() == trashBtn) {
recycleMode(recycleMod);
}
}
void recycleMode(boolean mod) {
if (!mod) {
recycleMod = true;
nFrameBtn.setText("Recycle Center");
nFrameBtn.setEnabled(mod);
nFrameBtn.setBackground(dColor);
listModal.removeAllElements();
listModal.addAll(titleSHMapDEL.values());
list.setBackground(lpr);
list.setForeground(blu);
//System.out.println(titleSHMapDEL);
} else {
nFrameBtn.setText("New Note");
nFrameBtn.setBackground(dColor);
nFrameBtn.setEnabled(mod);
nFrameBtn.enableInputMethods(true);
updateListModal();
list.setBackground(blu);
list.setForeground(Color.WHITE);
recycleMod = false;
}
}
static void reload()
{
dataBase.loadDataFromDB();
}
@Override
/*
* List selection event
*
*/
public void valueChanged(ListSelectionEvent e) {
String currKey = list.getSelectedValue();
if (list.getValueIsAdjusting() && titleHMap.containsValue(currKey) && !recycleMod) {
for (int id : titleHMap.keySet()) {
if (currKey == titleHMap.get(id)) {
openNote(id, titleHMap.get(id), textHMap.get(id));
}
}
} else if (list.getValueIsAdjusting() && titleSHMapDEL.containsValue(currKey) && recycleMod) {
for (int id : titleSHMapDEL.keySet()) {
if (currKey == titleSHMapDEL.get(id)) {
new DisabledNote(id, titleSHMapDEL.get(id), textSHMapDEL.get(id));
}
}
}
}
@Override
public void OnTextChanged(String text, int id) {
}
void successMgs(String msg) {
logUpdater.setText(msg);
btmFrame.setBackground(new Color(144, 238, 144));
bottom.setBackground(new Color(144, 238, 144));
// btmFrame.setBackground(new Color(204,204,204));
}
void failureMgs(String msg) {
logUpdater.setText(msg);
btmFrame.setBackground(new Color(250, 95, 85)); //
bottom.setBackground(new Color(250, 95, 85));
// btmFrame.setBackground(new Color(204,204,204));
}
// updating counter
private void upDateCounter() {
counter = dataBase.getNoRows();
notesCounter.setText("Notes Count : " + counter); // Note Counter Label
}
@Override
public void keyTyped(KeyEvent e) {
// searchAndUpdateList(searchField.toString());
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (searchField.getText().length() == 0) {
listModal.addAll(titleHMap.values());
notesCounter.setText("Notes Count : " + counter);
updateListModal();
successMgs(" ...");
} else if (searchField.getText().length() >= 1) {
searchAndUpdateList(searchField.getText());
}
}
}