home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
Data.java
< prev
next >
Wrap
Text File
|
1998-11-05
|
7KB
|
193 lines
// Copyright (c) 1997, 1998 Symantec, Inc. All Rights Reserved.
import java.io.Serializable;
import java.util.Vector;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/*
This class contains all of the non-UI program global data (notice: no import
of "java.awt").
Only one instance of this class is created.
This class (minus some potentially large vectors/arrays) is saved to disk
as the program's "ini" data. The Java serializable mechanism is used to
do this.
*/
public class Data implements Serializable {
// the filename of this program's "ini" file
static final String INI_FILENAME = "WLA.dat";
// The only data instance
static Data theData = null;
// Program Options
int userSessionIdleLimit = 30; // Delay in minutes for a user session to be considered "over"
boolean ignoreUnexpectedLogFileErrors = false;
// All of the reports
Vector reports = new Vector();
// The index of the currently selected report, or -1 if non selected
int currentReportIndex = -1;
/*
Constructs the default Data object.
*/
public Data() {
theData = this;
//{{INIT_CONTROLS
//}}
}
// Returns the one-and-only instance of this class
public static Data getDataInstance() {
return theData;
}
// Notes the one-and-only instance of this class
public static void setDataInstance(Data data) {
theData = data;
}
/*
Re-creates the data instance previously saved to disk using
saveDataInstance(). If no data has been saved or an error occurs, a default
data object is created.
Call getDataInstance() after this method to get the loaded/created data.
Returns null if all OK, otherwise error message.
*/
public static String loadDataInstance() {
String msg = null;
Data data = null;
try {
// get typical input stream
FileInputStream istream = istream = new FileInputStream(INI_FILENAME);
try {
// use input stream as an object input stream (java serialization)
ObjectInputStream p = new ObjectInputStream(istream);
try {
// read the saved Data object
data = (Data)p.readObject();
} catch(java.lang.ClassNotFoundException x) {
msg = x.toString();
} catch(java.io.OptionalDataException x) {
msg = x.toString();
}
// close the object input stream
p.close();
} catch(java.io.StreamCorruptedException x) {
msg = x.toString();
}
istream.close();
} catch(java.io.FileNotFoundException x) {
// can't find the program data file...(OK)
} catch(java.io.IOException x) {
// trouble reading program data file...notify user
msg = x.toString();
}
// If couldn't load, create defaults
if(data == null) {
// Couldn't read data, create new with default values
data = new Data();
} else {
// Set static that points to the one instance of program data
Data.setDataInstance(data);
}
return msg;
}
/**
Saves the current data object to disk.
The program data is saved using the Java serialization feature.
Potentially large vectors/arrays are freed before saving.
Returns null if all OK, otherwise error message.
*/
public static String saveDataInstance() {
// Don't save the logFile.records Vector, and other bulky non-essentials
freeUpMem();
// save program data
String msg = null;
try {
// get a typical output stream for writing to a file
FileOutputStream ostream = ostream = new FileOutputStream(INI_FILENAME);
try {
// use output stream as an object output stream (java serialization)
ObjectOutputStream p = new ObjectOutputStream(ostream);
try {
// write the data
p.writeObject(getDataInstance());
} catch(java.io.OptionalDataException x) {
msg = x.toString();
}
// close the object output stream
p.close();
} catch(java.io.StreamCorruptedException x) {
msg = x.toString();
}
ostream.close();
} catch(java.io.IOException x) {
x.printStackTrace();
msg = x.toString();
}
return msg;
}
/*
Frees up potentially large vectors/arrays in this object and child objects.
*/
public static void freeUpMem() {
Data data = getDataInstance();
int r = data.reports.size();
while(--r >= 0) {
Report report = data.getReport(r);
report.freeUpMem();
}
System.gc();
}
// Returns true if a report is currently selected.
public boolean isReportSelected() {
return currentReportIndex >= 0;
}
// Returns the report at the specified index.
public Report getReport(int index) {
try {
Report report = (Report)reports.elementAt(index);
return report;
} catch(java.lang.ArrayIndexOutOfBoundsException x) {
WLAUtil.fatalProgramError(x);
return null;
}
}
// Returns the current report, or null if none
public Report getCurrentReport() {
Report report = null;
try {
report = (Report)reports.elementAt(currentReportIndex);
} catch(java.lang.ArrayIndexOutOfBoundsException x) {
}
return report;
}
// Creates a new Report object and adds it to the vector of reports.
public Report newReport() {
Report report = new Report();
reports.addElement(report);
return report;
}
// Removes the report at the specified index from the vector of reports.
public void disposeReport(int idx) {
reports.removeElementAt(idx);
}
// Removes the given report from the vector of reports.
// Returns true if removed OK.
public boolean disposeReport(Report report) {
boolean bRemoved = reports.removeElement(report);
return bRemoved;
}
//{{DECLARE_CONTROLS
//}}
}