/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: LoadFUNFrame.java Description: This class creates a frame that allows the user to select the desired functor file (*.FUN) to load from the server. */ //import statements import java.applet.Applet; import java.net.*; import java.io.*; import java.util.*; import java.lang.System; import java.io.StringBufferInputStream; import java.awt.*; import java.awt.event.*; public class LoadFUNFrame extends Frame { public String filename = new String(); int check_enter = 0; Button exit = new Button(); Panel button_panel = new Panel(); BorderLayout borderLayout1 = new BorderLayout(); Button okButton = new Button(); java.awt.List catLoadList = new java.awt.List(); Button cancelButton = new Button(); Label label1 = new Label(); Label label2 = new Label(); Label label3 = new Label(); Label label4 = new Label(); Label noteLabel = new Label(); GridBagLayout gridBagLayout1 = new GridBagLayout(); GBConstraintsEditor g = new GBConstraintsEditor(); //global variables for reading file from server URL fileURL; InputStream input; DataInputStream dataInput; MainWindow edit; public LoadFUNFrame(MainWindow ed, Button temp1, java.awt.List temp2) { edit = ed; okButton = temp1; catLoadList = temp2; try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public boolean handleEvent(Event e) { if ((e.id == Event.WINDOW_DESTROY) || (e.id == Event.ACTION_EVENT)) { hide(); //hide frame dispose(); //free resources return true; } if (e.key == Event.ENTER) { if (check_enter == 0) check_enter++; else { hide(); //hide frame dispose(); //free resources } return true; } return super.handleEvent(e); } public boolean action(Event e, Object o) { if ((e.target == exit) || (e.key == Event.ENTER)) { hide(); //hide frame dispose(); //free resources return true; } return false; } private void jbInit() throws Exception { this.setTitle("GDCT: Load File from " + edit.ini.getServer()); this.setBackground(new Color(192,192,192)); this.setSize(new Dimension(397, 293)); //okButton.setLabel("OK"); //okButton.addActionListener(new java.awt.event.ActionListener() { // public void actionPerformed(ActionEvent e) { // catLoadList_okButton_actionPerformed(e); // } //}); cancelButton.setLabel("Cancel"); label1.setText("Files can only be loaded"); label2.setText("from the server when"); label3.setText("when connected to the"); label4.setText("Internet."); noteLabel.setText("NOTE:"); //catLoadList.addActionListener(new java.awt.event.ActionListener() { // public void actionPerformed(ActionEvent e) { // catLoadList_okButton_actionPerformed(e); // } //}); this.setLayout(gridBagLayout1); this.add(okButton, g.getConstraints(1, 5, 5, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 19, 0, 11), 123, 8)); this.add(catLoadList, g.getConstraints(0, 0, 1, 8, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(9, 7, 14, 0), 14, 139)); this.add(cancelButton, g.getConstraints(1, 6, 5, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 18, 0, 11), 103, 8)); this.add(noteLabel, g.getConstraints(1, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 16, 0, 0), 0, 0)); this.add(label1, g.getConstraints(1, 1, 3, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 16, 0, 0), -11, 0)); this.add(label2, g.getConstraints(1, 2, 3, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 18, 0, 0), 0, 0)); this.add(label3, g.getConstraints(1, 3, 3, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 18, 0, 0), 0, 0)); this.add(label4, g.getConstraints(1, 4, 2, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 39, -1)); boolean test = CreateCatList(); } void catLoadList_okButton_actionPerformed(ActionEvent e) { filename = catLoadList.getSelectedItem(); } public boolean CreateCatList() { try { fileURL = new URL(edit.ini.getServer() + edit.ini.getFunDir()); } catch (MalformedURLException except) { //String e1 = "Error\nUnable to open file\nFile unable to Load"; //error_message(e1); //showStatus("Exception: " + except.toString()); } boolean connected = ReadDirectory(); if (catLoadList.countItems() == 0) { if (connected == false) new MessageDialog(this, "Error", edit.ini.getServer() + edit.ini.getFunDir() + "contains no valid category files", true); //String e1 = "Error\nCannot find Category Files to Load\nNo Category Files on Server\n"; //error_message(e1); //catLoadList.clear(); } return (connected); } public boolean ReadDirectory() //This method loads a category file from a server //This allows the applet to access files on the home //server as well as files on any other server { String text; StringTokenizer tokens; int linecount = 0; String temp; try { input = fileURL.openStream(); dataInput = new DataInputStream(input); //viewbox.appendText("File Loaded\n\n"); //viewbox.appendText("The File Contents are:\n\n"); while((text=dataInput.readLine()) != null) { tokens = new StringTokenizer(text); while(tokens.hasMoreTokens()) { temp = tokens.nextToken(); if ((temp.charAt(0) == 'H') && (temp.charAt(1) == 'R')) { int i = 6; int j = 0; char catname[] = new char[50]; String cname = new String(); while ( temp.charAt(i) != '"') { catname[j] = temp.charAt(i); i++; j++; } cname = temp.substring(6, i); if (!cname.substring(0,1).equals("?") && !edit.ini.getServer().endsWith(cname)) { catLoadList.addItem(cname); } } } } dataInput.close(); } catch(IOException e) { new MessageDialog(this, "Error", "Unable to connect to " + edit.ini.getServer() + edit.ini.getFunDir(), true); return(false); } return(true); } }