Prevent crash when selecting cards without scripts

This commit is contained in:
drdev
2014-07-18 07:25:03 +00:00
parent 6841b4a727
commit 0d8dad2d4f

View File

@@ -60,54 +60,54 @@ public enum CCardScript implements ICDoc {
} }
private void updateDirtyFlag() { private void updateDirtyFlag() {
boolean isTextNowDirty = !VCardScript.SINGLETON_INSTANCE.getTxtScript().getText().equals(this.currentScriptInfo.getText()); boolean isTextNowDirty = currentScriptInfo != null && !VCardScript.SINGLETON_INSTANCE.getTxtScript().getText().equals(currentScriptInfo.getText());
if (this.isTextDirty == isTextNowDirty) { return; } if (isTextDirty == isTextNowDirty) { return; }
this.isTextDirty = isTextNowDirty; isTextDirty = isTextNowDirty;
VCardDesigner.SINGLETON_INSTANCE.getBtnSaveCard().setEnabled(isTextNowDirty); VCardDesigner.SINGLETON_INSTANCE.getBtnSaveCard().setEnabled(isTextNowDirty);
VCardScript.SINGLETON_INSTANCE.getTabLabel().setText((isTextNowDirty ? "*" : "") + "Card Script"); VCardScript.SINGLETON_INSTANCE.getTabLabel().setText((isTextNowDirty ? "*" : "") + "Card Script");
WorkshopFileMenu.updateSaveEnabled(); WorkshopFileMenu.updateSaveEnabled();
} }
public PaperCard getCurrentCard() { public PaperCard getCurrentCard() {
return this.currentCard; return currentCard;
} }
public void showCard(PaperCard card) { public void showCard(PaperCard card) {
if (this.currentCard == card || this.switchInProgress) { return; } if (currentCard == card || switchInProgress) { return; }
if (!canSwitchAway(true)) { //ensure current card saved before changing to a different card if (!canSwitchAway(true)) { //ensure current card saved before changing to a different card
VWorkshopCatalog.SINGLETON_INSTANCE.getCardManager().setSelectedItem(this.currentCard); //return selection to current card //TODO: fix so clicking away again doesn't cause weird selection problems VWorkshopCatalog.SINGLETON_INSTANCE.getCardManager().setSelectedItem(currentCard); //return selection to current card //TODO: fix so clicking away again doesn't cause weird selection problems
return; return;
} }
this.currentCard = card; currentCard = card;
this.currentScriptInfo = card != null ? CardScriptInfo.getScriptFor(this.currentCard.getRules().getName()) : null; currentScriptInfo = card != null ? CardScriptInfo.getScriptFor(currentCard.getRules().getName()) : null;
refresh(); refresh();
} }
public void refresh() { public void refresh() {
FTextEditor txtScript = VCardScript.SINGLETON_INSTANCE.getTxtScript(); FTextEditor txtScript = VCardScript.SINGLETON_INSTANCE.getTxtScript();
txtScript.setText(this.currentScriptInfo != null ? this.currentScriptInfo.getText() : ""); txtScript.setText(currentScriptInfo != null ? currentScriptInfo.getText() : "");
txtScript.setEditable(this.currentScriptInfo != null ? this.currentScriptInfo.canEdit() : false); txtScript.setEditable(currentScriptInfo != null ? currentScriptInfo.canEdit() : false);
txtScript.setCaretPosition(0); //keep scrolled to top txtScript.setCaretPosition(0); //keep scrolled to top
} }
public boolean hasChanges() { public boolean hasChanges() {
return (this.currentScriptInfo != null && this.isTextDirty); return (currentScriptInfo != null && isTextDirty);
} }
public boolean canSwitchAway(boolean isCardChanging) { public boolean canSwitchAway(boolean isCardChanging) {
if (this.switchInProgress) { return false; } if (switchInProgress) { return false; }
if (!hasChanges()) { return true; } if (!hasChanges()) { return true; }
this.switchInProgress = true; switchInProgress = true;
Singletons.getControl().ensureScreenActive(FScreen.WORKSHOP_SCREEN); //ensure Workshop is active before showing dialog Singletons.getControl().ensureScreenActive(FScreen.WORKSHOP_SCREEN); //ensure Workshop is active before showing dialog
final int choice = FOptionPane.showOptionDialog( final int choice = FOptionPane.showOptionDialog(
"Save changes to " + this.currentCard + "?", "Save changes to " + currentCard + "?",
"Save Changes?", "Save Changes?",
FOptionPane.QUESTION_ICON, FOptionPane.QUESTION_ICON,
new String[] {"Save", "Don't Save", "Cancel"}); new String[] {"Save", "Don't Save", "Cancel"});
this.switchInProgress = false; switchInProgress = false;
if (choice == -1 || choice == 2) { return false; } if (choice == -1 || choice == 2) { return false; }
@@ -123,13 +123,13 @@ public enum CCardScript implements ICDoc {
if (!hasChanges()) { return true; } //not need if text hasn't been changed if (!hasChanges()) { return true; } //not need if text hasn't been changed
String text = VCardScript.SINGLETON_INSTANCE.getTxtScript().getText(); String text = VCardScript.SINGLETON_INSTANCE.getTxtScript().getText();
if (!this.currentScriptInfo.trySetText(text)) { if (!currentScriptInfo.trySetText(text)) {
return false; return false;
} }
updateDirtyFlag(); updateDirtyFlag();
String oldName = this.currentCard.getName(); String oldName = currentCard.getName();
CardRules newRules = CardRules.fromScript(Arrays.asList(text.split("\n"))); CardRules newRules = CardRules.fromScript(Arrays.asList(text.split("\n")));
CardDb cardDb = newRules.isVariant() ? FModel.getMagicDb().getVariantCards() : CardDb cardDb = newRules.isVariant() ? FModel.getMagicDb().getVariantCards() :
@@ -137,15 +137,15 @@ public enum CCardScript implements ICDoc {
cardDb.getEditor().putCard(newRules); cardDb.getEditor().putCard(newRules);
if (newRules.getName().equals(oldName)) { if (newRules.getName().equals(oldName)) {
Card.updateCard(this.currentCard); Card.updateCard(currentCard);
} }
else { else {
this.currentCard = cardDb.getCard(newRules.getName()); currentCard = cardDb.getCard(newRules.getName());
} }
VWorkshopCatalog.SINGLETON_INSTANCE.getCardManager().repaint(); VWorkshopCatalog.SINGLETON_INSTANCE.getCardManager().repaint();
CDetail.SINGLETON_INSTANCE.showCard(this.currentCard); CDetail.SINGLETON_INSTANCE.showCard(currentCard);
CPicture.SINGLETON_INSTANCE.showImage(this.currentCard); CPicture.SINGLETON_INSTANCE.showImage(currentCard);
return true; return true;
} }