Fix so saving a deck multiple time works

Fix so adding/removing decks in All Decks pane updates deck choosers on home screen
Fix so saving a deck in Deck Editor updates All Decks pane right away
Fix so typing in deck title field marks deck as having changes to save
This commit is contained in:
drdev
2014-01-25 05:12:09 +00:00
parent b198e8a40e
commit f8d7d060b9
7 changed files with 85 additions and 21 deletions

View File

@@ -239,6 +239,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
} }
else { else {
removeAll(); removeAll();
restoreSavedState(); //ensure decks refreshed and state restored in case any deleted or added since last loaded
} }
this.setLayout(new MigLayout("insets 0, gap 0")); this.setLayout(new MigLayout("insets 0, gap 0"));
decksComboBox.addTo(this, "w 100%, h 30px!, gapbottom 5px, spanx 2, wrap"); decksComboBox.addTo(this, "w 100%, h 30px!, gapbottom 5px, spanx 2, wrap");

View File

@@ -3,7 +3,9 @@ package forge.gui.deckeditor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import forge.Singletons; import forge.Singletons;
import forge.gui.deckeditor.controllers.CAllDecks;
import forge.gui.deckeditor.controllers.DeckController; import forge.gui.deckeditor.controllers.DeckController;
import forge.gui.deckeditor.views.VAllDecks;
import forge.gui.deckeditor.views.VCurrentDeck; import forge.gui.deckeditor.views.VCurrentDeck;
import forge.gui.framework.FScreen; import forge.gui.framework.FScreen;
import forge.gui.toolbox.FOptionPane; import forge.gui.toolbox.FOptionPane;
@@ -14,7 +16,6 @@ import forge.gui.toolbox.FOptionPane;
* <br><br><i>(S at beginning of class name denotes a static factory.)</i> * <br><br><i>(S at beginning of class name denotes a static factory.)</i>
*/ */
public class SEditorIO { public class SEditorIO {
/** /**
* Saves the current deck, with various prompts depending on the * Saves the current deck, with various prompts depending on the
* current save environment. * current save environment.
@@ -40,12 +41,22 @@ public class SEditorIO {
"Overwrite Deck?"); "Overwrite Deck?");
} }
if (confirmResult) { controller.save(); } if (confirmResult) {
controller.save();
DeckProxy deck = VAllDecks.SINGLETON_INSTANCE.getLstDecks().stringToItem(controller.getModelPath() + "/" + name);
if (deck != null) { //reload DeckProxy to pull changes into deck list
deck.reloadFromStorage();
VAllDecks.SINGLETON_INSTANCE.getLstDecks().setSelectedItem(deck);
VAllDecks.SINGLETON_INSTANCE.getLstDecks().repaint();
}
}
} }
// Confirm if a new deck will be created // Confirm if a new deck will be created
else if (FOptionPane.showConfirmDialog("This will create a new deck named '" + else if (FOptionPane.showConfirmDialog("This will create a new deck named '" +
name + "'. Continue?", "Create Deck?")) { name + "'. Continue?", "Create Deck?")) {
controller.saveAs(name); controller.saveAs(name);
CAllDecks.SINGLETON_INSTANCE.refresh(); //pull new deck into deck list and select it
VAllDecks.SINGLETON_INSTANCE.getLstDecks().setSelectedString(controller.getModelPath() + "/" + name);
} }
return true; return true;

View File

@@ -31,6 +31,10 @@ public enum CAllDecks implements ICDoc {
*/ */
@Override @Override
public void initialize() { public void initialize() {
refresh();
}
public void refresh() {
VAllDecks.SINGLETON_INSTANCE.getLstDecks().setPool(DeckProxy.getAllConstructedDecks(Singletons.getModel().getDecks().getConstructed())); VAllDecks.SINGLETON_INSTANCE.getLstDecks().setPool(DeckProxy.getAllConstructedDecks(Singletons.getModel().getDecks().getConstructed()));
} }

View File

@@ -1,6 +1,8 @@
package forge.gui.deckeditor.controllers; package forge.gui.deckeditor.controllers;
import java.awt.Dialog.ModalityType; import java.awt.Dialog.ModalityType;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File; import java.io.File;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
@@ -111,6 +113,14 @@ public enum CCurrentDeck implements ICDoc {
importDeck(); importDeck();
} }
}); });
VCurrentDeck.SINGLETON_INSTANCE.getTxfTitle().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (Character.isLetterOrDigit(e.getKeyChar())) {
CDeckEditorUI.SINGLETON_INSTANCE.getCurrentEditorController().getDeckController().notifyModelChanged();
}
}
});
} }
/** /**

View File

@@ -37,6 +37,7 @@ public class DeckController<T extends DeckBase> {
private boolean modelInStorage; private boolean modelInStorage;
private final IStorage<T> rootFolder; private final IStorage<T> rootFolder;
private IStorage<T> currentFolder; private IStorage<T> currentFolder;
private String modelPath;
private final ACEditorBase<?, T> view; private final ACEditorBase<?, T> view;
private final Supplier<T> newModelCreator; private final Supplier<T> newModelCreator;
@@ -54,6 +55,7 @@ public class DeckController<T extends DeckBase> {
this.model = null; this.model = null;
this.saved = true; this.saved = true;
this.modelInStorage = false; this.modelInStorage = false;
this.modelPath = "";
this.newModelCreator = newModelCreator0; this.newModelCreator = newModelCreator0;
} }
@@ -66,6 +68,10 @@ public class DeckController<T extends DeckBase> {
return this.model; return this.model;
} }
public String getModelPath() {
return this.modelPath;
}
/** /**
* Sets the model. * Sets the model.
* *
@@ -82,7 +88,7 @@ public class DeckController<T extends DeckBase> {
CProbabilities.SINGLETON_INSTANCE.update(); CProbabilities.SINGLETON_INSTANCE.update();
if (this.isModelInSyncWithFolder()) { if (this.isModelInSyncWithFolder()) {
_setSaved(true); this.setSaved(true);
} }
else { else {
this.notifyModelChanged(); this.notifyModelChanged();
@@ -119,10 +125,12 @@ public class DeckController<T extends DeckBase> {
* Notify model changed. * Notify model changed.
*/ */
public void notifyModelChanged() { public void notifyModelChanged() {
_setSaved(false); if (saved) {
this.setSaved(false);
}
} }
private void _setSaved(boolean val) { private void setSaved(boolean val) {
saved = val; saved = val;
updateCaptions(); updateCaptions();
} }
@@ -147,6 +155,7 @@ public class DeckController<T extends DeckBase> {
else { else {
currentFolder = rootFolder.tryGetFolder(path); currentFolder = rootFolder.tryGetFolder(path);
} }
modelPath = path;
load(name); load(name);
} }
@@ -155,13 +164,14 @@ public class DeckController<T extends DeckBase> {
* *
* @param name the name * @param name the name
*/ */
@SuppressWarnings("unchecked") private void load(final String name) { @SuppressWarnings("unchecked")
private void load(final String name) {
T newModel = this.currentFolder.get(name); T newModel = this.currentFolder.get(name);
if (newModel != null) { if (newModel != null) {
this.setModel((T) newModel.copyTo(name), true); this.setModel((T) newModel.copyTo(name), true);
} }
else { else {
_setSaved(true); this.setSaved(true);
} }
} }
@@ -174,11 +184,10 @@ public class DeckController<T extends DeckBase> {
return; return;
} }
this.currentFolder.add(this.model); // copy to new instance before adding to current folder so further changes are auto-saved
// copy to new instance which will be edited and left if unsaved this.currentFolder.add((T) this.model.copyTo(this.model.getName()));
this.model = (T)this.model.copyTo(this.model.getName());
this.modelInStorage = true; this.modelInStorage = true;
_setSaved(true); this.setSaved(true);
} }
/** /**
@@ -191,6 +200,7 @@ public class DeckController<T extends DeckBase> {
this.model = (T)this.model.copyTo(name0); this.model = (T)this.model.copyTo(name0);
this.modelInStorage = false; this.modelInStorage = false;
this.save(); this.save();
this.view.resetTables(); //ensure pool updated in CCurrentDeck
} }
/** /**
@@ -238,7 +248,7 @@ public class DeckController<T extends DeckBase> {
*/ */
public void newModel() { public void newModel() {
this.model = this.newModelCreator.get(); this.model = this.newModelCreator.get();
_setSaved(true); this.setSaved(true);
this.view.resetTables(); this.view.resetTables();
} }

View File

@@ -79,6 +79,8 @@ public enum VAllDecks implements IVDoc<CAllDecks> {
*/ */
@Override @Override
public void populate() { public void populate() {
CAllDecks.SINGLETON_INSTANCE.refresh(); //ensure decks refreshed in case any deleted or added since last loaded
JPanel parentBody = parentCell.getBody(); JPanel parentBody = parentCell.getBody();
parentBody.setLayout(new MigLayout("insets 5, gap 0, wrap, hidemode 3")); parentBody.setLayout(new MigLayout("insets 5, gap 0, wrap, hidemode 3"));
parentBody.add(new ItemManagerContainer(lstDecks), "push, grow"); parentBody.add(new ItemManagerContainer(lstDecks), "push, grow");

View File

@@ -397,7 +397,6 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel {
this.setPool(ItemPool.createFrom(items, this.genericType), false); this.setPool(ItemPool.createFrom(items, this.genericType), false);
} }
/** /**
* *
* Sets the item pool. * Sets the item pool.
@@ -420,7 +419,7 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel {
* @param pool0 * @param pool0
* @param infinite * @param infinite
*/ */
protected void setPoolImpl(final ItemPool<T> pool0, boolean infinite) { private void setPoolImpl(final ItemPool<T> pool0, boolean infinite) {
this.model.clear(); this.model.clear();
this.pool = pool0; this.pool = pool0;
this.model.addItems(this.pool); this.model.addItems(this.pool);
@@ -506,6 +505,35 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel {
return this.table.setSelectedItems(items); return this.table.setSelectedItems(items);
} }
/**
*
* stringToItem.
*
* @param str - String to get item corresponding to
*/
public T stringToItem(String str) {
for (Entry<T, Integer> itemEntry : this.pool) {
if (itemEntry.getKey().toString().equals(str)) {
return itemEntry.getKey();
}
}
return null;
}
/**
*
* setSelectedString.
*
* @param str - String to select
*/
public boolean setSelectedString(String str) {
T item = stringToItem(str);
if (item != null) {
return this.setSelectedItem(item);
}
return false;
}
/** /**
* *
* setSelectedStrings. * setSelectedStrings.
@@ -514,12 +542,10 @@ public abstract class ItemManager<T extends InventoryItem> extends JPanel {
*/ */
public boolean setSelectedStrings(Iterable<String> strings) { public boolean setSelectedStrings(Iterable<String> strings) {
List<T> items = new ArrayList<T>(); List<T> items = new ArrayList<T>();
for (String itemName : strings) { for (String str : strings) {
for (Entry<T, Integer> itemEntry : this.pool) { T item = stringToItem(str);
if (itemEntry.getKey().toString().equals(itemName)) { if (item != null) {
items.add(itemEntry.getKey()); items.add(item);
break;
}
} }
} }
return this.setSelectedItems(items); return this.setSelectedItems(items);