/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: DualCategoryFrame.java Description: This file is displayed when the user selects the "Create Dual Category" menu option in the main window. The user can select a category file from a choice menu and then enter the name of its dual category in the text field below it. */ //import statements import java.awt.*; import java.awt.event.*; public class DualCategoryFrame extends Frame { Category c = new Category(); CategoryList l = new CategoryList(); //Constants IniSettings ini = new IniSettings(); //Objects in the Graphical User Interface(GUI) Button exit = new Button(); Panel button_panel = new Panel(); BorderLayout borderLayout1 = new BorderLayout(); Button okButton = new Button(); Button cancelButton = new Button(); Label label2 = new Label(); Label label1 = new Label(); TextField nameField = new TextField(); MainWindow edit; TextField categoryField = new TextField(); GridBagLayout gridBagLayout1 = new GridBagLayout(); GBConstraintsEditor g = new GBConstraintsEditor(); public DualCategoryFrame(MainWindow ed) //Default Constructor with the list of categories and the ok //button as parameters { edit = ed; edit.enableMenus(false); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public boolean handleEvent(Event e) { if ((e.id == Event.WINDOW_DESTROY) || (e.id == Event.ACTION_EVENT)) { edit.enableMenus(true); hide(); //hide frame dispose(); //free resources return true; } else if (e.key == Event.ENTER) { ok(); } return super.handleEvent(e); } public boolean action(Event e, Object o) { if (e.target == exit) { edit.enableMenus(true); hide(); //hide frame dispose(); //free resources return true; } else if (e.key == Event.ENTER) { ok(); } return false; } private void jbInit() throws Exception //This function intializes the GUI { this.setTitle("GDCT: Create Dual Category"); this.setBackground(new Color(192,192,192)); okButton.setLabel("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { okButton_actionPerformed(e); } }); cancelButton.setLabel("Cancel"); label2.setText("Current Category:"); this.setLayout(gridBagLayout1); categoryField.setBackground(Color.white); this.add(okButton, g.getConstraints(0, 2, 3, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(14, 16, 12, 0), 114, 8)); this.add(cancelButton, g.getConstraints(3, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(13, 8, 13, 15), 95, 8)); this.add(label2, g.getConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 16, 0, 0), 0, 0)); this.add(label1, g.getConstraints(0, 1, 2, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 16, 0, 0), 0, 0)); this.add(nameField, g.getConstraints(3, 1, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 7, 0, 16), 121, 1)); this.add(categoryField, g.getConstraints(3, 0, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 8, 0, 16), 120, 2)); c = edit.cat; nameField.setBackground(Color.white); categoryField.setText(c.name); categoryField.setEditable(false); label1.setText("Dual Category Name:"); } void okButton_actionPerformed(ActionEvent e) //Function called when OK button is pressed. This function //take the string in the nameField and makes it the name of //the current category { ok(); } void ok() { if (nameField.getText().equals("")) { new MessageDialog(this, "Error", "Dual category name must be filled in before dual category can be created", true); } else { makeDual(nameField.getText()); edit.enableMenus(true); hide(); dispose(); } } public void makeDual(String name) /* over, leave: variables for checking how a person wishes to save their category temp2, temp3: variables for storing temporary paths C: the new category */ { char over, leave; int i,j,temp; int temp2[] = new int [ini.getMAXWORD()]; int temp3[] = new int [ini.getMAXWORD()]; boolean done; //Category to make dual category from Category A = c; //New dual Category Category B = new Category(); B.name = name; //Copy numobj, numarr, num_rel B.numobj = A.numobj; B.numarr = A.numarr; B.num_rel = A.num_rel; //Copy objects for(i=0;i=0;j--) { temp2[path_len(A.rel_set[i].lhs)-1-j] = A.rel_set[i].lhs[j]; } temp2[path_len(A.rel_set[i].lhs)] = -1; //Copy rhs of relation to the lhs of dual relation //and reverse the order of the path //If the rhs is an identity arrow... if (path_len(A.rel_set[i].rhs) == 0) temp3[0] = A.rel_set[i].rhs[0]; else { for(j=path_len(A.rel_set[i].rhs)-1;j>=0;j--) { temp3[path_len(A.rel_set[i].rhs)-1-j] = A.rel_set[i].rhs[j]; } temp3[path_len(A.rel_set[i].rhs)] = -1; } //Copy lhs and rhs into new relation r Relation r = new Relation(); if(greater(temp2,temp3)) { r.lhs = copy_path(r.lhs,temp2); r.rhs = copy_path(r.rhs,temp3); } else { r.lhs = copy_path(r.lhs,temp3); r.rhs = copy_path(r.rhs,temp2); } //Add new relation to dual category B.rel_set[i] = r; } //Indicate that category B was created as a dual category B.mod.dualCat = true; //Save new category to list of categories edit.catList.insertNode(B); //put category in Linked List //Make temp copy of original gml B.gml = edit.cat.gml; //Create graphical display of dual category GMLEditor gEdit = new GMLEditor(); gEdit.removeArrow(B); gEdit.removeRelation(B); //Copy dual category into default category edit.cat = B; //Display category graphically edit.displayCategoryGraph(edit.cat.gml, edit.graph_); this.setTitle("Category Theory Database Tools [ CTDT 1999 ]"); //Display text version of category edit.displayCategoryText(edit.cat); //Ensure that category display is current display //mode in main window edit.textCardLayout.show(edit.textPanel, "displayPanel"); //Update category choice meny edit.categoryChoice.add(edit.cat.name); edit.categoryChoice.select(edit.cat.name); validate(); } public int path_len(int path[]) /* Parameters: path: an array of integers terminated by -1 which is a path Purpose: To determine the length of an array of integers terminated by -1 Design: Returns the number of integers in the path, does not count -1 */ { //length of path int length = 0; //If identity arrow return //length of 0 if (path[0] == -2) return(0); //Otherwise determine the length //of the path of arrows else { while (path[length] != -1) { length++; } return(length); } } public boolean greater(int path1[], int path2[]) /* Parameters: path1: an array of integers terminated by -1 path2: an array of integers terminated by -1 Purpose: To determine if path1 has a greater ordering than path2 Design: Returns true if path1 is of greater order than path2 */ { int j; //If path1 is longer than it is greater //so return true if(path_len(path1) > path_len(path2)) { return(true); } //Otherwise... else { //If the paths are equal in length if ((path_len(path1)) == (path_len(path2))) { //if ((path_len(path1) == 1) && (path1[0] == cons.id_code)) // return(false); j = 0; while (j < path_len(path1)) { //If index of current arrow in path2 is greater //then that in path1 return false since path2 //is greater than path1 if (path2[j] > path1[j]) { return(false); } //If index of current arrow in path2 is less than //that in path1 return true since path1 //is greater than path2 else { if (path2[j] < path1[j]) { return(true); } } j++; } } } //If path2 is greater in length than //path1 then return false return(false); } public int[] copy_path(int dest[], int source[]) /* Parameters: dest: an array of integers which will be set to equal source source: an array of integers which is copied into dest Purpose: To copy one array of integers to another array of integers Design: Copies source into dest */ { int i; int temp[] = new int [ini.getMAXWORD()]; for(i=0;i<=path_len(source);i++) { temp[i] = source[i]; } return(temp); } }