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; package Applicatie;
import java.io.EOFException; import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -14,42 +15,53 @@ import javax.swing.filechooser.FileFilter;
import Agenda.Agenda; import Agenda.Agenda;
import Objects.DrawObject; import Objects.DrawObject;
public class SaveLoad { public class SaveLoad
{
public static void save(Panel panel) public static void save(Panel panel)
{ {
JFileChooser fileChooser = new JFileChooser(); 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 fileChooser.setFileFilter(new FileFilter()
public String getDescription() { {
return ".agn"; @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"); fileChooser.setDialogTitle("Choose save location");
int userSelection = fileChooser.showSaveDialog(null); int userSelection = fileChooser.showSaveDialog(null);
if(userSelection == JFileChooser.APPROVE_OPTION) if (userSelection == JFileChooser.APPROVE_OPTION)
{ {
File file = fileChooser.getSelectedFile(); File file = fileChooser.getSelectedFile();
if(!file.getName().endsWith(".agn")) if (!file.getName().endsWith(".agn"))
{ {
file = new File(file.getAbsolutePath() + ".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); 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); FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(panel.getAgenda()); oos.writeObject(panel.getAgenda());
for (DrawObject object : panel.objects) { for (DrawObject object : panel.objects)
{
oos.writeObject(object); oos.writeObject(object);
} }
oos.close(); oos.close();
JOptionPane.showMessageDialog(null, "Saved succesfully"); JOptionPane.showMessageDialog(null, "Saved succesfully");
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void load(Panel panel) public static void load(Panel panel)
{ {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() { fileChooser.setFileFilter(new FileFilter()
{
@Override @Override
public boolean accept(File pathname) { public boolean accept(File pathname)
if(pathname.isFile() && pathname.getName().endsWith(".agn")) {
if (pathname.isFile() && pathname.getName().endsWith(".agn"))
{ {
return true; return true;
}else if(pathname.isDirectory()){return true;}else{return false;} }
else if (pathname.isDirectory())
{
return true;
}
else
{
return false;
}
} }
@Override @Override
public String getDescription() { public String getDescription()
{
return ".agn"; return ".agn";
} }
}); });
fileChooser.setDialogTitle("Choose file"); fileChooser.setDialogTitle("Choose file");
int userSelection = fileChooser.showOpenDialog(null); int userSelection = fileChooser.showOpenDialog(null);
if(userSelection == JFileChooser.APPROVE_OPTION) if (userSelection == JFileChooser.APPROVE_OPTION)
{ {
File file = fileChooser.getSelectedFile(); File file = fileChooser.getSelectedFile();
if(!file.exists()) if (!file.exists())
{ {
JOptionPane.showMessageDialog(null, "This file does not exist: " + file.getName()); 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; FileInputStream fis = null;
ObjectInputStream ois = null; ObjectInputStream ois = null;
try { try
{
fis = new FileInputStream(file); fis = new FileInputStream(file);
ois = new ObjectInputStream(fis); ois = new ObjectInputStream(fis);
Object object; Object object;
Agenda a = (Agenda) ois.readObject(); Agenda a = (Agenda) ois.readObject();
panel.setAgenda(a); panel.setAgenda(a);
try { try
{
object = ois.readObject(); object = ois.readObject();
panel.clearObjects(); panel.clearObjects();
while (object != null) { while (object != null)
panel.objects.add((DrawObject) object); {
if (object instanceof Waypoint)
{
panel.addWaypoint((Waypoint) object);
panel.objects.add((DrawObject) object);
}
else
{
panel.objects.add((DrawObject) object);
}
object = ois.readObject(); object = ois.readObject();
} }
} catch (EOFException e) {
} }
} catch (Exception e) { catch (EOFException e)
{
}
}
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
panel.update(); panel.update();
} }
} }