Add private buttons for loading and saving.
Just like any other buttons, you'll need to make two buttons, one for bringing up the load dialog, and one for the save dialog. You'll also have to add this as an actionListener for those buttons, just like other buttons you add. More on this later.
private Button btnSave, btnLoad;
Create your WebsaverConnection & Dialogs
In your applet's init() function, initialize your WebsaverConnection and dialog boxes like so:
// make all the websaver stuff
String websaver = getParameter("websaver");
if (websaver != null) {
// make the save button
btnSave = new Button("Save");
btnSave.addActionListener(this);
buttonPanel.add(btnSave);
// make the load button
btnLoad = new Button("Load");
btnLoad.addActionListener(this);
buttonPanel.add(btnLoad);
wsConn = new WebsaverConnection(websaver, "J2_applet");
dlgLoad = new WebsaverDialogLoad(wsConn, true);
dlgSave = new WebsaverDialogSave(wsConn, true);
dlgLoad.addWebsaverDialogListener(this);
dlgSave.addWebsaverDialogListener(this);
}
Make your load & save boxes show the dialogs
In your actionPerformed(), add the bold code:
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if ((Button)source == btnSave) {
dlgSave.setFilename(lastFilename);
dlgSave.showDialog(this);
}
else if ((Button)source == btnLoad) {
dlgLoad.showDialog(this);
}
}
Do the loading and saving!
Now it's time to do the actual loading and saving... remember those methods websaverFileLoad and websaverFileSave you made earlier? It's time to fill those in. The parts in bold below you have to do. The rest you can (and should) copy verbatim. Note that you can do anything you want with the Input and Output streams, but object serialization is the fastest and easiest way to save things.
public void websaverFileSave (WebsaverDialogEvent evt)
{
String filename = evt.getFilename();
lastFilename = filename;
try {
WebsaverOutputStream wos = new WebsaverOutputStream(wsConn,
filename,
evt.getPassword(),
true);
ObjectOutputStream oos = new ObjectOutputStream(wos);
// YOUR JOB: Save all your objects, like this:
// oos.writeObject(bgColor);
// oos.writeObject(fgColor);
// oos.writeObject(activeObjects);
oos.close();
dlgSave.hideDialog();
} catch (Exception e) {
System.out.println(e.toString());
DialogMessageBox dia = new DialogMessageBox(dlgLoad, "Error", e.toString());
}
}
public void websaverFileLoad (WebsaverDialogEvent evt)
{
String filename = evt.getFilename();
lastFilename = filename;
try {
WebsaverInputStream wis = new WebsaverInputStream(wsConn,
filename,
evt.getPassword());
ObjectInputStream ois = new ObjectInputStream(wis);
// YOUR JOB: Load in all your objects, like this:
// bgColor = (Color) ois.readObject();
// fgColor = (Color) ois.readObject();
// activeObjects = (Vector) ois.readObject();
ois.close();
dlgLoad.hideDialog();
} catch (Exception e) {
System.out.println(e.toString());
DialogMessageBox dia = new DialogMessageBox(dlgLoad, "Error", e.toString());
}
}
That's it!
That's it... you're done. Some things to note:
- Applets can only open socket connections to the host they were downloaded from, so if you're testing this from home, unless you relax your applet viewer's security settings, you will not be able to load/save from cubist. You have to upload all your .class files and your .html, and load your applet over the web. The appletviewer shipped with JDK 1.1.8 lets you turn off this restriction, but I don't know if J++ lets you or not.
- All the Websaver*.java classes are pretty well-documented. If not, bug me and I'll comment them better.
Need Help?
Post a message to the class mailing list at cse341@cs.washington.edu and if somebody doesn't help you by the time I check my mail, I'll help you out.