/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: NameDialog.java Description: This class displays a dialog box that prompts the user for a new name if the name of the category file being loaded, created, or opened already exists in the open categories list. */ //import statements import java.awt.*; import java.awt.event.*; public class NameDialog extends Dialog { Panel panel1 = new Panel(); Label label1 = new Label(); Label label2 = new Label(); TextField nameField = new TextField(); Button okButton = new Button(); MainWindow edit; Category cat = new Category(); GridBagLayout gridBagLayout1 = new GridBagLayout(); GBConstraintsEditor g = new GBConstraintsEditor(); public NameDialog(Frame frame, String title, boolean modal, MainWindow ed, Category c) { super(frame, title, modal); edit = ed; cat = c; enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); add(panel1); pack(); } catch (Exception ex) { ex.printStackTrace(); } pack(); //Determine center position for dialog on screen Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = this.getSize(); if (frameSize.height > screenSize.height) frameSize.height = screenSize.height; if (frameSize.width > screenSize.width) frameSize.width = screenSize.width; this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); show(); } void jbInit() throws Exception { label2.setText("Enter a new category name:"); okButton.setLabel("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { okButton_actionPerformed(e); } }); label1.setText("Current category name is already in use by another category"); panel1.setLayout(gridBagLayout1); panel1.add(label2, g.getConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 14, 0, 0), -6, -3)); panel1.add(nameField, g.getConstraints(1, 1, 2, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 152, 0)); panel1.add(okButton, g.getConstraints(0, 2, 2, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(14, 140, 15, 0), 75, 8)); panel1.add(label1, g.getConstraints(0, 0, 4, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 14, 0, 3), 4, 0)); } protected void processWindowEvent(WindowEvent e) { /*if (e.getID() == WindowEvent.WINDOW_CLOSING) { cancel(); } */ super.processWindowEvent(e); } void cancel() { dispose(); } void okButton_actionPerformed(ActionEvent e) //This occurs when a new name is entered { boolean name = false; //true is name already exists, false otherwise CategoryNode current = edit.catList.head; if (!nameField.getText().equals("")) { while(current != null) { if (current.cat.name.equals(nameField.getText())) name = true; current = current.next; } if (name == true) { new MessageDialog(edit, "Error", "There is already a category open with the name " + nameField.getText(), true); nameField.setText(""); } else { cat.name = nameField.getText(); dispose(); hide(); } } } void cancelButton_actionPerformed(ActionEvent e) { dispose(); hide(); } }