Board.java
jmud-0.11/src/Board.java · 3.6 KB
From the archive jmud-0_11.tgz
Download the archive « Back to the archive contents
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import util.log.Log;
import util.file.InvalidFileFormatException;
import misc.Separators;
import string.StrUtil;
import util.list.Vector;
class Board {
private int theId;
private String theTitle;
private int readLevel;
private int writeLevel;
private int admLevel;
private Vector messages = new Vector();
Board(int id, String title, int read, int write, int adm, Config cfg) {
theId = id;
theTitle = title;
readLevel = read;
writeLevel = write;
admLevel = adm;
load(cfg);
}
int getId() {
return theId;
}
String getTitle() {
return theTitle;
}
int getReadLevel() {
return readLevel;
}
int getWriteLevel() {
return writeLevel;
}
int getAdmLevel() {
return admLevel;
}
void load(Config cfg) {
String fName = cfg.getBoardFilesDir() + theId;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fName));
for (; ; ) {
if (!reader.ready()) {
Log.err("Arquivo de quadro com formato inválido: " + fName);
break;
}
String line = reader.readLine();
if (line.startsWith(Separators.EOF))
break;
if (!line.startsWith(Separators.BOR)) {
Log.err("Arquivo de quadro com formato invalido: " + fName);
break;
}
BoardEntry entry = new BoardEntry(reader);
if (Debug.full)
Log.log("Entrada: " + entry.getHeader());
messages.insert(entry);
}
}
catch(FileNotFoundException e) {
Log.err("Arquivo de quadro não encontrado: " + fName);
}
catch(IOException e) {
Log.err("Falha de E/S ao ler arquivo de quadro: " + fName);
}
catch(InvalidFileFormatException e) {
Log.err("Arquivo de quadro com formato inválido: " + fName);
}
if (reader != null) {
try {
reader.close();
}
catch(IOException e) {
Log.err("Falha de E/S ao fechar arquivo de quadro: " + fName);
}
}
messages.trim();
}
void save(Config cfg) {
String fName = cfg.getBoardFilesDir() + theId;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fName));
for (Enumeration enum = messages.elements(); enum.hasMoreElements(); ) {
BoardEntry be = (BoardEntry) enum.nextElement();
writer.write(Separators.BOR); writer.newLine();
be.save(writer);
}
writer.write(Separators.EOF); writer.newLine();
}
catch(IOException e) {
Log.err("Falha de E/S ao gravar arquivo de quadro: " + fName);
}
if (writer != null) {
try {
writer.close();
}
catch(IOException e) {
Log.err("Falha de E/S ao fechar arquivo de quadro: " + fName);
}
}
}
String getSheet() {
return "ID [" + theId + "], Título: " + theTitle + ", Ler: " + readLevel + ", Escrever: " + writeLevel + ", Adm: " + admLevel;
}
String getSummary() {
String summ = "";
int i = 1;
for (Enumeration enum = messages.elements(); enum.hasMoreElements(); ++i) {
BoardEntry entry = (BoardEntry) enum.nextElement();
summ += Separators.NL + StrUtil.formatNumber(i, 2) + ". " + entry.getHeader();
}
return summ;
}
BoardEntry getMessage(int ind) {
try {
return (BoardEntry) messages.getElementAt(ind - 1);
}
catch(ArrayIndexOutOfBoundsException e) {
}
return null;
}
void putMessage(BoardEntry be) {
messages.insert(be);
}
void eraseMessage(int ind) throws ArrayIndexOutOfBoundsException {
messages.removeFrom(ind - 1);
}
}