package NET.worlds.console; import NET.worlds.core.IniFile; import NET.worlds.network.NetUpdate; import NET.worlds.network.RemoteFileConst; import NET.worlds.network.URL; import NET.worlds.scape.FrameEvent; import NET.worlds.scape.Pilot; import NET.worlds.scape.Restorer; import NET.worlds.scape.Room; import NET.worlds.scape.Saver; import NET.worlds.scape.TeleportAction; import NET.worlds.scape.TeleportStatus; import NET.worlds.scape.World; import java.awt.Container; import java.awt.Event; import java.awt.Font; import java.awt.Menu; import java.awt.MenuItem; import java.awt.PopupMenu; import java.io.File; import java.util.Hashtable; import java.util.Locale; import java.util.Vector; public class WorldsMarkPart implements FramePart, DialogReceiver, TeleportStatus, RemoteFileConst { private static final String worldsMarksFileName = "Gamma.worldsmarks"; private static final int MAX_HISTORY = 10; private static URL userMarksURL = URL.make("home:Gamma.worldsmarks"); private String name; private Menu menu; private Menu letsMenu; private MenuItem addItem = new MenuItem(Console.message("Add-WorldsMark")); private MenuItem deleteItem = new MenuItem(Console.message("Delete-WorldsMark")); private MenuItem editItem = new MenuItem(Console.message("Edit-WorldsMark")); private MenuItem locationItem = new MenuItem(Console.message("Change-Location")); private Menu historyMenu = new Menu(Console.message("Back")); private static Font font; private boolean teleporting = false; private boolean startLocationDialog = false; static Vector systemMarksNames; private static Vector> systemMarks; private static Vector userMarks; private static Vector historyItems; private static int firstUserItem; private static Hashtable renamed; private static Vector excludes; private static Vector sequence; static { Locale currentLocale = Locale.getDefault(); System.out.println("System Locale is " + currentLocale); String defLang = IniFile.gamma().getIniString("DEFAULTLANGUAGE", ""); if (!defLang.equals("")) { String code1 = null; String code2 = null; if (defLang.length() >= 2) { code1 = defLang.substring(0, 2); } if (defLang.length() >= 5) { code2 = defLang.substring(3, 5); } if (code1 != null && code2 != null) { Locale.setDefault(new Locale(code1, code2)); currentLocale = Locale.getDefault(); System.out.println("New Locale is " + currentLocale); } else { System.out.println("ERROR: DEFAULTLANGUAGE mustbe in the form xx_XX"); } } font = new Font(Console.message("MenuFont"), 0, 12); historyItems = new Vector(); historyItems.addElement(new BookmarkMenuItem(Console.message("end-of-last"), "world:restart")); renamed = new Hashtable(); renamed.put("lets", "Hang"); renamed.put("worldschat", "WorldsCenter"); renamed.put("polygram", "WorldsStore.com"); renamed.put("stadium", "NY Yankees"); renamed.put("bowie", "Bowie I"); renamed.put("texas", "Texas"); renamed.put("hanson", "Hanson I"); excludes = new Vector(); excludes.addElement("funhouse"); excludes.addElement("chaos"); sequence = new Vector(); sequence.addElement(getFirstWorld().toLowerCase()); sequence.addElement("avatargallery"); sequence.addElement("dressingroom"); } public WorldsMarkPart() { this(null); } public WorldsMarkPart(String name) { this.name = name; } public boolean isTeleporting() { return this.teleporting; } public static String getExternalName(String name) { String extName = renamed.get(name.toLowerCase()); return extName != null ? extName : name; } public Menu getMenu() { if (this.menu == null) { if (this.name != null) { this.menu = new Menu(this.name); } else { this.menu = new PopupMenu(); } this.letsMenu = new PopupMenu(); this.historyMenu.setFont(font); this.letsMenu.add(this.historyMenu); this.letsMenu.addSeparator(); if (userMarks == null) { loadMarks(); } if (systemMarks.size() != 0) { for (int i = 0; i < systemMarks.size(); i++) { String subName = systemMarksNames.elementAt(i); Menu subMenu = new Menu(getExternalName(subName)); addItems(subMenu, systemMarks.elementAt(i)); subMenu.setFont(font); this.letsMenu.add(subMenu); } } this.addMainMenuItem(this.addItem); this.addMainMenuItem(this.deleteItem); this.addMainMenuItem(this.editItem); this.addMainMenuItem(this.locationItem); this.addMainMenuSeparator(); addItems(this.menu, userMarks); addItems(this.historyMenu, historyItems); } return this.menu; } public Menu getLetsMenu() { if (this.letsMenu == null) { this.getMenu(); } return this.letsMenu; } public static PopupMenu getPackageMenu(String pname) { int p = findPackageNum(pname); if (p < 0) { return null; } else { PopupMenu m = new PopupMenu(); Vector v = systemMarks.elementAt(p); for (int i = 0; i < v.size(); i++) { MenuItem mi = v.elementAt(i); if (mi instanceof BookmarkMenuItem) { BookmarkMenuItem bmi = v.elementAt(i); String name = bmi.getLabel(); String targ = bmi.getTarget(); URL u = URL.make(TeleportAction.toURLString(targ)); String pkg = TeleportAction.getPackageNameOfWorld(u); if (pkg != null && findPackage(pkg) == null) { String dirName = URL.make("home:" + pkg).unalias(); if (!new File(dirName).isDirectory()) { name = "Download " + name; } } m.add(new BookmarkMenuItem(name, targ)); } } boolean isFirst = true; for (int ix = 0; ix < userMarks.size(); ix++) { URL u = URL.make(TeleportAction.toURLString(getBookmarkTarget(ix))); String s = TeleportAction.getPackageNameOfWorld(u); if (s != null && s.equalsIgnoreCase(pname)) { if (isFirst) { isFirst = false; m.addSeparator(); } BookmarkMenuItem item = new BookmarkMenuItem(getBookmarkName(ix), getBookmarkTarget(ix)); m.add(item); } } return m; } } private static void addItems(Menu m, Vector v) { for (int i = 0; i < v.size(); i++) { MenuItem mItem = v.elementAt(i); mItem.setFont(font); m.add(mItem); } } private void addMainMenuItem(MenuItem item) { item.setFont(font); this.menu.add(item); firstUserItem++; } private void addMainMenuSeparator() { this.menu.addSeparator(); firstUserItem++; } public static String getFirstWorld() { IniFile ini = new IniFile("InstalledWorlds"); return ini.getIniString("InstalledWorld0", ""); } public static String getFirstSystemMarkURL() { if (userMarks == null) { loadMarks(); } for (int i = 0; i < systemMarks.size(); i++) { Vector v = systemMarks.elementAt(i); if (v.size() > 0) { return v.elementAt(0).getTarget(); } } int j = 0; return j < userMarks.size() ? userMarks.elementAt(j).getTarget() : null; } private static void shortenNames(String prefix, Vector v) { int count = v.size(); int pfxlen = prefix.length(); for (int i = 0; i < count; i++) { MenuItem item = v.elementAt(i); String label = item.getLabel(); if (label.startsWith(prefix)) { item.setLabel(label.substring(pfxlen).trim()); } } } private static int findPackageNum(String pname) { Vector v = systemMarksNames; if (v != null) { int i = v.size(); while (--i >= 0) { String s = v.elementAt(i); if (s.equalsIgnoreCase(pname)) { return i; } } } return -1; } public static String findPackage(String pname) { int i = findPackageNum(pname); return i < 0 ? null : systemMarksNames.elementAt(i); } private static void loadMarks() { systemMarks = new Vector>(); systemMarksNames = new Vector(); IniFile ini = new IniFile("InstalledWorlds"); String[] sequenced = new String[sequence.size()]; for (int count = 0; count < NetUpdate.maxInstalledWorlds(); count++) { String curvalue = ini.getIniString("InstalledWorld" + count, ""); if (!curvalue.equals("")) { NetUpdate.maxInstalledWorld(count); String pkgLower = curvalue.toLowerCase(); if (!excludes.contains(pkgLower) && (!pkgLower.equals("polygram") || !World.isWorldsStoreProscribed())) { int index = sequence.indexOf(pkgLower); if (index != -1) { sequenced[index] = curvalue; } else { systemMarksNames.addElement(curvalue); } } } } for (int i = sequenced.length - 1; i >= 0; i--) { if (sequenced[i] != null) { systemMarksNames.insertElementAt(sequenced[i], 0); } } int ix = 0; while (ix < systemMarksNames.size()) { String worldName = systemMarksNames.elementAt(ix); Vector v = (Vector)loadVector(URL.make("home:" + worldName + "/" + "Gamma.worldsmarks")); if (v.size() != 0) { shortenNames(worldName, v); systemMarks.addElement(v); ix++; } else { systemMarksNames.removeElementAt(ix); } } userMarks = (Vector)loadVector(userMarksURL); } private static Vector loadVector(URL url) { Vector v = null; try { Restorer r = new Restorer(url); v = r.restoreVector(); r.done(); } catch (Exception var3) { if (v != null) { saveVector(url, (Vector)v); } else { v = new Vector(); } } return v; } private static void saveMarks() { saveVector(userMarksURL, userMarks); } private static void saveVector(URL url, Vector v) { try { Saver s = new Saver(url); s.saveVector(v); s.done(); } catch (Exception var3) { } } static void gotoBookmark(int index) { TeleportAction.teleport(getBookmarkTarget(index), null); } static int getBookmarkCount() { return userMarks.size(); } private static BookmarkMenuItem getBookmark(int index) { return userMarks.elementAt(index); } static String getBookmarkName(int index) { return getBookmark(index).getLabel(); } static String getBookmarkTarget(int index) { return getBookmark(index).getTarget(); } void addBookmark(String name, String target) { BookmarkMenuItem item = new BookmarkMenuItem(name, target); this.menu.add(item); userMarks.addElement(item); saveMarks(); } void removeBookmark(int index) { userMarks.removeElementAt(index); this.menu.remove(index + firstUserItem); saveMarks(); } void changeBookmark(int index, String name, String target) { BookmarkMenuItem item = getBookmark(index); item.setLabel(name); item.setTarget(target); saveMarks(); } @Override public void dialogDone(Object who, boolean confirmed) { if (confirmed) { if (who instanceof LocationDialog) { LocationDialog locationDialog = (LocationDialog)who; String location = locationDialog.getLocationURL(); if (location.length() != 0) { TeleportAction.teleport(location, null); } } else { BookmarkAddDialog adder = (BookmarkAddDialog)who; BookmarkEditDialog editor = adder.getEditor(); this.addBookmark(editor.getName(), editor.getTarget()); } } } private static BookmarkMenuItem getHistory(int index) { return historyItems.elementAt(index); } private static BookmarkMenuItem getItemFromTarget(String target, Vector vec) { for (int i = 0; i < vec.size(); i++) { BookmarkMenuItem item = vec.elementAt(i); if (item.getTarget().equals(target)) { return item; } } return null; } private static BookmarkMenuItem getItemFromName(String name, Vector vec) { for (int i = 0; i < vec.size(); i++) { BookmarkMenuItem item = vec.elementAt(i); if (item.getLabel().equals(name)) { return item; } } return null; } private static String getWorldName(String url) { int rindex = url.toLowerCase().lastIndexOf(".world"); int windex; return rindex == -1 || (windex = url.lastIndexOf(47, rindex)) == -1 && (windex = url.lastIndexOf(58, rindex)) == -1 ? null : url.substring(windex + 1, rindex); } public static String getCurrentPackageName() { Pilot pilot = Pilot.getActive(); if (pilot == null) { return null; } else { World world = pilot.getWorld(); if (world == null) { return null; } else { URL url = world.getSourceURL(); return url == null ? null : TeleportAction.getPackageNameOfWorld(url); } } } private static String getCurrentURLName() { String name = ""; Pilot pilot = Pilot.getActive(); if (pilot != null) { World world = pilot.getWorld(); if (world != null) { URL url = world.getSourceURL(); if (url != null) { String worldFileName = getWorldName(url.getAbsolute()); if (worldFileName != null) { name = worldFileName; } } String worldName = world.getName(); if (worldName != null) { if (!name.equals("")) { name = name + " "; } name = name + worldName; } Room room = pilot.getRoom(); if (room != null) { String roomName = room.getName(); if (roomName != null) { if (!name.equals("")) { name = name + " "; } name = name + roomName; } } } } return name; } static String getCurrentPositionName() { return getCurrentPositionName(userMarks); } static String getCurrentPositionName(Vector v) { String position = getCurrentPositionURL(false); int count = systemMarks.size(); for (int i = 0; i < count; i++) { BookmarkMenuItem item; if ((item = getItemFromTarget(position, systemMarks.elementAt(i))) != null) { return systemMarksNames.elementAt(i) + " " + item.getLabel(); } } BookmarkMenuItem item; if ((item = getItemFromTarget(position, userMarks)) == null && (item = getItemFromTarget(position, historyItems)) == null) { String defaultName = getCurrentURLName(); String name = defaultName; int number = 1; while (getItemFromName(name, v) != null) { name = defaultName + "-" + ++number; } return name; } else { return item.getLabel(); } } static String getCurrentPositionURL(boolean mustHaveChannel) { Pilot pilot = Pilot.getActive(); if (pilot == null) { return ""; } else { String newURL = pilot.getURL(); if (newURL == null) { return ""; } else { if (!mustHaveChannel) { int dynamicChannelIndex = newURL.indexOf("@"); } int channelEndIndex = newURL.indexOf(">@"); if (dynamicChannelIndex > 0 && dynamicChannelIndex < channelEndIndex) { newURL = newURL.substring(0, dynamicChannelIndex) + newURL.substring(channelEndIndex + 1); } } return newURL; } } } @Override public void teleportStatus(String err, String url) { this.teleporting = err != null && err.length() == 0; if (err == null) { url = getCurrentPositionURL(true); if (url.length() == 0) { return; } Menu menu = this.getMenu(); for (int i = 0; i < historyItems.size(); i++) { if (getHistory(i).getTarget().equals(url)) { historyItems.removeElementAt(i); this.historyMenu.remove(i); break; } } BookmarkMenuItem item = new BookmarkMenuItem(getCurrentPositionName(historyItems), url); historyItems.insertElementAt(item, 0); this.historyMenu.insert(item, 0); while (historyItems.size() > 10) { historyItems.removeElementAt(10); this.historyMenu.remove(10); } } } @Override public void activate(Console c, Container f, Console prev) { } @Override public void deactivate() { if (this.menu != null) { this.menu.removeAll(); } this.menu = null; this.letsMenu = null; firstUserItem = 0; } @Override public boolean action(Event event, Object what) { Object target = event.target; if (this.menu != null) { if (target == this.addItem) { if (Pilot.getActive().getRoom().getAllowTeleport()) { new BookmarkAddDialog(Console.getFrame(), this); } else { Console.println(Console.message("no-teleport")); } } else if (target == this.deleteItem) { new BookmarkDeleteDialog(this); } else if (target == this.editItem) { new BookmarkListDialog(this); } else if (target == this.locationItem) { this.startLocationDialog = true; } else { if (!(target instanceof BookmarkMenuItem)) { return false; } TeleportAction.teleport(((BookmarkMenuItem)target).getTarget(), null); } return true; } else { return false; } } @Override public boolean handle(FrameEvent f) { if (this.startLocationDialog) { this.startLocationDialog = false; String url = ""; Pilot pilot = Pilot.getActive(); if (pilot != null) { url = pilot.getURL(); } new LocationDialog(Console.getFrame(), this, Console.message("Change-Location"), url); } return true; } }