Blame view

BRIC/src/LOGIC/Serialize.java 3.76 KB
eeb5cac08   Madiel de Souza Conserva Filho   first
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package LOGIC;
  
  
  import java.io.EOFException;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  /**
   *
   * @author Dalay
   */
  public class Serialize {
      private LinkedList<ObjectSaved> saves;
  
      public LinkedList<ObjectSaved> getSaves() {
          return saves;
      }
  
      public void setSaves(LinkedList<ObjectSaved> saves) {
          this.saves = saves;
      }
      
      public Serialize(){
          try {
              saves = deserializaLista();
          } catch (EOFException ex) {
              System.out.println(ex+": primeira vez salvando o arquivo...");
          }
      }
      
      public void save(String nome, ObjectList Obj){
          //EstadoSistema e = new EstadoSistema();
          //e.nome = nome;
          //ObjectSerialize.saves.add(e);
          ObjectSaved O = new ObjectSaved(Obj, nome);
          try {
              saves = deserializaLista();
          } catch (EOFException ex) {
              System.out.println("primeira vez salvando o arquivo...");
          }
          if (saves == null){
              saves = new LinkedList<ObjectSaved>();
          }
          saves.add(O);
          serializaLista();
          
          
      }
      
      public ObjectList open(int num){
          return saves.get(num).getSave();
          /*ObjectSerialize.saves = deserializaLista();
          //Instantiations.b = ObjectSerialize.saves.get(num).b;
          Instantiations.canais = ObjectSerialize.saves.get(num).canais;
          Instantiations.component = ObjectSerialize.saves.get(num).component;
          Instantiations.contratos = ObjectSerialize.saves.get(num).contratos;
          //Instantiations.csp = ObjectSerialize.saves.get(num).csp;
          Instantiations.instancias = ObjectSerialize.saves.get(num).instancias;
          Instantiations.types = ObjectSerialize.saves.get(num).types;*/
      }
      
      public void serializaLista() {
  	LinkedList<ObjectSaved> lista = saves;
          String arquivo = "Project/saved.dat";
          FileOutputStream arq = null;
  	ObjectOutputStream out = null;
  	try {
  		//arquivo no qual os dados vao ser gravados
  		arq = new FileOutputStream(arquivo);
                  
  		//objeto que vai escrever os dados
  		out = new ObjectOutputStream(arq);
                  out.reset();
  		//escreve todos os dados
  		out.writeObject(lista);
  	} catch (IOException ex) {
  		ex.printStackTrace();
  	} finally {
  		try {
  			arq.close();
  			out.close();
  		} catch (IOException ex) {
  			ex.printStackTrace();
  		}
  	}
      }
          
      public LinkedList<ObjectSaved> deserializaLista() throws EOFException {
          String arquivo = "Project/saved.dat";
  	FileInputStream arqLeitura = null;
  	ObjectInputStream in = null;
  	LinkedList<ObjectSaved> lista = null;
  	try {
  		//arquivo onde estao os dados serializados
  		arqLeitura = new FileInputStream(arquivo);
                  
  		//objeto que vai ler os dados do arquivo
                  
                  in = new ObjectInputStream(arqLeitura);
  		lista = (LinkedList<ObjectSaved>) in.readObject();
                  
  		//recupera os dados
  		//lista = (List<ObjectSaved>) in.readObject();
  	} catch (ClassNotFoundException ex) {
  		ex.printStackTrace();
  	} catch (IOException ex) {
  		saves = new LinkedList();
  	} finally {
  		try {
  			arqLeitura.close();
  //			in.close();
  		} catch (IOException ex) {
  			ex.printStackTrace();
  		}
  	}
   
  	return lista;
      }
      
      public void remove(int x){
          saves.remove(x);
          serializaLista();
          
      }
      
  }