/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: RemoveDataFrame.java Description: This file is displayed when the user selects the "Remove Data" menu option in the "Edit Category" menu of the main window. It allows the user to remove objects, arrows, and relations from the current category. */ //import statements import java.awt.*; import java.awt.event.*; public class RemoveDataFrame extends Frame { Category c = new Category(); CategoryList l = new CategoryList(); int check_enter = 0; //Objects in the Graphical User Interface(GUI) Button exit = new Button(); Panel button_panel = new Panel(); BorderLayout borderLayout1 = new BorderLayout(); Button okButton = new Button(); List relRemoveList = new List(); Button cancelButton = new Button(); Label label1 = new Label(); Label label2 = new Label(); TextField categoryField = new TextField(); MainWindow edit; Label label3 = new Label(); Label label4 = new Label(); List objRemoveList = new List(); List arrRemoveList = new List(); TextArea statusArea = new TextArea("", 10, 10, TextArea.SCROLLBARS_VERTICAL_ONLY); Label label5 = new Label(); Label removeLabel = new Label(); GridBagLayout gridBagLayout1 = new GridBagLayout(); GBConstraintsEditor g = new GBConstraintsEditor(); public RemoveDataFrame(MainWindow ed) //Default Constructor with the list of categories as a parameter { 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) { if (check_enter == 0) check_enter++; else { edit.enableMenus(true); 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)) { edit.enableMenus(true); hide(); //hide frame dispose(); //free resources return true; } return false; } private void jbInit() throws Exception //This function intializes the GUI { this.setTitle("GDCT: Remove Data from Category"); this.setBackground(new Color(192,192,192)); okButton.setLabel("OK"); okButton.setLabel("Remove"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { relRemoveList_okButton_actionPerformed(e); } }); cancelButton.setLabel("Close"); label1.setText("Relation(s) to Remove:"); label2.setText("Current Category:"); relRemoveList.setBackground(Color.white); relRemoveList.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { relRemoveList_okButton_actionPerformed(e); } }); this.setLayout(gridBagLayout1); categoryField.setBackground(Color.white); this.add(okButton, g.getConstraints(6, 5, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 12, 0, 11), 64, 5)); this.add(cancelButton, g.getConstraints(6, 6, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(6, 14, 5, 12), 74, 3)); this.add(categoryField, g.getConstraints(6, 3, 2, 2, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(32, 10, 0, 12), 101, 2)); this.add(statusArea, g.getConstraints(0, 4, 6, 3, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 9, 5, 0), 153, -81)); this.add(label5, g.getConstraints(0, 3, 2, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(12, 9, 0, 0), -8, 3)); this.add(removeLabel, g.getConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 9, 0, 0), 0, 0)); this.add(label4, g.getConstraints(0, 1, 4, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 9, 0, 0), -11, -3)); this.add(label3, g.getConstraints(4, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), -10, -2)); this.add(label1, g.getConstraints(6, 1, 2, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 9, 0, 8), -10, -4)); this.add(label2, g.getConstraints(6, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 2)); this.add(arrRemoveList, g.getConstraints(4, 2, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 8, 0, 0), -7, 28)); this.add(objRemoveList, g.getConstraints(0, 2, 3, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 9, 0, 7), -14, 28)); this.add(relRemoveList, g.getConstraints(6, 2, 2, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 13, 0, 15), 1, 32)); relRemoveList.clear(); //Allow for the selection of multiple arrows, objects, relations objRemoveList.setMultipleSelections(true); arrRemoveList.setMultipleSelections(true); relRemoveList.setMultipleSelections(true); c = edit.cat; this.setSize(new Dimension(394, 300)); removeLabel.setText("Remove Data"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cancelButton_actionPerformed(e); } }); label5.setText("Remove Status:"); label4.setText("Object(s) to Remove:"); label3.setText("Arrow(s) to Remove:"); categoryField.setEditable(false); categoryField.setText(c.name); //Add objects to remove objects list for (int i = 0; i < c.numobj; i++) objRemoveList.addItem("" + c.obj[i]); //if (objRemoveList.countItems() == 0) //{ // new MessageDialog(this, "Error", "Can not remove object, category has 0 objects", true); //} //Add arrows to remove arrows list for (int i = 0; i < c.numarr; i++) { arrRemoveList.addItem(c.arrow[i] + ":" + c.obj[c.arr[i][0]] + "->" + c.obj[c.arr[i][1]]); } //if (arrRemoveList.countItems() == 0) //{ // new MessageDialog(this, "Error", "Can not remove arrow, category has 0 arrows", true); //} //Add relations to remove relations list String rel = new String(); int j=0; for (int i = 0; i < c.num_rel; i++) { rel = new String(); j = 0; //Display left side of relation while (c.rel_set[i].lhs[j] != -1) { if (j == 0) rel = rel.concat("" + c.arrow[c.rel_set[i].lhs[j]]); else rel = rel.concat("*" + c.arrow[c.rel_set[i].lhs[j]]); j++; } rel = rel.concat(" = "); j = 0; //Display right side of relation if (c.rel_set[i].rhs[0] == edit.ini.getIdentityCode()) rel = rel.concat("1"); else { while (c.rel_set[i].rhs[j] != -1) { if (j == 0) rel = rel.concat("" + c.arrow[c.rel_set[i].rhs[j]]); else rel = rel.concat("*" + c.arrow[c.rel_set[i].rhs[j]]); j++; } } relRemoveList.addItem(rel); } } void relRemoveList_okButton_actionPerformed(ActionEvent e) //Function called when OK button is pressed or when an item //in the objRemoveList is double clicked { statusArea.setText(""); //Remove any relations that are selected first... if (relRemoveList.getSelectedItems() != null) { String remove_rels[] = relRemoveList.getSelectedItems(); for (int i = 0; i < remove_rels.length; i++) { try { String temp = remove_rels[i]; int nummenu = 0; for (int j = 0; j < relRemoveList.getItemCount(); j++) { if (temp.equals(relRemoveList.getItem(j))) nummenu = j; } nummenu++; int relnum = ask_for_rel(nummenu, c); if (relnum != -1) { remove_rel(relnum); relRemoveList.remove(temp); statusArea.append("Relation " + temp + " removed.\n\n"); //Edit graphical display of category GMLEditor gEdit = new GMLEditor(); gEdit.removeRelation(c); //Display category graphically edit.displayCategoryGraph(c.gml, edit.graph_); //Display text version of category edit.cat = c; edit.displayCategoryText(edit.cat); } //categoryField.setText(categoryField.getText() + " " + relnum + " " + temp); } catch (ArrayIndexOutOfBoundsException aie) { } } } else { new MessageDialog(this, "Error","Relation must be selected before it can be removed", true); } //Call method to remove arrows removeArrows(); //Call methods to remove objects removeObjects(); //Indicate that category has had data //removed (ie. modified) c.mod.dataRemoved = true; //Redraw the category with the modified changes //reDrawCat(); statusArea.setCaretPosition(0); } public void removeArrows() { int nummenu = 0; String temp; boolean test; char tmp; int check=0; //Remove any arrows that are selected second.... if (arrRemoveList.getSelectedItems() != null) { //Get List of selected arrows to remove String remove_arrs[] = arrRemoveList.getSelectedItems(); //For each arrow... for (int i = 0; i < remove_arrs.length; i++) { try { //Assign temp the value of the current array element temp = remove_arrs[i]; //*************************************************** //NOTE: Index of arrow in category is the same as //index of arrow in arrRemoveList //*************************************************** //Find index of arrow in arrRemoveList for (int j = 0; j < arrRemoveList.getItemCount(); j++) { if (temp.equals(arrRemoveList.getItem(j))) { //Save index of arrow in category in nummenu nummenu = j; } } //Ask for arrow and check to see if it is //in any relations, if it is, return -1 int arrnum = ask_for_arr(nummenu); //If arrnum returns index of arrow, //remove the arrow if (arrnum != -1) { statusArea.append("Arrow " + temp + " removed.\n\n"); remove_arr(arrnum); arrRemoveList.remove(temp); l.replaceNode(c); //Edit graphical display of category GMLEditor gEdit = new GMLEditor(); gEdit.removeArrow(c); //Display category graphically edit.displayCategoryGraph(c.gml, edit.graph_); //Display text version of category edit.cat = c; edit.displayCategoryText(edit.cat); } } catch (ArrayIndexOutOfBoundsException aie) { } } //end(); } else { new MessageDialog(this, "Error", "Arrow must be selected before it can be removed", true); } } public void removeObjects() { String temp; boolean test; char tmp; int check=0; int obj = -1; boolean depend_arrows = false; //Remove any objects that are selected third.... if (objRemoveList.getSelectedItems() != null) { String remove_objs[] = objRemoveList.getSelectedItems(); for (int i = 0; i < remove_objs.length; i++) { try { temp = remove_objs[i]; for(int j=0; j < c.numobj; j++) { if (c.obj[j].equals(temp)) obj = j; } String depend_arrs = new String(); //Determine if any arrows are dependent on //object being removed for(int j=0; j < c.numarr; j++) { if (c.arr[j][0] == obj || c.arr[j][1] == obj) { if (depend_arrows == false) depend_arrs = c.arrow[j]; else depend_arrs = depend_arrs.concat(", " + c.arrow[j]); depend_arrows = true; } } //If no objects are dependent... if (depend_arrows == false) { //Remove object and move all objects above //it to the left by one for(int j = obj; j < c.numobj; j++) { c.obj[j]= c.obj[j+1]; } //Adjust domain and codomain object numbers //to the left by one for object greater than //the one being removed for (int j=0; j < c.numarr; j++) { if (c.arr[j][0] > obj) c.arr[j][0]--; if (c.arr[j][1] > obj) c.arr[j][1]--; } //Decrease the number of objects c.numobj--; //Display message that object has been removed statusArea.append("Object " + temp + " removed.\n\n"); //Remove object from list of objects objRemoveList.remove(temp); //Edit graphical display of category GMLEditor gEdit = new GMLEditor(); gEdit.removeObject(c, temp); //Display category graphically edit.displayCategoryGraph(c.gml, edit.graph_); //Display text version of category edit.cat = c; edit.displayCategoryText(edit.cat); } else { //If dependent arrows exist, display message that object has not been removed statusArea.append("Object " + temp + " NOT removed. Arrows " + depend_arrs + " dependent on object " + temp + ".\n\n"); depend_arrows = false; } } catch (ArrayIndexOutOfBoundsException aie) { } } } else { new MessageDialog(this, "Error", "Object must be selected beore it can be removed", true); } } public int ask_for_rel(int n, Category c) //Returns relation index number or -1 if relation not found { if ((n <= c.num_rel) && (n > 0)) { return(n-1); } return(-1); } public void remove_rel(int rel) //Removes the relation from the category and uses the following parameters: // rel: the relation to be removed from the category // A : the category which contains the relation { int i; for (i=rel; i= 0)) { //viewbox.appendText(Integer.toString(n)); if (c.num_rel == 0) return(n); else if (!arrow_in_rel(n)) { return(n); } else { statusArea.append("Arrow " + c.arrow[n] + " NOT removed. Arrow has relations dependent on it.\n\n"); //new MessageDialog(this, "Error", "Unable to remove arrow, arrow has relations dependent on it", true); return(-1); } } return(-1); //n is not an arrow in category } public void remove_arr(int arr) //This function removes the specified arrow from the category. It accepts as //parameters: // arr: the arrow to be removed from the category // A : the category which contains the arrow { int i,j; for (i=arr;i< c.numarr - 1;i++) { c.arrow[i] = c.arrow[i+1]; c.arr[i][0] = c.arr[i+1][0]; c.arr[i][1] = c.arr[i+1][1]; } c.numarr--; for (i=0;i arr) { (c.rel_set[i].lhs[j])--; } } for (j=0;j arr) { (c.rel_set[i].rhs[j])--; } } } } 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 */ { int length = 0; if ((path[0] == edit.ini.getIdentityCode())) return(0); while (path[length] != -1) { length++; } return(length); } public boolean arrow_in_rel(int arr) /* Parameters: arr: the arrow being searched for in the set of relations A: the category which contains the set of relations being searched Purpose: To search the set of relations to see if the given arrow occurs in any of the relations Design: Returns true if the arrow does occur in at least one relation, and false if it does not occur in any of the relations. */ { int i,j; //For each relation... for (i=0;i