/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: ChangeNameFrame.java Description: This file is displayed when the user selects the "Change Name of Category" menu option in the main window. The user can select a category from a choice menu and then enter a new name in the text field below it. */ //import statements import java.awt.*; import java.awt.event.*; public class ChangeNameFrame extends Frame { Category c = new Category(); CategoryList l = new CategoryList(); //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 newNameField = new TextField(); TextField currNameField = new TextField(); MainWindow edit; GridBagLayout gridBagLayout1 = new GridBagLayout(); GBConstraintsEditor g = new GBConstraintsEditor(); public ChangeNameFrame(CategoryList ltemp, MainWindow ed) //Default Constructor with the list of categories as a parameter { l = ltemp; edit = ed; edit.enableMenus(false); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public boolean handleEvent(Event e) { if ((e.id == Event.WINDOW_DESTROY)) { 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("CTDT: Change Category Name"); 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"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { cancelButton_actionPerformed(e); } }); label2.setText("Current Category Name:"); label1.setText("New Category Name:"); this.setLayout(gridBagLayout1); currNameField.setBackground(Color.white); this.add(okButton, g.getConstraints(0, 2, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(14, 16, 12, 0), 114, 8)); this.add(cancelButton, g.getConstraints(2, 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, 2, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 16, 0, 0), 0, 0)); this.add(label1, g.getConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 16, 0, 0), 0, 0)); this.add(newNameField, g.getConstraints(2, 1, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 7, 0, 16), 121, 1)); this.add(currNameField, g.getConstraints(2, 0, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(8, 7, 0, 16), 121, 0)); CategoryNode current = l.head; currNameField.setEditable(false); currNameField.setText(edit.categoryChoice.getSelectedItem()); String name = edit.categoryChoice.getSelectedItem(); c = l.getNode(name); newNameField.setBackground(Color.white); } 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 { closeWindow(); } void cancelButton_actionPerformed(ActionEvent e) //Function called when Cencel button is pressed. This function //take the string in the nameField and makes it the name of //the current category { edit.enableMenus(true); hide(); dispose(); } public void closeWindow() { if (newNameField.getText().equals("")) { new MessageDialog(this, "Error", "New category name must be filled in before it can replace existing category name", true); } else if (edit.catList.doesExist(newNameField.getText())) { new MessageDialog(this, "Error", "A category with the specified name is already opened", true); } else { //Indicate that the category name has been modified c.mod.nameChanged = true; //Replace old name with new name c.name = newNameField.getText(); //Save category l.replaceNode(c); edit.categoryChoice.remove(edit.categoryChoice.getSelectedItem()); edit.categoryChoice.add(c.name); edit.categoryChoice.select(c.name); edit.enableMenus(true); hide(); dispose(); } } }