Support setting directory for newly created decks by adding fullPath to StorageBase

This commit is contained in:
drdev
2015-10-04 21:59:20 +00:00
parent c6d81ed4cd
commit 8c49a7d264
15 changed files with 125 additions and 107 deletions

View File

@@ -397,7 +397,6 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
public IItemReader<SealedProduct.Template> getBoosterGenerator() {
// TODO Auto-generated method stub
return new StorageReaderBase<SealedProduct.Template>(null) {
@Override
public Map<String, SealedProduct.Template> readAll() {
Map<String, SealedProduct.Template> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
@@ -411,6 +410,11 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
public String getItemKey(SealedProduct.Template item) {
return item.getEdition();
}
@Override
public String getFullPath() {
return null;
}
};
}

View File

@@ -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;

View File

@@ -26,21 +26,10 @@ import java.util.Map;
* @param <T> the generic type
*/
public interface IItemReader<T> {
String getFullPath();
/**
* Read all.
*
* @return the map
*/
Map<String, T> readAll();
// T read(File file);
/**
* Gets the item key.
*
* @param item the item
* @return the item key
*/
String getItemKey(T item);
Iterable<File> getSubFolders();

View File

@@ -24,6 +24,7 @@ import com.google.common.base.Predicate;
import forge.util.IHasName;
public interface IStorage<T> extends Iterable<T>, IHasName {
String getFullPath();
T get(String name);
T find(Predicate<T> condition);
Collection<String> getItemNames();

View File

@@ -35,42 +35,42 @@ import java.util.*;
public class StorageBase<T> implements IStorage<T> {
protected final Map<String, T> map;
public final static StorageBase<?> emptyMap = new StorageBase<Object>("Empty", new HashMap<String, Object>());
public final String name;
public final static StorageBase<?> emptyMap = new StorageBase<Object>("Empty", null, new HashMap<String, Object>());
public final String name, fullPath;
public StorageBase(final String name, final IItemReader<T> io) {
this.name = name;
this.map = io.readAll();
public StorageBase(final String name0, final IItemReader<T> io) {
this(name0, io.getFullPath(), io.readAll());
}
public StorageBase(final String name, final Map<String, T> inMap) {
this.name = name;
this.map = inMap;
public StorageBase(final String name0, final String fullPath0, final Map<String, T> 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<String> getItemNames() {
return new ArrayList<String>(this.map.keySet());
return new ArrayList<String>(map.keySet());
}
@Override
public Iterator<T> 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<T> implements IStorage<T> {
return (IStorage<IStorage<T>>) 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<T> tryGetFolder(String path) {
throw new UnsupportedOperationException("This storage does not support subfolders");

View File

@@ -9,7 +9,7 @@ public class StorageNestedFolders<T> extends StorageBase<IStorage<T>> {
private final File thisFolder;
public StorageNestedFolders(File thisFolder, Iterable<File> subfolders, Function<File, IStorage<T>> factory) {
super("<Subfolders>", new HashMap<String, IStorage<T>>());
super("<Subfolders>", thisFolder.getPath(), new HashMap<String, IStorage<T>>());
this.thisFolder = thisFolder;
for (File sf : subfolders) {
IStorage<T> newUnit = factory.apply(sf);

View File

@@ -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<T> extends StorageReaderBase<T> {
*/
public StorageReaderFile(final File file0, final Function<? super T, String> 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<T> extends StorageReaderBase<T> {
final Map<String, T> result = new TreeMap<String, T>();
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<T> extends StorageReaderBase<T> {
*/
@Override
public String getItemKey(final T item) {
return this.keySelector.apply(item);
return keySelector.apply(item);
}
}

View File

@@ -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<T> extends StorageReaderBase<T>
public StorageReaderFileSections(final File file0, final Function<? super T, String> keySelector0) {
super(keySelector0);
this.file = file0;
file = file0;
}
@Override
public String getFullPath() {
return file.getPath();
}
protected Map<String, T> createMap() {
@@ -57,13 +64,13 @@ public abstract class StorageReaderFileSections<T> extends StorageReaderBase<T>
final Map<String, T> result = createMap();
int idx = 0;
Iterable<String> file = FileUtil.readFile(this.file);
Iterable<String> contents = FileUtil.readFile(file);
List<String> accumulator = new ArrayList<String>();
String header = null;
for (final String s : file) {
if (!this.lineContainsObject(s)) {
for (final String s : contents) {
if (!lineContainsObject(s)) {
continue;
}
@@ -72,16 +79,18 @@ public abstract class StorageReaderFileSections<T> extends StorageReaderBase<T>
// 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()) {
@@ -98,10 +107,10 @@ public abstract class StorageReaderFileSections<T> extends StorageReaderBase<T>
}
private final T readItem(String header, Iterable<String> 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<T> extends StorageReaderBase<T>
*/
@Override
public String getItemKey(final T item) {
return this.keySelector.apply(item);
return keySelector.apply(item);
}
}

View File

@@ -40,6 +40,11 @@ public abstract class StorageReaderFolder<T> extends StorageReaderBase<T> {
return directory;
}
@Override
public String getFullPath() {
return directory.getPath();
}
protected final File directory;
/**

View File

@@ -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<T extends DeckBase> {
* @param newModelCreator0 the new model creator0
*/
public DeckController(final IStorage<T> folder0, final ACEditorBase<?, T> view0, final Supplier<T> 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<T extends DeckBase> {
* @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<T extends DeckBase> {
*
*/
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<T extends DeckBase> {
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<T extends DeckBase> {
* @return the view
*/
public ACEditorBase<?, T> getView() {
return this.view;
return view;
}
/**
@@ -132,7 +133,7 @@ public class DeckController<T extends DeckBase> {
*/
public void notifyModelChanged() {
if (saved) {
this.setSaved(false);
setSaved(false);
}
}
@@ -145,7 +146,7 @@ public class DeckController<T extends DeckBase> {
* 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<T extends DeckBase> {
*/
@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<T extends DeckBase> {
}
// 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<T extends DeckBase> {
*/
@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<T extends DeckBase> {
* @return true, if is saved
*/
public boolean isSaved() {
return this.saved;
return saved;
}
/**
@@ -227,18 +229,18 @@ public class DeckController<T extends DeckBase> {
* @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<T extends DeckBase> {
* 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);

View File

@@ -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<FDeckEditor> {
// 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);

View File

@@ -66,7 +66,7 @@ public class NetDeckCategory extends StorageBase<Deck> {
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<Deck> {
WaitCallback<Boolean> callback = new WaitCallback<Boolean>() {
@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<Deck> {
private final String url;
private NetDeckCategory(String name0, String url0) {
super(name0, new HashMap<String, Deck>());
super(name0, ForgeConstants.DECK_NET_DIR + name0, new HashMap<String, Deck>());
url = url0;
}
public String getDownloadLocation() {
return ForgeConstants.DECK_NET_DIR + name + "/";
}
public String getUrl() {
return url;
}

View File

@@ -24,7 +24,7 @@ import java.util.Map;
public class ConquestDeckMap extends StorageBase<Deck> {
public ConquestDeckMap(Map<String, Deck> in) {
super("Conquest decks", in);
super("Conquest decks", null, in);
}
@Override

View File

@@ -28,12 +28,11 @@ import forge.util.storage.StorageBase;
*
*/
public class QuestDeckGroupMap extends StorageBase<DeckGroup> {
/**
* Instantiates a new quest deck map.
*/
public QuestDeckGroupMap(Map<String, DeckGroup> in) {
super("Quest draft decks", in == null ? new HashMap<String, DeckGroup>() : in);
super("Quest draft decks", null, in == null ? new HashMap<String, DeckGroup>() : in);
}

View File

@@ -27,12 +27,11 @@ import java.util.Map;
*
*/
public class QuestDeckMap extends StorageBase<Deck> {
/**
* Instantiates a new quest deck map.
*/
public QuestDeckMap(Map<String, Deck> in) {
super("Quest decks", in);
super("Quest decks", null, in);
}