This repository was 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
Expand file tree
/
Copy pathLeafs.java
More file actions
524 lines (411 loc) · 14.4 KB
/
Leafs.java
File metadata and controls
524 lines (411 loc) · 14.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
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.font.TextAttribute;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
public class Leafs implements ActionListener, KeyListener {
// Tag index
int tagIndexS, tagIndexE;
// Attributes
SimpleAttributeSet atrSet = new SimpleAttributeSet();
SimpleAttributeSet DBSet = new SimpleAttributeSet();
int lastAttrIndex = 0;
StyledDocument Doc;
// Variables
Color dColor = Color.getHSBColor(255, 239, 175); // 80, 50, 20
Color sColor = Color.getHSBColor(80, 40, 30);
Color tColor = Color.getHSBColor(100, 0, 100);
// Buttons for edit panel
JButton boldBtn; // Bold button
JButton italicBtn; // Italic button
JButton upperCaseBtn; // Italic button
JButton underLineBtn; // underline button
JButton strikeThroughBtn; // strike through button
JButton deleteBtn;
JButton saveBtn;
StringBuilder DBTEXT = new StringBuilder();
int DBTextIndex = 0;
int LIndex = 0;
private HashMap<Integer, Integer> boldHMap = new HashMap<>();
private HashMap<Integer, Integer> italicHmap = new HashMap<>();
private HashMap<Integer, Integer> underlineHMap = new HashMap<>();
private HashMap<Integer, Integer> strikeThroughHMap = new HashMap<>();
// Font Variables
int dFontDize = 12;
int dStyle = 0; // 0 -> plain , 1 -> BOLD , 3 -> italic
String fFamily = "Sans";
Font dFont = new Font(fFamily, dStyle, dFontDize);
Font btnFonts = new Font(fFamily, 0, dFontDize); // style of buttons
Font taFont = new Font(fFamily, 0, 16);
// Bold font for Button
Font bFont = new Font(fFamily, Font.BOLD, dFontDize);
Font iFont = new Font(fFamily, Font.ITALIC, dFontDize);
Font hBFont = new Font(fFamily, Font.CENTER_BASELINE, dFontDize);
// Frame
JFrame leafFrame;
// Test Area
JTextPane tArea;
// Panels
JPanel ePanel, eBtnPanel, updatePanel;
JLabel charCountLabel, wordCountLabel;
String DBText;
// Bold and end
int BAtt = 0;
int BendAtt = 0;
// ** WORKING VARIABLES**//
boolean boldBtnActive = false; // 0 -> un-clicked or not clicked | 1-> Clicked and active
boolean italicBtnActive = false; // 0 -> un-clicked or not clicked | 1-> Clicked and active
boolean isStrikeThroughActive = false;
boolean isUnderLineActive = false;
// Testing Style doc
StyledDocument styledDocument;
// Title
private TextChangeTemp textChangeListner;
private int currID;
private boolean isNew = false;
Leafs(boolean isNew, int ID, String titleValue, String textValue, TextChangeTemp tChanged) {
currID = ID;
isNew = isNew;
textChangeListner = tChanged;
DBTEXT.append(textValue);
leafFrame = new JFrame(titleValue);
leafFrame.setSize(250, 250);
leafFrame.setLayout(new BorderLayout());
leafFrame.setBackground(dColor);
leafFrame.setLocation(450, 50);
tArea = new JTextPane();
tArea.setEditorKit(new StyledEditorKit());
Doc = tArea.getStyledDocument();
tArea.setFont(taFont);
tArea.setBackground(dColor);
tArea.setBorder(new EmptyBorder(10, 10, 10, 10));
tArea.addKeyListener(this);
tArea.setSize(leafFrame.getWidth(), leafFrame.getHeight());
// JScrollPane areaScrollPane = new JScrollPane(tArea);
// areaScrollPane.setVerticalScrollBarPolicy(
// JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// areaScrollPane.setPreferredSize(new Dimension(250, 250));
ePanel = new JPanel();
ePanel.setLayout(new BorderLayout());
eBtnPanel = new JPanel();
eBtnPanel.setBackground(sColor);
eBtnPanel.setSize(leafFrame.getWidth(), 80);
eBtnPanel.setLayout(new GridLayout(1, 5));
// Strike through FONT
Font Sfont = new Font("helvetica", Font.PLAIN, 12);
Map attributes = Sfont.getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
Font newFont = new Font(attributes);
// Underline FONT btn
Font fontU = new Font("helvetica", Font.PLAIN, 12);
Map attributesU = fontU.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
Font uFont = new Font(attributesU);
italicBtn = new JButton("i");
boldBtn = new JButton("B");
strikeThroughBtn = new JButton("S");
// upperCaseBtn = new JButton("U");
underLineBtn = new JButton("U");
// strikeThroughBtn.setFont(newFont);
// sets Style and
// adds Action Listener to all the Button instances
defaultStyle(italicBtn);
italicBtn.setFont(iFont);
defaultStyle(boldBtn);
defaultStyle(underLineBtn);
underLineBtn.setFont(uFont);
defaultStyle(strikeThroughBtn);
strikeThroughBtn.setFont(newFont);
// END of Edit Panel
// update panel
updatePanel = new JPanel();
updatePanel.setLayout(new GridLayout(1, 3));
updatePanel.setBackground(Notes.blu);
// updatePanel.setBounds(5,5,5,5);
// wordCountLabel
charCountLabel = new JLabel("Char: " + tArea.getText().toString().length());
charCountLabel.setFont(new Font("Sans", Font.ITALIC, 12));
charCountLabel.setForeground(Color.white);
ImageIcon Delicon = new ImageIcon(getClass().getResource("\\img\\delIcon.png"));
deleteBtn = new JButton(Delicon);
deleteBtn.setBackground(Notes.blu);
deleteBtn.setFocusable(false);
deleteBtn.addActionListener(this);
ImageIcon saveicon = new ImageIcon(getClass().getResource("\\img\\saveimg.png"));
saveBtn = new JButton(saveicon);
saveBtn.setBackground(Notes.blu);
saveBtn.setFocusable(false);
saveBtn.addActionListener(this);
wordCountLabel = new JLabel("Word Count : **");
wordCountLabel.setFont(new Font("Sans", Font.ITALIC, 15));
updatePanel.add(charCountLabel);
// updatePanel.add(wordCountLabel);
updatePanel.add(saveBtn);
updatePanel.add(deleteBtn);
ePanel.add(eBtnPanel, BorderLayout.NORTH);
ePanel.add(updatePanel, BorderLayout.SOUTH);
// removing the decorations from the db text
removeDecorations(DBTEXT.toString());
leafFrame.add(tArea, BorderLayout.CENTER);
leafFrame.add(ePanel, BorderLayout.SOUTH);
leafFrame.setResizable(true);
leafFrame.setDefaultCloseOperation(closeOperation());
}
// paints the Buttons to match the same
void defaultStyle(JButton obj) {
obj.setFont(btnFonts);
obj.setBackground(sColor);
obj.setBorderPainted(false);
obj.setFocusable(false);
obj.addActionListener(this);
obj.setBorder(new EmptyBorder(5, 5, 5, 5));
eBtnPanel.add(obj);
italicBtn.setFont(iFont);
eBtnPanel.repaint();
}
private void capitalizeText() {
if (tArea.getText().length() == 1) {
char capitalized = tArea.getText().charAt(0);
String cap = ("" + capitalized).toUpperCase() + tArea.getText().toString().substring(1);
tArea.setText(cap);
}
}
int getWordCount() {
int wCount = 0;
return wCount;
}
// close operation
int closeOperation() {
leafFrame.setVisible(false);
leafFrame.dispose();
Notes.reload();
return 1;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boldBtn) {
// BOLD BTN ACTION
if (!boldBtnActive) {
// if false
boldBtnActive = true;
StyleConstants.setBold(atrSet, true);
tArea.setCharacterAttributes(atrSet, true);
atrSet.removeAttributes(atrSet);
DBTEXT.append("**");
} else if (boldBtnActive) {
// if true
// editText('b', 'f');
boldBtnActive = false;
DBTEXT.append("**");
StyleConstants.setBold(atrSet, false);
tArea.setCharacterAttributes(atrSet, false);
} // END OF BOLD BTN ACTION
} else if (e.getSource() == italicBtn) {
// ITALIC BTN ACTION
if (!italicBtnActive) { // IF False
italicBtnActive = true;
italicBtn.setBackground(sColor);
StyleConstants.setItalic(atrSet, true);
tArea.setCharacterAttributes(atrSet, true);
DBTEXT.append("__");
} else if (italicBtnActive) {
// TRUE
italicBtnActive = false;
DBTEXT.append("__");
StyleConstants.setItalic(atrSet, false);
tArea.setCharacterAttributes(atrSet, false);
atrSet.removeAttributes(atrSet);
// END OF BOLD ITALIC ACTION
}
} else if (e.getSource() == strikeThroughBtn) {
// Strikethrough BTN ACTION
if (!isStrikeThroughActive) {
// false
tagIndexS = tArea.getCaretPosition();
italicBtn.setBackground(sColor);
StyleConstants.setStrikeThrough(atrSet, true);
tArea.setCharacterAttributes(atrSet, true);
DBTEXT.append("$$");
isStrikeThroughActive = true;
} else if (isStrikeThroughActive) {
// true
isStrikeThroughActive = false;
tagIndexE = tArea.getCaretPosition();
DBTEXT.append("$$");
StyleConstants.setStrikeThrough(atrSet, true);
tArea.setCharacterAttributes(atrSet, true);
// atrSet.removeAttributes(atrSet);
} // END OF BOLD BTN ACTION
} else if (e.getSource() == underLineBtn) {
// Strikethrough BTN ACTION
if (!isUnderLineActive) {
// false
tagIndexS = tArea.getCaretPosition();
underLineBtn.setBackground(sColor);
StyleConstants.setUnderline(atrSet, true);
tArea.setCharacterAttributes(atrSet, true);
DBTEXT.append("~~");
isUnderLineActive = true;
} else if (isUnderLineActive) {
// true
isUnderLineActive = false;
DBTextIndex = tagIndexE; // so next time we from this location
DBTEXT.append("~~");
// DBTextIndex++;
StyleConstants.setUnderline(atrSet, true);
tArea.setCharacterAttributes(atrSet, true);
// atrSet.removeAttributes(atrSet);
} // END OF BOLD BTN ACTION
} else if (e.getSource() == deleteBtn) {
// deleteBtn
Notes.dataBase.deleteRowWithID(currID);
closeOperation();
} else if (e.getSource() == saveBtn) {
// Notes.dataBase.updateTextAndTitle(currID, tArea.getText().toString(),
// tArea.getText().toString()); //DBText
new DB().updateTextAndTitle(currID, genTitle(), DBTEXT.toString());
new DB().loadDataFromDB();
Notes.updateListModal();
}
// END OF ACTION PERFORMED METH
// END OF LEAF CLASS
}
String genTitle() {
return tArea.getText().toString().substring(0,
tArea.getText().length() - 1 <= 22 && tArea.getText().length() - 1 > 1 ? tArea.getText().length() - 1
: 21);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
charCountLabel.setText("Char :" + tArea.getText().length());
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
DBTEXT.deleteCharAt(DBTEXT.length() - 1);
}
}
@Override
public void keyReleased(KeyEvent e) {
capitalizeText(); // Capitalize the First Letter using the method
getWordCount(); // word count method
if (tArea.getText().charAt(tArea.getText().toString().length() - 1) == ' '
&& e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != 16 && e.getKeyCode() != 11) {
Notes.dataBase.updateTextAndTitle(currID, genTitle(), DBTEXT.toString());
leafFrame.setTitle(genTitle());
new DB().loadDataFromDB();
Notes.updateListModal();
}
if (e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != 16 && e.getKeyCode() != 11)
DBTEXT.append(tArea.getText().charAt(tArea.getText().length() - 1));
}
// set attributes to the text from the DB
void removeDecorations(String input) {
MutableAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setForeground(normal, Color.black);
MutableAttributeSet bold = new SimpleAttributeSet();
StyleConstants.setBold(bold, true);
MutableAttributeSet italic = new SimpleAttributeSet();
StyleConstants.setItalic(italic, true);
MutableAttributeSet sThroug = new SimpleAttributeSet();
StyleConstants.setStrikeThrough(sThroug, true);
MutableAttributeSet uLine = new SimpleAttributeSet();
StyleConstants.setUnderline(uLine, true);
StringBuilder result = new StringBuilder(); // string builder
boolean attrP = false;
boolean isB = false;
boolean isI = false;
boolean isU = false;
boolean isS = false;
for (int cIndex = 0; cIndex < input.length() - 1; cIndex++) {
String key = String.valueOf(input.charAt(cIndex)) + String.valueOf(input.charAt(cIndex + 1));
char cChar = input.charAt(cIndex);
if (key.equals("**")) {
if (attrP) {
attrP = false;
isB = false;
} else {
isB = true;
attrP = true;
}
++cIndex;
} else if (key.equals("$$")) {
if (attrP) {
attrP = false;
isS = false;
} else {
isS = true;
attrP = true;
}
++cIndex;
} else if (key.equals("__")) {
if (attrP) {
attrP = false;
} else {
isI = true;
attrP = true;
}
++cIndex;
} else if (key.equals("~~")) {
if (attrP) {
attrP = false;
isU = false;
} else {
isU = true;
attrP = true;
}
++cIndex;
}
// checking the style and appending
if (cChar != 36 && cChar != 42 && cChar != 95 && cChar != 126) {
if (isB) {
append(Doc, bold, String.valueOf(input.charAt(cIndex)));
result.append(cChar);
} else if (isI) {
append(Doc, italic, String.valueOf(input.charAt(cIndex)));
result.append(cChar);
} else if (isU) {
append(Doc, uLine, String.valueOf(input.charAt(cIndex)));
result.append(cChar);
} else if (isS) {
append(Doc, sThroug, String.valueOf(input.charAt(cIndex)));
result.append(cChar);
} else {
result.append(cChar);
append(Doc, normal, String.valueOf(input.charAt(cIndex)));
}
}
}
// tArea.setText(result.toString());
tArea.setDocument(Doc);
tArea.repaint();
tagIndexS = tArea.getText().length() - 1;
}
private void append(StyledDocument doc, AttributeSet style, String text) {
try {
Doc.insertString(doc.getLength(), text, style);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}