/* Visual TODO List v1.1 Copyright Florent D'Halluin 2010 This file is part of Visual TODO List v1.1. See VisualTODO.pde for help. COPYRIGHT NOTICE ------------------------------------------------------------------------ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ------------------------------------------------------------------------ */ class FileSaver extends Saver { FileSaver() { e_maxid = 1; v_maxid = 1; } boolean save() { try { FileOutputStream fos = new FileOutputStream(f_name); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeInt(vertices.size()); out.writeInt(edges.size()); // Save vertices Set set = vertices.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry m = (Map.Entry) it.next(); Vertex v = (Vertex) m.getValue(); v.writeExternal(out); } // Save edges set = edges.entrySet(); it = set.iterator(); while (it.hasNext()) { Map.Entry m = (Map.Entry) it.next(); Edge e = (Edge) m.getValue(); e.writeExternal(out); } out.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } boolean load() { try { FileInputStream fis = new FileInputStream(f_name); ObjectInputStream in = new ObjectInputStream(fis); int v_count = in.readInt(); int e_count = in.readInt(); // Load vertices while (v_count > 0) { Vertex v = new Vertex(); v.readExternal(in); v_maxid = max(v_maxid, v.id); v_count -= 1; } // Load edges while (e_count > 0) { Edge e = new Edge(); e.readExternal(in); e_maxid = max(e_maxid, e.id); e_count -= 1; } in.close(); } catch (Exception ex) { vertices.clear(); edges.clear(); // Initialize default structure Vertex v = new Vertex(1, "group", "Home"); vertices.put(1, v); return true; } return true; } boolean saveVertex(Vertex v) { if (read_only) return false; if (v.id == int_max) { v_maxid += 1; v.id = v_maxid; vertices.put(v.id, v); } return this.save(); } boolean removeVertex(Vertex v) { if (read_only) return false; if (v.id == int_max) return false; vertices.remove(v.id); return this.save(); } boolean saveEdge(Edge e) { if (read_only) return false; if (e.id == int_max) { e_maxid += 1; e.id = e_maxid; edges.put(e.id, e); } return this.save(); } boolean removeEdge(Edge e) { if (read_only) return false; if (e.id == int_max) return false; edges.remove(e.id); return this.save(); } int e_maxid; int v_maxid; }