package NET.worlds.console; import NET.worlds.scape.FrameEvent; import NET.worlds.scape.Pilot; import NET.worlds.scape.TeleportAction; import NET.worlds.scape.TeleportStatus; import java.awt.Container; import java.awt.Event; import java.awt.Label; import java.awt.Menu; import java.awt.TextField; import java.util.Vector; public class URLLine extends TextField implements FramePart, TeleportStatus, DialogDisabled { private static final long serialVersionUID = -4154943921462354673L; protected static String nonloadSign = "URL: "; public Label label = new Label(nonloadSign); public Menu historyMenu; public boolean beingEdited = false; public String lastTextSet = ""; private String currentText = ""; private boolean okToCallGetText; private boolean isDialogDisabled; protected boolean teleporting; protected int lastURLUpdate; public static Vector historyItems = new Vector(); @Override public synchronized String getText() { return this.okToCallGetText ? super.getText() : this.currentText; } private synchronized void syncCurrentText() { this.okToCallGetText = true; this.currentText = this.getText(); this.okToCallGetText = false; } @Override public synchronized void setText(String s) { if (!this.beingEdited) { if (!this.currentText.equals(this.lastTextSet)) { this.beingEdited = true; } else { this.lastTextSet = s; if (!this.currentText.equals(s)) { int p = this.getSelectionStart(); super.setText(this.currentText = s); this.select(p, p); } } } } public void cancelEdit() { this.beingEdited = false; this.lastTextSet = this.getText(); this.select(1000, 1000); this.lastURLUpdate = 0; } @Override public boolean keyDown(Event event, int key) { if (key != 27) { return super.keyDown(event, key); } else { this.cancelEdit(); return true; } } @Override public void activate(Console c, Container f, Console prev) { this.historyMenu = new Menu("History"); this.copyBackupToHistoryMenu(); Console.getMenuBar().add(this.historyMenu); } @Override public void deactivate() { } @Override public void dialogDisable(boolean disable) { this.isDialogDisabled = disable; } @Override public boolean handleEvent(Event event) { if (this.isDialogDisabled) { return false; } else { boolean ret = super.handleEvent(event); if (event.target == this) { this.syncCurrentText(); } return ret; } } @Override public boolean action(Event event, Object what) { if (event.target == this) { what = this.getText(); if (((String)what).length() != 0) { TeleportAction.teleport((String)what, null); this.cancelEdit(); } return true; } else { return true; } } @Override public void teleportStatus(String err, String url) { this.teleporting = err != null && err.length() == 0; if (err == null) { this.addHistoryItem(getCurrentPositionURL()); this.setText(url); } } @Override public boolean handle(FrameEvent f) { if (f.time < this.lastURLUpdate + 500) { return true; } else { this.lastURLUpdate = f.time; this.setText(getCurrentPositionURL()); if (this.teleporting) { if (this.label.getText().equals("LOAD ")) { this.label.setText("_____ "); } else { this.label.setText("LOAD "); } } else if (!this.label.getText().equals(nonloadSign)) { this.label.setText(nonloadSign); } return true; } } public static String getCurrentPositionURL() { Pilot pilot = Pilot.getActive(); if (pilot == null) { return ""; } else { String newURL = pilot.getURL(); return newURL == null ? "" : newURL; } } private void addHistoryItem(String url) { if (url.length() != 0) { historyItems.insertElementAt(url, 0); int cnt = historyItems.size(); while (cnt > 10) { historyItems.removeElementAt(--cnt); } this.copyBackupToHistoryMenu(); } } private void copyBackupToHistoryMenu() { while (this.historyMenu.getItemCount() > 0) { this.historyMenu.remove(0); } int cnt = historyItems.size(); if (cnt == 0) { this.historyMenu.add(""); } for (int i = 0; i < cnt; i++) { this.historyMenu.add(historyItems.elementAt(i)); } } }