-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
1935 lines (1868 loc) · 84.7 KB
/
Copy pathView.java
File metadata and controls
1935 lines (1868 loc) · 84.7 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
/*
* EzTree by Derek Smith 2011
* derekjsmith@mail.com
*/
package EzTree;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/**
*
* @author Derek Smith
*/
public class View extends JFrame{
public static View thisview;
public static JFrame thisframe;
private final static Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
private final static int screenwidth = screensize.width;
private final static int screenheight = screensize.height;
private final static int framewidth = screenwidth - 200;
private final static int frameheight = screenheight - 200;
private final static int location_x = (screenwidth - framewidth)/2;
private final static int location_y = (screenheight - frameheight)/2;
private static LinkedList<String> Rawlines; // first file read, sometimes contains formatting code so has to be meticulously trimmed
private static LinkedList<String> Trimmedlines; // no spaces
public static LinkedList<String> Keys; // amount of spaces
public static LinkedList<String> Masterlist; // both the text and key put back together
private static Map<Integer, String> Tablemap; // the table of contents with numbers added
private static ArrayList<Integer> Tablemapkeys; // the indexes of the titles
private static String[] Tableofcontents; // the titles
public static ArrayList<String> Numbered_tree;
public static int START_NUMBERING_AT_LINE = 0;
public static ArrayList<Integer> Levels;
private static JTextArea jtextarea;
public static JTextArea writingArea;
private static TextboxHandler textbox_handler; // custom
public static Action actionCutText;
public static Action actionPasteText;
public static Action actionTextRight;
public static Action actionTextLeft;
public static Action actionClosePopup;
public static JPopupMenu popup_menu;
public static JMenuItem popup_menu_item1;
public static JMenuItem popup_menu_item2;
public static JMenuItem popup_menu_item3;
public static JMenuItem popup_menu_item4;
public static JMenuItem popup_menu_item5;
public static String SELECTED_TEXT = "";
public static boolean CLICKED_SCREEN_ONE = false;
public static int SCREEN_ONE_POSITION = 0;
private static ArrayList<Box> boxes;
public static JList jlist;
private static JSplitPane groovepane;
private static JTabbedPane tabbedpane;
public static boolean closed_file = false;
public static JComboBox combobox;
private static JToolBar top_toolbar;
public static JTextField textbox, file_label;
public static JLabel search_label, file_size_label;
private static JButton search_button, next_button, previous_button, up_button, down_button;
private static int Masterlistindex = 0;
private static String search_string = "";
private static int current_search_index = -1;
private static ArrayList<Integer> search_results;
public static String currentfilename = ""; // includes path
private static String shortfilename = "";
public static String newfilename = "";
private static JTree tree;
private static DefaultTreeModel treemodel;
private static DefaultMutableTreeNode rootnode = new DefaultMutableTreeNode("START");
public static String FILE_SIZE;
private static String intro_message =
"Spacer version 5.7\n"
+ "by Derek James Smith\n"
+ "email: support@infiniteoutline.com\n"
+ "copyright 2011 treeconverter.com\n"
+ "all rights reserved\n"
+ "--------What it does--------\n"
+ "Reads and writes .txt files (must resave Microsoft Word, .rtf, or Linux files as .txt files).\n"
+ "Converts outlines to data trees.\n"
+ "Write an outline into the writer area, then view it in the data-tree viewer or the table-of-contents viewer.\n"
+ "--------Disclaimer------\n"
+ "It is your responsibility to manage your own data.\n"
+ "We assume no liability for data loss.\n"
+ "We also make no guarantees that this program will run without errors.\n"
+ "We assume no responsibility for any computer problems experienced while running this program.";
private static String intro_message2 =
" Spacer version 5.7\ncopyright 2011 treeconverter.com\n\n --------What it does--------\n reads and writes .txt files (must resave .rtf or Word files as .txt files),\n finds any part of your outline instantly!\n but, you must organize the outline according to certain rules...\n the first screen allows you to create, change, or save an outline,\n select and right-click to quickly rearrange entire sections,\n the second two screens are for quickly reading an outine,\n if there are formatting mistakes, they are immediately reported.\n\n --------Formatting rules--------\n the far-left headings must have no indentations and must be unique, \n must indent evenly within each section,\n basically, just write an outline that has perfectly even spacing,\n do not use tabs,\n blank lines are removed.\n\n --------Example:--------\n EZ-TREE\n What it does\n reads and writes text files\n only .txt files\n will not read .rtf or Word files\n but, just resave them as .txt files\n helps you instantly find topics in your outline\n warns you if formatting rules are broken\n Formatting rules\n ...";
private static boolean case_sensitive_search = false;
private static boolean exact_matches_only_search = false;
private static Debugger debugger;
private static boolean MAKING_NEW_FILE = false;
private static boolean EMPTY_FILE = false;
public static boolean HAS_ERRORS = false;
public static int TAB_CLICKED = 0;
public static int LAST_TAB_CLICKED = 0;
public static boolean SYNC_TABLE_OF_CONTENTS = true;
public View() {
super("------------------------------ Spacer ----------------------------------------");
thisview = this;
thisframe = this;
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("starlogo_light_blue.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(framewidth, frameheight);
setLocation(location_x, location_y);
setBackground(Color.WHITE);
init_folder();
initarrays();
initcomponents(); // the string array for the jlist had to be made first
splash_screen();
setVisible(true);
}
public void splash_screen(){
JPanel splashScreen = new JPanel();
splashScreen.setSize(500, 500);
splashScreen.setBackground(Color.white);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
splashScreen.setLayout(layout);
ImageIcon img = new ImageIcon(getClass().getResource("Spacer.png"));
JLabel imagelabel = new JLabel(img);
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(imagelabel, constraints);
splashScreen.add(imagelabel);
for (int count = 0; count < 3; ++count){
JLabel spacer = new JLabel("\t");
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(spacer, constraints);
splashScreen.add(spacer);
}
JLabel instructions_label = new JLabel("to get started, please select 'info' from the main menu");
constraints.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(instructions_label, constraints);
splashScreen.add(instructions_label);
tabbedpane.addTab("splash screen", null, splashScreen, "splash screen");
}
public String applicationPath(){
String path = View.class.getProtectionDomain().getCodeSource().getLocation().getPath();
if (path.endsWith("/build/classes/")){ // Netbeans
path = path.replace("/build/classes/", "");
} else if (path.endsWith("\\build\\classes\\")){
path = path.replace("\\build\\classes\\", "");
} else if (path.endsWith(".jar")){
File file = new File(path);
path = file.getParent();
}
path = path.replace("%20", " ");
return path;
//return System.getProperty("user.dir");
}
public void init_folder(){
File folder = new File(applicationPath() + "/outlines");
if (!folder.exists() && !folder.isDirectory()){
folder.mkdir();
}
}
// INITVARIABLES
public void initarrays() {
Rawlines = new LinkedList<String>(); // first file read
Trimmedlines = new LinkedList<String>(); // just the text
Keys = new LinkedList<String>(); // just the key
Masterlist = new LinkedList<String>(); // both
Tablemap = new HashMap<Integer, String>(); // the table of contents as key and heading text
Tablemapkeys = new ArrayList<Integer>();
if (MAKING_NEW_FILE == true){
Tableofcontents = new String[100]; // the titles
}
search_results = new ArrayList<Integer>();
Numbered_tree = new ArrayList<String>();
Levels = new ArrayList<Integer>();
}
// LOAD AND READ FILE
public String getfilesize(){
return FILE_SIZE;
}
public static void setfilesize(long size){
if (size > 999999999){
int gb = (int)(size/1000000000);
int mb = (int)(size % 1000000000);
mb = (int)(mb/100000000);
FILE_SIZE = "" + gb + "." + mb + " GB";
} else if (size > 999999){
int mb = (int)(size/1000000);
int kb = (int)(size % 1000000);
kb = (int)(kb/100000);
FILE_SIZE = "" + mb + "." + kb + " MB";
} else if (size > 999){
int s = (int)(size/1000);
FILE_SIZE = "" + s + " KB";
} else {
FILE_SIZE = "" + size + " bytes";
}
file_size_label.setText(FILE_SIZE);
}
public boolean loadfileintoarrays(String... folder_file) {
try {
String folder = "";
String filename = "";
if (folder_file.length == 2){
folder = folder_file[0];
filename = folder_file[1];
} else {
folder = applicationPath();
filename = getfilename();
}
if (filename.equals("keeplooking")) {
String filepath = usefilechooser();
if (filepath != null) {
folder = "infilename";
filename = filepath;
} else {
JOptionPane.showMessageDialog(null, "unknown error");
System.exit(1);
}
} else if (filename.equals("")){
return false;
} else {
if (folder_file.length != 2){
folder = applicationPath() + "/" + "outlines";
}
}
loadfile(folder, filename);
currentfilename = folder.equals("infilename")? filename : folder + "/" + filename;
shortfilename = returnshortfilename();
maketableofcontents();
makemasterlist();
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Problem in loadfileintoarrays " + exception.toString());
return false;
}
return true;
}
public String getfilename() {
String result = "";
String[] strings = new File(applicationPath() + "/outlines").list();
boolean keepgoing = true;
while (keepgoing){
boolean keepgo = false;
for (int count = 0; count < strings.length; ++count){
if (count+1 < strings.length){
String s1 = strings[count].toLowerCase();
String s2 = strings[count+1].toLowerCase();
if (s2.charAt(0) < s1.charAt(0)){
String temp = strings[count];
strings[count] = strings[count+1];
strings[count+1] = temp;
keepgo = true;
}
}
}
keepgoing = keepgo;
}
Filedialog2 filedialog = new Filedialog2(new javax.swing.JFrame(), strings, true, true);
filedialog.setVisible(true);
currentfilename = filedialog.filename;
result = currentfilename;
return result;
}
public String returnlongfilename(){
File file = new File(currentfilename);
String result = file.getAbsolutePath();
return result;
}
public String returnshortfilename(){
String result = "";
try{
File file = new File(currentfilename);
result = file.getName();
} catch (Exception exception){}
return result;
}
public String returnpath(){ // folder name
String result = "";
try{
File file = new File(currentfilename);
if (file.isAbsolute()){
result = file.getParent();
} else {
result = "outlines";
}
} catch (Exception exception){}
return result;
}
public String usefilechooser() {
String result = "";
JFileChooser filechooser = new JFileChooser();
filechooser.setDialogTitle("******* PLEASE CHOOSE A .TXT FILE *********");
filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int chosefile = filechooser.showOpenDialog(null);
if (chosefile == JFileChooser.CANCEL_OPTION) {
result = null;
} else {
File pathname = filechooser.getSelectedFile();
if (pathname == null || pathname.getName().equals("")) {
JOptionPane.showMessageDialog(null,"unknown error");
System.exit(1);
}
result = pathname.getAbsolutePath();
currentfilename = result;
}
return result;
}
public void loadfile(String foldername, String filename) {
try {
if (foldername.equalsIgnoreCase(applicationPath() + "/" + "outlines")) {
File folder = new File(foldername);
if (folder.exists() && folder.isDirectory()) {
if (filename.endsWith(".txt")) {
File file = new File(folder, filename);
if (file.exists() && file.isFile()) {
readfile(file);
} else {
throw new Exception("File not found");
}
} else {
throw new Exception("Wrong file extension");
}
}
} else if (foldername.equals("infilename")) {
if (filename.endsWith(".txt")) {
File file = new File(filename);
if (file.exists() && file.isFile()) {
readfile(file);
} else {
throw new Exception("File not found");
}
} else {
throw new Exception("Wrong file extension");
}
} else {
throw new Exception("outlines folder not found ");
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, exception.getMessage() + exception.toString());
//System.exit(1);
}
}
public void readfile(File file) {
try {
EMPTY_FILE = false;
Rawlines.clear();
Scanner filescan = new Scanner(file);
while (filescan.hasNextLine()) {
String s = filescan.nextLine();
Rawlines.add(s);
}
if (filescan != null) {
filescan.close();
}
if (Rawlines.isEmpty()) {
EMPTY_FILE = true;
//throw new Exception("Empty file");
}
boolean tab_test = false;
Map<Integer, String> boxmap = new HashMap<Integer, String>();
String bads = "";
for (int count = 0; count < Rawlines.size(); ++count){
String r = Rawlines.get(count);
if (r.contains("\t")){
tab_test = true;
boxmap.put(count, r);
bads = bads + (count + 1) + ", ";
}
}
String message = "";
if (tab_test == true){
HAS_ERRORS = true;
message = "You have used tabs.\nPlease remove all tabs, they have been marked in red.\nHere are the first ten lines with tabs (counting from 1):\n" + bads + "\n";
if (debugger != null && debugger.is_open()){
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger != null && !debugger.is_open()){
debugger.open();
debugger.clear_message();
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger == null){
debugger = new Debugger(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
}
}
String empty_test = "";
for (String r : Rawlines){
r = r.trim();
empty_test += r;
}
if (empty_test.equals("")){
EMPTY_FILE = true;
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Problem in readfile, " + exception.getMessage());
}
}
public void loadstringintoarrays(String strng) {
try {
Rawlines.clear();
String[] strngs = strng.split("\n");
for (String s: strngs){
Rawlines.add(s);
}
if (Rawlines.isEmpty()) {
EMPTY_FILE = true;
//throw new Exception("Empty file");
}
boolean tab_test = false;
Map<Integer, String> boxmap = new HashMap<Integer, String>();
String bads = "";
for (int count = 0; count < Rawlines.size(); ++count) {
String r = Rawlines.get(count);
if (r.contains("\t")) {
tab_test = true;
boxmap.put(count, r);
bads = bads + (count + 1) + ", ";
}
}
String message = "";
if (tab_test == true) {
HAS_ERRORS = true;
message = "You have used tabs.\nPlease remove all tabs, they have been marked in red.\nHere are the first ten lines with tabs (counting from 1):\n" + bads + "\n";
if (debugger != null && debugger.is_open()) {
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger != null && !debugger.is_open()) {
debugger.open();
debugger.clear_message();
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger == null) {
debugger = new Debugger(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
}
}
String empty_test = "";
for (String r : Rawlines) {
r = r.trim();
empty_test += r;
}
if (empty_test.equals("")) {
EMPTY_FILE = true;
}
maketableofcontents();
makemasterlist();
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Problem in loadstringintoarrays, " + exception.getMessage());
}
}
// finds header lines from outline, makes linenumbers, trims keys and text, stores them separate in tablemap and together in tableofcontents
public void maketableofcontents() {
if (EMPTY_FILE){
//return;
}
ArrayList<String> temptable = new ArrayList<String>();
boolean has_repeats = false;
int chapternumber = 0;
char[] chars = new char[300];
Rawlines.remove("\n");
Rawlines.remove("\r");
ArrayList<String> badones = new ArrayList<String>();
Map<Integer, String> boxmap = new HashMap<Integer, String>();
int trimmedcount = -1;
for (String s : Rawlines) {
if (!s.trim().equals("")){
++trimmedcount;
}
chars = s.toCharArray();
if (chars.length == 0) {
continue;
}
if (!(Character.isLetterOrDigit(chars[0]) || Character.isDefined(chars[0]))) {
continue;
}
if (chars[0] == ' ' || chars[0] == '\t') { // rule: first level has no indentation
continue;
}
temptable.add(s);
//Tableofcontents[chapternumber] = s;
++chapternumber;
if (Tablemap.containsValue(s)){
has_repeats = true;
badones.add(s);
boxmap.put(trimmedcount, s);
}
Tablemap.put(chapternumber, s); // creates the option to list each chapter with the chapter number in front of it, but these numbers are one higher than Tableofcontents array index
Tablemapkeys.add(chapternumber);
}
Tableofcontents = new String[temptable.size()];
for (int count = 0; count < temptable.size(); ++count){
Tableofcontents[count] = temptable.get(count);
}
temptable.clear();
temptable = null;
if (has_repeats == true){
HAS_ERRORS = true;
String bads = "";
for (int count = 0; count < (badones.size() < 10? badones.size() : 10); ++count){
bads += badones.get(count) + "\n";
}
String message = "There are repeat headings, screen 2 will not work\n"
+ "Number of lines with problems: " + badones.size()
+ "\nRepeat headings have been marked in red."
+ "\nHere are the first ten repeat headings:\n"
+ bads;
if (debugger != null && debugger.is_open()){
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger != null && !debugger.is_open()){
debugger.open();
debugger.clear_message();
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger == null){
debugger = new Debugger(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
}
}
}
// trims the keys and texts and stores them in Textmap and trimmedlines
public void makemasterlist() {
if (EMPTY_FILE){
//return;
}
char[] chars = new char[300];
for (String s : Rawlines) {
chars = s.toCharArray();
StringBuilder stringbuilder = new StringBuilder("");
if (chars.length == 0) {
continue;
}
int textindex = -1;
int tabindex = 0;
for (char c : chars) {
if (c == ' ') {
++textindex;
} else if (c == '\t') {
++tabindex;
++textindex;
} else if (Character.isLetterOrDigit(c) || Character.isDefined(c)) {
++textindex;
break;
}
}
if (textindex != -1) {
if (tabindex > 0) {
for (int i = 0; i < tabindex; ++i) {
stringbuilder.append("\t");
}
Keys.add(stringbuilder.toString());
Trimmedlines.add(s.substring(textindex));
Masterlist.add(s);
} else {
int keyindex = (textindex == 0 ? 0 : textindex); // previously textindex - 1
Keys.add(s.substring(0, keyindex));
Trimmedlines.add(s.substring(textindex));
Masterlist.add(s);
}
}
} // end first loop
}
public void maketree() {
if (EMPTY_FILE){
return;
}
ArrayList<DefaultMutableTreeNode> nodelist = new ArrayList<DefaultMutableTreeNode>();
for (String s : Trimmedlines) {
nodelist.add(new DefaultMutableTreeNode(s));
}
rootnode.add((DefaultMutableTreeNode) nodelist.get(0));
if (Numbered_tree != null){
Numbered_tree.clear();
} else {
Numbered_tree = new ArrayList<String>();
}
if (Levels != null){
Levels.clear();
} else {
Levels = new ArrayList<Integer>();
}
try{
processtree(-1, 0, nodelist);
} catch (Exception exc){
JOptionPane.showMessageDialog(null, "unknown error in process tree: " + exc);
}
// heading numbers might have been skipped because they are all written first
boolean didnt_number_headings = false;
if (START_NUMBERING_AT_LINE < Numbered_tree.size() && !Character.isDigit(Numbered_tree.get(START_NUMBERING_AT_LINE).charAt(0))){
String s = Numbered_tree.get(START_NUMBERING_AT_LINE);
if (!s.contains(".")){
didnt_number_headings = true;
} else {
String num = s.split("[.]")[0].trim();
boolean all_numbers = true;
for (char c : num.toCharArray()){
if (!Character.isDigit(c)){
all_numbers = false;
}
}
if (!all_numbers){
didnt_number_headings = true;
}
}
}
if (didnt_number_headings){
int addcount = 1;
for (int count = START_NUMBERING_AT_LINE; count < Numbered_tree.size(); ++count){
if (!Numbered_tree.get(count).startsWith(" ")){
String line = Numbered_tree.get(count);
line = "" + addcount + ". " + line;
Numbered_tree.set(count, line);
++addcount;
}
}
}
Map<Integer, String> boxmap = new HashMap<Integer, String>();
if (Masterlist.get(0).startsWith(" ")){
String message = "Your first line should not be indented.\n"
+ "Even if you have an information area at the top of the file,\n"
+ "it still must fit into the tree correctly.\n"
+ "But, you can still make an area at the top with no line-numbers.\n";
boxmap.put(0, Masterlist.get(0));
if (debugger != null && debugger.is_open()) {
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger != null && !debugger.is_open()) {
debugger.clear_message();
debugger.add_message(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
} else if (debugger == null) {
debugger = new Debugger(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
}
} else {
try{
boolean incomplete = false;
ArrayList<String> badones = new ArrayList<String>();
int count = 1;
for (DefaultMutableTreeNode dmtn : nodelist) {
if (dmtn.getParent() == null) {
incomplete = true;
int rawcount = -1; // for external text file with untrimmed blank lines
for (int r = 0; r < Rawlines.size(); ++r){
String trimmedraw = Rawlines.get(r).trim();
if (trimmedraw.equals(dmtn.toString())){
rawcount = r;
break;
}
}
if (!boxmap.containsKey(count)){ // just use 'count' to fix within eztree
badones.add("line #" + count + ": " + dmtn.toString());
boxmap.put(count - 1, dmtn.toString());
}
}
++count;
}
if (incomplete == true) {
HAS_ERRORS = true;
String bads = "";
for (count = 0; count < (badones.size() < 10? badones.size() : 10); ++count){
bads += badones.get(count) + "\n";
}
String message = "Your indentation is uneven.\n"
+ "The tree view will have missing lines unless you straighten all the text.\n"
+ "Check the rules on the help menu.\n"
+ "Uneven lines have been marked in red.\n"
+ "Number of lines with problems: " + badones.size()
+ "\nHere are the first ten problems, with line numbers (counting from 1):\n"
+ bads;
if (debugger != null && debugger.is_open()){
debugger.add_message(message);
debugger.set_boxes(boxmap, thisview);
} else if (debugger != null && !debugger.is_open()){
debugger.clear_message();
debugger.add_message(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
} else if (debugger == null){
debugger = new Debugger(message);
debugger.open();
debugger.set_boxes(boxmap, thisview);
}
}
} catch (Exception exc){
JOptionPane.showMessageDialog(null, exc.getMessage());
}
}
}
public void processtree(int thisindex, int nextindex, ArrayList<DefaultMutableTreeNode> nodelist){
if (EMPTY_FILE){
return;
}
int COUNTER = 0;
for (int start = thisindex; start < nodelist.size(); ++start){
thisindex = start;
nextindex = start + 1;
if (Keys.size() == nextindex){
return;
}
if (thisindex == -1){ // root node
rootnode.add((DefaultMutableTreeNode) nodelist.get(0));
Levels.add(nodelist.get(0).getLevel());
if (Numbered_tree.size() < START_NUMBERING_AT_LINE){
Numbered_tree.add(Trimmedlines.get(0));
} else {
++COUNTER;
Numbered_tree.add(String.format("%d. %s", 1, Trimmedlines.get(0)));
}
} else if (Keys.get(nextindex).length() == Keys.get(thisindex).length()){ // sibling
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)nodelist.get(thisindex).getParent();
parent.add((DefaultMutableTreeNode)nodelist.get(nextindex));
Levels.add(nodelist.get(nextindex).getLevel());
if (nextindex < START_NUMBERING_AT_LINE){
Numbered_tree.add(String.format("%s%s", Keys.get(nextindex), Trimmedlines.get(nextindex)));
} else {
if (parent == rootnode){
++COUNTER;
String str = String.format("%d. %s", COUNTER, Trimmedlines.get(nextindex));
Numbered_tree.add(str);
} else {
Numbered_tree.add(String.format("%s%d. %s", Keys.get(nextindex), parent.getIndex(nodelist.get(nextindex)) + 1, Trimmedlines.get(nextindex)));
}
}
} else if (Keys.get(nextindex).length() < Keys.get(thisindex).length()){ // more shallow
boolean success = false;
//int LEFTMOST = Keys.get(nextindex).length();
for (int count = thisindex; count >= 0; --count){
if (Keys.get(count).length() == Keys.get(nextindex).length()){// && Keys.get(count).length() <= LEFTMOST){
success = true;
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)nodelist.get(count).getParent();
parent.add((DefaultMutableTreeNode)nodelist.get(nextindex));
Levels.add(nodelist.get(nextindex).getLevel());
if (nextindex < START_NUMBERING_AT_LINE){
Numbered_tree.add(String.format("%s%s", Keys.get(nextindex), Trimmedlines.get(nextindex)));
} else {
if (parent == rootnode){
++COUNTER;
String str = String.format("%d. %s", COUNTER, Trimmedlines.get(nextindex)); //parent.getIndex(nodelist.get(nextindex)) + 1 - START_NUMBERING_AT_LINE, Trimmedlines.get(nextindex));
Numbered_tree.add(str);
} else {
Numbered_tree.add(String.format("%s%d. %s", Keys.get(nextindex), nodelist.get(nextindex).getParent().getIndex(nodelist.get(nextindex)) + 1, Trimmedlines.get(nextindex)));
}
}
break;
//} else if (Keys.get(count).length() == 0){// && LEFTMOST != Keys.get(nextindex).length()){
//break;
} else if (Keys.get(count).length() < Keys.get(nextindex).length()){
break;//LEFTMOST = Keys.get(count).length();
}
}
if (success == false){
// finds afterward
}
} else if (Keys.get(nextindex).length() > Keys.get(thisindex).length()){ //child
nodelist.get(thisindex).add(nodelist.get(nextindex));
Levels.add(nodelist.get(nextindex).getLevel());
if (nextindex < START_NUMBERING_AT_LINE){
Numbered_tree.add(String.format("%s%s", Keys.get(nextindex), Trimmedlines.get(nextindex)));
} else {
Numbered_tree.add(String.format("%s%d. %s", Keys.get(nextindex), nodelist.get(nextindex).getParent().getIndex(nodelist.get(nextindex)) + 1, Trimmedlines.get(nextindex)));
}
} else {
//
}
}
}
public void processtree_recursive(int thisindex, int nextindex, ArrayList<DefaultMutableTreeNode> nodelist) {
if (EMPTY_FILE) {
return;
}
if (Keys.size() == nextindex){ //(nextindex >= Keys.size() - 1) {
return;
}
if (thisindex == -1) {
int level = Keys.get(nextindex).length();
for (int index = 0; index < Trimmedlines.size(); ++index) {
if (Keys.get(index).length() == level) {
rootnode.add((DefaultMutableTreeNode) nodelist.get(index));
try{
Levels.add(nodelist.get(index).getLevel());
if (Numbered_tree.size() < START_NUMBERING_AT_LINE){
Numbered_tree.add(Trimmedlines.get(index));
} else {
Numbered_tree.add(String.format("%d. %s", nodelist.get(index).getParent().getIndex(nodelist.get(index)) + 1 - START_NUMBERING_AT_LINE, Trimmedlines.get(index)));
}
} catch (Exception exc){
// file has heading or spacing errors
}
}
}
processtree(0, 1, nodelist);
} else if (Keys.get(nextindex).length() == Keys.get(thisindex).length()) {
if (nextindex < Trimmedlines.size() - 1) {
processtree(nextindex, nextindex + 1, nodelist);
}
} else if (Keys.get(nextindex).length() < Keys.get(thisindex).length()) {
if (nextindex < Trimmedlines.size() - 1) {
processtree(nextindex, nextindex + 1, nodelist);
}
} else if (Keys.get(nextindex).length() > Keys.get(thisindex).length()) {
int level = Keys.get(nextindex).length(); // custom - how many blanks
for (int index = thisindex; index < Trimmedlines.size(); ++index) {
if (Keys.get(index).length() < level && index != thisindex) {
break;
}
if (Keys.get(index).length() == level) {
((DefaultMutableTreeNode) nodelist.get(thisindex)).add((DefaultMutableTreeNode) nodelist.get(index));
DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)nodelist.get(index);
try{
Levels.add(thisindex + nodelist.get(thisindex).getChildCount(), dmtn.getLevel());
if (index < START_NUMBERING_AT_LINE){
Numbered_tree.add(thisindex + nodelist.get(thisindex).getChildCount(), String.format("%s%s", Keys.get(index), Trimmedlines.get(index)));
} else {
Numbered_tree.add(thisindex + nodelist.get(thisindex).getChildCount(), String.format("%s%d. %s", Keys.get(index), nodelist.get(index).getParent().getIndex(nodelist.get(index)) + 1, Trimmedlines.get(index)));
}
} catch (Exception exc){
// file has heading or spacing errors
}
}
}
if (nextindex < Trimmedlines.size() - 1) {
processtree(nextindex, nextindex + 1, nodelist);
}
}
}
class Tabhandler implements MouseListener {
public void mouseClicked(MouseEvent event) {
JTabbedPane source = (JTabbedPane)event.getSource();
TAB_CLICKED = source.getSelectedIndex();
if (TAB_CLICKED == LAST_TAB_CLICKED){
return;
}
boolean reset_tabs = false;
int tab = 0;
switch (source.getSelectedIndex()){
case 0:
reset_tabs = false;
tab = 0;
break;
case 1:
reset_tabs = true;
tab = 1;
break;
case 2:
reset_tabs = true;
tab = 2;
break;
default:
reset_tabs = false;
tab = 0;
break;
}
LAST_TAB_CLICKED = tab;
if (reset_tabs == true){
thisview.remove_tabs();
thisview.initarrays();
thisview.loadstringintoarrays(thisview.writingArea.getText());
thisview.maketree();
thisview.init_tabs();
thisview.set_writing_area(thisview.get_text_for_writing_area());
thisview.clean_up_resources();
thisview.jlist.setSelectedIndex(0);
thisview.file_label.setText(" " + thisview.returnshortfilename());
thisview.closed_file = false;
thisview.tabbedpane.setSelectedIndex(tab);
}
}
public void mousePressed(MouseEvent event){
//
}
public void mouseReleased(MouseEvent event){
//
}
public void mouseEntered(MouseEvent event){
//
}
public void mouseExited(MouseEvent event){
//
}
}
// INITCOMPONENTS
public void initcomponents() {
tabbedpane = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
Tabhandler tabhandler = new Tabhandler();
tabbedpane.addMouseListener(tabhandler);
add(tabbedpane, BorderLayout.CENTER);
top_toolbar = new JToolBar();
top_toolbar.setOrientation(JToolBar.HORIZONTAL);
top_toolbar.setBorder(new MatteBorder(1, 1, 1, 1, Color.GRAY));
top_toolbar.setFloatable(false);
add(top_toolbar, BorderLayout.NORTH);
combobox = new JComboBox();
combobox.setToolTipText("main menu");
combobox.addItem(" MAIN MENU");
combobox.addItem("New File");
combobox.addItem("Open File");
combobox.addItem("Close File");
combobox.addItem("Save File");
combobox.addItem("Numbering");
combobox.addItem("Options");
combobox.addItem("Delete File");
combobox.addItem("Info");
combobox.addItem("Exit");
Comboboxhandler comboboxhandler = new Comboboxhandler();
combobox.addItemListener(comboboxhandler);
combobox.setMaximumRowCount(10);
top_toolbar.add(combobox);
init_actions();
top_toolbar.add(actionCutText);
top_toolbar.add(actionPasteText);
top_toolbar.add(actionTextLeft);
top_toolbar.add(actionTextRight);
search_label = new JLabel("TYPE SEARCH TERM AND PRESS ENTER: ");
textbox = new JTextField(30);
textbox.setEditable(true);
textbox.setEnabled(false);
textbox.setToolTipText("search box");
textbox_handler = new TextboxHandler();
textbox.addActionListener(textbox_handler);
search_button = new JButton(" SEARCH");
search_button.setToolTipText("search");
search_button.setEnabled(false);
next_button = new JButton("NEXT ");
next_button.setToolTipText("next search result");
next_button.setEnabled(false);
previous_button = new JButton("PREVIOUS");
previous_button.setToolTipText("previous search result");
previous_button.setEnabled(false);
Buttonhandler buttonhandler = new Buttonhandler();
search_button.addActionListener(buttonhandler);
next_button.addActionListener(buttonhandler);
previous_button.addActionListener(buttonhandler);
top_toolbar.add(textbox);
top_toolbar.add(search_button);
top_toolbar.add(next_button);
top_toolbar.add(previous_button);
up_button = new JButton(" UP ");
up_button.setToolTipText("move second screen up");
up_button.setEnabled(false);
down_button = new JButton("DOWN");
down_button.setToolTipText("move second screen down");
down_button.setEnabled(false);
up_button.addActionListener(buttonhandler);
down_button.addActionListener(buttonhandler);
top_toolbar.add(up_button);
top_toolbar.add(down_button);
file_label = new JTextField(10);
file_label.setText("closed");
top_toolbar.add(file_label);
file_label.setEditable(false);
file_size_label = new JLabel("");