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
|
package NET.worlds.scape;
import NET.worlds.console.ConfirmDialog;
import NET.worlds.console.Console;
import NET.worlds.console.DialogReceiver;
import NET.worlds.console.OkCancelDialog;
import NET.worlds.console.PolledDialog;
import NET.worlds.core.Sort;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.Point;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
class MusicManagerDialog extends PolledDialog implements DialogReceiver {
private MusicManager manager;
private String lastRoom = "";
private Label curRoom = new Label();
private Label curMusic = new Label();
private Button assignCurButton = new Button(Console.message("Assign-Current"));
private Checkbox autoAssign = new Checkbox(Console.message("Auto-assign"));
private CheckboxGroup group = new CheckboxGroup();
private Checkbox musicBox = new Checkbox(Console.message("Music"), this.group, true);
private Checkbox aRoomsBox = new Checkbox(Console.message("Assigned-rooms"), this.group, false);
private Checkbox uRoomsBox = new Checkbox(Console.message("Unassigned-rooms"), this.group, false);
private List list = new List(10);
private Button editButton = new Button(Console.message("Edit"));
private Button addButton = new Button(Console.message("Add"));
private Button delButton = new Button(Console.message("Delete"));
private Button assignButton = new Button(Console.message("Assign"));
private Button saveButton = new Button(Console.message("Save-Changes"));
private Button okButton = new Button(Console.message("OK"));
private Button cancelButton = new Button(Console.message("Cancel"));
private PolledDialog subDialog;
private Vector allRooms;
private boolean madeChanges;
private boolean confirmingState;
static Point lastWindowLocation = null;
private static Font font = new Font(Console.message("MenuFont"), 0, 12);
private static Font bfont = new Font(Console.message("ButtonFont"), 0, 12);
private static final String title = Console.message("Music-Manager");
public MusicManagerDialog(MusicManager manager) {
super(Console.getFrame(), manager, title + manager.getName(), false);
this.setAlignment(2);
this.manager = manager;
this.ready();
}
public MusicManager getManager() {
return this.manager;
}
public String getCurRoom() {
return this.curRoom.getText();
}
public Vector getAllRooms() {
return this.allRooms;
}
@Override
protected void build() {
GridBagLayout gbag = new GridBagLayout();
this.setLayout(gbag);
GridBagConstraints c = new GridBagConstraints();
Panel p = new Panel(new BorderLayout());
this.musicBox.setFont(font);
this.aRoomsBox.setFont(font);
this.uRoomsBox.setFont(font);
p.add("West", this.musicBox);
p.add("Center", this.aRoomsBox);
p.add("East", this.uRoomsBox);
c.weightx = 0.0;
c.gridwidth = 0;
this.add(gbag, p, c);
c.fill = 1;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = 2;
c.gridheight = 6;
this.list.setFont(font);
this.add(gbag, this.list, c);
c.fill = 2;
c.weightx = 0.0;
c.weighty = 0.0;
c.gridwidth = 0;
c.gridheight = 1;
this.assignButton.setFont(bfont);
this.editButton.setFont(bfont);
this.addButton.setFont(bfont);
this.delButton.setFont(bfont);
this.add(gbag, this.assignButton, c);
this.add(gbag, this.editButton, c);
this.add(gbag, this.addButton, c);
this.add(gbag, this.delButton, c);
for (int i = 0; i < 2; i++) {
this.add(gbag, new Label(""), c);
}
c.gridwidth = 1;
c.fill = 0;
Label cRoom = new Label(Console.message("Current-room"));
cRoom.setFont(font);
this.add(gbag, cRoom, c);
c.gridwidth = 0;
c.weightx = 1.0;
c.fill = 2;
this.curRoom.setFont(font);
this.add(gbag, this.curRoom, c);
c.gridwidth = 1;
c.weightx = 0.0;
Label aMusic = new Label(Console.message("Assigned-music"));
aMusic.setFont(font);
this.add(gbag, aMusic, c);
c.fill = 0;
c.gridwidth = 0;
c.fill = 2;
c.weightx = 1.0;
this.curMusic.setFont(font);
this.add(gbag, this.curMusic, c);
c.weightx = 0.0;
c.weighty = 0.0;
c.gridwidth = 1;
c.fill = 0;
this.assignCurButton.setFont(bfont);
this.autoAssign.setFont(bfont);
this.add(gbag, this.assignCurButton, c);
c.gridwidth = 0;
this.add(gbag, this.autoAssign, c);
p = new Panel();
p.setFont(bfont);
p.add(this.saveButton);
p.add(this.okButton);
p.add(this.cancelButton);
c.fill = 2;
this.add(gbag, p, c);
this.rebuildList(false);
this.buildAllRoomsList();
}
@Override
protected boolean done(boolean confirmed) {
lastWindowLocation = this.getLocation();
return super.done(confirmed);
}
@Override
protected void initialSize(int width, int height) {
if (lastWindowLocation == null) {
super.initialSize(width, height);
} else {
this.setLocation(lastWindowLocation);
this.setSize(width, height);
}
}
private void buildAllRoomsList() {
this.allRooms = new Vector();
Pilot pilot = Pilot.getActive();
if (pilot != null) {
World world = pilot.getWorld();
if (world != null) {
Enumeration rooms = world.getRooms();
while (rooms.hasMoreElements()) {
this.allRooms.addElement(((Room)rooms.nextElement()).getName());
}
}
}
}
private void rebuildList(boolean changed) {
boolean enable = !this.uRoomsBox.getState();
this.assignButton.setEnabled(!this.musicBox.getState());
this.editButton.setEnabled(enable);
this.addButton.setEnabled(enable);
this.delButton.setEnabled(enable);
this.list.removeAll();
if (this.musicBox.getState()) {
Sort.sortInto(this.list, this.manager.getMusic());
} else if (this.aRoomsBox.getState()) {
String[] content = Sort.sortKeys(this.manager.getRooms());
for (int i = 0; i < content.length; i++) {
String name = content[i];
this.list.add(name + " (" + this.manager.getRoom(name).getMusicName() + ")");
}
} else {
Enumeration e = this.allRooms.elements();
Hashtable aRooms = this.manager.getRooms();
Vector v = new Vector();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
if (!aRooms.containsKey(name)) {
v.addElement(name);
}
}
Sort.sortInto(this.list, v);
}
if (changed) {
this.madeChanges = true;
this.manager.maybeChangedMusic();
}
}
@Override
protected synchronized void activeCallback() {
Pilot pilot = Pilot.getActive();
if (pilot != null) {
World world = pilot.getWorld();
if (world != null && world.getSourceURL().equals(this.manager.getWorld().getSourceURL())) {
Room room = pilot.getRoom();
if (room != null) {
String name = room.getName();
if (!name.equals(this.lastRoom)) {
this.lastRoom = name;
this.curRoom.setText(name);
MusicRoom mr = this.manager.getRoom(name);
if (mr != null) {
this.curMusic.setText(mr.getMusicName());
} else {
this.curMusic.setText("");
if (this.autoAssign.getState() && this.subDialog == null) {
this.subDialog = new EditRoomDialog(this, null, name);
}
}
}
return;
}
}
}
this.curRoom.setText("");
this.curMusic.setText("");
}
@Override
public synchronized boolean action(java.awt.Event event, Object what) {
Object target = event.target;
String selected = this.list.getSelectedItem();
boolean isMusic = this.musicBox.getState();
boolean haveMusic = this.manager.getMusic().size() != 0;
if (isMusic && target == this.editButton && selected != null) {
this.subDialog = new EditMusicDialog(this, this.manager.getMusic(selected));
} else if (isMusic && target == this.addButton) {
this.subDialog = new EditMusicDialog(this, null);
} else if (isMusic && target == this.delButton && selected != null) {
if (!this.isMusicInUse(selected)) {
this.manager.getMusic().remove(selected);
if (this.manager.getMusic().size() == 0) {
this.autoAssign.setState(false);
}
this.rebuildList(true);
} else {
this.cantChangeMusic();
}
} else if (!isMusic && target == this.editButton && selected != null && haveMusic) {
selected = selected.substring(0, selected.lastIndexOf(40) - 1);
this.subDialog = new EditRoomDialog(this, this.manager.getRoom(selected), selected);
} else if (!isMusic && target == this.addButton) {
if (haveMusic) {
this.subDialog = new EditRoomDialog(this, null, this.getCurRoom());
} else {
this.needMusic();
}
} else if (!isMusic && target == this.delButton && selected != null) {
selected = selected.substring(0, selected.lastIndexOf(40) - 1);
this.manager.getRooms().remove(selected);
this.rebuildList(true);
} else if (target == this.saveButton) {
this.manager.save();
this.madeChanges = false;
} else if (target == this.okButton) {
if (this.madeChanges) {
this.confirmingState = true;
new ConfirmDialog(this, this, Console.message("Save-Changes2"), Console.message("sure-save"));
} else {
this.done(false);
}
} else if (target == this.cancelButton) {
if (this.madeChanges) {
this.confirmingState = false;
new ConfirmDialog(this, this, Console.message("Abandon-Changes"), Console.message("sure-cancel"));
} else {
this.done(false);
}
} else if (target != this.musicBox && target != this.aRoomsBox && target != this.uRoomsBox) {
if ((target != this.assignButton || selected == null) && target != this.assignCurButton) {
if (target != this.autoAssign || !this.autoAssign.getState()) {
return false;
}
if (haveMusic) {
if (this.manager.getRoom(this.lastRoom) == null) {
this.subDialog = new EditRoomDialog(this, null, this.lastRoom);
}
} else {
this.autoAssign.setState(false);
this.needMusic();
}
} else if (haveMusic) {
String selection = target == this.assignButton ? selected : this.lastRoom;
this.subDialog = new EditRoomDialog(this, this.manager.getRoom(selection), selection);
} else {
this.needMusic();
}
} else {
this.rebuildList(false);
}
return true;
}
@Override
public boolean handleEvent(java.awt.Event event) {
if (event.key != 1004 && event.key != 1005 && event.key != 1006 && event.key != 1007) {
return super.handleEvent(event);
} else {
Console.getFrame().requestFocus();
return true;
}
}
private void needMusic() {
this.subDialog = new OkCancelDialog(this, this, Console.message("Need-Music"), null, Console.message("OK"), Console.message("at-least-one"), true);
}
private boolean isMusicInUse(String name) {
Enumeration e = this.manager.getRooms().elements();
while (e.hasMoreElements()) {
if (name.equals(((MusicRoom)e.nextElement()).getMusicName())) {
return true;
}
}
return false;
}
private void cantChangeMusic() {
this.subDialog = new OkCancelDialog(this, this, Console.message("Cant-Change"), null, Console.message("OK"), Console.message("may-not-delete"), true);
}
@Override
public synchronized void dialogDone(Object who, boolean confirmed) {
boolean showCantChange = false;
if (confirmed) {
if (who instanceof EditMusicDialog) {
EditMusicDialog em = (EditMusicDialog)who;
MusicTrack music = em.getMusicTrack();
String oldName = em.getOldName();
if (oldName != null && !oldName.equals(music.getName()) && this.isMusicInUse(oldName)) {
music.setName(oldName);
showCantChange = true;
}
this.manager.getMusic().put(music.getName(), music);
this.rebuildList(true);
} else if (who instanceof EditRoomDialog) {
EditRoomDialog er = (EditRoomDialog)who;
MusicRoom room = er.getMusicRoom();
this.manager.getRooms().put(room.getRoomName(), room);
this.rebuildList(true);
} else if (who instanceof ConfirmDialog) {
this.done(this.confirmingState);
}
if (this.lastRoom != null) {
MusicRoom mr = this.manager.getRoom(this.lastRoom);
if (mr != null) {
this.curMusic.setText(mr.getMusicName());
}
}
}
this.subDialog = null;
if (showCantChange) {
this.cantChangeMusic();
}
}
}
|