001: /*
002: * Created on 10/11/2004
003: *
004: * ============================================================================
005: * GNU Lesser General Public License
006: * ============================================================================
007: *
008: * Swing Components - visit http://sf.net/projects/gfd
009: *
010: * Copyright (C) 2004 Igor Regis da Silva Simões
011: *
012: * This library is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU Lesser General Public
014: * License as published by the Free Software Foundation; either
015: * version 2.1 of the License, or (at your option) any later version.
016: *
017: * This library is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020: * Lesser General Public License for more details.
021: *
022: * You should have received a copy of the GNU Lesser General Public
023: * License along with this library; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
025: */
026: package br.com.gfpshare.beans.aplicativos;
027:
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.ObjectInputStream;
033: import java.io.ObjectOutputStream;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036:
037: /**
038: * @author Igor Regis da Silva Simoes
039: */
040: public class ArquivosRecentes {
041: private transient ArrayList arqRecentes = new ArrayList(3);
042:
043: private String dir = null;
044:
045: /**
046: * @param dir
047: * @throws IOException
048: * @throws ClassNotFoundException
049: *
050: */
051: public ArquivosRecentes(String dir) throws IOException,
052: ClassNotFoundException {
053: super ();
054: this .dir = dir;
055: loadFile(dir);
056: }
057:
058: /**
059: * Carrega o arquivo com a lista de arquivos recentemente abertos
060: * @param dir Localização do arquivo
061: * @throws IOException
062: * @throws ClassNotFoundException
063: */
064: private void loadFile(String dir) throws IOException,
065: ClassNotFoundException {
066: File arquivo = new File(dir + File.separator
067: + "arqRecentes.arrayList");
068: if (!arquivo.exists()) {
069: arquivo.createNewFile();
070: return;
071: }
072:
073: try {
074: ObjectInputStream ois = new ObjectInputStream(
075: new FileInputStream(arquivo));
076: arqRecentes = (ArrayList) ois.readObject();
077: ois.close();
078: } catch (IOException ioe) {/*TODO*/
079: }
080:
081: //Removemos os arquivos que não existem mais
082: // for(int i=0; i<arqRecentes.size(); i++)
083: // if (!new File(arqRecentes.get(i).toString()).exists())
084: // remove(arqRecentes.get(i));
085: // saveFile(dir);
086: }
087:
088: /**
089: * Salva o arquivo com a lista de arquivos recentemente abertos
090: * @param dir Localização do arquivo
091: * @throws IOException
092: */
093: private void saveFile(String dir) throws IOException {
094: File arquivo = new File(dir + File.separator
095: + "arqRecentes.arrayList");
096: ObjectOutputStream oos = new ObjectOutputStream(
097: new FileOutputStream(arquivo));
098: oos.writeObject(arqRecentes);
099: oos.flush();
100: oos.close();
101: }
102:
103: /**
104: * Retorna um Iterator com a lista de registros dos arquivos recentes
105: * @return Iterator
106: */
107: public Iterator iterator() {
108: return arqRecentes.iterator();
109: }
110:
111: /**
112: * Adiciona um registro da lista de arquivos recentes
113: * @param o Registro a ser adicionado
114: * @return true se houve sucesso
115: * @throws IOException
116: */
117: public boolean add(Object o) throws IOException {
118: if (System.getProperty("os.name").toLowerCase().startsWith(
119: "win"))
120: o = o.toString().toLowerCase();
121: if (arqRecentes.contains(o))
122: return true;
123: boolean retorno = arqRecentes.add(o);
124: arqRecentes.trimToSize();
125: if (arqRecentes.size() > 9)
126: arqRecentes.remove(0);
127: saveFile(dir);
128: return retorno;
129: }
130:
131: /**
132: * Limpa a lista de arquivos recentes
133: * @throws IOException
134: *
135: */
136: public void clear() throws IOException {
137: arqRecentes.clear();
138: saveFile(dir);
139: }
140:
141: /**
142: * Remove um registro da lista de arquivos recentes
143: * @param o Registro a ser removido
144: * @return true se houve sucesso
145: * @throws IOException
146: */
147: public boolean remove(Object o) throws IOException {
148: boolean retorno = arqRecentes.remove(o);
149: saveFile(dir);
150: return retorno;
151: }
152:
153: /**
154: * Retorna a lista de arquivos em forma de um array de objetos
155: * @return array de objetos
156: */
157: public Object[] toArray() {
158: return arqRecentes.toArray();
159: }
160: }
|