/
LAmiableWordsInterface.java
2136 lines (2013 loc) · 85 KB
/
LAmiableWordsInterface.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.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class LAmiableWordsInterface extends JPanel{// implements ActionListener{
private static final long serialVersionUID = 1L;
//private static JLabel res;
private static final String INVALID_SIZE = "Alphabet must be of at least size 2.";
//private static final float CENTER_ALIGNMENT = 0;
public SwingWorker<Void, Void> mySwingWorker;
public JFrame frame;
@SuppressWarnings("resource")
public LAmiableWordsInterface() {
JPanel holder = new JPanel();
holder.setLayout(new BoxLayout(holder, BoxLayout.Y_AXIS));
JPanel matrixholder = new JPanel();
matrixholder.setLayout(new BoxLayout(matrixholder, BoxLayout.X_AXIS));
matrixholder.setAlignmentY(Component.TOP_ALIGNMENT);
JPanel outputholder = new JPanel();
outputholder.setLayout(new BoxLayout(outputholder, BoxLayout.Y_AXIS));
if (!Home.save.isSelected()) {
/*enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e3){
DialogWait wait = new DialogWait();
mySwingWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception { */
try { //checking if alphabet input is valid
Home.res.setVisible(false);
JPanel info1 = new JPanel();
JLabel infoText = new JLabel("The red words are your entered word and its");
JLabel link = new JLabel("<html><u>Lyndon conjugate.</u><html>");
link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
link.setForeground(Color.BLUE);
link.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>The Lyndon conjugate of a word is the conjugate that is<br>lexicographically smallest based on the order of the alphabet.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
info1.add(infoText);
info1.add(link);
outputholder.add(info1);
JPanel info2 = new JPanel();
//info2.setBorder(BorderFactory.createEmptyBorder(0,0,20,0));
JLabel infoText2 = new JLabel("The orange words are the words that are");
JLabel link2 = new JLabel("<html><u>L-amiable</u><html>");
JLabel infoText3 = new JLabel("with your word, and their Lyndon conjugates.");
link2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
link2.setForeground(Color.BLUE);
link2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>Two words are L-amiable if they share<br>the same Parikh and L-Parikh matrices.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
info2.add(infoText2);
info2.add(link2);
info2.add(infoText3);
outputholder.add(info2);
JLabel infoText4 = new JLabel("When a word's Lyndon conjugate is of interest, we display both words together as follows: Original Word (Lyndon Conjugate)");
JPanel info3 = new JPanel();
info3.setBorder(BorderFactory.createEmptyBorder(0,0,20,0));
info3.add(infoText4);
outputholder.add(info3);
outputholder.setVisible(true);
String alphIn = Home.alphabetInput.getText();
int enteredAlphabet;
if (alphIn.length()==0){
enteredAlphabet = 0;
}
else {
enteredAlphabet=Integer.parseInt(Home.alphabetInput.getText());
}
if(isValid(enteredAlphabet)) {
Home.res.setVisible(false);
try { //checking if word input is valid
//setProgress(1);
if (Home.mySwingWorker.isCancelled()) {
return;
}
Component[] ocomponents = outputholder.getComponents();
for (Component ocomponent : ocomponents) {
outputholder.remove(ocomponent);
}
Component[] mcomponents = matrixholder.getComponents();
for (Component mcomponent : mcomponents) {
matrixholder.remove(mcomponent);
}
outputholder.setVisible(true);
link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
link.setForeground(Color.BLUE);
link.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>The Lyndon conjugate of a word is the conjugate that is<br>lexicographically smallest based on the order of the alphabet.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
info1.add(infoText);
info1.add(link);
outputholder.add(info1);
//info2.setBorder(BorderFactory.createEmptyBorder(0,0,20,0));
link2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
link2.setForeground(Color.BLUE);
link2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>Two words are L-amiable if they share<br>the same Parikh and L-Parikh matrices.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
info2.add(infoText2);
info2.add(link2);
info2.add(infoText3);
outputholder.add(info2);
info3.add(infoText4);
outputholder.add(info3);
String enteredWord = Home.wordInput.getText();
if (enteredWord.length()==0){
throw new Exception("No word.");
}
ParikhMatrixCalculator pM = new ParikhMatrixCalculator();
int[] word = new int[enteredWord.length()];
int[] finalWord = new int[enteredWord.length()];
if (!isNumber(enteredWord)) {
for (int i=0;i<enteredWord.length();i++) {
String character = Character.toString(enteredWord.charAt(i));
int l = pM.getNumberForChar(character)-1;
if (l>=0){
word[i] = l;
finalWord[i]=l;
}
else {
throw new Exception("Used letters and numbers.");
}
}
}
else {
for (int i=0;i<enteredWord.length();i++) {
int l = Character.getNumericValue(enteredWord.charAt(i));
word[i] = l;
finalWord[i] = l;
}
}
//check alphabet size is suitable for word
int maxLetter = max(word);
if (alphIn.length()==0) {
enteredAlphabet = maxLetter+1;
}
if (maxLetter>=enteredAlphabet) {
throw new Exception("Alphabet size not possible.");
}
//setProgress(2);
if (Home.mySwingWorker.isCancelled()) {
return;
}
int[][] parikhMatrix = pM.ParikhMatrix(word,enteredAlphabet+1);
//add matrix to panel, then add to frame
JPanel matrix = new JPanel(new GridLayout(parikhMatrix.length,parikhMatrix.length));
for (int i=0;i<parikhMatrix.length;i++) {
for (int j=0;j<parikhMatrix.length;j++) {
JLabel current = new JLabel(Integer.toString(parikhMatrix[i][j]));
current.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
current.setAlignmentX(Component.CENTER_ALIGNMENT);
matrix.add(current);
}
}
//setProgress(3);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//adding brackets to either side of matrix
//open bracket
BufferedImage open = null;
try {
open = ImageIO.read(getClass().getResource("/Images/OpenBracketClear.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
Dimension matsize = matrix.getPreferredSize();
int width = (int) Math.round(matsize.getHeight()/70);
Image opendimg = open.getScaledInstance(15+width, (int) matsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon openIcon = new ImageIcon(opendimg);
JLabel openLabel = new JLabel(openIcon);
//close bracket
BufferedImage close = null;
try {
close = ImageIO.read(getClass().getResource("/Images/CloseBracketClear.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
Image dclose = close.getScaledInstance(15+width, (int) matsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon closeIcon = new ImageIcon(dclose);
JLabel closeLabel = new JLabel(closeIcon);
//add borders to brackets to give some space
openLabel.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
closeLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,20));
//add to panel
JPanel minimatrix = new JPanel();
JPanel parikh = new JPanel();
parikh.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel parikhMatrixl = new JLabel("<html><u>Parikh Matrix</u><html>:");
parikhMatrixl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
parikhMatrixl.setForeground(Color.BLUE);
parikhMatrixl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>The Parikh matrix of a word has size (k+1)x(k+1),<br>where k is the size of the alphabet. The diagonal<br>of the matrix is populated with 1's and all elements<br>below it are 0. The count of all subwords that consist<br>of consecutive letters in the alphabet and are of<br>length n in the word are found on the n-diagonal.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
Font parikhMatrixFontl = parikhMatrixl.getFont();
parikhMatrixl.setFont(new Font(parikhMatrixFontl.getName(), Font.PLAIN, 16));
parikhMatrixl.setHorizontalAlignment(JLabel.CENTER);
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
c.insets = new Insets(3,0,3,0);
parikh.add(parikhMatrixl,c);
minimatrix.setMaximumSize(new Dimension(500,20+(int)matsize.getHeight()));
minimatrix.add(openLabel);
minimatrix.add(matrix);
minimatrix.add(closeLabel);
minimatrix.setAlignmentX(Component.CENTER_ALIGNMENT);
c.gridx=0;
c.gridy=1;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
parikh.add(minimatrix,c);
//setProgress(4);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//now find all permutations of firstWord
PermuteArrayWithDuplicates perm = new PermuteArrayWithDuplicates();
List<List<Integer>> permute = perm.permute(word);
//setProgress(5);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//remove any words with incorrect matrix
int numWords = permute.size();
int[][] results = new int[numWords][word.length];
int counter = 0;
int[][] matchIndex = new int [numWords][2];
for (int i=0;i<numWords;i++) {
List<Integer> cW = permute.get(i);
int[] currentWord = cW.stream().mapToInt(j->j).toArray();
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
int[][] currentParikhMatrix = cPM.ParikhMatrix(currentWord,enteredAlphabet+1);
if (Arrays.deepEquals(parikhMatrix,currentParikhMatrix)) {
results[counter] = currentWord;
counter = counter+1;
matchIndex[i][0]=1;
}
else {
matchIndex[i][0]=0;
}
}
//setProgress(6);
if (Home.mySwingWorker.isCancelled()) {
return;
}
LyndonConjugate lC = new LyndonConjugate();
int[] lword = lC.lyndonConj(finalWord);
int[][] lparikhMatrix = pM.ParikhMatrix(lword,enteredAlphabet+1);
//setProgress(7);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//remove any words with incorrect matrix
int lnumWords = permute.size();
int[][] lresults = new int[lnumWords][lword.length];
int lcounter = 0;
ParikhMatrixCalculator lcPM = new ParikhMatrixCalculator();
for (int i=0;i<lnumWords;i++) {
List<Integer> lcW = permute.get(i);
int[] currentWord = lcW.stream().mapToInt(j->j).toArray();
int[] lcurrentWord = lC.lyndonConj(currentWord);
int[][] lcurrentParikhMatrix = lcPM.ParikhMatrix(lcurrentWord,enteredAlphabet+1);
if (Arrays.deepEquals(lparikhMatrix,lcurrentParikhMatrix)) {
lresults[lcounter] = lcurrentWord;
lcounter = lcounter+1;
matchIndex[i][1]=1;
}
else {
matchIndex[i][1]=0;
}
}
//setProgress(8);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//convert list of arrays into words that use letters
JPanel resultsLabel = new JPanel();
resultsLabel.setLayout(new BoxLayout(resultsLabel, BoxLayout.Y_AXIS));
JLabel numWords1;
JLabel numWords2;
JLabel numWords3;
if (counter==1) {
numWords1 = new JLabel("There is");
numWords2 = new JLabel(Integer.toString(counter));
numWords3 = new JLabel("word amiable with your word:");
}
else {
numWords1 = new JLabel("There are");
numWords2 = new JLabel(Integer.toString(counter));
numWords3 = new JLabel("words amiable with your word:");
}
Font font = numWords1.getFont();
numWords1.setFont(new Font(font.getName(), Font.PLAIN, 18));
numWords2.setFont(new Font(font.getName(), Font.PLAIN, 18));
numWords3.setFont(new Font(font.getName(), Font.PLAIN, 18));
JPanel numWordsP = new JPanel();
numWordsP.add(numWords1);
numWordsP.add(numWords2);
numWordsP.add(numWords3);
resultsLabel.add(numWordsP);
int amiablecounter = 0;
if (counter>0) {
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1) {
String convertWord = new String("");
List<Integer> cW = permute.get(i);
int[] currentWord = cW.stream().mapToInt(j->j).toArray();
for (int j=0;j<word.length;j++) {
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
convertWord = convertWord + cPM.getCharForNumber(currentWord[j]);
}
JLabel words = new JLabel(convertWord);
font = words.getFont();
words.setFont(new Font(font.getName(), Font.PLAIN, 18));
words.setHorizontalAlignment(JLabel.CENTER);
if (Arrays.equals(finalWord,currentWord)) {
words.setForeground(Color.red);
}
else if (matchIndex[i][1]==1 && matchIndex[i][0]==1) {
Color darkOrange = new Color(230,130,0);
words.setForeground(darkOrange);
}
words.setAlignmentX(Component.CENTER_ALIGNMENT);
resultsLabel.add(words);
amiablecounter = amiablecounter + 1;
}
}
}
else {
JLabel noSol = new JLabel("No words associated to this matrix.");
noSol.setFont(new Font(noSol.getFont().getName(), Font.PLAIN, 18));
resultsLabel.add(noSol);
}
//output final list and make it look clear
JPanel wordLabel = new JPanel();
resultsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//wordLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,10));
wordLabel.setLayout(new BoxLayout(wordLabel, BoxLayout.Y_AXIS));
wordLabel.add(resultsLabel);
c.gridx=0;
c.gridy=2;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
parikh.add(wordLabel,c);
parikh.setBorder(BorderFactory.createEmptyBorder(0,0,0,15));
matrixholder.add(parikh);
outputholder.add(matrixholder);
//now do the same but for the Lyndon conjugate of word
//first find Lyndon conjugate
//add matrix to panel, then add to frame
JPanel lmatrix = new JPanel(new GridLayout(lparikhMatrix.length,lparikhMatrix.length));
for (int i=0;i<lparikhMatrix.length;i++) {
for (int j=0;j<lparikhMatrix.length;j++) {
JLabel lcurrent = new JLabel(Integer.toString(lparikhMatrix[i][j]));
lcurrent.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
lcurrent.setAlignmentX(Component.CENTER_ALIGNMENT);
lmatrix.add(lcurrent);
}
}
//adding brackets to either side of matrix
//open bracket
BufferedImage lopen = null;
try {
lopen = ImageIO.read(getClass().getResource("/Images/OpenBracketClear.png"));
} catch (IOException le1) {
le1.printStackTrace();
}
Dimension lmatsize = lmatrix.getPreferredSize();
int lwidth = (int) Math.round(lmatsize.getHeight()/70);
Image lopendimg = lopen.getScaledInstance(15+lwidth, (int) lmatsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon lopenIcon = new ImageIcon(lopendimg);
JLabel lopenLabel = new JLabel(lopenIcon);
//close bracket
BufferedImage lclose = null;
try {
lclose = ImageIO.read(getClass().getResource("/Images/CloseBracketClear.png"));
} catch (IOException le1) {
le1.printStackTrace();
}
Image ldclose = lclose.getScaledInstance(15+lwidth, (int) lmatsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon lcloseIcon = new ImageIcon(ldclose);
JLabel lcloseLabel = new JLabel(lcloseIcon);
//add borders to brackets to give some space
lopenLabel.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
lcloseLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,20));
//add to panel
JPanel lminimatrix = new JPanel();
JPanel lparikh = new JPanel();
lparikh.setLayout(new GridBagLayout());
JLabel lParikhMatrix= new JLabel("<html><u>L-Parikh Matrix</u>:<html>");
lParikhMatrix.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
lParikhMatrix.setForeground(Color.BLUE);
lParikhMatrix.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>The L-Parikh matrix of a word is the Parikh matrix<br>of that word's Lyndon conjugate. To reduce a<br>word's ambiguity, we use both it's Parikh<br>matrix and L-Parikh matrix.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
Font lParikhMatrixFont = lParikhMatrix.getFont();
lParikhMatrix.setFont(new Font(lParikhMatrixFont.getName(), Font.PLAIN, 16));
lParikhMatrix.setHorizontalAlignment(JLabel.CENTER);
//lParikhMatrix.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
lparikh.add(lParikhMatrix,c);
lminimatrix.setMaximumSize(new Dimension(500,20+(int)matsize.getHeight()));
lminimatrix.add(lopenLabel);
lminimatrix.add(lmatrix);
lminimatrix.add(lcloseLabel);
lminimatrix.setAlignmentX(Component.CENTER_ALIGNMENT);
c.gridx=0;
c.gridy=1;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
lparikh.add(lminimatrix,c);
int lamiablecounter = 0;
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1 && matchIndex[i][1]==1) {
lamiablecounter = lamiablecounter+1;
}
}
//convert list of arrays into words that use letters
JPanel lresultsLabel = new JPanel();
lresultsLabel.setLayout(new BoxLayout(lresultsLabel, BoxLayout.Y_AXIS));
lresultsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel numWords4;
JLabel numWords5;
JLabel numWords6;
if (lamiablecounter==1) {
numWords4 = new JLabel("There is");
numWords5 = new JLabel(Integer.toString(lamiablecounter));
numWords6 = new JLabel("word L-amiable with your word:");
}
else {
numWords4 = new JLabel("There are");
numWords5 = new JLabel(Integer.toString(lamiablecounter));
numWords6 = new JLabel("words L-amiable with your word:");
}
numWords4.setFont(new Font(font.getName(), Font.PLAIN, 18));
numWords5.setFont(new Font(font.getName(), Font.PLAIN, 18));
numWords6.setFont(new Font(font.getName(), Font.PLAIN, 18));
JPanel numWordsP2 = new JPanel();
numWordsP2.add(numWords4);
numWordsP2.add(numWords5);
numWordsP2.add(numWords6);
lresultsLabel.add(numWordsP2);
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1 && matchIndex[i][1]==1) {
String convertWord = new String("");
String lconvertWord = new String("");
List<Integer> cW = permute.get(i);
int[] currentWord = cW.stream().mapToInt(j->j).toArray();
int[] lcurrentWord = lC.lyndonConj(currentWord);
for (int j=0;j<word.length;j++) {
convertWord = convertWord + lcPM.getCharForNumber(currentWord[j]);
}
for (int j=0;j<word.length;j++) {
lconvertWord = lconvertWord + lcPM.getCharForNumber(lcurrentWord[j]);
}
JLabel lwords = new JLabel();
lwords.setText(convertWord + " (" + lconvertWord + ")");
Font lfont = lwords.getFont();
lwords.setFont(new Font(lfont.getName(), Font.PLAIN, 18));
if (Arrays.equals(finalWord,currentWord)) {
lwords.setForeground(Color.red);
}
else if (matchIndex[i][1]==1 && matchIndex[i][0]==1) {
Color darkOrange = new Color(230,130,0);
lwords.setForeground(darkOrange);
}
//lwords.setHorizontalAlignment(JLabel.CENTER);
lwords.setAlignmentX(Component.CENTER_ALIGNMENT);
lresultsLabel.add(lwords);
//lamiablecounter = lamiablecounter+1;
}
}
//lresultsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel lwordLabel = new JPanel();
//lwordLabel.setBorder(BorderFactory.createEmptyBorder(0,10,0,0));
lwordLabel.setLayout(new BoxLayout(lwordLabel, BoxLayout.Y_AXIS));
lwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
lwordLabel.add(lresultsLabel);
c.gridx=0;
c.gridy=2;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
lparikh.add(lwordLabel,c);
lparikh.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
GridBagConstraints gbcFiller = new GridBagConstraints();
gbcFiller.weighty = 1.0;
gbcFiller.fill = GridBagConstraints.BOTH;
gbcFiller.gridheight=GridBagConstraints.REMAINDER;
lparikh.add(Box.createGlue(), gbcFiller);
matrixholder.add(lparikh);
outputholder.add(matrixholder);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int numNotWords = counter-lamiablecounter;
double wordSize = (finalWord.length * 55);
int numColmaybe = (int) Math.floor(screenSize.getWidth()/wordSize);
int numCol=0;
if (numColmaybe<numNotWords || numNotWords==0) {
numCol = numColmaybe;
}
else {
numCol = numNotWords;
}
JPanel lnotresultsLabel = new JPanel(new GridLayout(0,numCol));
lnotresultsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
if (!(amiablecounter==lamiablecounter)) {
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1 && matchIndex[i][1]==0) {
String notconvertWord = new String("");
String notlconvertWord = new String("");
List<Integer> cW = permute.get(i);
int[] notcurrentWord = cW.stream().mapToInt(j->j).toArray();
int[] notlcurrentWord = lC.lyndonConj(notcurrentWord);
for (int j=0;j<word.length;j++) {
notconvertWord = notconvertWord + lcPM.getCharForNumber(notcurrentWord[j]);
}
notconvertWord = notconvertWord + " (";
for (int j=0;j<word.length;j++) {
notlconvertWord = notlconvertWord + lcPM.getCharForNumber(notlcurrentWord[j]);
}
notconvertWord = notconvertWord + notlconvertWord + ")";
JLabel notlwords = new JLabel(notconvertWord);
Font notlfont = notlwords.getFont();
notlwords.setFont(new Font(notlfont.getName(), Font.PLAIN, 16));
notlwords.setHorizontalAlignment(JLabel.CENTER);
lnotresultsLabel.add(notlwords);
}
}
//output final list and make it look clear
JPanel notlwordLabel = new JPanel();
notlwordLabel.setLayout(new BoxLayout(notlwordLabel, BoxLayout.Y_AXIS));
JPanel notInfo = new JPanel();
JLabel notInfoBefore = new JLabel();
int distinctCount = counter - lamiablecounter;
if (distinctCount==1) {
notInfoBefore.setText("There is " + distinctCount + " word that is");
}
else {
notInfoBefore.setText("There are " + distinctCount + " words that are");
}
JLabel notInfoLink = new JLabel("<html><u>L-distinct</u><html>");
JLabel notInfoAfter = new JLabel("with your entered word:");
notInfoLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
notInfoLink.setForeground(Color.BLUE);
notInfoLink.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>Two words are L-distinct if they share the same<br>Parikh matrix, but have different L-Parikh matrices.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
Font notInfoFont = notInfoBefore.getFont();
notInfoBefore.setFont(new Font(notInfoFont.getName(), Font.PLAIN, 16));
notInfoLink.setFont(new Font(notInfoFont.getName(), Font.PLAIN, 16));
notInfoAfter.setFont(new Font(notInfoFont.getName(), Font.PLAIN, 16));
notInfo.add(notInfoBefore);
notInfo.add(notInfoLink);
notInfo.add(notInfoAfter);
//JLabel notInfo2 = new JLabel("Original Word (Lyndon Conjugate)");
notInfo.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
//notInfo2.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
notInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
//notInfo2.setAlignmentX(Component.CENTER_ALIGNMENT);
//notInfo2.setHorizontalAlignment(JLabel.CENTER);
lnotresultsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
notlwordLabel.add(notInfo);
//notlwordLabel.add(notInfo2);
notlwordLabel.setBorder(BorderFactory.createEmptyBorder(30,0,0,0));
notlwordLabel.add(lnotresultsLabel);
notlwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
outputholder.add(notlwordLabel);
holder.add(outputholder);
}
else {
JPanel notlwordLabel = new JPanel();
notlwordLabel.setLayout(new BoxLayout(notlwordLabel, BoxLayout.Y_AXIS));
notlwordLabel.setBorder(BorderFactory.createEmptyBorder(30,0,0,0));
JLabel notInfo = new JLabel();
if (counter==1) {
notInfo.setText("Your word is described uniquely by its Parikh matrix.");
notInfo.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
Font notInfoFont = notInfo.getFont();
notInfo.setFont(new Font(notInfoFont.getName(), Font.PLAIN, 16));
notInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
notlwordLabel.add(notInfo);
}
else {
JPanel notInfoP = new JPanel();
notInfo.setText("The Parikh matrix of your entered word is not");
JLabel notInfo2 = new JLabel("<html><u>L-distinguishable</u>.<html>");
notInfo2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
notInfo2.setForeground(Color.BLUE);
notInfo2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>A Parikh matrix is L-distinguishable if it<br>describes at least two distinct words<br>whose respective L-Parikh matrices are<br>different.</center><html>");
container.add(parikhMatrixInfo);
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
dialog1.setLocation(x, y);
dialog1.setUndecorated(true);
dialog1.add(container);
dialog1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog1.setVisible(true);
dialog1.pack();
dialog1.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
}
public void windowLostFocus(WindowEvent e) {
if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog1)) {
return;
}
dialog1.setVisible(false);
}
});
}
});
notInfoP.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
Font notInfoFont = notInfo.getFont();
notInfo.setFont(new Font(notInfoFont.getName(), Font.PLAIN, 16));
notInfo2.setFont(new Font(notInfoFont.getName(), Font.PLAIN, 16));
notInfoP.add(notInfo);
notInfoP.add(notInfo2);
notInfoP.setAlignmentX(Component.CENTER_ALIGNMENT);
notlwordLabel.add(notInfoP);
}
notlwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
outputholder.add(notlwordLabel);
holder.add(outputholder);
}
//setProgress(9);
if (Home.mySwingWorker.isCancelled()) {
return;
}
revalidate();
repaint();
}
catch (Exception e1){
//setProgress(20);
Home.dialog.setVisible(false);
Home.res.setVisible(true);
Home.res.setText("Please enter a word that consist of only letters or only numbers.");
String noWord = new String("No word.");
String badAlphabet = new String("Alphabet size not possible.");
if (badAlphabet.equals(e1.getMessage())) {
Home.res.setText("Alphabet size not possible - check entered word.");
}
else if (noWord.equals(e1.getMessage())){
Home.res.setText("");
}
else {
Home.res.setText("Please enter a word that consists of only letters or only numbers.");
}
outputholder.setVisible(false);
}
}
else {
//setProgress(20);
Home.dialog.setVisible(false);
Home.res.setVisible(true);
outputholder.setVisible(false);
Home.res.setText(INVALID_SIZE);
}
}
catch (NumberFormatException ex) {
//setProgress(20);
Home.dialog.setVisible(false);
outputholder.setVisible(false);
Home.res.setVisible(true);
Home.res.setText("Incorrect alphabet input type - please enter an integer.");
}
/*
wait.close();
return null;
}
};
mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e){
if ("progress".equals(e.getPropertyName())) {
//progressBar.setIndeterminate(false);
wait.progressBar.setValue((Integer) e.getNewValue());
if ((Integer) e.getNewValue() == 1) {
wait.progressInfo.setText("Step 1 of 9 - Reading Input");
}
else if ((Integer) e.getNewValue() == 2) {
wait.progressInfo.setText("Step 2 of 9 - Calculating Parikh Matrix");
}
else if ((Integer) e.getNewValue() == 3) {
wait.progressInfo.setText("Step 3 of 9 - Painting Parikh Matrix");
}
else if ((Integer) e.getNewValue() == 4) {
wait.progressInfo.setText("Step 4 of 9 - Calculating Permutations");
}
else if ((Integer) e.getNewValue() == 5) {
wait.progressInfo.setText("Step 5 of 9 - Finding Parikh Matrices");
}
else if ((Integer) e.getNewValue() == 6) {
wait.progressInfo.setText("Step 6 of 9 - Calculating Lyndon Conjugate");
}
else if ((Integer) e.getNewValue() == 7) {
wait.progressInfo.setText("Step 7 of 9 - Calculating L-Parikh Matrices");
}
else if ((Integer) e.getNewValue() == 8) {
wait.progressInfo.setText("Step 8 of 9 - Compiling Results");
}
else if ((Integer) e.getNewValue() == 9) {
wait.progressInfo.setText("Step 9 of 9 - Complete");
}
else if ((Integer) e.getNewValue() == 20) {
wait.close();
}
}
}
});
mySwingWorker.execute();
wait.makeWait(e3);
}
});*/
}
else {
//JButton enterFile = new JButton("Enter (and save as .txt file)");
JFileChooser chooser = new JFileChooser();
/*enterFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e3){