Load waypoints from filez

This commit is contained in:
Yorick
2015-04-08 21:45:27 +02:00
parent 9d88f92a98
commit f2f0b36d0e
+92 -52
View File
@@ -1,4 +1,5 @@
package Applicatie;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
@@ -14,42 +15,53 @@ import javax.swing.filechooser.FileFilter;
import Agenda.Agenda;
import Objects.DrawObject;
public class SaveLoad {
public class SaveLoad
{
public static void save(Panel panel)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.isFile() && pathname.getName().endsWith(".agn"))
{
return true;
}else if(pathname.isDirectory()){return true;}else{return false;}
}
@Override
public String getDescription() {
return ".agn";
fileChooser.setFileFilter(new FileFilter()
{
@Override
public boolean accept(File pathname)
{
if (pathname.isFile() && pathname.getName().endsWith(".agn"))
{
return true;
}
else if (pathname.isDirectory())
{
return true;
}
else
{
return false;
}
}
@Override
public String getDescription()
{
return ".agn";
}
});
fileChooser.setDialogTitle("Choose save location");
int userSelection = fileChooser.showSaveDialog(null);
if(userSelection == JFileChooser.APPROVE_OPTION)
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
if(!file.getName().endsWith(".agn"))
if (!file.getName().endsWith(".agn"))
{
file = new File(file.getAbsolutePath() + ".agn");
}
if(file.exists())
if (file.exists())
{
if(JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite: " + file.getName(), "Overwrite", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
if (JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite: " + file.getName(), "Overwrite", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
{
saveTerrain(file, panel);
}
@@ -60,50 +72,65 @@ public class SaveLoad {
}
}
}
public static void saveTerrain(File file, Panel panel) {
try {
public static void saveTerrain(File file, Panel panel)
{
try
{
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(panel.getAgenda());
for (DrawObject object : panel.objects) {
for (DrawObject object : panel.objects)
{
oos.writeObject(object);
}
oos.close();
JOptionPane.showMessageDialog(null, "Saved succesfully");
} catch (IOException e) {
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void load(Panel panel)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
fileChooser.setFileFilter(new FileFilter()
{
@Override
public boolean accept(File pathname) {
if(pathname.isFile() && pathname.getName().endsWith(".agn"))
public boolean accept(File pathname)
{
if (pathname.isFile() && pathname.getName().endsWith(".agn"))
{
return true;
}else if(pathname.isDirectory()){return true;}else{return false;}
}
else if (pathname.isDirectory())
{
return true;
}
else
{
return false;
}
}
@Override
public String getDescription() {
public String getDescription()
{
return ".agn";
}
});
fileChooser.setDialogTitle("Choose file");
int userSelection = fileChooser.showOpenDialog(null);
if(userSelection == JFileChooser.APPROVE_OPTION)
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
if(!file.exists())
if (!file.exists())
{
JOptionPane.showMessageDialog(null, "This file does not exist: " + file.getName());
}
@@ -114,35 +141,48 @@ public class SaveLoad {
}
}
}
public static void fillTerrain(File file, Panel panel) {
public static void fillTerrain(File file, Panel panel)
{
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
try
{
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
Object object;
Agenda a = (Agenda) ois.readObject();
panel.setAgenda(a);
try {
try
{
object = ois.readObject();
panel.clearObjects();
while (object != null) {
panel.objects.add((DrawObject) object);
while (object != null)
{
if (object instanceof Waypoint)
{
panel.addWaypoint((Waypoint) object);
panel.objects.add((DrawObject) object);
}
else
{
panel.objects.add((DrawObject) object);
}
object = ois.readObject();
}
} catch (EOFException e) {
}
} catch (Exception e) {
catch (EOFException e)
{
}
}
catch (Exception e)
{
e.printStackTrace();
}
panel.update();
}
}