/************************************************************************** Java Demo chooser CS 406 Internet Programming with Java Dr. Webster Fall 1996 Randall E. Kaufman this java application presents the user with a choice of demos to run, simply by picking the corresponding button. it requires the file demo.dat to be in the same directory. see that file for proper formatting ***************************************************************************/ import java.awt.*; import java.io.*; import java.util.*; public class DemoGUI extends Frame { String demoFile = "demo.dat"; String title; Vector lineV; Vector demoArray; void readData(DataInputStream is) throws IOException { String tempLine; tempLine = is.readLine(); do { tempLine = is.readLine(); } while (tempLine.startsWith("#")); title = tempLine; lineV = new Vector(); tempLine = is.readLine(); do { lineV.addElement(tempLine); tempLine = new String(); tempLine = is.readLine(); } while (!tempLine.equals("START DEMO LIST")); demoArray = new Vector(); tempLine = is.readLine(); do { demoArray.addElement(new Demo(tempLine)); tempLine = is.readLine(); } while (!tempLine.equals("END DEMO LIST")); } public DemoGUI() { try { DataInputStream is = new DataInputStream(new FileInputStream(demoFile)); readData(is); } catch(IOException e) { System.out.println("Error: " + e); System.exit(1); } setTitle(title); Panel top = new Panel(); top.setLayout(new GridLayout(lineV.size(),1,1,1)); for (int i = 0; i < lineV.size(); i++) top.add(new Label((String)(lineV.elementAt(i)),Label.CENTER)); Panel middle = new Panel(); int numRows = demoArray.size()/4 + 1; middle.setLayout(new GridLayout(numRows,4,2,2)); for (int i = 0; i < demoArray.size(); i++) middle.add(new Button(((Demo)(demoArray.elementAt(i))).getDesc())); Panel bottom = new Panel(); bottom.setLayout(new FlowLayout()); bottom.add(new Label(" ")); bottom.add(new Button("Quit")); bottom.add(new Label(" ")); setLayout(new BorderLayout()); add("North",top); add("Center",middle); add("South",bottom); pack(); show(); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if (arg.equals("Quit")) { System.exit(0); } else { int i = 0; int progSelected = 0; while (i < demoArray.size()) { if (arg.equals(((Demo)(demoArray.elementAt(i))).getDesc())) { progSelected = i; i = demoArray.size()+1; } else i++; } if (i > demoArray.size()) { Demo tempDemo = (Demo)(demoArray.elementAt(progSelected)); File tempFile = tempDemo.getFile(); // these println's were originally for debugging, put are useful when // adding new items. they can be commented out w/o harm. System.out.println("Trying to run program: " + tempFile.getName()); System.out.println(" getName: " + tempFile.getName()); System.out.println(" getParent: " + tempFile.getParent()); System.out.println(" getPath: " + tempFile.getPath()); System.out.println(" getAbsolutePath: " + tempFile.getAbsolutePath()); System.out.println(" isFile: " + tempFile.isFile()); System.out.println(" isDirectory: "+ tempFile.isDirectory()); Runtime rt = Runtime.getRuntime(); String cmd = tempFile.getPath(); Process pr = null; /* this blows up RWW */ try pr = rt.exec(cmd); catch (java.io.IOException ex1) { System.out.println("IO Exception when calling exec"); System.exit(1); } } else return false; } return true; } public static void main(String[] args) { DemoGUI d = new DemoGUI(); } } class Demo { private File filename; private String desc; public Demo(String rawString) { StringTokenizer t = new StringTokenizer(rawString, ":"); desc = t.nextToken(); filename = new File(t.nextToken()); } public Demo(String d, String path) { filename = new File(path); desc = d; } public File getFile() {return filename;} public String getDesc() {return desc;} } //Demo