/
PParikhMatrixInterface.java
1241 lines (1156 loc) · 46.6 KB
/
PParikhMatrixInterface.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.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 javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
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 PParikhMatrixInterface extends JPanel{
private static final long serialVersionUID = 1L;
//private static JLabel res;
private static final String INVALID_SIZE = "Alphabet must be of at least size 2.";
public SwingWorker<Void, Void> mySwingWorker;
public JFrame frame;
@SuppressWarnings("resource")
public PParikhMatrixInterface() {
JPanel holder = new JPanel();
holder.setLayout(new BoxLayout(holder, BoxLayout.Y_AXIS));
JPanel matrixholder = new JPanel();
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);
matrixholder.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[] mcomponents = matrixholder.getComponents();
for (Component mcomponent : mcomponents) {
matrixholder.remove(mcomponent);
}
matrixholder.setVisible(true);
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.");
}
//convert input set to array of unique integers
String set = Home.setInput.getText();
if (set.length()==0) {
throw new Exception("No projection entered.");
}
set = set.replaceAll("[^a-zA-Z0-9]", ""); //remove special characters
set = set.toLowerCase();
int[] pset = new int[set.length()];
int countNum = 0;
int countLet = 0;
for (int i=0;i<set.length();i++) {
if (Character.isLetter(set.charAt(i))){
String character = Character.toString(set.charAt(i));
pset[i] = pM.getNumberForChar(character)-1;
countLet = countLet+1;
}
else if (Character.isDigit(set.charAt(i))) {
pset[i] = Character.getNumericValue(set.charAt(i));
countNum= countNum+1;
}
}
Arrays.sort(pset);
int prev = pset[0];
int[] uniqueset = new int[pset.length];
uniqueset[0] = prev;
int setcount = 1;
for (int i=0;i<pset.length;i++) {
if (!(prev==pset[i])) {
uniqueset[setcount]=pset[i];
setcount = setcount+1;
}
prev = pset[i];
}
int[] setP = Arrays.copyOfRange(uniqueset, 0, setcount);
int maxProjLetter = max(setP);
int projInWord = 0;
for (int i=0;i<word.length;i++) {
for (int j=0;j<setP.length;j++) {
if (word[i]==setP[j]) {
projInWord=projInWord+1;
break;
}
}
}
if ((isNumber(set) && !isNumber(enteredWord)) || (!isNumber(set) && isNumber(enteredWord))) {
throw new Exception("Opposing alphanumeric word and set.");
}
else if (countNum!=0 && countLet!=0) {
throw new Exception ("Mixed set characters.");
}
else if (maxProjLetter>=enteredAlphabet) {
throw new Exception("Entered projection not possible - alphabet size.");
}
else if (projInWord==0) {
throw new Exception("Entered projection not possible - projection not in word.");
}
//setProgress(2);
if (Home.mySwingWorker.isCancelled()) {
return;
}
int[][] parikhMatrix = pM.ParikhMatrix(word,enteredAlphabet+1);
//setProgress(3);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//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);
}
}
//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);
String convertWord = new String("");
for (int j=0;j<finalWord.length;j++) {
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
convertWord = convertWord + cPM.getCharForNumber(finalWord[j]);
}
JLabel wordLabel = new JLabel(convertWord);
JPanel wordLabelP = new JPanel();
Font font = wordLabel.getFont();
wordLabel.setFont(new Font(font.getName(), Font.PLAIN, 18));
wordLabelP.setAlignmentX(Component.CENTER_ALIGNMENT);
wordLabelP.add(wordLabel);
c.gridx=0;
c.gridy=2;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
parikh.add(wordLabelP,c);
parikh.setAlignmentX(Component.CENTER_ALIGNMENT);
matrixholder.add(parikh);
holder.add(matrixholder);
//setProgress(4);
if (Home.mySwingWorker.isCancelled()) {
return;
}
Projection p = new Projection();
int[] pWord = p.ProjectionCalculator(finalWord,setP,enteredAlphabet);
int[] mappedWord = p.InverseProjection(pWord,setP); //map back to original set
//setProgress(5);
if (Home.mySwingWorker.isCancelled()) {
return;
}
int[][] pparikhMatrix = pM.ParikhMatrix(pWord,setP.length+1);
//setProgress(6);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//add matrix to panel, then add to frame
JPanel pmatrix = new JPanel(new GridLayout(pparikhMatrix.length,pparikhMatrix.length));
for (int i=0;i<pparikhMatrix.length;i++) {
for (int j=0;j<pparikhMatrix.length;j++) {
JLabel pcurrent = new JLabel(Integer.toString(pparikhMatrix[i][j]));
pcurrent.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
pcurrent.setAlignmentX(Component.CENTER_ALIGNMENT);
pmatrix.add(pcurrent);
}
}
//adding brackets to either side of matrix
//open bracket
BufferedImage popen = null;
try {
popen = ImageIO.read(getClass().getResource("/Images/OpenBracketClear.png"));
} catch (IOException pe1) {
pe1.printStackTrace();
}
Dimension pmatsize = pmatrix.getPreferredSize();
int pwidth = (int) Math.round(pmatsize.getHeight()/70);
Image popendimg = popen.getScaledInstance(15+pwidth, (int) pmatsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon popenIcon = new ImageIcon(popendimg);
JLabel popenLabel = new JLabel(popenIcon);
//close bracket
BufferedImage pclose = null;
try {
pclose = ImageIO.read(getClass().getResource("/Images/CloseBracketClear.png"));
} catch (IOException pe1) {
pe1.printStackTrace();
}
Image pdclose = pclose.getScaledInstance(15+pwidth, (int) pmatsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon pcloseIcon = new ImageIcon(pdclose);
JLabel pcloseLabel = new JLabel(pcloseIcon);
//add borders to brackets to give some space
popenLabel.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
pcloseLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,20));
//add to panel
JPanel pminimatrix = new JPanel();
JPanel pparikh = new JPanel();
pparikh.setLayout(new GridBagLayout());
JLabel pParikhMatrix= new JLabel("<html><u>P-Parikh Matrix</u>:<html>");
if (Home.mySwingWorker.isCancelled()) {
return;
}
//JLabel lParikhMatrix= new JLabel("<html><u>L-Parikh Matrix</u>:<html>");
pParikhMatrix.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
pParikhMatrix.setForeground(Color.BLUE);
pParikhMatrix.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>Let S be a set that contains letters in your given<br>alphabet. We define the P-Parikh matrix of a word with respect<br>to S as the Parikh matrix of the word obtained from the<br>following. For each letter in your word, if that letter<br>is in S, it remains, otherwise replace that letter with<br>the empty word.</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);
}
});
}
});
if (Home.mySwingWorker.isCancelled()) {
return;
}
Font pParikhMatrixFont = pParikhMatrix.getFont();
pParikhMatrix.setFont(new Font(pParikhMatrixFont.getName(), Font.PLAIN, 16));
pParikhMatrix.setHorizontalAlignment(JLabel.CENTER);
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
pparikh.add(pParikhMatrix,c);
pminimatrix.setMaximumSize(new Dimension(500,20+(int)matsize.getHeight()));
pminimatrix.add(popenLabel);
pminimatrix.add(pmatrix);
pminimatrix.add(pcloseLabel);
pminimatrix.setAlignmentX(Component.CENTER_ALIGNMENT);
c.gridx=0;
c.gridy=1;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
pparikh.add(pminimatrix,c);
String pconvertWord = new String("");
for (int j=0;j<mappedWord.length;j++) {
ParikhMatrixCalculator lcPM = new ParikhMatrixCalculator();
pconvertWord = pconvertWord + lcPM.getCharForNumber(mappedWord[j]);
}
JLabel pwordLabel = new JLabel(pconvertWord);
Font pfont = pwordLabel.getFont();
pwordLabel.setFont(new Font(pfont.getName(), Font.PLAIN, 18));
pwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//pwordLabel.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));
c.gridx=0;
c.gridy=2;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
JPanel pwordLabelP = new JPanel();
pwordLabelP.add(pwordLabel);
pparikh.add(pwordLabelP,c);
pparikh.setAlignmentX(Component.CENTER_ALIGNMENT);
//pparikh.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
matrixholder.add(pparikh);
holder.add(matrixholder);
//setProgress(7);
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 badAlphabet = new String("Alphabet size not possible.");
String badProjection1 = new String("Entered projection not possible - alphabet size.");
String badProjection2 = new String("Entered projection not possible - projection not in word.");
String badProjection3 = new String("No projection entered.");
String badProjection4 = new String("Opposing alphanumeric word and set.");
String badProjection5 = new String("Mixed set characters.");
if (badAlphabet.equals(e1.getMessage())) {
Home.res.setText("Alphabet size not possible - check entered word.");
}
else if (badProjection1.equals(e1.getMessage())) {
Home.res.setText("Entered projection not possible. Set contains letter not in alphabet.");
}
else if (badProjection2.equals(e1.getMessage())) {
Home.res.setText("Entered projection not possible. Set does not contain any letter in entered word.");
}
else if (badProjection3.equals(e1.getMessage())) {
Home.res.setText("Please enter a projection set.");
}
else if (badProjection4.equals(e1.getMessage())) {
Home.res.setText("Please ensure your projection set uses the same characters as your word.");
}
else if (badProjection5.contentEquals(e1.getMessage())) {
Home.res.setText("Please enter a projection set that consists of only letters or only numbers.");
}
else {
Home.res.setText("Please enter a word that consists of only letters or only numbers.");
}
matrixholder.setVisible(false);
}
}
else {
//setProgress(20);
Home.dialog.setVisible(false);
Home.res.setVisible(true);
matrixholder.setVisible(false);
Home.res.setText(INVALID_SIZE);
}
}
catch (NumberFormatException ex) {
//setProgress(20);
Home.dialog.setVisible(false);
matrixholder.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 7 - Reading Input");
}
else if ((Integer) e.getNewValue() == 2) {
wait.progressInfo.setText("Step 2 of 7 - Calculating Parikh Matrix");
}
else if ((Integer) e.getNewValue() == 3) {
wait.progressInfo.setText("Step 3 of 7 - Painting Parikh Matrix");
}
else if ((Integer) e.getNewValue() == 4) {
wait.progressInfo.setText("Step 4 of 7 - Calculating Projected Word");
}
else if ((Integer) e.getNewValue() == 5) {
wait.progressInfo.setText("Step 5 of 7 - Calculating P-Parikh Matrix");
}
else if ((Integer) e.getNewValue() == 6) {
wait.progressInfo.setText("Step 6 of 7 - Compiling Results");
}
else if ((Integer) e.getNewValue() == 7) {
wait.progressInfo.setText("Step 7 of 7 - Complete");
}
else if ((Integer) e.getNewValue() == 20) {
wait.close();
}
}
}
});
mySwingWorker.execute();
wait.makeWait(e3);
}
private boolean isValid(int size) {
return (size > 1 || size ==0);
}
}
);
*/
//JButton enterFile = new JButton("Enter (and save as .txt file)");
}
else {
JFileChooser chooser = new JFileChooser();
/*enterFile.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);
matrixholder.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[] mcomponents = matrixholder.getComponents();
for (Component mcomponent : mcomponents) {
matrixholder.remove(mcomponent);
}
matrixholder.setVisible(true);
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.");
}
//convert input set to array of unique integers
String set = Home.setInput.getText();
if (set.length()==0) {
throw new Exception("No projection entered.");
}
set = set.replaceAll("[^a-zA-Z0-9]", ""); //remove special characters
set = set.toLowerCase();
int[] pset = new int[set.length()];
for (int i=0;i<set.length();i++) {
if (Character.isLetter(set.charAt(i))){
String character = Character.toString(set.charAt(i));
pset[i] = pM.getNumberForChar(character)-1;
}
else if (Character.isDigit(set.charAt(i))) {
pset[i] = Character.getNumericValue(set.charAt(i));
}
}
Arrays.sort(pset);
int prev = pset[0];
int[] uniqueset = new int[pset.length];
uniqueset[0] = prev;
int setcount = 1;
for (int i=0;i<pset.length;i++) {
if (!(prev==pset[i])) {
uniqueset[setcount]=pset[i];
setcount = setcount+1;
}
prev = pset[i];
}
int[] setP = Arrays.copyOfRange(uniqueset, 0, setcount);
int maxProjLetter = max(setP);
int projInWord = 0;
for (int i=0;i<word.length;i++) {
for (int j=0;j<setP.length;j++) {
if (word[i]==setP[j]) {
projInWord=projInWord+1;
break;
}
}
}
if (maxProjLetter>=enteredAlphabet) {
throw new Exception("Entered projection not possible - alphabet size.");
}
else if (projInWord==0) {
throw new Exception("Entered projection not possible - projection not in word.");
}
//setProgress(2);
if (Home.mySwingWorker.isCancelled()) {
return;
}
String convertinputWord = new String("");
for (int j=0;j<word.length;j++) {
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
convertinputWord = convertinputWord + cPM.getCharForNumber(finalWord[j]);
}
//Open save dialog and create file
if (Home.mySwingWorker.isCancelled()) {
return;
}
chooser.setDialogTitle("Select Folder");
String defaultTitle = "P-Parikh Matrix Of " + convertinputWord + ".txt";
chooser.setCurrentDirectory(new File(Home.filePath));
chooser.setSelectedFile(new File(defaultTitle));
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
Home.filePath = chooser.getSelectedFile().getParent();
String path = chooser.getCurrentDirectory().getAbsolutePath();
String filename=chooser.getSelectedFile().getName();
if (!filename.endsWith(".txt")){
filename=filename+".txt";
}
File outputfile = new File(path,filename);
outputfile.createNewFile();
FileWriter write = new FileWriter(outputfile, false);
FileWriter append = new FileWriter(outputfile, true);
PrintWriter print_line = new PrintWriter(write);
PrintWriter add_line = new PrintWriter(append);
int[][] parikhMatrix = pM.ParikhMatrix(word,enteredAlphabet+1);
if (Home.mySwingWorker.isCancelled()) {
return;
}
String start = new String("The Parikh matrix of your word ");
print_line.printf("%s", start);
print_line.printf("%s"+":"+"%n", convertinputWord);
print_line.close();
for (int i=0;i<parikhMatrix.length;i++) {
for (int j=0;j<parikhMatrix.length;j++) {
add_line.printf("%4d", parikhMatrix[i][j]);
}
add_line.printf("%n");
}
add_line.printf("%n");
//setProgress(3);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//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);
}
}
//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);
String convertWord = new String("");
for (int j=0;j<finalWord.length;j++) {
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
convertWord = convertWord + cPM.getCharForNumber(finalWord[j]);
}
JLabel wordLabel = new JLabel(convertWord);
JPanel wordLabelP = new JPanel();
Font font = wordLabel.getFont();
wordLabel.setFont(new Font(font.getName(), Font.PLAIN, 18));
wordLabelP.setAlignmentX(Component.CENTER_ALIGNMENT);
wordLabelP.add(wordLabel);
c.gridx=0;
c.gridy=2;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
parikh.add(wordLabelP,c);
parikh.setAlignmentX(Component.CENTER_ALIGNMENT);
matrixholder.add(parikh);
holder.add(matrixholder);
//setProgress(4);
if (Home.mySwingWorker.isCancelled()) {
return;
}
Projection p = new Projection();
int[] pWord = p.ProjectionCalculator(finalWord,setP,enteredAlphabet);
int[] mappedWord = p.InverseProjection(pWord,setP); //map back to original set
//setProgress(5);
if (Home.mySwingWorker.isCancelled()) {
return;
}
int[][] pparikhMatrix = pM.ParikhMatrix(pWord,setP.length+1);
//setProgress(6);
if (Home.mySwingWorker.isCancelled()) {
return;
}
//add matrix to panel, then add to frame
JPanel pmatrix = new JPanel(new GridLayout(pparikhMatrix.length,pparikhMatrix.length));
for (int i=0;i<pparikhMatrix.length;i++) {
for (int j=0;j<pparikhMatrix.length;j++) {
JLabel pcurrent = new JLabel(Integer.toString(pparikhMatrix[i][j]));
pcurrent.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
pcurrent.setAlignmentX(Component.CENTER_ALIGNMENT);
pmatrix.add(pcurrent);
}
}
//adding brackets to either side of matrix
//open bracket
BufferedImage popen = null;
try {
popen = ImageIO.read(getClass().getResource("/Images/OpenBracketClear.png"));
} catch (IOException pe1) {
pe1.printStackTrace();
}
Dimension pmatsize = pmatrix.getPreferredSize();
int pwidth = (int) Math.round(pmatsize.getHeight()/70);
Image popendimg = popen.getScaledInstance(15+pwidth, (int) pmatsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon popenIcon = new ImageIcon(popendimg);
JLabel popenLabel = new JLabel(popenIcon);
//close bracket
BufferedImage pclose = null;
try {
pclose = ImageIO.read(getClass().getResource("/Images/CloseBracketClear.png"));
} catch (IOException pe1) {
pe1.printStackTrace();
}
Image pdclose = pclose.getScaledInstance(15+pwidth, (int) pmatsize.getHeight(),Image.SCALE_SMOOTH);
ImageIcon pcloseIcon = new ImageIcon(pdclose);
JLabel pcloseLabel = new JLabel(pcloseIcon);
//add borders to brackets to give some space
popenLabel.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
pcloseLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,20));
//add to panel
JPanel pminimatrix = new JPanel();
JPanel pparikh = new JPanel();
pparikh.setLayout(new GridBagLayout());
JLabel pParikhMatrix= new JLabel("<html><u>P-Parikh Matrix</u>:<html>");
if (Home.mySwingWorker.isCancelled()) {
return;
}
//JLabel lParikhMatrix= new JLabel("<html><u>L-Parikh Matrix</u>:<html>");
pParikhMatrix.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
pParikhMatrix.setForeground(Color.BLUE);
pParikhMatrix.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog dialog1 = new JDialog(frame);
JPanel container = new JPanel();
JLabel parikhMatrixInfo = new JLabel("<html><center>Let S be a set that contains letters in your given<br>alphabet. We define the P-Parikh matrix of a word with respect<br>to S as the Parikh matrix of the word obtained from the<br>following. For each letter in your word, if that letter<br>is in S, it remains, otherwise replace that letter with<br>the empty word.</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);
}
});
}
});
if (Home.mySwingWorker.isCancelled()) {
return;
}
Font pParikhMatrixFont = pParikhMatrix.getFont();
pParikhMatrix.setFont(new Font(pParikhMatrixFont.getName(), Font.PLAIN, 16));
pParikhMatrix.setHorizontalAlignment(JLabel.CENTER);
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
pparikh.add(pParikhMatrix,c);
pminimatrix.setMaximumSize(new Dimension(500,20+(int)matsize.getHeight()));
pminimatrix.add(popenLabel);
pminimatrix.add(pmatrix);
pminimatrix.add(pcloseLabel);
pminimatrix.setAlignmentX(Component.CENTER_ALIGNMENT);
c.gridx=0;
c.gridy=1;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.NORTH;
pparikh.add(pminimatrix,c);
String pconvertWord = new String("");
for (int j=0;j<mappedWord.length;j++) {
ParikhMatrixCalculator lcPM = new ParikhMatrixCalculator();
pconvertWord = pconvertWord + lcPM.getCharForNumber(mappedWord[j]);
}
String start2 = new String("The projection of your word ");
add_line.printf("%s", start2);
add_line.printf("%s"+" with your projection set {", convertinputWord);
for (int i=0;i<pset.length;i++) {
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
if (i==pset.length-1) {
add_line.printf("%s}: %n", cPM.getCharForNumber(pset[i]));
}
else {
add_line.printf("%s,", cPM.getCharForNumber(pset[i]));
}
}
add_line.printf("%s"+"%n", pconvertWord);
add_line.printf("%n");
String start3 = new String("The P-Parikh matrix of your word ");
add_line.printf("%s", start3);
add_line.printf("%s"+":"+"%n", convertinputWord);
for (int i=0;i<pparikhMatrix.length;i++) {