package NET.worlds.console; import NET.worlds.network.URL; import NET.worlds.scape.BGLoaded; import NET.worlds.scape.BackgroundLoader; import NET.worlds.scape.NoSuchPropertyException; import NET.worlds.scape.Property; import NET.worlds.scape.Restorer; import NET.worlds.scape.Room; import NET.worlds.scape.Saver; import NET.worlds.scape.SuperRoot; import NET.worlds.scape.TooNewException; import NET.worlds.scape.URLPropertyEditor; import java.io.IOException; import java.text.MessageFormat; import java.util.Enumeration; import java.util.Hashtable; public class Cursor extends SuperRoot implements BGLoaded { private URL url = URL.make("system:DEFAULT_CURSOR"); private int hCursor; private static Cursor active; private static Hashtable sysCursors = new Hashtable(); private static int defaultCursor = retrieveSystemCursor(addCursor("DEFAULT_CURSOR", "IDC_ARROW")); private static Object classCookie; static { assert defaultCursor != 0; addCursor("CROSSHAIR_CURSOR", "IDC_CROSS"); addCursor("TEXT_CURSOR", "IDC_IBEAM"); addCursor("WAIT_CURSOR", "IDC_WAIT"); addCursor("NE_RESIZE_CURSOR", "IDC_SIZENESW"); addCursor("SW_RESIZE_CURSOR", "IDC_SIZENESW"); addCursor("NW_RESIZE_CURSOR", "IDC_SIZENWSE"); addCursor("SE_RESIZE_CURSOR", "IDC_SIZENWSE"); addCursor("N_RESIZE_CURSOR", "IDC_SIZENS"); addCursor("S_RESIZE_CURSOR", "IDC_SIZENS"); addCursor("W_RESIZE_CURSOR", "IDC_SIZEWE"); addCursor("E_RESIZE_CURSOR", "IDC_SIZEWE"); addCursor("HAND_CURSOR", "IDC_UPARROW"); addCursor("MOVE_CURSOR", "IDC_SIZEALL"); addCursor("CANNOT_CURSOR", "IDC_NO"); classCookie = new Object(); } private static URL addCursor(String javaName, String win32Name) { URL url = URL.make("system:" + javaName); sysCursors.put(url, win32Name); return url; } public Cursor() { } public Cursor(URL url) { this.setURL(url); } public void setURL(URL url) { this.url = url; if (!url.unalias().startsWith("system:")) { BackgroundLoader.get(this, url); } else { this.activate(); } } public static Cursor getActive() { return active; } public void activate() { SuperRoot owner = this.getOwner(); if (owner != null && owner == Console.getActive()) { int sysCurs = retrieveSystemCursor(this.url); if (sysCurs != 0) { Window.setCursor(sysCurs); this.maybeDestroyCursor(); this.hCursor = sysCurs; } else if (this.hCursor != 0) { Window.setCursor(this.hCursor); } active = this; } } public void deactivate() { if (active == this) { active = null; } } public URL getURL() { return this.url; } public static Enumeration getSysCursorURLs() { return sysCursors.keys(); } public static native int getSystemCursorWidth(); public static native int getSystemCursorHeight(); public static native int getSystemCursorDepth(); private static native int loadCursor(String var0); private static native int loadSystemCursor(String var0); private static native void destroyCursor(int var0); private static int retrieveSystemCursor(URL url) { int handle = 0; Object c = sysCursors.get(url); if (c != null) { if (c instanceof String) { handle = loadSystemCursor((String)c); sysCursors.put(url, new Integer(handle)); } else { handle = (Integer)c; } } return handle; } private void maybeDestroyCursor() { if (this.hCursor != 0) { if (!sysCursors.contains(new Integer(this.hCursor))) { destroyCursor(this.hCursor); } this.hCursor = 0; } } @Override public Object asyncBackgroundLoad(String localName, URL remoteName) { this.deactivate(); this.maybeDestroyCursor(); if (localName == null) { return null; } else { if ((this.hCursor = loadCursor(localName)) != 0) { this.activate(); } return null; } } @Override public boolean syncBackgroundLoad(Object obj, URL remoteURL) { if (this.hCursor == 0) { Object[] arguments = new Object[]{new String("" + this.url)}; Console.println(MessageFormat.format(Console.message("Load-cursor"), arguments)); } return false; } @Override public Room getBackgroundLoadRoom() { SuperRoot owner = this.getOwner(); return owner != null && owner instanceof Console ? ((Console)owner).getPilot().getRoom() : null; } @Override public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException { Object ret = null; switch (index - offset) { case 0: if (mode == 0) { ret = URLPropertyEditor.make(new Property(this, index, "File"), "cur;ani", getSysCursorURLs()); } else if (mode == 1) { ret = this.getURL(); } else if (mode == 2) { this.setURL((URL)value); } break; default: ret = super.properties(index, offset + 1, mode, value); } return ret; } @Override public void saveState(Saver s) throws IOException { s.saveVersion(1, classCookie); super.saveState(s); URL.save(s, this.url); } @Override public void restoreState(Restorer r) throws IOException, TooNewException { switch (r.restoreVersion(classCookie)) { case 0: r.restoreMaybeNull(); String s = r.restoreString(); if (!s.endsWith(".cur") && !s.endsWith(".ani")) { this.setURL(URL.make("system:" + this.url)); } else { this.setURL(URL.restore(r, s, null)); } break; case 1: super.restoreState(r); this.setURL(URL.restore(r)); break; default: throw new TooNewException(); } } }