I am creating program whose function is that reading "n" .txt or .java files and of these files creates a UML diagram. I have reading method, but I came to problem with load more same files. I would like to deny load same files, because it makes problems with creating a UML diagram.
I trying to solve it so that I store uploaded file into ArrayList and checking each load file with files saved in ArrayList, where are previous loaded files.
Next problem is that I click to button Yes or No when I choose same file, file is equally loaded.
And when I creating this answer I found next problem. When user select more then one file, ArrayList don't know how to add two files at once.
Is there anyone option how I would solve this problem more easily?
ArrayList<String> filenames = new ArrayList<String>();
JTabbedPane tabbedPaneUML_Files = new JTabbedPane();
private void readFiles() {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
FileNameExtensionFilter fileFilter =
new FileNameExtensionFilter("Only .txt a .java files",
"txt", "java");
fc.setFileFilter(fileFilter);
int returnValue = fc.showOpenDialog(this);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File[] files = fc.getSelectedFiles();
File file;
tabbedPaneUML_Files.addTab("UML diagram", panelUML);
for (int i = 0; i < files.length; i++) {
file = files[i];
for (int j = 0; j < filenames.size(); j++) {
if (filenames.get(i).equals(fc.getSelectedFile().getName())) {
Object[] options = {"Yes", "No"};
int answer = JOptionPane.showOptionDialog(this,
"Unable to load the same files! To retrieve the other files?",
"Load new file", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
if(answer == 0) {
readFiles();
}
}
}
filenames.add(file.getName());
JTextArea loadCode = new JTextArea();
JScrollPane scrollingFile = new JScrollPane();
scrollingFile.setViewportView(loadCode);
tabbedPaneUML_Files.addTab("" + file.getName(), scrollingFile);
int ch;
try {
Reader charsReader =
new InputStreamReader(new FileInputStream(file),
"UTF-8");
while ((ch = charsReader.read()) != -1) {
loadCode.append(Character.toString((char) ch));
}
loadCode.setSelectionStart(0);
loadCode.setSelectionEnd(0);
loadCode.setEditable(false);
} catch (FileNotFoundException ex) {
Logger.getLogger(HlavniOkno.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(HlavniOkno.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Thanks for any advice. I already lost ideas. Sorry for my English.