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.Singletons;
import forge.error.BugReporter;
import forge.gui.SOverlayUtils;
import forge.properties.FileLocation;
import forge.properties.ForgeConstants;
@@ -36,6 +37,7 @@ import forge.toolbox.SaveOpenDialog;
import forge.toolbox.SaveOpenDialog.Filetypes;
import forge.util.CollectionSuppliers;
import forge.util.ThreadUtil;
import forge.util.gui.SOptionPane;
import forge.util.maps.HashMapOfLists;
import forge.util.maps.MapOfLists;
import forge.view.FFrame;
@@ -307,6 +309,8 @@ public final class SLayoutIO {
FileOutputStream fos = null;
XMLEventWriter writer = null;
try {
String layoutSerial = getLayoutSerial(file.defaultLoc);
fos = new FileOutputStream(fWriteTo);
writer = out.createXMLEventWriter(fos);
final List<DragCell> cells = FView.SINGLETON_INSTANCE.getDragCells();
@@ -314,6 +318,7 @@ public final class SLayoutIO {
writer.add(EF.createStartDocument());
writer.add(NEWLINE);
writer.add(EF.createStartElement("", "", "layout"));
writer.add(EF.createAttribute("serial", layoutSerial));
writer.add(NEWLINE);
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) {
final FView view = FView.SINGLETON_INSTANCE;
String defaultLayoutSerial = "";
String userLayoutSerial = "";
Boolean resetLayout = false;
FScreen screen = Singletons.getControl().getCurrentScreen();
FAbsolutePositioner.SINGLETON_INSTANCE.hideAll();
view.getPnlInsets().removeAll();
view.getPnlInsets().setLayout(new BorderLayout());
@@ -365,7 +425,7 @@ public final class SLayoutIO {
view.getPnlInsets().setBorder(new EmptyBorder(SLayoutConstants.BORDER_T, SLayoutConstants.BORDER_T, 0, 0));
view.removeAllDragCells();
FileLocation file = Singletons.getControl().getCurrentScreen().getLayoutFile();
FileLocation file = screen.getLayoutFile();
if (file != null) {
// Read a model for new layout
MapOfLists<LayoutInfo, EDocID> model = null;
@@ -379,8 +439,27 @@ public final class SLayoutIO {
else {
File userSetting = new File(file.userPrefLoc);
if (userSetting.exists()) {
usedCustomPrefsFile = true;
defaultLayoutSerial = getLayoutSerial(file.defaultLoc);
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 {
fis = new FileInputStream(file.defaultLoc);
@@ -413,15 +492,15 @@ public final class SLayoutIO {
throw new RuntimeException(e);
}
finally {
if (fis != null) {
try {
if (fis != null) {
fis.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
// Apply new layout
for (Entry<LayoutInfo, Collection<EDocID>> kv : model.entrySet()) {

View File

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