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(); } } }