-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDE.java
2023 lines (1719 loc) Β· 82.1 KB
/
IDE.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
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
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import javax.swing.text.*;
//fonts
// keyadapter - abstract chass for receiving keyboard events
// action listner - evernt handler class, define what should be done when an user performs certain operation
// keylistner - listener interface for receiving keyboard events (keystrokes)
public class IDE extends KeyAdapter implements ActionListener, KeyListener {
JSplitPane splitPane;
static Color Key_H = Color.BLUE;
static String python_name = "";
static String javac_name = "";
static String java_name = "";
static String cpp_name = "";
static String c_name = "";
// Default Font Size for text
int fsize = 17;
static String current_file_path = "";
String all_terminal_text = "";
static String exit_code;
private static JTextPane area;
private JTextPane terminal;
private JScrollPane scpane;
private JScrollPane scpanet;
String text = "";
static String current_language = "None";
// Creating Frame and setting up the title
JFrame f = new JFrame("Saturn IDE");
JTextField title;
Font newFont;
JPanel bottomPanel;
JLabel detailsOfFile;
JPanel runPanel;
Button runX;
JList fontFamilyList, fontStyleList, fontSizeList;
JScrollPane sb;
JMenuBar menuBar;
JMenu file, edit, format, theme;
JMenuItem newdoc, open, save, print, exit, copy, paste, cut, selectall, fontfamily, fontstyle, fontsize, run, light,
dark;
// Defining List of Fonts for Text
String fontFamilyValues[] = { "Dyuthi", "Chilanka", "DejaVu Sans", "FreeMono", "FreeSerif", "Nimbus Sans",
"URW Gothic",
"Tlwg Mono", "Trimmana", "Ubuntu Mono", "Ubuntu" };
// Defining List of Font Size for Text
String fontSizeValues[] = { "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70" };
int[] stylevalue = { Font.PLAIN, Font.BOLD, Font.ITALIC };
// Defining List of Font Styles for Text
String[] fontStyleValues = { "PLAIN", "BOLD", "ITALIC" };
String fontFamily, fontSize, fontStyle;
int fstyle;
int cl;
int linecount;
JScrollPane sp;
Color text_color_dark = new Color(255, 255, 255);
Color background_color_dark = new Color(0, 0, 0);
Color terminal_color_dark = new Color(20, 20, 20);
Color line_no_Color_dark = new Color(39, 55, 77);
Color line_no_Color_light = new Color(221, 230, 237);
Color text_color_light = new Color(0, 0, 0);
Color background_color_light = new Color(255, 255, 255);
Color terminal_color_light = new Color(220, 220, 220);
JTextArea lines;
public IDE() {
// Calling initUI() method to initiliaze UI
initUI();
// Calling addActionEvents() method to add events
addActionEvents();
}
/**
* The actionPerformed method handles various actions performed by the user,
* such as opening,
* saving, printing, copying, pasting, cutting, selecting all, changing font
* family, style, and
* size, running code, and changing the theme.
*
* @param ae The parameter "ae" is of type ActionEvent. It represents the event
* that occurred, such
* as a button click or menu item selection.
*/
public void actionPerformed(ActionEvent ae) {
// if new option is choosen
if (ae.getActionCommand().equals("New")) {
// Setting Text as empty by default
area.setText("");
} // if open option is chosen
else if (ae.getActionCommand().equals("Open")) {
// Setting current by default directory "C" folder
JFileChooser chooser = new JFileChooser("C:/");
chooser.setAcceptAllFileFilterUsed(false);
// Allowing only text (.txt) files extension to open
FileNameExtensionFilter txt = new FileNameExtensionFilter(".txt files", "txt");
chooser.addChoosableFileFilter(txt);
FileNameExtensionFilter py = new FileNameExtensionFilter(".py files", "py");
chooser.addChoosableFileFilter(py);
FileNameExtensionFilter cpp_files = new FileNameExtensionFilter(".cpp files", "cpp");
chooser.addChoosableFileFilter(cpp_files);
FileNameExtensionFilter js_files = new FileNameExtensionFilter(".js files", "js");
chooser.addChoosableFileFilter(js_files);
FileNameExtensionFilter c_files = new FileNameExtensionFilter(".c files", "c");
chooser.addChoosableFileFilter(c_files);
FileNameExtensionFilter java_files = new FileNameExtensionFilter(".java files", "java");
chooser.addChoosableFileFilter(java_files);
int result = chooser.showOpenDialog(f);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
String filename = file.getName();
current_language = filename.substring(filename.lastIndexOf("."), filename.length());
current_file_path = file.getAbsolutePath();
detailsOfFile.setText("Path: " + current_file_path + " Lang: " + current_language);
System.out.println(current_language + " aaraha hai");
try {
// Reading the file
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
area.read(br, null);
// Closing the file after reading
// Clearing the memory
br.close();
area.requestFocus();
readFileAndHighlight(area, current_file_path, current_language);
lines.setText(getText());
} catch (Exception e) {
System.out.print(e);
}
f.setTitle(current_file_path + " - Saturn IDE");
}
} // if save option is choosen
else if (ae.getActionCommand().equals("Save")) {
if (current_file_path == "") {
final JFileChooser SaveAs = new JFileChooser();
JFileChooser chooser = new JFileChooser("C:/");
chooser.setAcceptAllFileFilterUsed(false);
// Allowing only text (.txt) files extension to open
FileNameExtensionFilter txt = new FileNameExtensionFilter(".txt files", "txt");
SaveAs.addChoosableFileFilter(txt);
FileNameExtensionFilter py = new FileNameExtensionFilter(".py files", "py");
SaveAs.addChoosableFileFilter(py);
FileNameExtensionFilter cpp_files = new FileNameExtensionFilter(".cpp files", "cpp");
SaveAs.addChoosableFileFilter(cpp_files);
FileNameExtensionFilter js_files = new FileNameExtensionFilter(".js files", "js");
SaveAs.addChoosableFileFilter(js_files);
FileNameExtensionFilter c_files = new FileNameExtensionFilter(".c files", "c");
SaveAs.addChoosableFileFilter(c_files);
FileNameExtensionFilter java_files = new FileNameExtensionFilter(".java files", "java");
SaveAs.addChoosableFileFilter(java_files);
SaveAs.setApproveButtonText("Save");
// Opening the dialog and asking from user where to save the file.
int actionDialog = SaveAs.showOpenDialog(f);
if (actionDialog != JFileChooser.APPROVE_OPTION) {
return;
}
///////////////////////////////////////////////////////////////////
String ext = "";
String extension = SaveAs.getFileFilter().getDescription();
if (extension.equals(".py files")) {
ext = ".py";
} else if (extension.equals(".txt files")) {
ext = ".txt";
} else if (extension.equals(".cpp files")) {
ext = ".cpp";
} else if (extension.equals(".c files")) {
ext = ".c";
} else if (extension.equals(".js files")) {
ext = ".js";
} else if (extension.equals(".java files")) {
ext = ".java";
}
current_file_path = SaveAs.getSelectedFile().getAbsolutePath() + ext;
current_language = ext;
detailsOfFile.setText("Path: " + current_file_path + " Lang: " + current_language);
System.out.println(current_file_path);
System.out.println(extension);
f.setTitle(current_file_path + " - Saturn IDE");
File fileName = new File(SaveAs.getSelectedFile() + ext);
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(fileName));
area.write(outFile);
} catch (IOException e) {
e.printStackTrace();
}
lines.setText(getText());
} else if (current_file_path != "") {
File fileName = new File(current_file_path);
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(fileName));
area.write(outFile);
// readFileAndHighlight(area, current_file_path, current_language);
} catch (Exception e) {
e.printStackTrace();
}
lines.setText(getText());
}
} // if print option is choosen
else if (ae.getActionCommand().equals("Print")) {
try {
// printer dialog will open
area.print();
} catch (Exception e) {
}
} // if exit option is choosen
else if (ae.getActionCommand().equals("Exit")) {
// Destroying/Closing the frame/window
f.dispose();
} // if copy option is choosen
else if (ae.getActionCommand().equals("Copy")) {
// Getting Selected Selected Text
text = area.getSelectedText();
} // if paste option is choosen
else if (ae.getActionCommand().equals("Paste")) {
area.paste();
// area.insertComponent(copy);(text, area.getCaretPosition());
} // if cut option is choosen
else if (ae.getActionCommand().equals("Cut")) {
text = area.getSelectedText();
// area.replaceRange("", area.getSelectionStart(), area.getSelectionEnd()); //
} // if select all option is choosen
else if (ae.getActionCommand().equals("Select All")) {
// Selecting all text
area.selectAll();
} // if font family change option is choosen
else if (ae.getActionCommand().equals("Font Family")) {
// Setting up Font Family
JOptionPane.showConfirmDialog(null, fontFamilyList, "Choose Font Family", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
fontFamily = String.valueOf(fontFamilyList.getSelectedValue());
newFont = new Font(fontFamily, fstyle, fsize);
area.setFont(newFont);
lines.setFont(newFont);
terminal.setFont(newFont);
lines.setText(getText());
} // if font style change option is choosen
else if (ae.getActionCommand().equals("Font Style")) {
// Setting up Font Style
JOptionPane.showConfirmDialog(null, fontStyleList, "Choose Font Style", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
fstyle = stylevalue[fontStyleList.getSelectedIndex()];
newFont = new Font(fontFamily, fstyle, fsize);
area.setFont(newFont);
lines.setFont(newFont);
terminal.setFont(newFont);
lines.setText(getText());
} // if font size change option is choosen
else if (ae.getActionCommand().equals("Font Size")) {
// Setting up Font Size
JOptionPane.showConfirmDialog(null, fontSizeList, "Choose Font Size", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
fontSize = String.valueOf(fontSizeList.getSelectedValue());
fsize = Integer.parseInt(fontSize);
newFont = new Font(fontFamily, fstyle, fsize);
area.setFont(newFont);
lines.setFont(newFont);
terminal.setFont(newFont);
lines.setText(getText());
} else if (ae.getActionCommand().equals("Run")) {
if (current_file_path == "") {
final JFileChooser SaveAs = new JFileChooser();
JFileChooser chooser = new JFileChooser("C:/");
chooser.setAcceptAllFileFilterUsed(false);
// Allowing only text (.txt) files extension to open
FileNameExtensionFilter txt = new FileNameExtensionFilter(".txt files", "txt");
SaveAs.addChoosableFileFilter(txt);
FileNameExtensionFilter py = new FileNameExtensionFilter(".py files", "py");
SaveAs.addChoosableFileFilter(py);
FileNameExtensionFilter cpp_files = new FileNameExtensionFilter(".cpp files", "cpp");
SaveAs.addChoosableFileFilter(cpp_files);
FileNameExtensionFilter js_files = new FileNameExtensionFilter(".js files", "js");
SaveAs.addChoosableFileFilter(js_files);
FileNameExtensionFilter c_files = new FileNameExtensionFilter(".c files", "c");
SaveAs.addChoosableFileFilter(c_files);
FileNameExtensionFilter java_files = new FileNameExtensionFilter(".java files", "java");
SaveAs.addChoosableFileFilter(java_files);
SaveAs.setApproveButtonText("Save");
// Opening the dialog and asking from user where to save the file.
int actionDialog = SaveAs.showOpenDialog(f);
if (actionDialog != JFileChooser.APPROVE_OPTION) {
return;
}
///////////////////////////////////////////////////////////////////
String ext = "";
String extension = SaveAs.getFileFilter().getDescription();
if (extension.equals(".py files")) {
ext = ".py";
} else if (extension.equals(".txt files")) {
ext = ".txt";
} else if (extension.equals(".cpp files")) {
ext = ".cpp";
} else if (extension.equals(".c files")) {
ext = ".c";
} else if (extension.equals(".js files")) {
ext = ".js";
} else if (extension.equals(".java files")) {
ext = ".java";
}
current_file_path = SaveAs.getSelectedFile().getAbsolutePath() + ext;
current_language = ext;
detailsOfFile.setText("Path: " + current_file_path + " Lang: " + current_language);
System.out.println(current_file_path);
System.out.println(extension);
f.setTitle(current_file_path + " - Saturn IDE");
File fileName = new File(SaveAs.getSelectedFile() + ext);
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(fileName));
area.write(outFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
// readFileAndHighlight(area, current_file_path, extension);
} catch (Exception e) {
System.out.print(e);
}
lines.setText(getText());
} else if (current_file_path != "") {
File fileName = new File(current_file_path);
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(fileName));
area.write(outFile);
// readFileAndHighlight(area, current_file_path, current_language);
} catch (Exception e) {
e.printStackTrace();
}
lines.setText(getText());
}
// Setting up Font Size
System.out.println(current_language);
if (current_language.equals(".py")) {
if (python_name == "") {
python_name = getPythonInterpreterPath();
String out = runCommand(python_name + " " + current_file_path);
all_terminal_text += out + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
} else {
String out = runCommand(python_name + " " + current_file_path);
all_terminal_text += out + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
}
try {
// readFileAndHighlight(area, current_file_path, ".py");
} catch (Exception e) {
e.printStackTrace();
}
} else if (current_language.equals(".java")) {
if (javac_name == "" && java_name == "") {
javac_name = getJavacInterpreterPath();
java_name = getJavaInterpreterPath();
System.out.println(java_name);
System.out.println(javac_name);
String out = runCommand(javac_name + " " + current_file_path);
String out2 = runJavaFileCommand(java_name, current_file_path);
System.out.println(out + " " + out2);
all_terminal_text += out + "\n" + out2 + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
} else {
System.out.println(java_name);
System.out.println(javac_name);
String out = runCommand(javac_name + " " + current_file_path);
String out2 = runJavaFileCommand(java_name, current_file_path);
System.out.println(out + " " + out2);
all_terminal_text += out + "\n" + out2 + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
}
try {
readFileAndHighlight(area, current_file_path, ".java");
} catch (Exception e) {
e.printStackTrace();
}
} else if (current_language.equals(".cpp")) {
if (cpp_name == "") {
cpp_name = getCppInterpreterPath();
String out = runCppFileCommand(cpp_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
} else {
String out = runCppFileCommand(cpp_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
}
try {
readFileAndHighlight(area, current_file_path, ".cpp");
} catch (Exception e) {
e.printStackTrace();
}
} else if (current_language.equals(".c")) {
if (c_name == "") {
c_name = getCInterpreterPath();
String out = runCFileCommand(c_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
} else {
String out = runCFileCommand(c_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
}
try {
readFileAndHighlight(area, current_file_path, ".c");
} catch (Exception e) {
e.printStackTrace();
}
} else if (current_language.equals(".js")) {
try {
readFileAndHighlight(area, current_file_path, ".js");
cantRunLanguageAlert("js");
} catch (Exception e) {
e.printStackTrace();
}
} else if (current_language.equals(".txt")) {
cantRunLanguageAlert(".txt");
}
} else if (ae.getActionCommand().equals("Dark")) {
darkTheme();
// System.out.println("trying to highlight", area.gates);
if (current_language != "") {
// highlight(area, area.getStyledDocument(), current_language);
}
} else if (ae.getActionCommand().equals("Light")) {
lightTheme();
if (current_language != "") {
highlight(area, area.getStyledDocument(), current_language);
}
}
}
/**
* The function `runCommand` executes a command in the command line and returns
* the output as a
* string.
*
* @param command The "command" parameter is a string that represents the
* command you want to
* execute in the command line. It can be any valid command that
* you would normally run in the
* command prompt or terminal.
* @return The method is returning a String, which is the output of the command
* that was executed.
*/
public static String runCommand(String command) {
String out = "";
try {
// Execute the command
Process process = Runtime.getRuntime().exec(command);
// Read the output of the command
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
out += line + "\n";
}
// Wait for the process to complete
int exitCode = process.waitFor();
exit_code = "Exited with error code : " + exitCode;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return out;
}
/**
* The `initUI()` function initializes the user interface for a Java
* application, including
* creating menus, text areas, scroll panes, and setting up event listeners.
*/
public void initUI() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // do this
detailsOfFile = new JLabel();
bottomPanel = new JPanel();
runPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JPanel panel = new JPanel(new GridLayout(2, 1));
// Creating Menubar
menuBar = new JMenuBar();
// Creating Menu "File"
file = new JMenu("File");
// Creating MenuItem "New"
newdoc = new JMenuItem("New");
// Assigning shortcut "Cntrl + N" for "New" Menu Item
newdoc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
// Creating MenuItem "Open"
open = new JMenuItem("Open");
// Assigning shortcut "Cntrl + O" for "Open" Menu Item
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
run = new JMenuItem("Run");
// Assigning shortcut "Cntrl + O" for "Open" Menu Item
run.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
// Creating MenuItem "Save"
save = new JMenuItem("Save");
// Assigning shortcut "Cntrl + S" for "Save" Menu Item
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
// Creating MenuItem "Print"
print = new JMenuItem("Print");
// Assigning shortcut "Cntl + P" for "Print" Menu Item
print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
// Creating MenuItem "Exit"
exit = new JMenuItem("Exit");
// Assigning shortcut "ESC" for "Exit" Menu Item
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
// Creating Menu "Edit"
edit = new JMenu("Edit");
// Creating MenuItem "Copy"
copy = new JMenuItem("Copy");
// Assigning shortcut "Cntrl + C" for "Copy" Menu Item
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
// Creating MenuItem "Paste"
paste = new JMenuItem("Paste");
// Assigning shortcut "Cntrl + V" for "Paste" Menu Item
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
// Creating MenuItem "Cut"
cut = new JMenuItem("Cut");
// Assigning shortcut "Cntrl + X" for "Cut" Menu Item
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
// Creating MenuItem "Select All"
selectall = new JMenuItem("Select All");
// Assigning shortcut "Cntrl + A" for "Select All" Menu Item
selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
// Creating Menu "Format"
format = new JMenu("Format");
// Creating MenuItem "Font Family"
fontfamily = new JMenuItem("Font Family");
// Creating MenuItem "Font Style"
fontstyle = new JMenuItem("Font Style");
// Creating MenuItem "Font Size"
fontsize = new JMenuItem("Font Size");
// Creating List of Font Family and assigning the list values
fontFamilyList = new JList(fontFamilyValues);
// Creating List of Font Styles and assigning the list values
fontStyleList = new JList(fontStyleValues);
// Creating List of Font Size and assigning the list values
fontSizeList = new JList(fontSizeValues);
theme = new JMenu("Theme");
light = new JMenuItem("Light");
dark = new JMenuItem("Dark");
// Allowing user to select only one option
fontFamilyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontStyleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// JScrollPane jsp = new JScrollPane();
// TextArea / Editor of Notepad
area = new JTextPane();
lines = new JTextArea("1");
lines.setBackground(Color.LIGHT_GRAY);
lines.setEditable(false);
area.getDocument().addDocumentListener(new DocumentListener() {
public String getText() {
int caretPosition = area.getDocument().getLength();
Element root = area.getDocument().getDefaultRootElement();
String text = "1" + System.getProperty("line.separator");
for (int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
text += i + System.getProperty("line.separator");
}
return text;
}
@Override
public void changedUpdate(DocumentEvent de) {
lines.setText(getText());
}
@Override
public void insertUpdate(DocumentEvent de) {
lines.setText(getText());
}
@Override
public void removeUpdate(DocumentEvent de) {
lines.setText(getText());
}
});
// Default font will be sam_serif and default font style will be plain and
// default style will be 20.
area.setFont(new Font("FreeMono", Font.BOLD, 20));
lines.setFont(new Font("FreeMono", Font.BOLD, 20));
// Sets the line-wrapping policy of the text area no line wap
// Sets the word-wrapping policy of the text area
// area.setWrapStyleWord(true);
// Creating Scrollables around textarea
scpane = new JScrollPane();
scpane.getViewport().add(area);
scpane.setRowHeaderView(lines);
panel.add(scpane);
// //Creating border for scrollpane
// scpane.setBorder(BorderFactory.createEmptyBorder());
// Adding menubar in frame
f.setJMenuBar(menuBar);
// Adding all menus in menubars
menuBar.add(file);
menuBar.add(edit);
menuBar.add(format);
menuBar.add(theme);
file.add(newdoc);
file.add(open);
file.add(run);
file.add(save);
file.add(print);
file.add(exit);
edit.add(copy);
edit.add(paste);
edit.add(cut);
edit.add(selectall);
format.add(fontfamily);
format.add(fontstyle);
format.add(fontsize);
theme.add(light);
theme.add(dark);
terminal = new JTextPane();
// //Default font will be sam_serif and default font style will be plain and
// default style will be 20.
terminal.setFont(new Font("FreeMono", Font.BOLD, 15));
// //Sets the line-wrapping policy of the text area
scpanet = new JScrollPane(terminal);
// Creating border for scrollpane
// scpanet.setBorder(BorderFactory.createEmptyBorder());
Color color = new Color(220, 220, 220);
terminal.setBackground(color);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scpane, scpanet);
splitPane.setResizeWeight(0.8); // Set initial resize weight (80% for top and 20% for bottom)
// panel.add(scpanet);
splitPane.setUI(new BasicSplitPaneUI() {
@Override
public BasicSplitPaneDivider createDefaultDivider() {
return new BasicSplitPaneDivider(this) {
public void setBorder(Border b) {
}
@Override
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, getSize().width, getSize().height);
super.paint(g);
}
};
}
});
splitPane.setBorder(null);
bottomPanel.add(detailsOfFile);
runX = new Button("βΆοΈ");
runX.setForeground(new Color(0, 153, 51));
runX.setFont(new Font("Arial", Font.PLAIN, 15));
runPanel.add(runX);
runX.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(current_language);
if (current_file_path == "") {
final JFileChooser SaveAs = new JFileChooser();
JFileChooser chooser = new JFileChooser("C:/");
chooser.setAcceptAllFileFilterUsed(false);
// Allowing only text (.txt) files extension to open
FileNameExtensionFilter txt = new FileNameExtensionFilter(".txt files", "txt");
SaveAs.addChoosableFileFilter(txt);
FileNameExtensionFilter py = new FileNameExtensionFilter(".py files", "py");
SaveAs.addChoosableFileFilter(py);
FileNameExtensionFilter cpp_files = new FileNameExtensionFilter(".cpp files", "cpp");
SaveAs.addChoosableFileFilter(cpp_files);
FileNameExtensionFilter js_files = new FileNameExtensionFilter(".js files", "js");
SaveAs.addChoosableFileFilter(js_files);
FileNameExtensionFilter c_files = new FileNameExtensionFilter(".c files", "c");
SaveAs.addChoosableFileFilter(c_files);
FileNameExtensionFilter java_files = new FileNameExtensionFilter(".java files", "java");
SaveAs.addChoosableFileFilter(java_files);
SaveAs.setApproveButtonText("Save");
// Opening the dialog and asking from user where to save the file.
int actionDialog = SaveAs.showOpenDialog(f);
if (actionDialog != JFileChooser.APPROVE_OPTION) {
return;
}
///////////////////////////////////////////////////////////////////
String ext = "";
String extension = SaveAs.getFileFilter().getDescription();
if (extension.equals(".py files")) {
ext = ".py";
} else if (extension.equals(".txt files")) {
ext = ".txt";
} else if (extension.equals(".cpp files")) {
ext = ".cpp";
} else if (extension.equals(".c files")) {
ext = ".c";
} else if (extension.equals(".js files")) {
ext = ".js";
} else if (extension.equals(".java files")) {
ext = ".java";
}
current_file_path = SaveAs.getSelectedFile().getAbsolutePath() + ext;
current_language = ext;
detailsOfFile.setText("Path: " + current_file_path + " Lang: " + current_language);
System.out.println(current_file_path);
System.out.println(extension);
f.setTitle(current_file_path + " - Saturn IDE");
File fileName = new File(SaveAs.getSelectedFile() + ext);
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(fileName));
area.write(outFile);
} catch (IOException ew) {
ew.printStackTrace();
}
try {
// readFileAndHighlight(area, current_file_path, extension);
} catch (Exception es) {
System.out.print(es);
}
lines.setText(getText());
} else if (current_file_path != "") {
File fileName = new File(current_file_path);
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(fileName));
area.write(outFile);
// readFileAndHighlight(area, current_file_path, current_language);
} catch (Exception ea) {
ea.printStackTrace();
}
lines.setText(getText());
}
if (current_language.equals(".py")) {
try {
readFileAndHighlight(area, current_file_path, ".py");
} catch (Exception e1) {
e1.printStackTrace();
}
if (python_name == "") {
python_name = getPythonInterpreterPath();
String out = runCommand(python_name + " " + current_file_path);
all_terminal_text += out + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
} else {
String out = runCommand(python_name + " " + current_file_path);
all_terminal_text += out + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
}
} else if (current_language.equals(".java")) {
try {
readFileAndHighlight(area, current_file_path, ".java");
} catch (Exception e5) {
e5.printStackTrace();
}
if (javac_name == "" && java_name == "") {
javac_name = getJavacInterpreterPath();
java_name = getJavaInterpreterPath();
System.out.println(java_name);
System.out.println(javac_name);
String out = runCommand(javac_name + " " + current_file_path);
String out2 = runJavaFileCommand(java_name, current_file_path);
System.out.println(out + " " + out2);
all_terminal_text += out + "\n" + out2 + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
} else {
System.out.println(java_name);
System.out.println(javac_name);
String out = runCommand(javac_name + " " + current_file_path);
String out2 = runJavaFileCommand(java_name, current_file_path);
System.out.println(out + " " + out2);
all_terminal_text += out + "\n" + out2 + "\n" + exit_code + "\n";
terminal.setText(all_terminal_text);
}
} else if (current_language.equals(".cpp")) {
try {
readFileAndHighlight(area, current_file_path, ".cpp");
} catch (Exception e2) {
e2.printStackTrace();
}
if (cpp_name == "") {
cpp_name = getCppInterpreterPath();
String out = runCppFileCommand(cpp_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
} else {
String out = runCppFileCommand(cpp_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
}
} else if (current_language.equals(".c")) {
try {
readFileAndHighlight(area, current_file_path, ".c");
} catch (Exception e3) {
e3.printStackTrace();
}
if (c_name == "") {
c_name = getCInterpreterPath();
String out = runCFileCommand(c_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
} else {
String out = runCFileCommand(c_name, current_file_path);
all_terminal_text += out + "\n";
terminal.setText(all_terminal_text);
}
} else if (current_language.equals(".js")) {
try {
readFileAndHighlight(area, current_file_path, ".js");
cantRunLanguageAlert("js");
} catch (Exception e4) {
e4.printStackTrace();
}
} else if (current_language.equals(".txt")) {
cantRunLanguageAlert(".txt");
}
}
});
// Setting up the size of frame
f.setSize(980, 480);
// Setting up the layout of frame
f.setLayout(new BorderLayout());
f.add(splitPane, BorderLayout.CENTER);
// Adding panels in frame
// f.add(panel);
// f.add(, BorderLayout.CENTER);
f.add(bottomPanel, BorderLayout.SOUTH);
f.add(runPanel, BorderLayout.NORTH);
// Setting Frame visible to user
f.setLocationRelativeTo(null);
f.setVisible(true);
}
/**
* The darkTheme() function sets the background and foreground colors of various
* components to
* create a dark theme.
*/
public void darkTheme() {
terminal.setBackground(terminal_color_dark);
terminal.setForeground(text_color_dark);