/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Matt Graves, Jesse Tweedle Mount Allison University 2003 File: CreateFunctorDialog.java Description: Displays an internal frame to prompt the user for new functor information. Then stores the information in a new functor and somehow sends it back to the calling program. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.beans.*; public class CreateFunctorDialog extends JInternalFrame { //to hold data for the functor that is created private FunctorNode creation; private InputCheck ic; int num_obj; //overall gui stuff private JPanel cards; private CardLayout cardlayout; //file selection card private JPanel filePanel; private JLabel file1_label; private JLabel file2_label; private JLabel name_label; private JButton browse1; private JButton browse2; private JTextField file1_name; private JTextField file2_name; private JTextField func_name; private JButton next_objects; //object mapping private JPanel objectPanel; private JLabel object_instructions; private JLabel object_domain_label; private JLabel object_codomain_label; private JList domain_object_list; private JList codomain_object_list; private JList done_object_list; private DefaultListModel domain_object_listModel; private DefaultListModel codomain_object_listModel; private DefaultListModel done_object_model; private JScrollPane domain_object_scroller; private JScrollPane codomain_object_scroller; private JScrollPane done_object_scroller; private JButton addmapping_object; private JButton next_arrows; //arrow mapping card private JPanel arrowPanel; private JLabel arrow_instructions; private JLabel arrow_domain_label; private JLabel arrow_path_label; private JLabel arrow_complete_label; private JList domain_arrow_list; private JTextField path_name; private JList done_arrow_list; private DefaultListModel domain_arrow_listModel; private DefaultListModel done_arrow_model; private JScrollPane domain_arrow_scroller; private JScrollPane done_arrow_scroller; private JButton addmapping_arrow; private JButton done; private IniSettings ini; public CreateFunctorDialog(IniSettings ini2) { super("Create Functor", false, //resizable true, //closable false, //maximizable true);//iconifiable setPreferredSize(new Dimension(450, 380)); //give it a super fun icon setFrameIcon(new ImageIcon("fun.gif")); //set the location of the window to a random location so they //are not all on top of each other. setLocation(new java.util.Random().nextInt(400) , new java.util.Random().nextInt(250) ); num_obj = 0; ini = ini2; ic = new InputCheck(); creation = new FunctorNode(); //create the objects for file selection filePanel = new JPanel(); file1_label = new JLabel("Domain Category"); file2_label = new JLabel("Codomain Category"); name_label = new JLabel("Functor Name"); browse1 = new JButton("Browse"); browse2 = new JButton("Browse"); file1_name = new JTextField(); file1_name.setEditable(false); file2_name = new JTextField(); file2_name.setEditable(false); func_name = new JTextField(); next_objects = new JButton("Next -->"); //a couple of action listeners for the file selection card browse1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { file1_name.setText(getfile()); } }); browse2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { file2_name.setText(getfile()); } }); next_objects.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { next_objects(); } }); //construct the card for file selection filePanel.setLayout(new GridBagLayout()); filePanel.add(name_label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); filePanel.add(func_name, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); filePanel.add(file1_label, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); filePanel.add(file2_label, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); filePanel.add(browse1, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); filePanel.add(browse2, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); filePanel.add(file1_name, new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 280, 0)); filePanel.add(file2_name, new GridBagConstraints(0, 4, 2, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 280, 0)); filePanel.add(next_objects, new GridBagConstraints(2, 5, 1, 1, 1.0, 1.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); //create objects for the object mapping card objectPanel = new JPanel(); object_instructions = new JLabel("Match objects from Domain to Codomain:"); object_domain_label = new JLabel("- Domain:"); object_codomain_label = new JLabel("- Codomain:"); domain_object_listModel = new DefaultListModel(); codomain_object_listModel = new DefaultListModel(); done_object_model = new DefaultListModel(); domain_object_list = new JList(domain_object_listModel); codomain_object_list = new JList(codomain_object_listModel); done_object_list = new JList(done_object_model); domain_object_list.setVisibleRowCount(5); codomain_object_list.setVisibleRowCount(5); done_object_list.setVisibleRowCount(5); domain_object_scroller = new JScrollPane(domain_object_list); codomain_object_scroller = new JScrollPane(codomain_object_list); done_object_scroller = new JScrollPane(done_object_list); addmapping_object = new JButton("Add Mapping->"); next_arrows = new JButton("Next -->"); //action listeners for object mapping card addmapping_object.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addObjectMap(); } }); next_arrows.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { next_arrows(); } }); //construct the card for object mappings objectPanel.setLayout(new GridBagLayout()); objectPanel.add(object_instructions, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(object_domain_label, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(object_codomain_label, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(domain_object_scroller, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(codomain_object_scroller, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(done_object_scroller, new GridBagConstraints(3, 2, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(addmapping_object, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); objectPanel.add(next_arrows, new GridBagConstraints(3, 3, 1, 1, 1.0, 0.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); //create objects for the arrow mapping card arrowPanel = new JPanel(); arrow_instructions = new JLabel("Match arrows from Domain to Codomain:"); arrow_domain_label = new JLabel("Domain Arrows:"); arrow_path_label = new JLabel("Codomain Path:"); arrow_complete_label = new JLabel("Completed:"); path_name = new JTextField(); domain_arrow_listModel = new DefaultListModel(); done_arrow_model = new DefaultListModel(); domain_arrow_list = new JList(domain_arrow_listModel); done_arrow_list = new JList(done_arrow_model); domain_arrow_scroller = new JScrollPane(domain_arrow_list); done_arrow_scroller = new JScrollPane(done_arrow_list); addmapping_arrow = new JButton("Add Mapping ->"); done = new JButton("Done!"); //arrow action listeners addmapping_arrow.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addArrowMap(); } }); done.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { done(); } }); //construct arrow mapping card arrowPanel.setLayout(new GridBagLayout()); arrowPanel.add(arrow_instructions, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(arrow_domain_label, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(domain_arrow_scroller, new GridBagConstraints(0, 2, 1, 4, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(arrow_path_label, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(path_name, new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(addmapping_arrow, new GridBagConstraints(1, 5, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(arrow_complete_label, new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(done_arrow_scroller, new GridBagConstraints(2, 2, 1, 4, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 3, 3, 3), 0, 0)); arrowPanel.add(done, new GridBagConstraints(2, 6, 1, 1, 1.0, 1.0 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0)); //add the cards to the cardlayout cards = new JPanel(); cards.setLayout(new CardLayout()); cards.add(filePanel, "Categories"); cards.add(objectPanel,"Objects"); cards.add(arrowPanel,"Arrows"); this.getContentPane().add(cards); } public boolean next_arrows() { if(domain_object_listModel.isEmpty()) { arrow_domain_label.setText(creation.A.name +" Domain:"); arrow_path_label.setText(creation.B.name+" Codomain:"); CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, "Arrows"); for(int i=0; i= 0 && codomain_object_list.getSelectedIndex()>=0) { String from = domain_object_listModel.getElementAt(domain_object_list.getSelectedIndex()).toString(); String to = codomain_object_listModel.getElementAt(codomain_object_list.getSelectedIndex()).toString(); done_object_model.addElement(from + " |--> " + to); domain_object_listModel.remove(domain_object_list.getSelectedIndex()); //first the index of from object... int fromindex; for (fromindex = 0; creation.A.obj[fromindex] != from; fromindex++) {} int toindex = 0; for (toindex = 0; creation.B.obj[toindex]!=to; toindex++) {} //j now has the index of this object //must use this hack because of the structure of the way objects are //stored in a Category and CatFunctor. A higher level Category and CatFunctor //would remove the need to make sure that things are in the right order. //System.out.println("-->"+j+" "+creation.A.obj[j]); creation.f.obj[fromindex] = toindex; } } public void done() { //now we want to actually create the functor stuff //but first need to check that there are no arrows left to try if(domain_arrow_listModel.isEmpty()) { //but first must create the diagram this function was in openfunctor creation.updateDiagramCategory(); FunctorInternalFrame frame = new FunctorInternalFrame(creation); frame.setVisible(true); getDesktopPane().add(frame); try{ frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {} try{ this.setClosed(true); } catch(java.beans.PropertyVetoException e){} } } public void addArrowMap() { if(domain_arrow_list.getSelectedIndex() >= 0) { if(path_name.getText().length()>0) { //get the entered path and do some checking on it... String path_string = path_name.getText(); int editsub = 0; int path[] = new int[ini.getMAXWORD()]; if(path_string.equals("1"))//an identity arrow... { // System.out.println("BurP!"); //deal with this!!! int place = domain_arrow_list.getSelectedIndex(); String s = domain_arrow_listModel.getElementAt(place).toString(); //s has the value of the selected arrow int j = 0; for(j = 0; creation.A.arrow[j]!=s; j++){} //now j has the right index creation.f.arr[j][0] = ini.getIdentityCode(); creation.f.arr[j][1] = -1; done_arrow_model.addElement(s +" |--> "+ path_name.getText()); domain_arrow_listModel.remove(domain_arrow_list.getSelectedIndex()); path_name.setText(""); } else { //checks to see that the string the user enters //contains only arrows that exist in category B if(!(creation.B.check_string(path_string))) { JOptionPane.showInternalMessageDialog(getDesktopPane(),"One or more of the arrows specified do not exist in Category " + creation.B.name,"Need Valid Path",JOptionPane.ERROR_MESSAGE); } else { //represents the path of the path_string as a series of intergers... path = creation.B.copy_path(creation.B.string_to_path(path_string)); //checks to see that domains and codomains of each arrow in path match up if(!(creation.B.check_path(path))) { JOptionPane.showInternalMessageDialog(getDesktopPane(),"Domains and codomains of each arrow in path do not match","Need Valid Path",JOptionPane.ERROR_MESSAGE); } else { //Checks to see that domain and codomain of path match up with //domain and codomain of arrow under the functor //put editsub to the right value... int place = domain_arrow_list.getSelectedIndex(); String s = domain_arrow_listModel.getElementAt(place).toString(); //s has the value of the selected arrow int j = 0; for(j = 0; creation.A.arrow[j]!=s; j++){} if((!(creation.check_domain(path,j))) || (!(creation.check_codomain(path,j)))) { JOptionPane.showInternalMessageDialog(getDesktopPane(),"Domain or codomain of path does not match up with arrow under the functor","Need Valid Path",JOptionPane.ERROR_MESSAGE); } else { int reduced_path[] = new int[ini.getMAXWORD()]; reduced_path = creation.B.reduce(path); creation.f.arr[j] = reduced_path; //editsub++; //String from = domain_arrow_listModel.getElementAt(domain_arrow_list.getSelectedIndex()).toString(); done_arrow_model.addElement(s +" |--> "+ path_name.getText()); domain_arrow_listModel.remove(domain_arrow_list.getSelectedIndex()); path_name.setText(""); } } } } } else { JOptionPane.showInternalMessageDialog(getDesktopPane(),"You need to enter a valid path.","Need Valid Path",JOptionPane.ERROR_MESSAGE); } } else { //say that need some arrows suggest they click done JOptionPane.showInternalMessageDialog(getDesktopPane(),"You need to select an arrow from the domain category.","Need Arrows",JOptionPane.ERROR_MESSAGE); } } }