Deck editor:

+ added warning messages dialog after load/import decks with errors (instead throw exception);
 + added loading cursors on import-load decks;
 - fixed null exception error on empty/error deck loading;
This commit is contained in:
Oleg Agafonov 2017-12-08 00:20:18 +04:00
parent 1efc062f66
commit 5fc0393bc7
3 changed files with 96 additions and 34 deletions

View file

@ -805,6 +805,14 @@ public class DeckEditorPanel extends javax.swing.JPanel {
.addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)); .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE));
} }
private void processAndShowImportErrors(StringBuilder errorMessages){
// show up errors list
if (errorMessages.length() > 0){
String mes = "Founded problems with deck: \n\n" + errorMessages.toString();
JOptionPane.showMessageDialog(MageFrame.getDesktop(), mes.substring(0, Math.min(1000, mes.length())), "Errors while loading deck", JOptionPane.WARNING_MESSAGE);
}
}
/** /**
* @param evt ActionEvent * @param evt ActionEvent
*/ */
@ -817,14 +825,22 @@ public class DeckEditorPanel extends javax.swing.JPanel {
@Override @Override
public void windowClosed(WindowEvent e) { public void windowClosed(WindowEvent e) {
Deck newDeck = null; Deck newDeck = null;
StringBuilder errorMessages = new StringBuilder();
MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
try { try {
newDeck = Deck.load(DeckImporterUtil.importDeck(dialog.getTmpPath()), true, true); newDeck = Deck.load(DeckImporterUtil.importDeck(dialog.getTmpPath(), errorMessages), true, true);
processAndShowImportErrors(errorMessages);
if (newDeck != null) { if (newDeck != null) {
deck = newDeck; deck = newDeck;
refreshDeck(); refreshDeck();
} }
} catch (GameException e1) { } catch (GameException e1) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE);
}finally {
MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} }
} }
}); });
@ -842,16 +858,22 @@ public class DeckEditorPanel extends javax.swing.JPanel {
@Override @Override
public void windowClosed(WindowEvent e) { public void windowClosed(WindowEvent e) {
Deck deckToAppend = null; Deck deckToAppend = null;
StringBuilder errorMessages = new StringBuilder();
MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
try { try {
deckToAppend = Deck.load(DeckImporterUtil.importDeck(dialog.getTmpPath()), true, true); deckToAppend = Deck.load(DeckImporterUtil.importDeck(dialog.getTmpPath(), errorMessages), true, true);
processAndShowImportErrors(errorMessages);
if (deckToAppend != null) { if (deckToAppend != null) {
deck = Deck.append(deckToAppend, deck); deck = Deck.append(deckToAppend, deck);
refreshDeck(); refreshDeck();
} }
} catch (GameException e1) { } catch (GameException e1) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE);
}finally {
MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} }
} }
}); });
} }
@ -881,20 +903,31 @@ public class DeckEditorPanel extends javax.swing.JPanel {
} }
} }
} }
MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
try { try {
setCursor(new Cursor(Cursor.WAIT_CURSOR)); Deck newDeck = null;
deck = Deck.load(DeckImporterUtil.importDeck(file.getPath()), true, true); StringBuilder errorMessages = new StringBuilder();
newDeck = Deck.load(DeckImporterUtil.importDeck(file.getPath(), errorMessages), true, true);
processAndShowImportErrors(errorMessages);
if (newDeck != null) {
deck = newDeck;
refreshDeck(true);
}
// save last deck history
try {
MageFrame.getPreferences().put("lastDeckFolder", file.getCanonicalPath());
} catch (IOException ex) {
logger.error("Error on save last load deck folder: " + ex.getMessage());
}
} catch (GameException ex) { } catch (GameException ex) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE);
} finally { } finally {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
refreshDeck(true);
try {
if (file != null) {
MageFrame.getPreferences().put("lastDeckFolder", file.getCanonicalPath());
}
} catch (IOException ex) {
} }
} }
fcSelectDeck.setSelectedFile(null); fcSelectDeck.setSelectedFile(null);
@ -930,7 +963,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
if (!fileName.endsWith(".dck")) { if (!fileName.endsWith(".dck")) {
fileName += ".dck"; fileName += ".dck";
} }
setCursor(new Cursor(Cursor.WAIT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
DeckCardLists cardLists = deck.getDeckCardLists(); DeckCardLists cardLists = deck.getDeckCardLists();
cardLists.setCardLayout(deckArea.getCardLayout()); cardLists.setCardLayout(deckArea.getCardLayout());
cardLists.setSideboardLayout(deckArea.getSideboardLayout()); cardLists.setSideboardLayout(deckArea.getSideboardLayout());
@ -938,7 +971,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage() + "\nTry ensuring that the selected directory is writable.", "Error saving deck", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage() + "\nTry ensuring that the selected directory is writable.", "Error saving deck", JOptionPane.ERROR_MESSAGE);
} finally { } finally {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} }
try { try {
MageFrame.getPreferences().put("lastDeckFolder", file.getCanonicalPath()); MageFrame.getPreferences().put("lastDeckFolder", file.getCanonicalPath());
@ -973,29 +1006,36 @@ public class DeckEditorPanel extends javax.swing.JPanel {
int ret = fcImportDeck.showOpenDialog(this); int ret = fcImportDeck.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) { if (ret == JFileChooser.APPROVE_OPTION) {
File file = fcImportDeck.getSelectedFile(); File file = fcImportDeck.getSelectedFile();
MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
try { try {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
DeckImporter importer = DeckImporterUtil.getDeckImporter(file.getPath()); DeckImporter importer = DeckImporterUtil.getDeckImporter(file.getPath());
if (importer != null) { if (importer != null) {
deck = Deck.load(importer.importDeck(file.getPath())); StringBuilder errorMessages = new StringBuilder();
String errors = importer.getErrors(); Deck newDeck = null;
if (!errors.isEmpty()) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), errors, "Error importing deck", JOptionPane.ERROR_MESSAGE); newDeck = Deck.load(importer.importDeck(file.getPath(), errorMessages));
processAndShowImportErrors(errorMessages);
if (newDeck != null) {
deck = newDeck;
refreshDeck();
} }
// save last deck import folder
try {
MageFrame.getPreferences().put("lastImportFolder", file.getCanonicalPath());
} catch (IOException ex) {
logger.error("Error on save last used import folder: " + ex.getMessage());
}
} else { } else {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Unknown deck format", "Error importing deck", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Unknown deck format", "Error importing deck", JOptionPane.ERROR_MESSAGE);
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.fatal(ex); logger.fatal(ex);
} finally { } finally {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
refreshDeck();
try {
if (file != null) {
MageFrame.getPreferences().put("lastImportFolder", file.getCanonicalPath());
}
} catch (IOException ex) {
} }
} }
fcImportDeck.setSelectedFile(null); fcImportDeck.setSelectedFile(null);
@ -1037,7 +1077,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
private void btnGenDeckActionPerformed(ActionEvent evt) { private void btnGenDeckActionPerformed(ActionEvent evt) {
try { try {
setCursor(new Cursor(Cursor.WAIT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
String path = DeckGenerator.generateDeck(); String path = DeckGenerator.generateDeck();
deck = Deck.load(DeckImporterUtil.importDeck(path), true, true); deck = Deck.load(DeckImporterUtil.importDeck(path), true, true);
} catch (GameException ex) { } catch (GameException ex) {
@ -1045,7 +1085,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
} catch (DeckGeneratorException ex) { } catch (DeckGeneratorException ex) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Generator error", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage(), "Generator error", JOptionPane.ERROR_MESSAGE);
} finally { } finally {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} }
refreshDeck(); refreshDeck();
} }

View file

@ -45,7 +45,14 @@ public abstract class DeckImporter {
protected StringBuilder sbMessage = new StringBuilder(); //TODO we should stop using this not garbage collectable StringBuilder. It just bloats protected StringBuilder sbMessage = new StringBuilder(); //TODO we should stop using this not garbage collectable StringBuilder. It just bloats
protected int lineCount; protected int lineCount;
public DeckCardLists importDeck(String file) {
/**
*
* @param file file to import
* @param errorMessages you can setup output messages to showup to user (set null for fatal exception on messages.count > 0)
* @return decks list
*/
public DeckCardLists importDeck(String file, StringBuilder errorMessages) {
File f = new File(file); File f = new File(file);
DeckCardLists deckList = new DeckCardLists(); DeckCardLists deckList = new DeckCardLists();
if (!f.exists()) { if (!f.exists()) {
@ -62,8 +69,15 @@ public abstract class DeckImporter {
lineCount++; lineCount++;
readLine(line, deckList); readLine(line, deckList);
} }
if (sbMessage.length() > 0) { if (sbMessage.length() > 0) {
logger.fatal(sbMessage); if(errorMessages != null) {
// normal output for user
errorMessages.append(sbMessage);
}else{
// fatal error
logger.fatal(sbMessage);
}
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.fatal(null, ex); logger.fatal(null, ex);
@ -74,6 +88,10 @@ public abstract class DeckImporter {
return deckList; return deckList;
} }
public DeckCardLists importDeck(String file) {
return importDeck(file, null);
}
public String getErrors(){ public String getErrors(){
return sbMessage.toString(); return sbMessage.toString();
} }

View file

@ -51,12 +51,16 @@ public final class DeckImporterUtil {
} }
} }
public static DeckCardLists importDeck(String file) { public static DeckCardLists importDeck(String file, StringBuilder errorMessages) {
DeckImporter deckImporter = getDeckImporter(file); DeckImporter deckImporter = getDeckImporter(file);
if (deckImporter != null) { if (deckImporter != null) {
return deckImporter.importDeck(file); return deckImporter.importDeck(file, errorMessages);
} else { } else {
return new DeckCardLists(); return new DeckCardLists();
} }
} }
public static DeckCardLists importDeck(String file) {
return importDeck(file, null);
}
} }