text import: show more confirmation dialogs

This commit is contained in:
Honfika
2014-04-21 20:16:12 +02:00
parent 16fa1c3f2b
commit 9e10ff7859
3 changed files with 104 additions and 76 deletions

View File

@@ -1677,6 +1677,105 @@ public final class MainPanel extends JPanel implements ActionListener, TreeSelec
return result;
}
private void importTextsSingleFile(File textsFile, SWF swf) {
String texts = Helper.readTextFile(textsFile.getPath());
Map<Integer, String[]> records = splitTextRecords(texts);
if (records != null) {
for (int characterId : records.keySet()) {
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
String[] currentRecords = records.get(characterId);
String text = textTag.getFormattedText();
if (!saveText(textTag, text, currentRecords)) {
if (View.showConfirmDialog(this, translate("error.text.import"), translate("error"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
return;
}
}
break;
}
}
}
}
}
}
private void importTextsSingleFileFormatted(File textsFile, SWF swf) {
String texts = Helper.readTextFile(textsFile.getPath());
Map<Integer, String[]> records = splitTextRecords(texts);
if (records != null) {
for (int characterId : records.keySet()) {
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
String[] currentRecords = records.get(characterId);
if (!saveText(textTag, currentRecords[0], null)) {
if (View.showConfirmDialog(this, translate("error.text.import"), translate("error"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
return;
}
}
break;
}
}
}
}
}
}
private void importTextsMultipleFiles(String folder, SWF swf) {
File textsFolder = new File(Path.combine(folder, TextExporter.TEXT_EXPORT_FOLDER));
String[] files = textsFolder.list(new FilenameFilter() {
private Pattern pat = Pattern.compile("\\d+\\.txt", Pattern.CASE_INSENSITIVE);
@Override
public boolean accept(File dir, String name) {
return pat.matcher(name).matches();
}
});
for (String fileName : files) {
String texts = Helper.readTextFile(Path.combine(textsFolder.getPath(), fileName));
int characterId = Integer.parseInt(fileName.split("\\.")[0]);
String recordSeparator = Helper.newLine + Configuration.textExportSingleFileRecordSeparator.get() + Helper.newLine;
boolean formatted = !texts.contains(recordSeparator) && texts.startsWith("[" + Helper.newLine);
if (!formatted) {
String[] records = texts.split(recordSeparator);
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
String text = textTag.getFormattedText();
if (!saveText(textTag, text, records)) {
if (View.showConfirmDialog(this, translate("error.text.import"), translate("error"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
return;
}
}
break;
}
}
}
} else {
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
if (!saveText(textTag, texts, null)) {
if (View.showConfirmDialog(this, translate("error.text.import"), translate("error"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
return;
}
}
break;
}
}
}
}
}
}
public void importText(final SWF swf) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(Configuration.lastExportDir.get()));
@@ -1688,85 +1787,14 @@ public final class MainPanel extends JPanel implements ActionListener, TreeSelec
File textsFile = new File(Path.combine(selFile, TextExporter.TEXT_EXPORT_FOLDER, TextExporter.TEXT_EXPORT_FILENAME_FORMATTED));
// try to import formatted texts
if (textsFile.exists()) {
String texts = Helper.readTextFile(textsFile.getPath());
Map<Integer, String[]> records = splitTextRecords(texts);
if (records != null) {
for (int characterId : records.keySet()) {
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
String[] currentRecords = records.get(characterId);
saveText(textTag, currentRecords[0], null);
break;
}
}
}
}
}
importTextsSingleFileFormatted(textsFile, swf);
} else {
textsFile = new File(Path.combine(selFile, TextExporter.TEXT_EXPORT_FOLDER, TextExporter.TEXT_EXPORT_FILENAME_PLAIN));
// try to import plain texts
if (textsFile.exists()) {
String texts = Helper.readTextFile(textsFile.getPath());
Map<Integer, String[]> records = splitTextRecords(texts);
if (records != null) {
for (int characterId : records.keySet()) {
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
String[] currentRecords = records.get(characterId);
String text = textTag.getFormattedText();
saveText(textTag, text, currentRecords);
break;
}
}
}
}
}
importTextsSingleFile(textsFile, swf);
} else {
textsFile = new File(Path.combine(selFile, TextExporter.TEXT_EXPORT_FOLDER));
String[] files = textsFile.list(new FilenameFilter() {
private Pattern pat = Pattern.compile("\\d+\\.txt", Pattern.CASE_INSENSITIVE);
@Override
public boolean accept(File dir, String name) {
return pat.matcher(name).matches();
}
});
for (String fileName : files) {
String texts = Helper.readTextFile(Path.combine(textsFile.getPath(), fileName));
int characterId = Integer.parseInt(fileName.split("\\.")[0]);
String recordSeparator = Helper.newLine + Configuration.textExportSingleFileRecordSeparator.get() + Helper.newLine;
boolean formatted = !texts.contains(recordSeparator) && texts.startsWith("[" + Helper.newLine);
if (!formatted) {
String[] records = texts.split(recordSeparator);
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
String text = textTag.getFormattedText();
saveText(textTag, text, records);
break;
}
}
}
} else {
for (Tag tag : swf.tags) {
if (tag instanceof TextTag) {
TextTag textTag = (TextTag) tag;
if (textTag.getCharacterId() == characterId) {
saveText(textTag, texts, null);
break;
}
}
}
}
}
importTextsMultipleFiles(selFile, swf);
}
}

View File

@@ -475,4 +475,4 @@ message.confirm.donotshowagain = Do not show again
menu.import = Import
menu.file.import.text = Import text
import.select.directory = Select directory to import
error.text.import = Error during text import
error.text.import = Error during text import. Do you want to continue?

View File

@@ -475,4 +475,4 @@ message.confirm.donotshowagain = Ne mutassa \u00fajra
menu.import = Import\u00e1l\u00e1s
menu.file.import.text = Sz\u00f6veg import\u00e1l\u00e1sa
import.select.directory = V\u00e1lassza ki a mapp\u00e1t az import\u00e1l\u00e1shoz
error.text.import = Hiba sz\u00f6veg import\u00e1l\u00e1s k\u00f6zben
error.text.import = Hiba sz\u00f6veg import\u00e1l\u00e1s k\u00f6zben. Folytatja?