/* 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.*; import javax.swing.*; import java.beans.*; public class RemoveDataFrame extends JInternalFrame { CategoryInternalFrame categoryInternalFrame_; Category category_ = new Category(); Category original_category; //Objects in the Graphical User Interface(GUI) JButton closeButton = new JButton("Close"); JButton okButton = new JButton("Remove"); JButton cancelButton = new JButton("Cancel"); DefaultListModel relModel = new DefaultListModel(); DefaultListModel objModel = new DefaultListModel(); DefaultListModel arrModel = new DefaultListModel(); JList relRemoveList = new JList(relModel); JList objRemoveList = new JList(objModel); JList arrRemoveList = new JList(arrModel); JLabel relationLabel = new JLabel("Relation(s) to Remove:"); JLabel objectLabel = new JLabel("Object(s) to Remove:"); JLabel arrowLabel = new JLabel("Relation(s) to Remove:"); JLabel statusLabel = new JLabel("Remove Status:"); JTextArea statusArea = new JTextArea("", 10, 10); public RemoveDataFrame(CategoryInternalFrame categoryInternalFrame) { super(categoryInternalFrame.getTitle() + ": Remove Data", true, true, false, false); categoryInternalFrame_ = categoryInternalFrame; original_category = categoryInternalFrame_.getCategory(); category_.copyCategory(original_category); try {jbInit();} catch (Exception e) {e.printStackTrace();} } private void jbInit() throws Exception //This function intializes the GUI { okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { relRemoveList_okButton_actionPerformed(); } }); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { close_button_action(); } }); relationLabel.setText("Relation(s) to Remove:"); relRemoveList.setBackground(Color.white); JScrollPane statusScrollPane = new JScrollPane(statusArea); statusScrollPane.setBorder(BorderFactory.createEtchedBorder()); statusScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); statusArea.setWrapStyleWord(true); Container contentPane = getContentPane(); contentPane.setLayout(new GridBagLayout()); contentPane.add(closeButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(okButton, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(cancelButton, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(statusScrollPane, new GridBagConstraints(0, 3, 3, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(statusLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(objectLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(arrowLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(relationLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(arrRemoveList, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(objRemoveList, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); contentPane.add(relRemoveList, new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); relRemoveList.clearSelection(); //Allow for the selection of multiple arrows, objects, relations objRemoveList.setBackground(Color.white); objRemoveList.setBorder(BorderFactory.createEtchedBorder()); objRemoveList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); for (int i=0; i" + category_.obj[category_.arr[i][1]]); relRemoveList.setBackground(Color.white); relRemoveList.setBorder(BorderFactory.createEtchedBorder()); relRemoveList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cancelButton_actionPerformed(e); } }); statusLabel.setText("Remove Status:"); //Add relations to remove relations list String rel = new String(); int j=0; for (int i = 0; i < category_.num_relations; i++) { rel = new String(); j = 0; //Display left side of relation while (category_.relations[i].lhs[j] != -1) { if (j == 0) rel = rel.concat("" + category_.arrow[category_.relations[i].lhs[j]]); else rel = rel.concat("*" + category_.arrow[category_.relations[i].lhs[j]]); j++; } rel = rel.concat(" = "); j = 0; //Display right side of relation if (category_.relations[i].rhs[0] == -2 /*edit.ini.getIdentityCode()*/) rel = rel.concat("1"); else { while (category_.relations[i].rhs[j] != -1) { if (j == 0) rel = rel.concat("" + category_.arrow[category_.relations[i].rhs[j]]); else rel = rel.concat("*" + category_.arrow[category_.relations[i].rhs[j]]); j++; } } relModel.addElement(rel); } } private void relRemoveList_okButton_actionPerformed() //Function called when OK button is pressed or when an item //in the objRemoveList is double clicked { // statusArea.setText(""); Object[] rela = relRemoveList.getSelectedValues(); //Remove any relations that are selected first... if (rela != null) { String remove_rels[] = new String[rela.length]; for (int i=0; i< rela.length; i++) remove_rels[i] = (String)rela[i]; for (int i = 0; i < remove_rels.length; i++) { try { String temp = remove_rels[i]; int nummenu = 0; for (int j = 0; j < relModel.getSize(); j++) { if (temp.equals(relModel.getElementAt(j))) nummenu = j; } nummenu++; int relnum = ask_for_rel(nummenu, category_); if (relnum != -1) { remove_rel(relnum); relModel.removeElement(temp); statusArea.append("Relation " + temp + " removed.\n\n"); //Edit graphical display of category GMLEditor gEdit = new GMLEditor(); gEdit.removeRelation(category_); } } 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) category_.mod.dataRemoved = true; //Redraw the category with the modified changes //reDrawCat(); statusArea.setCaretPosition(0); } private void removeArrows() { int nummenu = 0; String temp; boolean test; char tmp; int check=0; Object[] arro = arrRemoveList.getSelectedValues(); //Remove any arrows that are selected second.... if (arro != null) { //Get List of selected arrows to remove String remove_arrs[] = new String[arro.length];// = (String[])arrRemoveList.getSelectedValues(); for (int i=0; i obj) category_.arr[j][0]--; if (category_.arr[j][1] > obj) category_.arr[j][1]--; } //Decrease the number of objects category_.num_objects--; //Display message that object has been removed statusArea.append("Object " + temp + " removed.\n\n"); //Remove object from list of objects objModel.removeElement(temp); //Edit graphical display of category GMLEditor gEdit = new GMLEditor(); gEdit.removeObject(category_, temp); } 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); } } private int ask_for_rel(int n, Category c) //Returns relation index number or -1 if relation not found { if ((n <= c.num_relations) && (n > 0)) { return(n-1); } return(-1); } private 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 (category_.num_relations == 0) return(n); else if (!arrow_in_rel(n)) { return(n); } else { statusArea.append("Arrow " + category_.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 } private 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< category_.num_arrows - 1;i++) { category_.arrow[i] = category_.arrow[i+1]; category_.arr[i][0] = category_.arr[i+1][0]; category_.arr[i][1] = category_.arr[i+1][1]; } category_.num_arrows--; for (i=0;i arr) { (category_.relations[i].lhs[j])--; } } for (j=0;j arr) { (category_.relations[i].rhs[j])--; } } } } private 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] == -2 /*edit.ini.getIdentityCode()*/)) return(0); while (path[length] != -1) { length++; } return length; } private 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