/
ParikhMatrixUploadInterface.java
156 lines (144 loc) · 4.78 KB
/
ParikhMatrixUploadInterface.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
import java.awt.Component;
import java.awt.Container;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class ParikhMatrixUploadInterface {
public JFrame frame;
public JFileChooser chooser;
public static JFileChooser chooserSave;
public static int iteration;
public static File uploadedFile;
public static Scanner scanner;
public ParikhMatrixUploadInterface(int[] word, int alphabet, String savePath, int it) {
try {
//uploadedFile=uploadFile;
ParikhMatrixCalculator pM = new ParikhMatrixCalculator();
int[][] parikhMatrix = pM.ParikhMatrix(word,alphabet+1);
String convertinputWord = new String("");
for (int j=0;j<word.length;j++) {
convertinputWord = convertinputWord + pM.getCharForNumber(word[j]);
}
String defaultTitle = "Parikh Matrix Of " + convertinputWord + ".txt";
if (it==1) {
chooserSave.setSelectedFile(new File("Parikh Matrix Of Your Word.txt"));
}
if (it==0) {
File output = new File(savePath,defaultTitle);
output.createNewFile();
FileWriter write = new FileWriter(output, false);
FileWriter append = new FileWriter(output, true);
PrintWriter print_line = new PrintWriter(write);
PrintWriter add_line = new PrintWriter(append);
String start = new String("The Parikh matrix of your word ");
print_line.printf("%s", start);
print_line.close();
add_line.printf("%s"+":"+"%n", convertinputWord);
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");
add_line.close();
}
if (!scanner.hasNext()) {
Home.dialog.dispose();
}
}
catch (Exception e){
e.printStackTrace();
Home.dialog.dispose();
}
}
public static void actioned() {
try {
uploadedFile = Home.uploadedFile;
Home.dialog.setVisible(false);
chooserSave = new JFileChooser();
//Create file chooser for user to select a location to save the output files to (default to same location as uploaded file)
chooserSave.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
disableTextField(chooserSave); //prevent user from altering file name as these are auto generated
chooserSave.setDialogTitle("Select Location To Save Output Files");
chooserSave.setSelectedFile(new File("Parikh Matrix Of...")); //default empty name - required to click "save" but will not be used in file generation
chooserSave.setCurrentDirectory(uploadedFile);
int returnVal = chooserSave.showSaveDialog(null);
String path = chooserSave.getCurrentDirectory().getAbsolutePath();
//if user selects a location
if (returnVal == JFileChooser.APPROVE_OPTION) {
scanner = new Scanner(uploadedFile);
scanner.useDelimiter(",|\\n");
while (scanner.hasNext()){
if (Home.mySwingWorker.isCancelled()) {
return;
}
String enteredWord = scanner.next();
int[] word = new int[enteredWord.length()];
ParikhMatrixCalculator pM = new ParikhMatrixCalculator();
//convert next word in file into int[] for handling
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;
}
}
}
else {
for (int i=0;i<enteredWord.length();i++) {
int l = Character.getNumericValue(enteredWord.charAt(i));
word[i] = l;
}
}
//calculate these every iteration to allow different alphabet sizes
int maxLetter = max(word);
int enteredAlphabet = maxLetter+1;
//calculate Parikh matrix of word and create file that contains it
new ParikhMatrixUploadInterface(word,enteredAlphabet, path, 0);
}
scanner.close();
Home.dialog.dispose();
}
}
catch (Exception e) {
e.printStackTrace();
Home.dialog.dispose();
}
Home.dialog.dispose();
}
private static boolean isNumber(String s)
{
for (int i = 0; i < s.length(); i++)
if (Character.isDigit(s.charAt(i)) == false)
return false;
return true;
}
private static int max(int[] t) {
int maximum = t[0]; // start with the first value
for (int i=1; i<t.length; i++) {
if (t[i] > maximum) {
maximum = t[i]; // new maximum
}
}
return maximum;
}
public static boolean disableTextField(Container container) {
Component[] comps = container.getComponents();
for (Component comp : comps) {
if (comp instanceof JTextField) {
((JTextField) comp).setEnabled(false);
return true;
}
if (comp instanceof Container) {
if (disableTextField((Container) comp)) return true;
}
}
return false;
}
}