-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexteditor.java
More file actions
1452 lines (1318 loc) · 56.6 KB
/
Copy pathTexteditor.java
File metadata and controls
1452 lines (1318 loc) · 56.6 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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.juttx.editor;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.undo.*;
class Pal {
Color bg, bg2, surface, raised, border, text, dim, ember, gold, mint, sky, danger;
Pal(Color bg, Color bg2, Color surface, Color raised, Color border, Color text, Color dim,
Color ember, Color gold, Color mint, Color sky, Color danger) {
this.bg = bg; this.bg2 = bg2; this.surface = surface; this.raised = raised;
this.border = border; this.text = text; this.dim = dim; this.ember = ember;
this.gold = gold; this.mint = mint; this.sky = sky; this.danger = danger;
}
static Color mix(Color a, Color b, float t) {
return new Color(
(int) (a.getRed() + (b.getRed() - a.getRed()) * t),
(int) (a.getGreen() + (b.getGreen() - a.getGreen()) * t),
(int) (a.getBlue() + (b.getBlue() - a.getBlue()) * t));
}
static Color alpha(Color c, float a) {
float cl = Math.max(0f, Math.min(1f, a));
return new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * cl));
}
static Pal lerp(Pal a, Pal b, float t) {
return new Pal(mix(a.bg, b.bg, t), mix(a.bg2, b.bg2, t), mix(a.surface, b.surface, t),
mix(a.raised, b.raised, t), mix(a.border, b.border, t), mix(a.text, b.text, t),
mix(a.dim, b.dim, t), mix(a.ember, b.ember, t), mix(a.gold, b.gold, t),
mix(a.mint, b.mint, t), mix(a.sky, b.sky, t), mix(a.danger, b.danger, t));
}
Pal copy() { return lerp(this, this, 0f); }
}
class BubblyButton extends JButton {
float hover = 0f;
boolean hov = false;
boolean pressed = false;
Timer anim;
Color base = new Color(29, 47, 61);
Color over = new Color(45, 66, 84);
Color glowC = new Color(111, 183, 232);
int radius = 16;
boolean primary = false;
BubblyButton(String html, String tip, int radius, boolean primary) {
super(html);
setToolTipText(tip);
this.radius = radius;
this.primary = primary;
setContentAreaFilled(false);
setFocusPainted(false);
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
setHorizontalAlignment(SwingConstants.CENTER);
setVerticalAlignment(SwingConstants.CENTER);
setCursor(new Cursor(Cursor.HAND_CURSOR));
addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) { hov = true; run(); }
public void mouseExited(MouseEvent e) { hov = false; pressed = false; run(); }
public void mousePressed(MouseEvent e) { pressed = true; repaint(); }
public void mouseReleased(MouseEvent e) { pressed = false; repaint(); }
});
anim = new Timer(14, new ActionListener() {
public void actionPerformed(ActionEvent e) {
float target = hov ? 1f : 0f;
hover += (target - hover) * 0.22f;
if (Math.abs(target - hover) < 0.02f) { hover = target; anim.stop(); }
repaint();
}
});
}
void run() { if (!anim.isRunning()) anim.start(); }
void setPalette(Color base, Color over, Color glow, Color fg) {
this.base = base; this.over = over; this.glowC = glow;
setForeground(fg);
repaint();
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
if (hover > 0.02f) {
g2.setColor(Pal.alpha(glowC, 0.16f * hover));
g2.fillRoundRect(1 - 4, 1 - 4, w - 2 + 8, h - 2 + 8, radius + 8, radius + 8);
g2.setColor(Pal.alpha(glowC, 0.22f * hover));
g2.fillRoundRect(1 - 2, 1 - 2, w - 2 + 4, h - 2 + 4, radius + 4, radius + 4);
}
Color bgc = Pal.mix(base, over, hover);
if (pressed) bgc = bgc.darker();
int dy = pressed ? 2 : -(int) (2 * hover);
g2.translate(0, dy);
if (primary) {
GradientPaint gp = new GradientPaint(0, 0, Pal.mix(bgc, Color.WHITE, 0.12f), 0, h, Pal.mix(bgc, Color.BLACK, 0.12f));
g2.setPaint(gp);
} else {
g2.setColor(bgc);
}
g2.fillRoundRect(1, 1, w - 2, h - 2, radius, radius);
g2.setColor(Pal.alpha(Color.WHITE, primary ? 0.28f : 0.07f));
g2.fillRoundRect(3, 3, w - 6, (h - 6) / 2, radius - 2, radius - 2);
g2.setColor(Pal.alpha(primary ? Color.WHITE : glowC, 0.35f + 0.25f * hover));
g2.setStroke(new BasicStroke(1.2f));
g2.drawRoundRect(1, 1, w - 3, h - 3, radius, radius);
super.paintComponent(g2);
if (isFocusOwner()) {
g2.setColor(Pal.alpha(glowC, 0.8f));
g2.setStroke(new BasicStroke(1.6f));
g2.drawRoundRect(-1, -1, w + 1, h + 1, radius + 2, radius + 2);
}
g2.dispose();
}
}
class ToggleSwitch extends JComponent {
boolean on;
float k;
Timer anim;
Color trackOn = new Color(255, 138, 92);
Color trackOff = new Color(43, 65, 82);
Color knobC = Color.WHITE;
Color glyphC = new Color(138, 162, 176);
String gOff;
String gOn;
ActionListener listener;
ToggleSwitch(boolean initial, String gOff, String gOn) {
on = initial;
k = initial ? 1f : 0f;
this.gOff = gOff;
this.gOn = gOn;
setPreferredSize(new Dimension(54, 26));
setMaximumSize(new Dimension(54, 26));
setCursor(new Cursor(Cursor.HAND_CURSOR));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
on = !on;
animate();
if (listener != null)
listener.actionPerformed(new ActionEvent(ToggleSwitch.this, 0, "toggle"));
}
});
anim = new Timer(14, new ActionListener() {
public void actionPerformed(ActionEvent e) {
float target = on ? 1f : 0f;
k += (target - k) * 0.25f;
if (Math.abs(target - k) < 0.02f) { k = target; anim.stop(); }
repaint();
}
});
}
void animate() { if (!anim.isRunning()) anim.start(); }
void setOn(boolean v) { on = v; animate(); }
boolean isOn() { return on; }
void setActionListener(ActionListener al) { listener = al; }
void setPalette(Color onC, Color offC, Color knob, Color glyph) {
trackOn = onC; trackOff = offC; knobC = knob; glyphC = glyph;
repaint();
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
g2.setColor(Pal.mix(trackOff, trackOn, k));
g2.fillRoundRect(0, 0, w - 1, h - 1, h, h);
g2.setFont(new Font("Segoe UI Symbol", Font.PLAIN, 10));
FontMetrics fm = g2.getFontMetrics();
g2.setColor(Pal.alpha(glyphC, 0.45f + 0.45f * (1 - k)));
String sl = gOff;
g2.drawString(sl, 9 - fm.stringWidth(sl) / 2, h / 2 + fm.getAscent() / 2 - 1);
g2.setColor(Pal.alpha(glyphC, 0.45f + 0.45f * k));
String sr = gOn;
g2.drawString(sr, w - 10 - fm.stringWidth(sr) / 2, h / 2 + fm.getAscent() / 2 - 1);
int knobD = h - 8;
int knobX = 4 + (int) ((w - knobD - 8) * k);
g2.setColor(new Color(0, 0, 0, 55));
g2.fillOval(knobX + 1, 5, knobD, knobD);
g2.setColor(knobC);
g2.fillOval(knobX, 4, knobD, knobD);
g2.setColor(Pal.alpha(Color.WHITE, 0.5f));
g2.fillOval(knobX + 3, 6, knobD / 3, knobD / 3);
g2.dispose();
}
}
class PulseDot extends JComponent {
Color c = new Color(79, 216, 165);
float phase = 0f;
Timer t;
PulseDot() {
setPreferredSize(new Dimension(14, 14));
t = new Timer(40, new ActionListener() {
public void actionPerformed(ActionEvent e) { phase += 0.12f; repaint(); }
});
t.start();
}
void setColor(Color nc) { c = nc; }
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
float pulse = 0.5f + 0.5f * (float) Math.sin(phase);
g2.setColor(Pal.alpha(c, 0.18f + 0.22f * pulse));
g2.fillOval(0, 0, 14, 14);
g2.setColor(c);
g2.fillOval(4, 4, 6, 6);
g2.dispose();
}
}
class LogoBadge extends JPanel {
Pal pal;
float phase = 0f;
Timer t;
LogoBadge() {
setPreferredSize(new Dimension(52, 52));
setMaximumSize(new Dimension(52, 52));
setOpaque(false);
t = new Timer(40, new ActionListener() {
public void actionPerformed(ActionEvent e) { phase += 0.09f; repaint(); }
});
t.start();
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color em = pal == null ? new Color(255, 138, 92) : pal.ember;
Color dn = pal == null ? new Color(255, 107, 122) : pal.danger;
float pulse = 0.5f + 0.5f * (float) Math.sin(phase);
g2.setColor(Pal.alpha(em, 0.20f + 0.30f * pulse));
g2.setStroke(new BasicStroke(2.4f));
g2.drawRoundRect(1, 1, 50, 50, 20, 20);
GradientPaint gp = new GradientPaint(0, 0, em, 52, 52, dn);
g2.setPaint(gp);
g2.fillRoundRect(5, 5, 42, 42, 16, 16);
g2.setColor(Pal.alpha(Color.WHITE, 0.30f));
g2.fillRoundRect(8, 8, 36, 16, 12, 12);
g2.setColor(Color.WHITE);
g2.setFont(Texteditor.displayFont(Font.BOLD, 19f));
FontMetrics fm = g2.getFontMetrics();
String s = "JX";
g2.drawString(s, 26 - fm.stringWidth(s) / 2, 26 + fm.getAscent() / 2 - 2);
g2.dispose();
}
}
class SideBar extends JPanel {
Texteditor ed;
Pal pal;
ArrayList<BubblyButton> btns = new ArrayList<BubblyButton>();
ArrayList<JLabel> sectionLabels = new ArrayList<JLabel>();
ArrayList<JPanel> dividers = new ArrayList<JPanel>();
LogoBadge logo;
JLabel wordmark;
JLabel subMark;
JLabel versionLbl;
ToggleSwitch themeSw;
ToggleSwitch wrapSw;
JLabel themeLbl;
JLabel wrapLbl;
SideBar(Texteditor ed) {
this.ed = ed;
setPreferredSize(new Dimension(98, 10));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
logo = new LogoBadge();
logo.setAlignmentX(Component.CENTER_ALIGNMENT);
add(Box.createRigidArea(new Dimension(0, 14)));
add(logo);
wordmark = new JLabel("JuttX");
wordmark.setFont(Texteditor.displayFont(Font.BOLD, 15f));
wordmark.setAlignmentX(Component.CENTER_ALIGNMENT);
add(wordmark);
subMark = new JLabel("TEXT EDITOR");
subMark.setFont(Texteditor.displayFont(Font.BOLD, 8f));
subMark.setAlignmentX(Component.CENTER_ALIGNMENT);
add(subMark);
add(Box.createRigidArea(new Dimension(0, 10)));
addDivider();
addSection("FILE");
addBtn("\uFF0B", "NEW", "New", "Create a new document", false);
addBtn("\uD83D\uDCC2", "OPEN", "Open", "Open a file", false);
addBtn("\uD83D\uDCBE", "SAVE", "Save", "Save this file", true);
addBtn("\uD83D\uDDB6", "PRINT", "Print", "Print document", false);
addSection("EDIT");
addBtn("\u21B6", "UNDO", "Undo", "Undo (Ctrl+Z)", false);
addBtn("\u21B7", "REDO", "Redo", "Redo (Ctrl+Y)", false);
addBtn("\u2702", "CUT", "Cut", "Cut selection", false);
addBtn("\u29C9", "COPY", "Copy", "Copy selection", false);
addBtn("\uD83D\uDCCB", "PASTE", "Paste", "Paste from clipboard", false);
addBtn("\uD83D\uDD0D", "FIND", "Find", "Find text (Ctrl+F)", false);
addSection("FORMAT");
addBtn("Aa", "FONT", "Choose Font", "Choose editor font", false);
addBtn("\uFF0B", "ZOOM+", "Zoom In", "Zoom in (Ctrl+=)", false);
addBtn("\uFF0D", "ZOOM-", "Zoom Out", "Zoom out (Ctrl+-)", false);
addSection("HELP");
addBtn("\u24D8", "ABOUT", "About Texteditor", "About this editor", false);
add(Box.createVerticalGlue());
addDivider();
themeLbl = new JLabel("THEME");
themeLbl.setFont(Texteditor.displayFont(Font.BOLD, 8f));
themeLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
add(themeLbl);
themeSw = new ToggleSwitch(true, "\u263C", "\u263E");
themeSw.setAlignmentX(Component.CENTER_ALIGNMENT);
themeSw.setActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ed.actionPerformed(new ActionEvent(this, 0, "ThemeToggle"));
}
});
add(themeSw);
add(Box.createRigidArea(new Dimension(0, 8)));
wrapLbl = new JLabel("WRAP");
wrapLbl.setFont(Texteditor.displayFont(Font.BOLD, 8f));
wrapLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
add(wrapLbl);
wrapSw = new ToggleSwitch(false, "\u2261", "\u224B");
wrapSw.setAlignmentX(Component.CENTER_ALIGNMENT);
wrapSw.setActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ed.actionPerformed(new ActionEvent(this, 0, "WrapToggle"));
}
});
add(wrapSw);
add(Box.createRigidArea(new Dimension(0, 8)));
versionLbl = new JLabel("v2.1 \u00B7 by JuttX");
versionLbl.setFont(Texteditor.uiFont(Font.PLAIN, 9f));
versionLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
add(versionLbl);
add(Box.createRigidArea(new Dimension(0, 10)));
}
void addSection(String name) {
JLabel l = new JLabel(name);
l.setFont(Texteditor.displayFont(Font.BOLD, 9f));
l.setAlignmentX(Component.CENTER_ALIGNMENT);
l.setBorder(BorderFactory.createEmptyBorder(10, 0, 4, 0));
sectionLabels.add(l);
add(l);
}
void addDivider() {
JPanel d = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color c = pal == null ? new Color(43, 65, 82) : pal.border;
g.setColor(c);
g.fillRect(10, 0, getWidth() - 20, 1);
}
};
d.setOpaque(false);
d.setPreferredSize(new Dimension(98, 5));
d.setMaximumSize(new Dimension(98, 5));
d.setAlignmentX(Component.CENTER_ALIGNMENT);
dividers.add(d);
add(d);
}
void addBtn(String glyph, String label, String cmd, String tip, boolean primary) {
String html = "<html><body style='text-align:center'>"
+ "<span style='font-size:14px'>" + glyph + "</span><br>"
+ "<span style='font-size:8px'>" + label + "</span></body></html>";
BubblyButton b = new BubblyButton(html, tip, 16, primary);
b.setActionCommand(cmd);
b.addActionListener(ed);
b.setFont(Texteditor.uiFont(Font.PLAIN, 11f));
b.setPreferredSize(new Dimension(80, 50));
b.setMaximumSize(new Dimension(80, 50));
b.setAlignmentX(Component.CENTER_ALIGNMENT);
btns.add(b);
add(b);
add(Box.createRigidArea(new Dimension(0, 5)));
}
void retheme(Pal p) {
pal = p;
for (BubblyButton b : btns) {
if (b.primary) {
b.setPalette(p.ember, p.gold, p.ember, new Color(30, 14, 8));
} else {
b.setPalette(p.raised, Pal.mix(p.raised, p.sky, 0.30f), p.sky, p.text);
}
}
for (JLabel l : sectionLabels) l.setForeground(p.gold);
wordmark.setForeground(p.text);
subMark.setForeground(p.dim);
versionLbl.setForeground(p.dim);
themeLbl.setForeground(p.dim);
wrapLbl.setForeground(p.dim);
themeSw.setPalette(p.ember, p.border, Color.WHITE, p.dim);
wrapSw.setPalette(p.mint, p.border, Color.WHITE, p.dim);
for (JPanel d : dividers) d.repaint();
logo.pal = p;
logo.repaint();
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Pal p = pal == null ? Texteditor.DARK : pal;
GradientPaint gp = new GradientPaint(0, 0, p.bg2, 0, getHeight(), p.bg);
g2.setPaint(gp);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(p.border);
g2.fillRect(getWidth() - 1, 0, 1, getHeight());
g2.setColor(Pal.alpha(Color.WHITE, 0.03f));
g2.fillRect(0, 0, 1, getHeight());
g2.dispose();
}
}
public class Texteditor extends JFrame implements ActionListener, Printable {
static final Pal DARK = new Pal(
new Color(13, 21, 29), new Color(17, 28, 37), new Color(22, 35, 46),
new Color(29, 47, 61), new Color(43, 65, 82), new Color(234, 242, 246),
new Color(138, 162, 176), new Color(255, 138, 92), new Color(255, 198, 107),
new Color(79, 216, 165), new Color(111, 183, 232), new Color(255, 107, 122));
static final Pal LIGHT = new Pal(
new Color(233, 239, 243), new Color(223, 232, 237), new Color(255, 255, 255),
new Color(242, 246, 248), new Color(195, 210, 219), new Color(27, 42, 53),
new Color(94, 116, 131), new Color(238, 108, 60), new Color(217, 154, 43),
new Color(23, 168, 119), new Color(47, 127, 193), new Color(225, 75, 90));
Pal cur = DARK.copy();
boolean isDark = true;
float themeT = 1f;
Timer themeTimer;
Timer fadeTimer;
float alpha = 0f;
JTextArea ta;
JScrollPane scrollPane;
JPanel editorPanel;
JPanel fadeRoot;
JPanel statusBar;
JPanel statusCenter;
JLabel statusLabel;
JLabel infoLabel;
JLabel stateLabel;
PulseDot pulseDot;
LineNumberPanel lineNumbers;
SideBar sideBar;
UndoManager undoManager;
boolean unsaved = false;
float currentFontSize = 15f;
String months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
String s2 = "", s8 = "", s9 = "";
JCheckBoxMenuItem chkb = new JCheckBoxMenuItem("Word Wrap");
JCheckBoxMenuItem darkToggle = new JCheckBoxMenuItem("Dark Theme", true);
JMenuBar mb;
JMenu m1, m2, m3, m4;
static Font displayFont(int style, float size) {
String[] names = {"Bahnschrift", "Avenir Next", "Montserrat", "Trebuchet MS"};
for (int i = 0; i < names.length; i++) {
Font f = new Font(names[i], style, 12);
if (f.getFamily().equalsIgnoreCase(names[i])) return f.deriveFont(style, size);
}
return new Font("Dialog", style, (int) size);
}
static Font uiFont(int style, float size) {
String[] names = {"Segoe UI", "Tahoma", "Dialog"};
for (int i = 0; i < names.length; i++) {
Font f = new Font(names[i], style, 12);
if (f.getFamily().equalsIgnoreCase(names[i]) || names[i].equals("Dialog"))
return f.deriveFont(style, size);
}
return new Font("Dialog", style, (int) size);
}
static Font editorFont(float size) {
String[] names = {"JetBrains Mono", "Cascadia Code", "Consolas", "Menlo"};
for (int i = 0; i < names.length; i++) {
Font f = new Font(names[i], Font.PLAIN, 12);
if (f.getFamily().equalsIgnoreCase(names[i])) return f.deriveFont(size);
}
return new Font("Monospaced", Font.PLAIN, (int) size);
}
public Texteditor() {
setTitle("JuttX Editor");
setSize(900, 660);
setMinimumSize(new Dimension(760, 560));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
undoManager = new UndoManager();
ta = new JTextArea();
ta.setFont(editorFont(currentFontSize));
ta.setLineWrap(false);
ta.setWrapStyleWord(true);
ta.setTabSize(4);
ta.setMargin(new Insets(16, 16, 16, 16));
ta.getDocument().addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
}
});
ta.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) { markUnsaved(); updateInfo(); }
public void removeUpdate(DocumentEvent e) { markUnsaved(); updateInfo(); }
public void changedUpdate(DocumentEvent e) { updateInfo(); }
});
ta.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) { updateCaretInfo(); }
});
lineNumbers = new LineNumberPanel(ta);
scrollPane = new JScrollPane(ta);
scrollPane.setRowHeaderView(lineNumbers);
scrollPane.setBorder(BorderFactory.createEmptyBorder(14, 14, 14, 14));
scrollPane.getVerticalScrollBar().setUI(new BubblyScrollBarUI());
scrollPane.getHorizontalScrollBar().setUI(new BubblyScrollBarUI());
editorPanel = new JPanel(new BorderLayout()) {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(cur.bg);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Pal.alpha(cur.border, 0.35f));
for (int x = 10; x < getWidth(); x += 24) {
for (int y = 10; y < getHeight(); y += 24) {
g2.fillOval(x, y, 2, 2);
}
}
g2.dispose();
}
};
editorPanel.add(scrollPane, BorderLayout.CENTER);
sideBar = new SideBar(this);
pulseDot = new PulseDot();
stateLabel = new JLabel("All changes saved");
stateLabel.setFont(uiFont(Font.PLAIN, 11f));
statusLabel = new JLabel("\u2728 Developed by JuttX", SwingConstants.CENTER);
statusLabel.setFont(uiFont(Font.BOLD, 12f));
statusLabel.setOpaque(true);
infoLabel = new JLabel("Ln 1, Col 1 | Words: 0 | Chars: 0 | Zoom: 100%");
infoLabel.setFont(uiFont(Font.PLAIN, 11f));
statusBar = new JPanel(new BorderLayout());
statusBar.setBorder(BorderFactory.createEmptyBorder(6, 14, 10, 14));
JPanel west = new JPanel(new FlowLayout(FlowLayout.LEFT, 7, 0));
west.setOpaque(false);
west.add(pulseDot);
west.add(stateLabel);
statusCenter = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
statusCenter.setOpaque(false);
statusCenter.add(statusLabel);
statusBar.add(west, BorderLayout.WEST);
statusBar.add(statusCenter, BorderLayout.CENTER);
statusBar.add(infoLabel, BorderLayout.EAST);
fadeRoot = new JPanel(new BorderLayout()) {
protected void paintChildren(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, Math.max(0.02f, alpha)));
super.paintChildren(g2);
g2.dispose();
}
};
fadeRoot.setOpaque(false);
fadeRoot.add(sideBar, BorderLayout.WEST);
fadeRoot.add(editorPanel, BorderLayout.CENTER);
fadeRoot.add(statusBar, BorderLayout.SOUTH);
add(fadeRoot);
mb = new JMenuBar();
setJMenuBar(mb);
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("Tools");
m4 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
JMenuItem mi1[] = {
new JMenuItem("New"), new JMenuItem("Open"), new JMenuItem("Save"),
new JMenuItem("Save As"), new JMenuItem("Print"), new JMenuItem("Exit")
};
JMenuItem mi2[] = {
new JMenuItem("Undo"), new JMenuItem("Redo"), new JMenuItem("Cut"),
new JMenuItem("Copy"), new JMenuItem("Paste"), new JMenuItem("Delete"),
new JMenuItem("Find"), new JMenuItem("Replace"), new JMenuItem("Go To"),
new JMenuItem("Select All"), new JMenuItem("Time Stamp")
};
JMenuItem mi3[] = {
new JMenuItem("Choose Font"), new JMenuItem("Zoom In"),
new JMenuItem("Zoom Out"), new JMenuItem("Reset Zoom")
};
JMenuItem mi4[] = {
new JMenuItem("Help Topics"), new JMenuItem("About Texteditor")
};
addMenuItems(m1, mi1);
addMenuItems(m2, mi2);
m3.add(chkb);
chkb.addActionListener(this);
m3.add(darkToggle);
darkToggle.addActionListener(this);
m3.addSeparator();
addMenuItems(m3, mi3);
addMenuItems(m4, mi4);
registerShortcuts();
applyThemeColors();
setSaved(false);
startFadeIn();
updateInfo();
}
void addMenuItems(JMenu menu, JMenuItem[] items) {
for (int i = 0; i < items.length; i++) {
items[i].setFont(uiFont(Font.PLAIN, 12f));
items[i].setOpaque(true);
menu.add(items[i]);
items[i].addActionListener(this);
}
}
void applyThemeColors() {
getContentPane().setBackground(cur.bg);
scrollPane.getViewport().setBackground(cur.surface);
ta.setBackground(cur.surface);
ta.setForeground(cur.text);
ta.setCaretColor(cur.ember);
ta.setSelectionColor(Pal.alpha(cur.ember, 0.35f));
ta.setBorder(BorderFactory.createCompoundBorder(
new RoundedBorder(16, cur.border, 2),
BorderFactory.createEmptyBorder(4, 4, 4, 4)));
lineNumbers.setBackground(cur.surface);
lineNumbers.setForeground(cur.dim);
lineNumbers.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, cur.border));
editorPanel.repaint();
mb.setBackground(cur.bg2);
mb.setBorder(BorderFactory.createCompoundBorder(
new RoundedBorder(8, cur.border, 1),
BorderFactory.createEmptyBorder(4, 10, 4, 10)));
styleMenu(m1);
styleMenu(m2);
styleMenu(m3);
styleMenu(m4);
chkb.setBackground(cur.bg2);
chkb.setForeground(cur.text);
darkToggle.setBackground(cur.bg2);
darkToggle.setForeground(cur.text);
statusBar.setBackground(cur.bg2);
stateLabel.setForeground(cur.dim);
infoLabel.setForeground(cur.dim);
statusLabel.setBackground(cur.ember);
statusLabel.setForeground(new Color(30, 14, 8));
statusLabel.setBorder(BorderFactory.createCompoundBorder(
new RoundedBorder(12, cur.ember, 0),
BorderFactory.createEmptyBorder(7, 18, 7, 18)));
sideBar.retheme(cur);
}
void styleMenu(JMenu menu) {
menu.setFont(displayFont(Font.BOLD, 13f));
menu.setForeground(cur.text);
menu.setBackground(cur.bg2);
menu.setOpaque(true);
}
void animateTheme() {
final Pal from = cur.copy();
final Pal to = isDark ? DARK : LIGHT;
themeT = 0f;
if (themeTimer != null) themeTimer.stop();
themeTimer = new Timer(16, new ActionListener() {
public void actionPerformed(ActionEvent e) {
themeT += 0.07f;
if (themeT >= 1f) {
themeT = 1f;
((Timer) e.getSource()).stop();
}
float ease = themeT * themeT * (3 - 2 * themeT);
cur = Pal.lerp(from, to, ease);
applyThemeColors();
}
});
themeTimer.start();
}
void startFadeIn() {
alpha = 0f;
fadeTimer = new Timer(16, new ActionListener() {
public void actionPerformed(ActionEvent e) {
alpha += 0.06f;
if (alpha >= 1f) {
alpha = 1f;
((Timer) e.getSource()).stop();
}
fadeRoot.repaint();
}
});
fadeTimer.start();
}
void markUnsaved() {
if (!unsaved) setSaved(true);
}
void setSaved(boolean nowUnsaved) {
unsaved = nowUnsaved;
if (unsaved) {
pulseDot.setColor(cur.gold);
stateLabel.setText("Unsaved changes");
} else {
pulseDot.setColor(cur.mint);
stateLabel.setText("All changes saved");
}
}
void registerShortcuts() {
InputMap im = ta.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap am = ta.getActionMap();
String[] keys = {"control Z", "control Y", "control F", "control H", "control G",
"control EQUALS", "control MINUS", "control P"};
String[] cmds = {"Undo", "Redo", "Find", "Replace", "Go To", "Zoom In", "Zoom Out", "Print"};
for (int i = 0; i < keys.length; i++) {
final String cmd = cmds[i];
im.put(KeyStroke.getKeyStroke(keys[i]), cmd);
am.put(cmd, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Texteditor.this.actionPerformed(new ActionEvent(ta, 0, cmd));
}
});
}
}
void updateInfo() {
String txt = ta.getText();
int words = txt.trim().isEmpty() ? 0 : txt.trim().split("\\s+").length;
int chars = txt.length();
int zoom = (int) ((currentFontSize / 15f) * 100);
infoLabel.setText("Ln 1, Col 1 | Words: " + words + " | Chars: " + chars + " | Zoom: " + zoom + "%");
lineNumbers.repaint();
}
void updateCaretInfo() {
int caretPos = ta.getCaretPosition();
int line = 1;
int col = 1;
try {
line = ta.getLineOfOffset(caretPos) + 1;
col = caretPos - ta.getLineStartOffset(line - 1) + 1;
} catch (Exception ex) {
}
String txt = ta.getText();
int words = txt.trim().isEmpty() ? 0 : txt.trim().split("\\s+").length;
int chars = txt.length();
int zoom = (int) ((currentFontSize / 15f) * 100);
infoLabel.setText("Ln " + line + ", Col " + col + " | Words: " + words + " | Chars: " + chars + " | Zoom: " + zoom + "%");
}
void changeZoom(float delta) {
currentFontSize = Math.max(8, Math.min(72, currentFontSize + delta));
ta.setFont(editorFont(currentFontSize));
lineNumbers.repaint();
updateCaretInfo();
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
ta.paint(g2);
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent ae) {
String arg = ae.getActionCommand();
if (arg.equals("New")) {
Texteditor t11 = new Texteditor();
t11.setVisible(true);
} else if (arg.equals("Open")) {
try {
JFileChooser fd1 = new JFileChooser();
fd1.setDialogTitle("Select File");
if (fd1.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File f = fd1.getSelectedFile();
s2 = f.getName();
StringBuilder s4 = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) s4.append(line).append("\n");
br.close();
ta.setText(s4.toString());
undoManager.discardAllEdits();
this.setTitle(s2 + " \u2014 JuttX Editor");
setSaved(false);
updateInfo();
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (arg.equals("Save") || arg.equals("Save As")) {
try {
JFileChooser dialog1 = new JFileChooser();
dialog1.setDialogTitle("Save As");
if (dialog1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File f1 = dialog1.getSelectedFile();
s9 = f1.getAbsolutePath();
if (!s9.endsWith(".txt")) {
s9 = s9 + ".txt";
f1 = new File(s9);
}
s8 = f1.getName();
BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
bw.write(ta.getText());
bw.close();
this.setTitle(s8 + " \u2014 JuttX Editor");
setSaved(false);
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (arg.equals("Print")) {
try {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
if (job.printDialog()) job.print();
} catch (PrinterException e) {
JOptionPane.showMessageDialog(this, "Print error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else if (arg.equals("Exit")) {
System.exit(0);
} else if (arg.equals("Undo")) {
try { if (undoManager.canUndo()) undoManager.undo(); } catch (Exception e) { }
} else if (arg.equals("Redo")) {
try { if (undoManager.canRedo()) undoManager.redo(); } catch (Exception e) { }
} else if (arg.equals("Cut")) {
ta.cut();
} else if (arg.equals("Copy")) {
ta.copy();
} else if (arg.equals("Paste")) {
ta.paste();
} else if (arg.equals("Delete")) {
int ds = ta.getSelectionStart();
int de = ta.getSelectionEnd();
if (ds != de) ta.replaceRange("", ds, de);
} else if (arg.equals("Find")) {
new FindReplaceDialog(this, false, cur.copy()).setVisible(true);
} else if (arg.equals("Replace")) {
new FindReplaceDialog(this, true, cur.copy()).setVisible(true);
} else if (arg.equals("Go To")) {
new GoToDialog(this, cur.copy()).setVisible(true);
} else if (arg.equals("Select All")) {
ta.selectAll();
} else if (arg.equals("Time Stamp")) {
GregorianCalendar gc = new GregorianCalendar();
String hms = "Time - " + gc.get(Calendar.HOUR_OF_DAY) + ":" + gc.get(Calendar.MINUTE) + ":" + gc.get(Calendar.SECOND)
+ " Date - " + gc.get(Calendar.DATE) + " " + months[gc.get(Calendar.MONTH)] + " " + gc.get(Calendar.YEAR) + " ";
ta.insert(hms, ta.getCaretPosition());
} else if (arg.equals("Choose Font")) {
JFontChooser fc = new JFontChooser(cur.copy());
fc.setSelectedFont(ta.getFont());
if (fc.showDialog(this) == JFontChooser.OK_OPTION) {
ta.setFont(fc.getSelectedFont());
currentFontSize = fc.getSelectedFont().getSize();
updateCaretInfo();
}
} else if (arg.equals("Zoom In")) {
changeZoom(2f);
} else if (arg.equals("Zoom Out")) {
changeZoom(-2f);
} else if (arg.equals("Reset Zoom")) {
currentFontSize = 15f;
ta.setFont(editorFont(15f));
updateCaretInfo();
} else if (arg.equals("Word Wrap")) {
boolean w = chkb.getState();
ta.setLineWrap(w);
ta.setWrapStyleWord(w);
sideBar.wrapSw.setOn(w);
} else if (arg.equals("WrapToggle")) {
boolean w = sideBar.wrapSw.isOn();
ta.setLineWrap(w);
ta.setWrapStyleWord(w);
chkb.setState(w);
} else if (arg.equals("Dark Theme")) {
isDark = darkToggle.getState();
sideBar.themeSw.setOn(isDark);
animateTheme();
} else if (arg.equals("ThemeToggle")) {
isDark = sideBar.themeSw.isOn();
darkToggle.setState(isDark);
animateTheme();
} else if (arg.equals("About Texteditor")) {
new AboutDialog(this, "About Texteditor", cur.copy()).setVisible(true);
} else if (arg.equals("Help Topics")) {
JOptionPane.showMessageDialog(this,
"Shortcuts:\nCtrl+Z Undo | Ctrl+Y Redo\nCtrl+F Find | Ctrl+H Replace\nCtrl+G Go To Line\nCtrl+= Zoom In | Ctrl+- Zoom Out\nCtrl+P Print",
"Help", JOptionPane.INFORMATION_MESSAGE);
}
updateInfo();
}
static class RoundedBorder extends AbstractBorder {
int radius;
Color color;
int thickness;
public RoundedBorder(int r, Color c, int t) {
radius = r;
color = c;
thickness = t;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(color);
g2.setStroke(new BasicStroke(thickness));
g2.drawRoundRect(x, y, w - 1, h - 1, radius, radius);
g2.dispose();
}
public Insets getBorderInsets(Component c) {
return new Insets(thickness + 2, thickness + 2, thickness + 2, thickness + 2);
}
}
class BubblyScrollBarUI extends javax.swing.plaf.basic.BasicScrollBarUI {
protected void configureScrollBarColors() {
trackColor = new Color(0, 0, 0, 0);
thumbColor = Pal.alpha(cur.sky, 0.5f);
}
protected JButton createDecreaseButton(int orientation) {
JButton b = new JButton();
b.setPreferredSize(new Dimension(0, 0));
return b;
}
protected JButton createIncreaseButton(int orientation) {
JButton b = new JButton();
b.setPreferredSize(new Dimension(0, 0));
return b;
}
protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
boolean hover = r.contains(MouseInfo.getPointerInfo() == null ? new Point(-1, -1) :
SwingUtilities.convertPoint(MouseInfo.getPointerInfo(), new Point(MouseInfo.getPointerInfo().getLocation()), c));
g2.setColor(Pal.alpha(cur.sky, hover ? 0.85f : 0.5f));
g2.fillRoundRect(r.x + 2, r.y + 2, r.width - 4, r.height - 4, 10, 10);
g2.dispose();
}
protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(Pal.alpha(cur.border, 0.25f));
g2.fillRoundRect(r.x, r.y, r.width, r.height, 8, 8);
g2.dispose();