diff --git a/forge-core/src/main/java/forge/card/CardEdition.java b/forge-core/src/main/java/forge/card/CardEdition.java index fdcd7107759..d644d129850 100644 --- a/forge-core/src/main/java/forge/card/CardEdition.java +++ b/forge-core/src/main/java/forge/card/CardEdition.java @@ -397,7 +397,6 @@ public final class CardEdition implements Comparable { // immutable public IItemReader getBoosterGenerator() { // TODO Auto-generated method stub return new StorageReaderBase(null) { - @Override public Map readAll() { Map map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); @@ -411,6 +410,11 @@ public final class CardEdition implements Comparable { // immutable public String getItemKey(SealedProduct.Template item) { return item.getEdition(); } + + @Override + public String getFullPath() { + return null; + } }; } diff --git a/forge-core/src/main/java/forge/item/PreconDeck.java b/forge-core/src/main/java/forge/item/PreconDeck.java index cda6a84e45e..63c588c3087 100644 --- a/forge-core/src/main/java/forge/item/PreconDeck.java +++ b/forge-core/src/main/java/forge/item/PreconDeck.java @@ -18,6 +18,7 @@ package forge.item; import com.google.common.base.Function; + import forge.StaticData; import forge.deck.Deck; import forge.deck.io.DeckSerializer; diff --git a/forge-core/src/main/java/forge/util/IItemReader.java b/forge-core/src/main/java/forge/util/IItemReader.java index 4f57bcea2df..030cecab448 100644 --- a/forge-core/src/main/java/forge/util/IItemReader.java +++ b/forge-core/src/main/java/forge/util/IItemReader.java @@ -26,21 +26,10 @@ import java.util.Map; * @param the generic type */ public interface IItemReader { + String getFullPath(); - /** - * Read all. - * - * @return the map - */ Map readAll(); - // T read(File file); - /** - * Gets the item key. - * - * @param item the item - * @return the item key - */ String getItemKey(T item); Iterable getSubFolders(); diff --git a/forge-core/src/main/java/forge/util/storage/IStorage.java b/forge-core/src/main/java/forge/util/storage/IStorage.java index 465a4ceb531..6c0cb2f8c4a 100644 --- a/forge-core/src/main/java/forge/util/storage/IStorage.java +++ b/forge-core/src/main/java/forge/util/storage/IStorage.java @@ -24,6 +24,7 @@ import com.google.common.base.Predicate; import forge.util.IHasName; public interface IStorage extends Iterable, IHasName { + String getFullPath(); T get(String name); T find(Predicate condition); Collection getItemNames(); diff --git a/forge-core/src/main/java/forge/util/storage/StorageBase.java b/forge-core/src/main/java/forge/util/storage/StorageBase.java index 97de009c62c..623bc326a42 100644 --- a/forge-core/src/main/java/forge/util/storage/StorageBase.java +++ b/forge-core/src/main/java/forge/util/storage/StorageBase.java @@ -35,42 +35,42 @@ import java.util.*; public class StorageBase implements IStorage { protected final Map map; - public final static StorageBase emptyMap = new StorageBase("Empty", new HashMap()); - public final String name; + public final static StorageBase emptyMap = new StorageBase("Empty", null, new HashMap()); + public final String name, fullPath; - public StorageBase(final String name, final IItemReader io) { - this.name = name; - this.map = io.readAll(); + public StorageBase(final String name0, final IItemReader io) { + this(name0, io.getFullPath(), io.readAll()); } - public StorageBase(final String name, final Map inMap) { - this.name = name; - this.map = inMap; + public StorageBase(final String name0, final String fullPath0, final Map map0) { + name = name0; + fullPath = fullPath0; + map = map0; } @Override public T get(final String name) { - return this.map.get(name); + return map.get(name); } @Override public final Collection getItemNames() { - return new ArrayList(this.map.keySet()); + return new ArrayList(map.keySet()); } @Override public Iterator iterator() { - return this.map.values().iterator(); + return map.values().iterator(); } @Override public boolean contains(String name) { - return name == null ? false : this.map.containsKey(name); + return name == null ? false : map.containsKey(name); } @Override public int size() { - return this.map.size(); + return map.size(); } @Override @@ -95,15 +95,19 @@ public class StorageBase implements IStorage { return (IStorage>) emptyMap; } - /* (non-Javadoc) - * @see forge.util.IHasName#getName() - */ @Override - public String getName() { - // TODO Auto-generated method stub + public final String getName() { return name; } + @Override + public final String getFullPath() { + if (fullPath == null) { + return name; + } + return fullPath; + } + @Override public IStorage tryGetFolder(String path) { throw new UnsupportedOperationException("This storage does not support subfolders"); diff --git a/forge-core/src/main/java/forge/util/storage/StorageNestedFolders.java b/forge-core/src/main/java/forge/util/storage/StorageNestedFolders.java index bc41f8d7dac..d48df1bef2e 100644 --- a/forge-core/src/main/java/forge/util/storage/StorageNestedFolders.java +++ b/forge-core/src/main/java/forge/util/storage/StorageNestedFolders.java @@ -9,7 +9,7 @@ public class StorageNestedFolders extends StorageBase> { private final File thisFolder; public StorageNestedFolders(File thisFolder, Iterable subfolders, Function> factory) { - super("", new HashMap>()); + super("", thisFolder.getPath(), new HashMap>()); this.thisFolder = thisFolder; for (File sf : subfolders) { IStorage newUnit = factory.apply(sf); diff --git a/forge-core/src/main/java/forge/util/storage/StorageReaderFile.java b/forge-core/src/main/java/forge/util/storage/StorageReaderFile.java index 97f11840369..b5a4ba45f0d 100644 --- a/forge-core/src/main/java/forge/util/storage/StorageReaderFile.java +++ b/forge-core/src/main/java/forge/util/storage/StorageReaderFile.java @@ -18,7 +18,9 @@ package forge.util.storage; import com.google.common.base.Function; + import forge.util.FileUtil; + import org.apache.commons.lang3.StringUtils; import java.io.File; @@ -52,7 +54,12 @@ public abstract class StorageReaderFile extends StorageReaderBase { */ public StorageReaderFile(final File file0, final Function keySelector0) { super(keySelector0); - this.file = file0; + file = file0; + } + + @Override + public String getFullPath() { + return file.getPath(); } /* (non-Javadoc) @@ -63,14 +70,14 @@ public abstract class StorageReaderFile extends StorageReaderBase { final Map result = new TreeMap(); int idx = 0; - for (final String s : FileUtil.readFile(this.file)) { - if (!this.lineContainsObject(s)) { + for (final String s : FileUtil.readFile(file)) { + if (!lineContainsObject(s)) { continue; } - final T item = this.read(s, idx); + final T item = read(s, idx); if (null == item) { - final String msg = "An object stored in " + this.file.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file attached."; + final String msg = "An object stored in " + file.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file attached."; throw new RuntimeException(msg); } @@ -110,6 +117,6 @@ public abstract class StorageReaderFile extends StorageReaderBase { */ @Override public String getItemKey(final T item) { - return this.keySelector.apply(item); + return keySelector.apply(item); } } diff --git a/forge-core/src/main/java/forge/util/storage/StorageReaderFileSections.java b/forge-core/src/main/java/forge/util/storage/StorageReaderFileSections.java index a3395f9230a..27131b66bc2 100644 --- a/forge-core/src/main/java/forge/util/storage/StorageReaderFileSections.java +++ b/forge-core/src/main/java/forge/util/storage/StorageReaderFileSections.java @@ -18,7 +18,9 @@ package forge.util.storage; import com.google.common.base.Function; + import forge.util.FileUtil; + import org.apache.commons.lang3.StringUtils; import java.io.File; @@ -42,7 +44,12 @@ public abstract class StorageReaderFileSections extends StorageReaderBase public StorageReaderFileSections(final File file0, final Function keySelector0) { super(keySelector0); - this.file = file0; + file = file0; + } + + @Override + public String getFullPath() { + return file.getPath(); } protected Map createMap() { @@ -57,34 +64,36 @@ public abstract class StorageReaderFileSections extends StorageReaderBase final Map result = createMap(); int idx = 0; - Iterable file = FileUtil.readFile(this.file); + Iterable contents = FileUtil.readFile(file); List accumulator = new ArrayList(); String header = null; - for (final String s : file) { - if (!this.lineContainsObject(s)) { + for (final String s : contents) { + if (!lineContainsObject(s)) { continue; } - if(s.charAt(0) == '[') { + if (s.charAt(0) == '[') { if( header != null ) { // read previously collected item T item = readItem(header, accumulator, idx); if( item != null ) { - result.put(this.keySelector.apply(item), item); + result.put(keySelector.apply(item), item); idx++; } } header = StringUtils.strip(s, "[] "); accumulator.clear(); - } else + } + else { accumulator.add(s); + } } // store the last item - if ( !accumulator.isEmpty() ) { + if (!accumulator.isEmpty()) { T item = readItem(header, accumulator, idx); if( item != null ) { String newKey = keySelector.apply(item); @@ -98,10 +107,10 @@ public abstract class StorageReaderFileSections extends StorageReaderBase } private final T readItem(String header, Iterable accumulator, int idx) { - final T item = this.read(header, accumulator, idx); + final T item = read(header, accumulator, idx); if (null != item) return item; - final String msg = "An object stored in " + this.file.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file attached."; + final String msg = "An object stored in " + file.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file attached."; throw new RuntimeException(msg); } @@ -130,6 +139,6 @@ public abstract class StorageReaderFileSections extends StorageReaderBase */ @Override public String getItemKey(final T item) { - return this.keySelector.apply(item); + return keySelector.apply(item); } } diff --git a/forge-core/src/main/java/forge/util/storage/StorageReaderFolder.java b/forge-core/src/main/java/forge/util/storage/StorageReaderFolder.java index e52a5956703..844b001abf9 100644 --- a/forge-core/src/main/java/forge/util/storage/StorageReaderFolder.java +++ b/forge-core/src/main/java/forge/util/storage/StorageReaderFolder.java @@ -40,6 +40,11 @@ public abstract class StorageReaderFolder extends StorageReaderBase { return directory; } + @Override + public String getFullPath() { + return directory.getPath(); + } + protected final File directory; /** diff --git a/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/DeckController.java b/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/DeckController.java index b744dbed765..4eb6408cbd6 100644 --- a/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/DeckController.java +++ b/forge-gui-desktop/src/main/java/forge/screens/deckeditor/controllers/DeckController.java @@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils; import com.google.common.base.Supplier; import forge.deck.DeckBase; +import forge.properties.ForgeConstants; import forge.screens.deckeditor.menus.DeckFileMenu; import forge.screens.deckeditor.views.VCurrentDeck; import forge.screens.home.sanctioned.VSubmenuConstructed; @@ -45,14 +46,14 @@ public class DeckController { * @param newModelCreator0 the new model creator0 */ public DeckController(final IStorage folder0, final ACEditorBase view0, final Supplier newModelCreator0) { - this.rootFolder = folder0; - this.currentFolder = rootFolder; - this.view = view0; - this.model = null; - this.saved = true; - this.modelInStorage = false; - this.modelPath = ""; - this.newModelCreator = newModelCreator0; + rootFolder = folder0; + currentFolder = rootFolder; + view = view0; + model = null; + saved = true; + modelInStorage = false; + modelPath = ""; + newModelCreator = newModelCreator0; } /** @@ -61,11 +62,11 @@ public class DeckController { * @return the document */ public T getModel() { - return this.model; + return model; } public String getModelPath() { - return this.modelPath; + return modelPath; } public boolean isEmpty() { @@ -77,27 +78,27 @@ public class DeckController { * */ public void setModel(final T document) { - this.setModel(document, false); + setModel(document, false); } public void setModel(final T document, final boolean isStored) { - this.modelInStorage = isStored; - this.model = document; - this.view.resetTables(); + modelInStorage = isStored; + model = document; + view.resetTables(); CStatistics.SINGLETON_INSTANCE.update(); CProbabilities.SINGLETON_INSTANCE.update(); if (isStored) { - if (this.isModelInSyncWithFolder()) { - this.setSaved(true); + if (isModelInSyncWithFolder()) { + setSaved(true); } else { - this.notifyModelChanged(); + notifyModelChanged(); } } else { //TODO: Make this smarter - this.currentFolder = this.rootFolder; - this.modelPath = ""; - this.setSaved(true); + currentFolder = rootFolder; + modelPath = ""; + setSaved(true); } } @@ -106,16 +107,16 @@ public class DeckController { return true; } - final T modelStored = this.currentFolder.get(this.model.getName()); + final T modelStored = currentFolder.get(model.getName()); // checks presence in dictionary only. - if (modelStored == this.model) { + if (modelStored == model) { return true; } if (modelStored == null) { return false; } - return modelStored.equals(this.model); + return modelStored.equals(model); } /** @@ -124,7 +125,7 @@ public class DeckController { * @return the view */ public ACEditorBase getView() { - return this.view; + return view; } /** @@ -132,7 +133,7 @@ public class DeckController { */ public void notifyModelChanged() { if (saved) { - this.setSaved(false); + setSaved(false); } } @@ -145,7 +146,7 @@ public class DeckController { * Reload current model */ public void reload() { - final String name = this.getModelName(); + final String name = getModelName(); if (name.isEmpty()) { newModel(); } @@ -172,12 +173,12 @@ public class DeckController { */ @SuppressWarnings("unchecked") private void load(final String name) { - final T newModel = this.currentFolder.get(name); + final T newModel = currentFolder.get(name); if (newModel != null) { - this.setModel((T) newModel.copyTo(name), true); + setModel((T) newModel.copyTo(name), true); } else { - this.setSaved(true); + setSaved(true); } } @@ -191,9 +192,10 @@ public class DeckController { } // copy to new instance before adding to current folder so further changes are auto-saved - this.currentFolder.add((T) this.model.copyTo(this.model.getName())); - this.modelInStorage = true; - this.setSaved(true); + currentFolder.add((T) model.copyTo(model.getName())); + model.setDirectory(currentFolder.getFullPath().substring(ForgeConstants.DECK_BASE_DIR.length())); + modelInStorage = true; + setSaved(true); VSubmenuConstructed.SINGLETON_INSTANCE.getLobby().updateDeckPanel(); } @@ -205,10 +207,10 @@ public class DeckController { */ @SuppressWarnings("unchecked") public void saveAs(final String name0) { - this.model = (T)this.model.copyTo(name0); - this.modelInStorage = false; - this.save(); - this.view.resetTables(); //ensure pool updated in CCurrentDeck + model = (T)model.copyTo(name0); + modelInStorage = false; + save(); + view.resetTables(); //ensure pool updated in CCurrentDeck } /** @@ -217,7 +219,7 @@ public class DeckController { * @return true, if is saved */ public boolean isSaved() { - return this.saved; + return saved; } /** @@ -227,18 +229,18 @@ public class DeckController { * @return true, if successful */ public boolean fileExists(final String deckName) { - return this.currentFolder.contains(deckName); + return currentFolder.contains(deckName); } /** * Refresh current model or create new one if none */ public void refreshModel() { - if (this.model == null) { + if (model == null) { newModel(); } else { - setModel(this.model, this.modelInStorage); + setModel(model, modelInStorage); } } @@ -246,25 +248,25 @@ public class DeckController { * New model. */ public void newModel() { - this.model = this.newModelCreator.get(); - this.setSaved(true); - this.view.resetTables(); + model = newModelCreator.get(); + setSaved(true); + view.resetTables(); } public String getModelName() { - return this.model != null ? this.model.getName() : ""; + return model != null ? model.getName() : ""; } public void updateCaptions() { String tabCaption = "Current Deck"; - final String title = this.getModelName(); + final String title = getModelName(); String itemManagerCaption = title.isEmpty() ? "[Untitled]" : title; if (!saved) { tabCaption = "*" + tabCaption; itemManagerCaption = "*" + itemManagerCaption; } - itemManagerCaption += " - " + this.view.getSectionMode().name(); + itemManagerCaption += " - " + view.getSectionMode().name(); VCurrentDeck.SINGLETON_INSTANCE.getTabLabel().setText(tabCaption); VCurrentDeck.SINGLETON_INSTANCE.getTxfTitle().setText(title); diff --git a/forge-gui-mobile/src/forge/deck/FDeckEditor.java b/forge-gui-mobile/src/forge/deck/FDeckEditor.java index ec1b43b8cd9..10d866eb370 100644 --- a/forge-gui-mobile/src/forge/deck/FDeckEditor.java +++ b/forge-gui-mobile/src/forge/deck/FDeckEditor.java @@ -28,6 +28,7 @@ import forge.menu.FMenuItem; import forge.menu.FPopupMenu; import forge.model.FModel; import forge.planarconquest.ConquestUtil; +import forge.properties.ForgeConstants; import forge.properties.ForgePreferences.FPref; import forge.quest.data.QuestPreferences.QPref; import forge.screens.FScreen; @@ -1443,6 +1444,7 @@ public class FDeckEditor extends TabPageScreen { // copy to new instance before adding to current folder so further changes are auto-saved currentFolder.add((T) model.copyTo(model.getName())); + model.setDirectory(currentFolder.getFullPath().substring(ForgeConstants.DECK_BASE_DIR.length())); modelInStorage = true; setSaved(true); diff --git a/forge-gui/src/main/java/forge/deck/NetDeckCategory.java b/forge-gui/src/main/java/forge/deck/NetDeckCategory.java index 275f9278513..137cae3e250 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckCategory.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckCategory.java @@ -66,7 +66,7 @@ public class NetDeckCategory extends StorageBase { NetDeckCategory category = categories.get(name); if (category != null && category.map.isEmpty()) { //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getDownloadLocation()); + File downloadDir = new File(category.getFullPath()); if (downloadDir.exists()) { for (File file : downloadDir.listFiles(DeckStorage.DCK_FILE_FILTER)) { Deck deck = DeckSerializer.fromFile(file); @@ -86,7 +86,7 @@ public class NetDeckCategory extends StorageBase { WaitCallback callback = new WaitCallback() { @Override public void run() { - String downloadLoc = c.getDownloadLocation(); + String downloadLoc = c.getFullPath(); GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { @Override protected void copyInputStream(InputStream in, String outPath) throws IOException { @@ -108,14 +108,10 @@ public class NetDeckCategory extends StorageBase { private final String url; private NetDeckCategory(String name0, String url0) { - super(name0, new HashMap()); + super(name0, ForgeConstants.DECK_NET_DIR + name0, new HashMap()); url = url0; } - public String getDownloadLocation() { - return ForgeConstants.DECK_NET_DIR + name + "/"; - } - public String getUrl() { return url; } diff --git a/forge-gui/src/main/java/forge/planarconquest/ConquestDeckMap.java b/forge-gui/src/main/java/forge/planarconquest/ConquestDeckMap.java index 741f3666ef8..d20ea4430cf 100644 --- a/forge-gui/src/main/java/forge/planarconquest/ConquestDeckMap.java +++ b/forge-gui/src/main/java/forge/planarconquest/ConquestDeckMap.java @@ -24,7 +24,7 @@ import java.util.Map; public class ConquestDeckMap extends StorageBase { public ConquestDeckMap(Map in) { - super("Conquest decks", in); + super("Conquest decks", null, in); } @Override diff --git a/forge-gui/src/main/java/forge/quest/QuestDeckGroupMap.java b/forge-gui/src/main/java/forge/quest/QuestDeckGroupMap.java index ad1095fe4e6..17c9538ab5c 100644 --- a/forge-gui/src/main/java/forge/quest/QuestDeckGroupMap.java +++ b/forge-gui/src/main/java/forge/quest/QuestDeckGroupMap.java @@ -28,12 +28,11 @@ import forge.util.storage.StorageBase; * */ public class QuestDeckGroupMap extends StorageBase { - /** * Instantiates a new quest deck map. */ public QuestDeckGroupMap(Map in) { - super("Quest draft decks", in == null ? new HashMap() : in); + super("Quest draft decks", null, in == null ? new HashMap() : in); } diff --git a/forge-gui/src/main/java/forge/quest/QuestDeckMap.java b/forge-gui/src/main/java/forge/quest/QuestDeckMap.java index b86bd857137..e3dbe2affd0 100644 --- a/forge-gui/src/main/java/forge/quest/QuestDeckMap.java +++ b/forge-gui/src/main/java/forge/quest/QuestDeckMap.java @@ -27,12 +27,11 @@ import java.util.Map; * */ public class QuestDeckMap extends StorageBase { - /** * Instantiates a new quest deck map. */ public QuestDeckMap(Map in) { - super("Quest decks", in); + super("Quest decks", null, in); }