package homework07;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* An application for creating a frame, and setting a menu with menu items that do certain actions.
* The two menu items are Open and Reset--the open is used to open a file for reading, and the reset
* button will redisplay the tree starting at the root.
*
* @author Drake Bennion and Kyle Hiroyasu
* @version March 19, 2014
*/
public class ScrollPaneDemo extends JPanel
{
//an instance variable to hold our file
static File ourFile;
/**
* The application entry point.
*
* @param args ignored
*/
public static void main (String[] args)
{
// Create a frame to hold the scroll pane.
JFrame frame = new JFrame ("Scroll Pane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout (new BorderLayout());
final JPanel topLevelPanel = new JPanel ();
topLevelPanel.setLayout(new GridLayout(1, 2));
topLevelPanel.add(new CoordinatePanel().getEnclosingPane());
/**The following code came mainly from http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
*
* This creates a menu with menu items
*/
//create the variables needed to make the menu
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
JMenuItem openItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("Kyle and Drake's Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"Here is a menu.");
menuBar.add(menu);
//a couple of JMenuItems
menuItem = new JMenuItem("Reset",
KeyEvent.VK_T);
openItem=new JMenuItem("Open", KeyEvent.VK_T);
openItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menu.add(openItem);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This resets the display");
menu.add(menuItem);
//set the menu of frame
frame.setJMenuBar(menuBar);
//add an action listener for the reset button
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//if the file is not null, reset the display by redrawing the tree
if(ourFile!=null)
{
Tree userTree=new Tree();
userTree.parse(ourFile);
topLevelPanel.removeAll();
topLevelPanel.add (new CoordinatePanel (ourFile).getEnclosingPane());
topLevelPanel.revalidate();
}
}
}
);
/**This came from the javadoc tutorial for the filechooser class.
* Adds an actionlistener to our open button of our menu.
*
*/
openItem.addActionListener(new ActionListener()
{
//This only runs when we actually use the Open
@Override
public void actionPerformed(ActionEvent arg0)
{
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Tree files", "tree");
chooser.setFileFilter(filter);
Component parent=null;
int returnVal = chooser.showOpenDialog(parent);
//
if(returnVal == JFileChooser.APPROVE_OPTION)
{
topLevelPanel.removeAll();
ourFile=chooser.getSelectedFile();
ourFile.getName();
topLevelPanel.add (new CoordinatePanel (ourFile).getEnclosingPane());
topLevelPanel.revalidate();
}
}
}
);
frame.setContentPane(topLevelPanel);
// Pack the frame and make it visible.
frame.pack();
frame.setVisible (true);
}
}