-
Notifications
You must be signed in to change notification settings - Fork 4
/
JapeFrame.java
439 lines (378 loc) · 10.4 KB
/
JapeFrame.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Written 1999 by Douglas Greiman.
This software may be used and distributed according to the terms
of the GNU Public License, incorporated herein by reference.
*/
package duggelz.jape;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JapeFrame extends Frame implements DataChangeListener
{
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 450;
// Major GUI elements
private GridBagLayout layout = new GridBagLayout();
private List actorList;
private StatPanel statPanel;
private ItemPanel itemPanel;
// Menu
private MenuBar menuBar;
private Menu fileMenu;
private MenuItem openItem;
private MenuItem saveItem;
private MenuItem closeItem;
private MenuItem quitItem;
private Menu helpMenu;
private MenuItem aboutItem;
// The save game engine
private SaveGame saveGame;
private boolean saveGameModified;
private Actor currentActor;
private Mercenary currentMerc;
private String currentDir = System.getProperty("user.dir");
public JapeFrame()
{
super("JAPE");
// Menu
this.createMenuBar();
// Create body panel
Panel body = new Panel();
body.setLayout(this.layout);
this.add("Center", body);
// Create actor list
this.actorList = new List();
this.actorList.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
doSelectActor();
}});
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = c1.BOTH;
c1.gridx = 0;
c1.gridy = 0;
c1.weighty = 1;
c1.weightx = 0;
body.add(this.actorList, c1);
// Create stat panel
this.statPanel = new StatPanel(this);
this.statPanel.addDataChangeListener(this);
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = c2.BOTH;
c2.anchor = c2.NORTHWEST;
c2.gridx = 1;
c2.gridy = 0;
c2.weighty = 1;
c2.weightx = 1;
body.add(this.statPanel, c2);
// Create item panel
this.itemPanel = new ItemPanel(this);
this.itemPanel.addDataChangeListener(this);
GridBagConstraints c3 = new GridBagConstraints();
c3.fill = c3.BOTH;
c3.anchor = c3.NORTHWEST;
c3.gridx = 2;
c3.gridy = 0;
c3.weighty = 1;
c3.weightx = 1;
body.add(this.itemPanel, c3);
// Handle window events
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
doQuit();
}});
// Size the frame
this.addNotify();
// Insets frameInsets = this.getInsets();
// int width = frameInsets.left + frameInsets.right + DEFAULT_WIDTH;
// int height = frameInsets.top + frameInsets.bottom + DEFAULT_HEIGHT;
// this.setSize(width, height);
// this.setLocation(50,50);
// Init the panels
this.populateActorList();
// Show
this.pack();
this.repaint();
}
private void createMenuBar() {
// Create menu bar
this.menuBar = new MenuBar();
// File menu
this.fileMenu = new Menu("File");
this.menuBar.add(this.fileMenu);
this.openItem = new MenuItem("Open...");
this.openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doOpen();
}});
this.fileMenu.add(this.openItem);
this.saveItem = new MenuItem("Save");
this.saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doSave();
}});
this.fileMenu.add(this.saveItem);
this.saveItem.setEnabled(false);
this.closeItem = new MenuItem("Close");
this.closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doClose();
}});
this.fileMenu.add(this.closeItem);
this.closeItem.setEnabled(false);
this.fileMenu.addSeparator();
this.quitItem = new MenuItem("Quit");
this.quitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doQuit();
}});
this.fileMenu.add(this.quitItem);
// About menu
this.helpMenu = new Menu("Help");
this.menuBar.add(this.helpMenu);
this.menuBar.setHelpMenu(this.helpMenu);
this.aboutItem = new MenuItem("About...");
this.aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doAbout();
}});
this.helpMenu.add(this.aboutItem);
// Attach menubar to frame
this.setMenuBar(this.menuBar);
}
public boolean doAbout() {
new JapeAbout(this).show();
return true;
}
public boolean doClose() {
// Do we currently have a save game open?
if ( (this.saveGame != null) && (this.saveGameModified) )
{
// Prompt user for save
String shortName = new File(this.saveGame.filename).getName();
int option = OptionDialog.showConfirmDialog(
this,
"The file " + shortName + " has been modifed.\n" +
"Do you want to save the changes?",
this.getTitle(),
OptionDialog.YES_NO_CANCEL_OPTION,
OptionDialog.WARNING_MESSAGE);
if ( option == OptionDialog.CANCEL_OPTION ||
option == OptionDialog.CLOSED_OPTION )
{
return false;
} else if ( option == OptionDialog.YES_OPTION )
{
boolean success = doSave();
if ( success == false ) {
return false;
}
}
}
// Disable relevant menu items
this.saveItem.setEnabled(false);
this.closeItem.setEnabled(false);
// Close current save
this.saveGame = null;
this.saveGameModified = false;
this.populateActorList();
return true;
}
public boolean doOpen() {
// Close any existing save
boolean success = this.doClose();
if ( ! success ) {
return false;
}
// Create open file dialog
FileDialog fileDialog = new FileDialog(this, "Open", FileDialog.LOAD);
if( this.currentDir != null ) {
fileDialog.setDirectory(this.currentDir);
}
// Show dialog
fileDialog.setFile("*.sav");
fileDialog.show();
// What did the user choose?
String directory = fileDialog.getDirectory();
String name = fileDialog.getFile();
if( name == null ) {
return false;
}
this.currentDir = directory;
String filename = new File(directory, name).toString();
// Open the selected file
SaveGame saveGame = new SaveGame();
try {
saveGame.load(filename);
} catch( EOFException e )
{
// Display error message
OptionDialog.showMessageDialog(
this,
"Unable to load file.\n" +
"Unexpected end of file reached.",
this.getTitle(),
OptionDialog.ERROR_MESSAGE);
return false;
} catch( FileNotFoundException e )
{
// Display error message
OptionDialog.showMessageDialog(
this,
"Unable to load file.\n" +
"The system cannot find the file specified.",
this.getTitle(),
OptionDialog.ERROR_MESSAGE);
return false;
} catch( IOException e )
{
// Display error message
String errorMessage = e.getMessage();
if( e.getMessage() == null )
{
errorMessage = "An unknown error occurred.";
}
OptionDialog.showMessageDialog(
this,
"Unable to load file.\n" +
errorMessage,
this.getTitle(),
OptionDialog.ERROR_MESSAGE);
return false;
}
catch( SaveGame.FormatException e )
{
// Display error message
//e.printStackTrace(System.err);
OptionDialog.showMessageDialog(
this,
"Unable to load file.\n" +
"The save game format was not recognized.\n" +
e.getMessage(),
this.getTitle(),
OptionDialog.ERROR_MESSAGE);
return false;
}
// Enable relevant menu items
this.saveItem.setEnabled(true);
this.closeItem.setEnabled(true);
// Display the file
this.saveGame = saveGame;
// Select the first actor
this.populateActorList();
this.actorList.select(0);
this.doSelectActor();
this.saveGameModified = false;
return true;
}
public boolean doSave() {
// Do we have a save game open
if ( this.saveGame == null )
{
return true;
}
// Save the game
try {
this.saveGame.save();
} catch( IOException e )
{
// Display error message
String errorMessage = e.getMessage();
if( e.getMessage() == null )
{
errorMessage = "An unknown error occurred.";
}
OptionDialog.showMessageDialog(
this,
"Unable to save file.\n" +
errorMessage,
this.getTitle(),
OptionDialog.ERROR_MESSAGE);
return false;
}
// Reset modified flag
this.saveGameModified = false;
this.statPanel.setModified(false);
this.itemPanel.setModified(false);
return true;
}
public boolean doQuit()
{
// Close any existing save
boolean success = this.doClose();
if ( ! success ) {
return false;
}
// Close window
this.setVisible(false);
this.dispose();
System.exit(0);
return true;
}
private boolean doSelectActor()
{
// Which name did the user select
int idx = this.actorList.getSelectedIndex();
// Lookup that actor in the save
Actor actor = null;
Mercenary merc = null;
if ( idx != -1 )
{
// Get actor and/or merc
actor = this.saveGame.getActor(idx);
String nickname = actor.get("Nickname");
merc = this.saveGame.getMercByNick(nickname);
}
// Do it
this.doSetActor(actor, merc);
return true;
}
private void doSetActor(Actor actor, Mercenary merc)
{
this.currentActor = actor;
this.currentMerc = merc;
// Redisplay the gui
this.statPanel.setActor(this.currentActor, this.currentMerc);
this.itemPanel.setActor(this.currentActor, this.currentMerc);
}
private void populateActorList()
{
// Clear all entries
this.actorList.removeAll();
this.doSetActor(null, null);
// Game loaded?
if ( this.saveGame == null ) {
return;
}
// Iterator over actors in the save game
for( int idx = 0; idx < this.saveGame.actorCount; ++idx )
{
// Get actor
Actor actor = this.saveGame.getActor(idx);
String nickname = actor.get("Nickname");
// Is there an associated merc record?
Mercenary merc = this.saveGame.getMercByNick(nickname);
if ( merc != null )
{
nickname = "*" + nickname;
}
// Add actor nickname to list
this.actorList.add(nickname);
}
}
public void dataChanged(DataChangeEvent event) {
this.saveGameModified = true;
}
// Class methods
public static void main(String[] args)
{
// Create frame
JapeFrame japeFrame = new JapeFrame();
// Make frame visible
japeFrame.show();
// Open a save
//japeFrame.doOpen();
}
}
// Local Variables:
// tab-width: 8
// End: