import javax.swing.*; import java.io.*; import java.awt.event.*; import java.awt.*; public class FunctorInternalFrame extends JInternalFrame { FunctorNode functor_;//used to wrap up functor data JTextArea vbox = new JTextArea(); JLabel nameLabel = new JLabel("Name:"); JLabel objects = new JLabel("Objects"); JLabel arrows = new JLabel("Arrows"); JTextArea functorObjectArea = new JTextArea(10,10);//text area to show the objects JTextArea functorArrowArea = new JTextArea(10,10);//text area to show the arrows JScrollPane functorObject = new JScrollPane(functorObjectArea);//wraps the object area in a scroller JScrollPane functorArrow = new JScrollPane(functorArrowArea);//wraps the arrow area in a scroller Graph functorToGraph = new Graph(); Graph functorFromGraph = new Graph(); GraphCanvas functorToCanvas; GraphCanvas functorFromCanvas; ViewportScroller fromPort; ViewportScroller toPort; JPanel bigPanel = new JPanel();//panel to hold everything in this inernal frame JPanel leftPanel = new JPanel();//panel on left hand of screen... JPanel funcPanel1 = new JPanel(); JPanel funcPanel2 = new JPanel(); JPanel twoCatPanel = new JPanel(); JPanel funcDisplayControls; JPanel functorDisplayPanel = new JPanel(); JPanel diagramPanel = new JPanel(); JTabbedPane tabbedPane = new JTabbedPane(); ScrolledPanel functorToPanel; ScrolledPanel functorFromPanel; // JButton resetButton = new JButton("Reset Button"); // JButton forwardButton = new JButton("Forward >>"); // JButton backButton = new JButton("<< Back"); // JButton animateButton = new JButton("Animate"); //for diagram mode Graph g = new Graph(); GraphCanvas gc; Category c = new Category(); AngleControlPanel acp; ViewportScroller portScroller_; ScrolledPanel viewingPanel_; public FunctorInternalFrame(FunctorNode fnode){ super(fnode.f.name, true, //resizable true, //closable true, //maximizable true);//iconifiable //give it a super fun icon setFrameIcon(new ImageIcon("fun.gif")); setPreferredSize(new Dimension(640, 480)); setLocation(new java.util.Random().nextInt(250) , new java.util.Random().nextInt(100) ); functor_ = fnode; funcPanel1.setLayout(new BorderLayout()); funcPanel2.setLayout(new BorderLayout()); functorFromCanvas = new GraphCanvas(functorFromGraph, this, functor_.A); functorFromCanvas.setMouseMode(GraphCanvas.SELECT_BOTH); functorToCanvas = new GraphCanvas(functorToGraph, this, functor_.B); functorToCanvas.setMouseMode(GraphCanvas.SELECT_BOTH); functorToPanel = new ScrolledPanel(functorToCanvas); functorFromPanel = new ScrolledPanel(functorFromCanvas); toPort = new ViewportScroller(90, 90, 500.0, 500.0, 400.0, 400.0, 0.0, 0.0); fromPort = new ViewportScroller(90, 90, 500.0, 500.0, 400.0, 400.0, 0.0, 0.0); //for diagram stuff gc = new GraphCanvas(g, this, functor_.diagram); gc.setMouseMode(GraphCanvas.SELECT_BOTH); //instantiated port scroller details... portScroller_ = new ViewportScroller(90, 90, 500.0, 500.0, 400.0, 400.0, 0.0, 0.0); viewingPanel_ = new ScrolledPanel(gc); viewingPanel_.setSize(500,500); acp = new AngleControlPanel(180, 76); //this next group of if statements has to do with deciding whether //or not the gml should be created for this graph. if the graph is //of type fgl it should already have a graph and these lines //do not set the gml. A *.fun file will have a default gml representation //of: //graph [ //directed 1 //] if(functor_.A.gml == "graph [\ndirected 1\n]\n"){ functor_.A.setGML(); functor_.B.setGML(); functor_.diagram.setGML(); } //display the graphs now to the canvases displayCategoryGraph(functor_.A.gml, functorFromGraph); displayCategoryGraph(functor_.B.gml, functorToGraph); displayCategoryGraph(functor_.diagram.gml, g); displayFunctor(functor_); diagramPanel.setLayout(new GridBagLayout()); diagramPanel.add(viewingPanel_, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.5 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); diagramPanel.add(acp, new GridBagConstraints(0, 1, 1, 1, 0.5, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 10, 10, 10), 0, 50)); diagramPanel.add(portScroller_, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0, 0)); funcPanel1.add(BorderLayout.CENTER, functorFromPanel); //funcPanel1.add(BorderLayout.SOUTH, fromPort); funcPanel2.add(BorderLayout.CENTER, functorToPanel); //funcPanel2.add(BorderLayout.SOUTH, toPort); funcDisplayControls = new JPanel(); funcDisplayControls.setLayout(new GridBagLayout()); funcDisplayControls.add(fromPort, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 50, 50)); // funcDisplayControls.add(backButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 // ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); // funcDisplayControls.add(forwardButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0 // ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); // funcDisplayControls.add(resetButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0 // ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); funcDisplayControls.add(toPort, new GridBagConstraints(3, 0, 3, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 50, 50)); funcDisplayControls.setBackground(Color.red); twoCatPanel = new JPanel(); twoCatPanel.setLayout(new GridBagLayout()); twoCatPanel.add(funcPanel1, new GridBagConstraints(0, 0, 1, 4, 0.5, 0.5 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 5, 0, 0), 314, 487)); twoCatPanel.add(funcPanel2, new GridBagConstraints(1, 0, 1, 4, 0.5, 0.5 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 14, 0, 3), 302, 487)); // twoCatPanel.add(funcDisplayControls, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0 // ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 5, 3, 4), 182, 4)); twoCatPanel.add(fromPort, new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 14, 0, 3), 302, 487)); twoCatPanel.add(toPort, new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 14, 0, 3), 302, 487)); //this.getContentPane().add(twoCatPanel);//add visual display leftPanel.setLayout(new GridBagLayout()); functorObjectArea.setEditable(false);//to prevent the user from just typing here functorArrowArea.setEditable(false); /* leftPanel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,10,0,0), 0, 0)); //functor name leftPanel.add(actualName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,10,0,0), 0, 0)); //actual name */ leftPanel.add(objects, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,0,0,0), 0, 0));//objects label leftPanel.add(functorObject, new GridBagConstraints(0, 4, 2, 1, 1.0, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10,10,10,10), 0, 0));//object scroller leftPanel.add(arrows, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,0,0,0), 0, 0));//arrow label leftPanel.add(functorArrow, new GridBagConstraints(0, 6, 2, 1, 1.0, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10,10,10,10), 0, 0));//arrow scroller tabbedPane.add("Two Cat Display",twoCatPanel); tabbedPane.add("Diagram Display",diagramPanel); bigPanel.setLayout(new GridBagLayout()); bigPanel.add(leftPanel,new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 200, 0));//arrow scroller bigPanel.add(tabbedPane,new GridBagConstraints(1, 0, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));//arrow scroller //this one will be changed intot the panel that has switching between cards this.getContentPane().add(bigPanel); setJMenuBar(createJMenuBar()); //...Then set the window size or call pack... setSize(640,480); functorFromCanvas.update(false); functorToCanvas.update(false); gc.update(false); } public JMenuBar createJMenuBar() { JMenuBar functorMenuBar = new JMenuBar(); JMenu file_menu = new JMenu("File"); // JMenu tools_menu = new JMenu("Tools"); JMenu settings_menu = new JMenu("Settings"); JMenuItem save_functor = new JMenuItem("Save"); // save_functor.setMnemonic(KeyEvent.VK_S); save_functor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { save_functor_action(); } }); JMenuItem save_as_functor = new JMenuItem("Save As"); save_as_functor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { save_as_functor_action(); } }); JMenuItem close_functor = new JMenuItem("Close"); // close_functor.setMnemonic(KeyEvent.VK_C); close_functor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { close_functor_action(); } }); /* JMenuItem limit = new JMenuItem("Limit?"); limit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { limit_action(); } }); */ file_menu.add(save_functor); file_menu.add(save_as_functor); file_menu.add(close_functor); // tools_menu.add(limit); functorMenuBar.add(file_menu); functorMenuBar.add(settings_menu); // functorMenuBar.add(tools_menu); return functorMenuBar; } // end of createMenuBar() public boolean handleEvent(Event event) { // -------------- A scrolledPanel event; either RESIZE or OFFSET ------------- if(event.target == viewingPanel_)//instanceof ScrolledPanel) { if(event.id == ScrolledPanel.RESIZE) // RESIZE event from panel; update portScroller_ (small box) dimensions { DDimension tmp_dim; tmp_dim = viewingPanel_.getPortSize(); portScroller_.setPortSize(tmp_dim.width, tmp_dim.height); tmp_dim = viewingPanel_.getContentSize(); portScroller_.setContentSize(tmp_dim.width, tmp_dim.height); tmp_dim = viewingPanel_.getOffset(); portScroller_.setOffset(tmp_dim.width, tmp_dim.height); return true; } // end RESIZE if else if(event.id == ScrolledPanel.OFFSET) // OFFSET event; update portScroller_ (small box) offsets { DDimension tmp_dim = viewingPanel_.getOffset(); portScroller_.setOffset(tmp_dim.width, tmp_dim.height); return true; } // end OFFSET else if } // end ScrolledPanel else if // -------------------------------------------------------------------------- // -------------- A scrolledPanel event; either RESIZE or OFFSET ------------- else if(event.target == functorFromPanel)//instanceof ScrolledPanel) { if(event.id == ScrolledPanel.RESIZE) // RESIZE event from panel; update portScroller_ (small box) dimensions { DDimension tmp_dim; tmp_dim = functorFromPanel.getPortSize(); fromPort.setPortSize(tmp_dim.width, tmp_dim.height); tmp_dim = functorFromPanel.getContentSize(); fromPort.setContentSize(tmp_dim.width, tmp_dim.height); tmp_dim = functorFromPanel.getOffset(); fromPort.setOffset(tmp_dim.width, tmp_dim.height); return true; } // end RESIZE if else if(event.id == ScrolledPanel.OFFSET) // OFFSET event; update portScroller_ (small box) offsets { DDimension tmp_dim = functorFromPanel.getOffset(); fromPort.setOffset(tmp_dim.width, tmp_dim.height); return true; } // end OFFSET else if } // end ScrolledPanel else if // -------------------------------------------------------------------------- // -------------- A scrolledPanel event; either RESIZE or OFFSET ------------- else if(event.target == functorToPanel)//instanceof ScrolledPanel) { if(event.id == ScrolledPanel.RESIZE) // RESIZE event from panel; update portScroller_ (small box) dimensions { DDimension tmp_dim; tmp_dim = functorToPanel.getPortSize(); toPort.setPortSize(tmp_dim.width, tmp_dim.height); tmp_dim = functorToPanel.getContentSize(); toPort.setContentSize(tmp_dim.width, tmp_dim.height); tmp_dim = functorToPanel.getOffset(); toPort.setOffset(tmp_dim.width, tmp_dim.height); return true; } // end RESIZE if else if(event.id == ScrolledPanel.OFFSET) // OFFSET event; update portScroller_ (small box) offsets { DDimension tmp_dim = functorToPanel.getOffset(); toPort.setOffset(tmp_dim.width, tmp_dim.height); return true; } // end OFFSET else if } // end ScrolledPanel else if // -------------------------------------------------------------------------- // ----------- A viewportScroller event; either SCROLL or DONE -------------- else if(event.target == portScroller_)//instanceof ViewportScroller) { if(event.id == ViewportScroller.SCROLL) { // display only edges (no objects); then scroll to specified location gc.setWireframe(true); viewingPanel_.scrollTo((int)portScroller_.getOffsetX(),(int)portScroller_.getOffsetY()); } // end SCROLL if else if(event.id == ViewportScroller.DONE) { // display both edges and objects; scroll to specified location gc.setWireframe(false); viewingPanel_.scrollTo((int)portScroller_.getOffsetX(),(int)portScroller_.getOffsetY()); } // end DONE else if } // end ViewportScroller else if // -------------------------------------------------------------------------- //*** // ----------- A viewportScroller event; either SCROLL or DONE -------------- else if(event.target == fromPort)//instanceof ViewportScroller) { if(event.id == ViewportScroller.SCROLL) { // display only edges (no objects); then scroll to specified location functorFromCanvas.setWireframe(true); functorFromPanel.scrollTo((int)fromPort.getOffsetX(),(int)fromPort.getOffsetY()); } // end SCROLL if else if(event.id == ViewportScroller.DONE) { // display both edges and objects; scroll to specified location functorFromCanvas.setWireframe(false); functorFromPanel.scrollTo((int)fromPort.getOffsetX(),(int)fromPort.getOffsetY()); } // end DONE else if } // end ViewportScroller else if // -------------------------------------------------------------------------- // ----------- A viewportScroller event; either SCROLL or DONE -------------- else if(event.target == toPort)//instanceof ViewportScroller) { if(event.id == ViewportScroller.SCROLL) { // display only edges (no objects); then scroll to specified location functorToCanvas.setWireframe(true); functorToPanel.scrollTo((int)toPort.getOffsetX(),(int)toPort.getOffsetY()); } // end SCROLL if else if(event.id == ViewportScroller.DONE) { // display both edges and objects; scroll to specified location functorToCanvas.setWireframe(false); functorToPanel.scrollTo((int)toPort.getOffsetX(),(int)toPort.getOffsetY()); } // end DONE else if } // end ViewportScroller else if // -------------------------------------------------------------------------- //*** // ----------- An AngleControlPanel event; either ANGLE or DONE ------------- else if(event.target instanceof AngleControlPanel) { if(event.id == AngleControlPanel.ANGLE) { // display only edges (no objects); then set desired angles DPoint angles = (DPoint)event.arg; gc.setWireframe(true); gc.setViewAngles(angles.x, angles.y); } // end ANGLE if else if(event.id == AngleControlPanel.DONE) { // display both edges and objects; set desired angles DPoint angles = (DPoint)event.arg; gc.setWireframe(false); gc.setViewAngles(angles.x, angles.y); } // end DONE else if } // end AngleControlPanel else if // -------------------------------------------------------------------------- // call inherited handler return super.handleEvent(event); } private void save_functor_action() { //Save Functor information including current display //of category in file if (functor_.file_ == null) {save_as_functor_action();} else { functorFromCanvas.unselectItems(); functorToCanvas.unselectItems(); gc.unselectItems(); SaveFileWriter save_ = new SaveFileWriter(functor_.file_, this); save_.saveFunctor(); } } private void save_as_functor_action() { functorFromCanvas.unselectItems(); functorToCanvas.unselectItems(); gc.unselectItems(); //Save Category information including current display //of category in file JFileChooser save_file = new JFileChooser(); save_file.addChoosableFileFilter(new FunFilter()); save_file.addChoosableFileFilter(new FglFilter()); save_file.setAcceptAllFileFilterUsed(false); int fileOption = save_file.showSaveDialog(this); if (fileOption == JFileChooser.CANCEL_OPTION) { } else if (fileOption == JFileChooser.APPROVE_OPTION) { int replaceFile = JOptionPane.YES_OPTION; File file_ = save_file.getSelectedFile(); if (file_.exists()) { replaceFile = JOptionPane.showInternalConfirmDialog(this, "File " + file_.getPath() + " already exists.\n Do you want to replace it?", "Save As", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); } /* if (!file_.getName().toLowerCase().endsWith(".fgl") && !file_.getName().toLowerCase().endsWith(".fun")) file_ = new File(file_.getPath() + ".fgl");*/ if (save_file.getFileFilter() instanceof FunFilter) { file_ = new File(file_.getPath() + ".fun"); } else if (save_file.getFileFilter() instanceof FglFilter) { file_ = new File(file_.getPath() + ".fgl"); } if (replaceFile == JOptionPane.YES_OPTION) { SaveFileWriter save_ = new SaveFileWriter(file_, this); save_.saveFunctor(); } } } private void close_functor_action() { int saveOption = JOptionPane.NO_OPTION; if (saveOption == JOptionPane.CANCEL_OPTION) {return;} // don't exit, don't save else if (saveOption == JOptionPane.YES_OPTION) {save_functor_action();} // save and exit else if (saveOption == JOptionPane.NO_OPTION) {} this.dispose(); } /* private void limit_action() { LimitFrame lf = new LimitFrame(this); getDesktopPane().add(lf); // lf.setVisible(true); lf.setLayer(JDesktopPane.MODAL_LAYER); lf.pack(); lf.show(); } */ //start of display graph public boolean displayCategoryGraph(String gmltext, Graph g) // perform a graph update here...need to pass text to graph object { String text = gmltext; StringBufferInputStream stream = new StringBufferInputStream(text); GMLlexer lexer = new GMLlexer(stream); Graph newgraph = null; try { GMLobject GMLgraph = new GMLobject(lexer, null); GMLobject GMLtmp; // If the GML doesn't contain a graph, assume it is a graph. GMLtmp = GMLgraph.getGMLSubObject("graph", GMLobject.GMLlist, false); if (GMLtmp != null) GMLgraph = GMLtmp; newgraph = new Graph(GMLgraph); g.copy(newgraph); } catch(ParseError error) { JOptionPane.showInternalMessageDialog(this, error.getMessage() + " at line " + lexer.getLineNumber() + " at or near \"" + lexer.getStringval() + "\".", "Error", JOptionPane.ERROR_MESSAGE); return true; } catch(IOException error) { JOptionPane.showInternalMessageDialog(this, error.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); return true; } return true; }//end of display graph //displayfunctor puts information into text areas public void displayFunctor(FunctorNode f) //This method displays a functor in the specified text area. The //parameters are F which is the functor to be displayed and t which //is the text area where the functor will be displayed { int i =0,j; //clear display areas functorObjectArea.setText(""); functorArrowArea.setText(""); //Display functor objects for (i=0;i " + f.B.obj[f.f.obj[i]] + "\n"); functorObjectArea.append(f.A.obj[i] + " |--> " + f.B.obj[f.f.obj[i]] + "\n"); //Display functor arrows for (i=0;i "); j = 0; if (f.f.arr[i][j] == -2) functorArrowArea.append("1\n"); else { while (f.f.arr[i][j] != -1) { if (j == 0) functorArrowArea.append("" + f.B.arrow[f.f.arr[i][j]]); else functorArrowArea.append("*" + f.B.arrow[f.f.arr[i][j]]); j++; } } functorArrowArea.append("\n"); } //If all arrows not displayed... if (f.A.num_arrows != 0) { functorArrowArea.append(f.A.arrow[i] + " |--> "); //functorArrowArea.append(F.name + "(" + A.arrow[i] + ") = "); j = 0; if (f.f.arr[i][j] == -2) functorArrowArea.append("1\n"); else { while (f.f.arr[i][j] != -1) { if (j == 0) functorArrowArea.append("" + f.B.arrow[f.f.arr[i][j]]); else functorArrowArea.append("*" + f.B.arrow[f.f.arr[i][j]]); j++; } } functorArrowArea.append("\n"); } else functorArrowArea.append("\n"); }//end of displayfunctor public FunctorNode getFunctor() { return functor_; } }