add dated serial to layout file

Signed-off-by: Jamin W. Collins <jamin.collins@gmail.com>
This commit is contained in:
Jamin W. Collins
2018-09-06 18:30:19 -06:00
parent 33d7a0adcc
commit df91964573
2 changed files with 90 additions and 11 deletions

View File

@@ -28,6 +28,7 @@ import javax.xml.stream.events.XMLEvent;
import forge.FThreads; import forge.FThreads;
import forge.Singletons; import forge.Singletons;
import forge.error.BugReporter;
import forge.gui.SOverlayUtils; import forge.gui.SOverlayUtils;
import forge.properties.FileLocation; import forge.properties.FileLocation;
import forge.properties.ForgeConstants; import forge.properties.ForgeConstants;
@@ -36,6 +37,7 @@ import forge.toolbox.SaveOpenDialog;
import forge.toolbox.SaveOpenDialog.Filetypes; import forge.toolbox.SaveOpenDialog.Filetypes;
import forge.util.CollectionSuppliers; import forge.util.CollectionSuppliers;
import forge.util.ThreadUtil; import forge.util.ThreadUtil;
import forge.util.gui.SOptionPane;
import forge.util.maps.HashMapOfLists; import forge.util.maps.HashMapOfLists;
import forge.util.maps.MapOfLists; import forge.util.maps.MapOfLists;
import forge.view.FFrame; import forge.view.FFrame;
@@ -307,6 +309,8 @@ public final class SLayoutIO {
FileOutputStream fos = null; FileOutputStream fos = null;
XMLEventWriter writer = null; XMLEventWriter writer = null;
try { try {
String layoutSerial = getLayoutSerial(file.defaultLoc);
fos = new FileOutputStream(fWriteTo); fos = new FileOutputStream(fWriteTo);
writer = out.createXMLEventWriter(fos); writer = out.createXMLEventWriter(fos);
final List<DragCell> cells = FView.SINGLETON_INSTANCE.getDragCells(); final List<DragCell> cells = FView.SINGLETON_INSTANCE.getDragCells();
@@ -314,6 +318,7 @@ public final class SLayoutIO {
writer.add(EF.createStartDocument()); writer.add(EF.createStartDocument());
writer.add(NEWLINE); writer.add(NEWLINE);
writer.add(EF.createStartElement("", "", "layout")); writer.add(EF.createStartElement("", "", "layout"));
writer.add(EF.createAttribute("serial", layoutSerial));
writer.add(NEWLINE); writer.add(NEWLINE);
for (final DragCell cell : cells) { for (final DragCell cell : cells) {
@@ -356,8 +361,63 @@ public final class SLayoutIO {
} }
} }
private static String getLayoutSerial(String layoutFileName) {
final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
FileInputStream fis = null;
XMLEventReader reader = null;
XMLEvent event;
StartElement element;
Iterator<?> attributes;
Attribute attribute;
try {
fis = new FileInputStream(layoutFileName);
reader = inputFactory.createXMLEventReader(fis);
while (null != reader && reader.hasNext()) {
event = reader.nextEvent();
if (event.isStartElement()) {
element = event.asStartElement();
if (element.getName().getLocalPart().equals("layout")) {
attributes = element.getAttributes();
while (attributes.hasNext()) {
attribute = (Attribute) attributes.next();
String atrName = attribute.getName().toString();
if (atrName.equals("serial")) {
return attribute.getValue();
}
}
}
}
}
} catch (final Exception e) { // I don't care what happened inside, the layout is wrong
e.printStackTrace();
}
finally {
try {
if (reader != null) {
reader.close();
}
if (fis != null) {
fis.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
public static void loadLayout(final File f) { public static void loadLayout(final File f) {
final FView view = FView.SINGLETON_INSTANCE; final FView view = FView.SINGLETON_INSTANCE;
String defaultLayoutSerial = "";
String userLayoutSerial = "";
Boolean resetLayout = false;
FScreen screen = Singletons.getControl().getCurrentScreen();
FAbsolutePositioner.SINGLETON_INSTANCE.hideAll(); FAbsolutePositioner.SINGLETON_INSTANCE.hideAll();
view.getPnlInsets().removeAll(); view.getPnlInsets().removeAll();
view.getPnlInsets().setLayout(new BorderLayout()); view.getPnlInsets().setLayout(new BorderLayout());
@@ -365,13 +425,13 @@ public final class SLayoutIO {
view.getPnlInsets().setBorder(new EmptyBorder(SLayoutConstants.BORDER_T, SLayoutConstants.BORDER_T, 0, 0)); view.getPnlInsets().setBorder(new EmptyBorder(SLayoutConstants.BORDER_T, SLayoutConstants.BORDER_T, 0, 0));
view.removeAllDragCells(); view.removeAllDragCells();
FileLocation file = Singletons.getControl().getCurrentScreen().getLayoutFile(); FileLocation file = screen.getLayoutFile();
if (file != null) { if (file != null) {
// Read a model for new layout // Read a model for new layout
MapOfLists<LayoutInfo, EDocID> model = null; MapOfLists<LayoutInfo, EDocID> model = null;
boolean usedCustomPrefsFile = false; boolean usedCustomPrefsFile = false;
FileInputStream fis = null; FileInputStream fis = null;
try { try {
if (f != null && f.exists()) { if (f != null && f.exists()) {
fis = new FileInputStream(f); fis = new FileInputStream(f);
@@ -379,8 +439,27 @@ public final class SLayoutIO {
else { else {
File userSetting = new File(file.userPrefLoc); File userSetting = new File(file.userPrefLoc);
if (userSetting.exists()) { if (userSetting.exists()) {
usedCustomPrefsFile = true; defaultLayoutSerial = getLayoutSerial(file.defaultLoc);
fis = new FileInputStream(userSetting); userLayoutSerial = getLayoutSerial(file.userPrefLoc);
if (defaultLayoutSerial.compareTo(userLayoutSerial) > 0) {
// prompt the user that their saved layout is older
resetLayout = SOptionPane.showConfirmDialog(
String.format("Your %s layout file is from an older template.",
screen.getTabCaption()
),
"Reset Layout?",
"Reset",
"Keep");
}
if (resetLayout) {
// delete the old layout file
screen.deleteLayoutFile();
fis = new FileInputStream(file.defaultLoc);
} else {
fis = new FileInputStream(userSetting);
usedCustomPrefsFile = true;
}
} }
else { else {
fis = new FileInputStream(file.defaultLoc); fis = new FileInputStream(file.defaultLoc);
@@ -390,7 +469,7 @@ public final class SLayoutIO {
final XMLInputFactory inputFactory = XMLInputFactory.newInstance(); final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader xer = null; XMLEventReader xer = null;
try { try {
xer = inputFactory.createXMLEventReader(fis); xer = inputFactory.createXMLEventReader(fis);
model = readLayout(xer); model = readLayout(xer);
} catch (final Exception e) { // I don't care what happened inside, the layout is wrong } catch (final Exception e) { // I don't care what happened inside, the layout is wrong
try { try {
@@ -407,19 +486,19 @@ public final class SLayoutIO {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
finally { finally {
if (fis != null) { try {
try { if (fis != null) {
fis.close(); fis.close();
} }
catch (IOException e) { }
catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}
} }
} }

View File

@@ -1,5 +1,5 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<layout> <layout serial="2018090601">
<cell x="0.77443" y="0.37698" w="0.22557" h="0.62302"> <cell x="0.77443" y="0.37698" w="0.22557" h="0.62302">
<doc>CARD_PICTURE</doc> <doc>CARD_PICTURE</doc>
</cell> </cell>