package NET.worlds.scape; import NET.worlds.console.Console; import NET.worlds.console.Cursor; import NET.worlds.console.Main; import NET.worlds.console.MainCallback; import NET.worlds.core.IniFile; import NET.worlds.network.URL; import java.awt.Color; import java.awt.Component; import java.awt.Point; import java.io.File; import java.util.Enumeration; import java.util.Vector; public class LibrariesTile extends TabbedPanel implements LibEventHandler, Properties, MainCallback, LibraryDropTarget { private static final int NOTHING = 0; private static final int ADD_LIBRARY = 1; private static final int ADD_ELEMENT = 2; private static URL libURL = URL.make("home:libraries/"); private static String libSubdir; private Vector libraries = new Vector(); private int queue = 0; private boolean iconsVisible = false; private Cursor dragCursor; private Cursor cantCursor; private LibraryEntry leftClickedOn; static { String s = libURL.unalias(); libSubdir = s.substring(0, s.length() - 1); } public static String getLibSubdir() { return libSubdir; } public LibrariesTile() { this.setBackground(Color.lightGray); Vector libNames = new FileList(libSubdir, "library").getList(); Enumeration e = libNames.elements(); while (e.hasMoreElements()) { Library lib = Library.load(URL.make(libURL, (String)e.nextElement())); if (lib != null) { this.addLibrary(lib); } } if (this.libraries.size() != 0) { this.select(0); } Main.register(this); } public synchronized void addLibrary() { if (this.queue == 0) { this.queue = 1; } } public synchronized void addElement() { if (this.queue == 0) { this.queue = 2; } } @Override public synchronized void mainCallback() { switch (this.queue) { case 1: this.syncAddLibrary(new Library()); break; case 2: this.syncAddElement(new LibraryEntry()); } this.queue = 0; } private boolean isUniqueLibraryURL(URL name) { if (name == null) { return false; } else { Enumeration e = this.libraries.elements(); while (e.hasMoreElements()) { Library lib = (Library)e.nextElement(); if (lib.getSourceURL().equals(name)) { return false; } } return true; } } private boolean isUniqueLibraryName(String name) { if (name == null) { return false; } else { Enumeration e = this.libraries.elements(); while (e.hasMoreElements()) { Library lib = (Library)e.nextElement(); if (lib.getName().equals(name)) { return false; } } return true; } } private void syncAddLibrary(Library lib) { boolean newEntry = lib.getNameMaybeNull() == null; if (!this.isUniqueLibraryURL(lib.getSourceURL())) { int i = 1; URL url; do { url = URL.make(libURL, "lib" + i++ + ".library"); } while (!this.isUniqueLibraryURL(url)); lib.setSourceURL(url); } if (this.libraries.size() == 0) { File f = new File(libSubdir); f.mkdir(); } if (!this.isUniqueLibraryName(lib.getNameMaybeNull())) { int num = 1; String libName; do { libName = "Category" + num++; } while (!this.isUniqueLibraryName(libName)); lib.setName(libName); } if (this.saveAllowed()) { lib.save(); } else { Console.println(Console.message("AllowChangeLibrary")); } this.addLibrary(lib); if (newEntry) { this.select(this.libraries.indexOf(lib)); } } private void syncDeleteLibrary(Library lib) { if (this.saveAllowed()) { lib.delete(); } else { Console.println(Console.message("AllowChangeLibrary")); } int index = this.libraries.indexOf(lib); this.libraries.removeElementAt(index); this.removeItem(index); } private void syncAddElement(LibraryEntry ent) { if (this.libraries.size() != 0) { int selected = this.selected(); Library lib = (Library)this.libraries.elementAt(selected); lib.add(ent); } } private void addLibrary(Library lib) { int count = this.libraries.size(); String name = lib.getName(); int i = 0; while (i < count && name.compareTo(((Library)this.libraries.elementAt(i)).getName()) >= 0) { i++; } this.libraries.insertElementAt(lib, i); this.insertItem(i, lib.getName(), new ScrollingImagePanel(this, lib.getContents(), this.iconsVisible)); lib.setEventHandler(this); lib.setOwningDialog(this); } public boolean isIconsVisible() { return this.iconsVisible; } public void setIconsVisible(boolean showIcons) { this.iconsVisible = showIcons; int count = this.libraries.size(); for (int i = 0; i < count; i++) { ScrollingImagePanel c = (ScrollingImagePanel)this.getComponent(i); c.setIconsVisible(showIcons); } } @Override public void libraryChanged(Library lib) { Library selected = (Library)this.libraries.elementAt(this.selected()); int index = this.libraries.indexOf(lib); this.libraries.removeElementAt(index); this.removeItem(index); this.addLibrary(lib); this.select(this.libraries.indexOf(selected)); if (this.saveAllowed()) { lib.save(); } else { Console.println(Console.message("AllowChangeLibrary")); } } private boolean saveAllowed() { return IniFile.gamma().getIniInt("AllowChangeLibrary", 0) == 1; } private Library getLibrary(ScrollingImagePanel panel) { int count = this.libraries.size(); for (int i = 0; i < count; i++) { if (this.getComponent(i) == panel) { return (Library)this.libraries.elementAt(i); } } return null; } private LibraryEntry getLibraryEntry(Component comp, Point location) { if (comp instanceof ScrollingImagePanel) { ScrollingImagePanel panel = (ScrollingImagePanel)comp; Library lib = this.getLibrary(panel); if (lib != null) { int item = panel.itemAt(location); if (item >= 0) { return lib.getEntry(item); } } } return null; } private boolean maybeMoveEntry(LibraryEntry src, Component comp, Point loc) { LibraryEntry dst = this.getLibraryEntry(comp, loc); if (dst != null && dst != src) { Library srcOwner = (Library)src.getOwner(); Library dstOwner = (Library)dst.getOwner(); assert srcOwner == dstOwner; srcOwner.move(src, dst); return true; } else { if (comp == this) { Library newLib = (Library)this.libraries.elementAt(this.itemAt(loc)); Library oldLib = (Library)src.getOwner(); if (newLib != oldLib) { oldLib.delete(src); newLib.add(src); return true; } } return false; } } @Override public void clickEvent(Component who, Point location, int flags) { if ((flags & 1) != 0) { if ((flags & 4) != 0 && who == this) { Console.getFrame().getEditTile().viewProperties(this.libraries.elementAt(this.itemAt(location))); } else { LibraryEntry ent; if ((ent = this.getLibraryEntry(who, location)) != null) { this.leftClickedOn = null; if (ent != null) { if ((flags & 4) != 0) { Console.getFrame().getEditTile().viewProperties(ent); } else { this.leftClickedOn = ent; Console console = Console.getActive(); if (this.dragCursor == null) { this.dragCursor = new Cursor(URL.make("home:drag.cur")); } else { this.dragCursor.detach(); console.addCursor(this.dragCursor); this.dragCursor.activate(); } } } } } } else if ((flags & 2) != 0) { Cursor active = Cursor.getActive(); if (active != null && (active == this.dragCursor || active == this.cantCursor)) { Console.getActive().getCursor().activate(); } if (this.leftClickedOn != null) { if (!this.maybeMoveEntry(this.leftClickedOn, who, location)) { URL url = this.leftClickedOn.getContentURL(); String propName = this.leftClickedOn.getPropertyName(true); if (url != null) { Console.getFrame().getEditTile().libraryDrop(url, propName, who, location); } } this.leftClickedOn = null; } } else { Cursor activex = Cursor.getActive(); boolean isDropTarget = who instanceof LibraryDropTarget; if (activex == this.dragCursor) { if (!isDropTarget) { Console console = Console.getActive(); if (this.cantCursor == null) { this.cantCursor = new Cursor(URL.make("system:CANNOT_CURSOR")); } else { this.cantCursor.detach(); console.addCursor(this.cantCursor); this.cantCursor.activate(); } this.cantCursor.activate(); } } else if (activex != null && activex == this.cantCursor && isDropTarget) { this.dragCursor.activate(); } } } @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 = PropAdder.make(new VectorProperty(this, index, "Contents")); } else if (mode == 1) { ret = this.libraries; } else if (mode == 3) { this.syncAddLibrary((Library)value); } else if (mode == 4) { this.syncDeleteLibrary((Library)value); } else if (mode == 5 && value instanceof Library) { ret = value; } return ret; default: throw new NoSuchPropertyException(); } } @Override public Object propertyParent() { return null; } @Override public String toString() { return "Libraries"; } }