import java.lang.*; import java.net.*; import java.io.*; import java.awt.*; import java.applet.*; public class editor implements Runnable, AppletStub { final static String DEFAULT_IMAGELIB = "http://web.mit.edu/ttonchev/java/classes/ships.dir"; String recording, imagelib; boolean observer; Frame window; Applet applet; Dimension gsize; public static void main(String argv[]) { String recording = null; String imagelib = DEFAULT_IMAGELIB; boolean observer = false; for (int i = 0; i < argv.length; i++) if (argv[i].equals("-obs")) observer = true; else if (argv[i].equals("-lib")) { if (++i < argv.length) imagelib = argv[i]; } else if (argv[i].charAt(0) == '-') System.err.println("Unknown option " + argv[i]); else recording = argv[i]; if (recording == null) { System.err.println("Syntax: editor [-obs] [-lib ] "); System.exit(0); } editor instance = new editor(recording, imagelib, observer); Thread edit = new Thread(instance); edit.start(); } public editor(String rec, String lib, boolean obs) { recording = rec; imagelib = lib; observer = obs; } public void run() { window = new Frame("Netrek Recording Editor"); applet = new editorApplet(window); applet.setStub(this); applet.init(); window.setResizable(false); window.add("Center", applet); window.show(); gsize = applet.preferredSize(); window.resize(gsize.width, gsize.height); window.layout(); applet.start(); } // AppletStub methods public boolean isActive() { return true; } public URL getDocumentBase() { File fl = new File(""); try return new URL("file://" + fl.getAbsolutePath()); catch (MalformedURLException ex) { return null; } } public URL getCodeBase() { File fl = new File(""); try return new URL("file://" + fl.getAbsolutePath()); catch (MalformedURLException ex) { return null; } } public String getParameter(String name) { if (name.equals("IMAGELIB")) return DEFAULT_IMAGELIB; if (name.equals("RECORDING")) { return recording; } if (name.equals("OBSERVER") && observer) return "ON"; return null; } public AppletContext getAppletContext() { return null; } public void appletResize(int width, int height) { gsize = new Dimension(width, height); window.resize(gsize.width, gsize.height); } } class editorApplet extends viewer implements Runnable { int mfrom, mto, pfrom, pto, oldstep; boolean do_save; Label lfrom, lto; Button bsetfrom, bsetto, bsave, bquit; ByteArrayOutputStream outbuf; DataOutputStream dataoutbuf; Frame myframe; SaveDialog save_popup; public editorApplet(Frame frame) { myframe = frame; mfrom = -1; mto = -1; do_save = false; outbuf = new ByteArrayOutputStream(); dataoutbuf = new DataOutputStream(outbuf); } void setupLayout() { super.setupLayout(); lfrom = new MyLabel(" Start: "); lto = new MyLabel("End: "); pan1.add(lfrom); pan1.add(lto); bsetfrom = new Button("Mark Start"); bsetto = new Button("Mark End"); bsave = new Button("Save Extract"); bquit = new Button("Quit"); bsave.disable(); pan2.add(bsetfrom); pan2.add(bsetto); pan2.add(bsave); pan2.add(bquit); save_popup = new SaveDialog(myframe, this); } public synchronized boolean action(Event evt, Object what) { if (evt.target == bquit) System.exit(0); if (evt.target == save_popup) { SaveExtract(save_popup.getFile()); save_popup.hide(); do_save = false; //step = oldstep; // commented: one has to see the status msg } if (do_save) return false; if (evt.target == bsetfrom && playing) { mfrom = packet; lfrom.setText(" Start: " + mfrom); if (mfrom < mto) bsave.enable(); else bsave.disable(); pfrom = inbuf.getPosition(); outbuf.reset(); if (mfrom > 0) try { game.DumpStateInit(dataoutbuf); game.DumpState(dataoutbuf); } catch (IOException ex) { } } else if (evt.target == bsetto && (playing || packet > 0)) { mto = packet; lto.setText("End: " + mto); if (mfrom < mto && mfrom >= 0) bsave.enable(); else bsave.disable(); pto = inbuf.getPosition(); } else if (evt.target == bsave) { oldstep = step; step = 0; save_popup.show(); do_save = true; } return super.action(evt, what); } vOid SaveExtract(String fname) { FileOutputStream outs; try { outs = new FileOutputStream(fname); } catch (IOException ex) { stat.setText("Bad file name..."); return; } int current = inbuf.getPosition(); try { if (mfrom > 0) outbuf.writeTo(outs); inbuf.setPosition(pfrom); int pos = pfrom; byte[] buf = new byte[16384]; while (pos < pto) { int cnt = pto - pos; if (cnt > buf.length) cnt = buf.length; int len = inbuf.read(buf, 0, cnt); outs.write(buf, 0, len); pos += len; } outs.close(); stat.setText("Extract saved..."); } catch (IOException ex) { stat.setText("I/O Error occurred..."); } try inbuf.setPosition(current); catch (IOException ex) { } } } class SaveDialog extends Dialog { Component mynotify; boolean layed_out = false; Label save_label; TextField save_fld; public SaveDialog(Frame myframe, Component notify) { super(myframe, "Save File", false); mynotify = notify; save_label = new Label("Enter filename:"); save_fld = new TextField(40); add("West", save_label); add("South", save_fld); resize(340, 50); } public synchronized boolean action(Event evt, Object what) { if (evt.target == save_fld) { evt.target = this; mynotify.action(evt, what); evt.target = save_fld; } return false; } public String getFile() { return save_fld.getText(); } public void show() { super.show(); } public void paint(Graphics g) { if (!layed_out) { Dimension size = preferredSize(); resize(size.width, size.height); layout(); layed_out = true; } super.paint(g); } }