import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.util.Date; import java.text.*; import java.net.URL; /** * Text Editor class, creates a menu, toolbar, editor, and status line * */ public class TextEditor extends JFrame implements ActionListener{ // inherit JFrame EditorObject editor = null; JPanel mainPanel = null; BorderLayout layout = null; JLabel statusLine = null; Timer tick = null; /** * This Constructor sets up the window and creates a simple menu */ public TextEditor() { // // setup the frames banner/title bar // super("TextEditor"); // call JFrame's constructor and give it a title setSize(600,400); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // setup a borderlayout // mainPanel = new JPanel(); // create a Panel for the Frames content area layout = new BorderLayout(); // create a borderlayout object to control the format of the Panel mainPanel.setLayout(layout); // tell the Panel about the layout manager setContentPane(mainPanel); // tell the Frame about its content pane // // create a status line // statusLine = new JLabel(" ",JLabel.RIGHT); // create a right justified label object mainPanel.add("South",statusLine); // add it to the bottom tick = new Timer(1000,this); // create a timer to update time every second tick.start(); // start the timer // // create an edit object // editor = new EditorObject(this); // create the editor object (a scrolling pane) mainPanel.add("Center",editor); // add it to the center area // // Create the menus and toolbar // setupMenus(); // build the menus setupToolbar(); // build the toolbars } /** * Listen for timer events */ public void actionPerformed(ActionEvent e) { if(e.getSource() == tick) { // only do the following if the event was generated by the timer we created SimpleDateFormat df = new SimpleDateFormat("h:mm:ss aaa"); // create a date format object Date now = new Date(System.currentTimeMillis()); // creaate a date object based on current time statusLine.setText(df.format(now)); // update the time on the status line } } /********************************************************************* * Setup all the menus */ void setupMenus() { // // Create the menus // JMenuBar mainMenu = new JMenuBar(); // create a menubar // // create the file menu // JMenu fileMenu = new JMenu("File"); // Create the file menu fileMenu.setMnemonic(KeyEvent.VK_F); // set its Alt character sequence addMenuItem(fileMenu, "New", KeyEvent.VK_N, KeyEvent.VK_N,KeyEvent.CTRL_MASK,new FileNewListener(editor)); addMenuItem(fileMenu, "Open", KeyEvent.VK_O, KeyEvent.VK_O,KeyEvent.CTRL_MASK,new FileOpenListener(editor)); addMenuItem(fileMenu, "Save", KeyEvent.VK_S, KeyEvent.VK_S,KeyEvent.CTRL_MASK,new FileSaveListener(editor)); addMenuItem(fileMenu, "SaveAs", KeyEvent.VK_A, new FileSaveAsListener(editor)); fileMenu.addSeparator(); // add a separator between open and exit addMenuItem(fileMenu, "Exit", KeyEvent.VK_X,new FileExitListener()); JMenu editMenu = new JMenu("Edit"); // create the edit menu editMenu.setMnemonic(KeyEvent.VK_E); addMenuItem(editMenu, "Cut", KeyEvent.VK_T, KeyEvent.VK_Q,KeyEvent.CTRL_MASK,new EditCutListener(editor)); addMenuItem(editMenu, "Copy", KeyEvent.VK_C, KeyEvent.VK_C,KeyEvent.CTRL_MASK,new EditCopyListener(editor)); addMenuItem(editMenu, "Paste", KeyEvent.VK_P, KeyEvent.VK_V,KeyEvent.CTRL_MASK,new EditPasteListener(editor)); mainMenu.add(fileMenu); // add the file menu to the main menu bar mainMenu.add(editMenu); // add the edit menu to the main menu bar // // Add it to the frame // setJMenuBar(mainMenu); // tell the frame to use this menu. } /** * This routine creates and adds a menu item to a menu * @param menu reference to a JMenu object to add this item to * @param text text string to appear on the menu * @param mnemonic virtual key to use as the mnemonic for the item e.g. KeyEvent.VK_F * @param acceleratorKey virtual key to use for the keyboard accelerator, e.g. KeyEvent.VK_O * @param acceleratorModifier virtual mask to use as a modifier, eg. KeyEvent.CTRL_MASK * @param listener reference to an object that implements an ActionListener */ void addMenuItem(JMenu menu, String text, int mnemonic, int acceleratorKey, int acceleratorModifier, ActionListener listener) { JMenuItem anItem; anItem = new JMenuItem(text,mnemonic); // create a menu item. anItem.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey,acceleratorModifier)); // define accelerator for open anItem.addActionListener(listener); // tell menuitem what object will listen for it. menu.add(anItem); // add the item to the menu } /** * This routine creates and adds a menu item to a menu, no accelerator * @param menu reference to a JMenu object to add this item to * @param text text string to appear on the menu * @param mnemonic virtual key to use as the mnemonic for the item e.g. KeyEvent.VK_F * @param listener reference to an object that implements an ActionListener */ void addMenuItem(JMenu menu, String text, int mnemonic,ActionListener listener) { JMenuItem anItem; anItem = new JMenuItem(text,mnemonic); // create a menu item. anItem.addActionListener(listener); // tell menuitem what object will listen for it. menu.add(anItem); // add the item to the menu } /******************************************************************************** * Setup the toolbar */ void setupToolbar(){ // // Create a JToolbar and add it to the panel in the top (North) position // JToolBar tools = new JToolBar(JToolBar.HORIZONTAL); // create a horizontal tool bar mainPanel.add("North",tools); // add the tool bar to the panel // // Create the buttons, set an action listner for each, and add them to the toolbar // addImageButton(tools,"new.gif","Create a new document",new FileNewListener(editor)); // add image and text addImageButton(tools,"open.gif","Open a file",new FileOpenListener(editor)); // add image and text addImageButton(tools,"save.gif","Save the file",new FileSaveListener(editor)); // add image and text addImageButton(tools,"copy.gif","Copy to clipboard",new EditCopyListener(editor)); // add image and text addImageButton(tools,"paste.gif","Paste from clipboard",new EditPasteListener(editor)); // add image and text } /** * This method creates and adds an image button to the tool bar * @param bar reference to a JToolBar * @param file filename of the .gif image file to appear on the button * @param tooltip text string to appear when cursor lingers over the button * @param listener reference to an object that implements an ActionListener */ void addImageButton(JToolBar bar, String file, String tooltip,ActionListener listener) { ImageIcon icon = new ImageIcon(file); // create an image based on the file JButton b1 = new JButton(icon); // create the button b1.addActionListener(listener); // tell it who to send events to b1.setToolTipText(tooltip); // tell it what the tip should be bar.add(b1); } /********************************* * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ TextEditor fr = new TextEditor(); // create the TextEditor frame fr.setVisible(true); // allow it to be seen } } /******************************************************************* * Helper Classes, one acts as a listener for each user interface item * * Alternatively you could just implement one actionPerformed routine * in the Frame class, however, that routine would have to do a lot of * work just to figure out what menu item, button, etc, created the event. * this is easier, cleaner, and isolates code for each UI item/ */ /** * Handle the File-Exit menu item */ class FileExitListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.exit(0); } } /** * Handle the File-New menu item */ class FileNewListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ FileNewListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.newFile(); } } /** * Handle the File-Open menu item */ class FileOpenListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ FileOpenListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.open(); } } /** * Handle the File-Save menu item */ class FileSaveListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ FileSaveListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.save(); } } /** * Handle the File-SaveAs menu item */ class FileSaveAsListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ FileSaveAsListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.saveAs(); } } /** * Handle the Edit-Cut menu item */ class EditCutListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ EditCutListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.cut(); } } /** * Handle the Edit-Copy menu item */ class EditCopyListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ EditCopyListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.copy(); } } /** * Handle the Edit-paste menu item */ class EditPasteListener implements ActionListener{ EditorObject theEditor = null; /** * create the listener object, remember reference to the editor * @param aEditor reference to an EditorObject */ EditPasteListener(EditorObject aEditor){ theEditor = aEditor; } public void actionPerformed(ActionEvent event) { theEditor.paste(); } } ----------------------------------------------------------------------------------- import javax.swing.text.*; import javax.swing.*; import java.awt.*; import java.io.*; /** * Simple text editor object that handles clipboard and file operations * This editor inherits/extends JScrollPane so that it may be placed in * a layout and so that the scroll bars are automatically taken care of. */ public class EditorObject extends JScrollPane { JTextArea tEditor; // keep track of the TextArea object String fileName = ""; // place to remember the name of the file being editied JFrame parent; // parent frame /** * * create a scrollable editor in a JScrollPane * */ EditorObject(JFrame aParent) { super(VERTICAL_SCROLLBAR_ALWAYS,HORIZONTAL_SCROLLBAR_ALWAYS); // tell ScrollPane to leave the bars on tEditor = new JTextArea(); // create an editor component setViewportView(tEditor); // tell the scrollPane to scroll the editor parent = aParent; // remember the parent } /** * edit cut operation */ void cut(){ tEditor.cut(); // tell the editor to cut its selected contents to the clipboard } /** * edit copy operation */ void copy(){ tEditor.copy();// tell the editor to Copy its selected contents to the clipboard } /** * edit paste operation */ void paste(){ tEditor.paste(); // tell the editor to cut its selected contents to the clipboard } /** * File New operation * clear text in the editor object, clear filename, tell parent frame to * clear its title bar */ void newFile(){ tEditor.setText(""); fileName = ""; parent.setTitle(fileName); } /** * file open operation * open a file selection dialog box * if a file was selected then set the filename, parent's title * and tell the edit box to read it in. */ void open(){ FileDialog dialog = new FileDialog(parent, "Open",FileDialog.LOAD); // create a filedialog object for reading dialog.setVisible(true); // set the dialog to be visible if(dialog.getFile() == null) // get the file name selected, if its null then no file was selected, just exit return; fileName = dialog.getDirectory()+dialog.getFile(); // create a full file name parent.setTitle(fileName); // set the name in the parents title bar try{ FileReader input = new FileReader(fileName); // create a reader object for the file tEditor.read(input,fileName); // tell the editor to read the file input.close(); // close the reader object } catch(IOException e) { JOptionPane.showMessageDialog(parent,e.getMessage(),"IOException",JOptionPane.ERROR_MESSAGE); // popup an error message System.out.println("IOException : "+e.getMessage()); // write it to dos window as well (this isn't really necessary) } } /** * file saveAs operation * Open a file selection dialog to allow the user to enter a name * tell the editor to save to the file, set the fileaName and title bar. */ void saveAs(){ FileDialog dialog = new FileDialog(parent, "Open",FileDialog.SAVE); dialog.setVisible(true); if(dialog.getFile() == null) return; fileName = dialog.getDirectory()+dialog.getFile(); // build the file name parent.setTitle(fileName); // tell the parent to set the title bar try{ FileWriter output = new FileWriter(fileName); // create a writer object tEditor.write(output); // tell the editor to write to it output.close(); // close the writer object } catch(IOException e) { JOptionPane.showMessageDialog(parent,e.getMessage(),"IOException",JOptionPane.ERROR_MESSAGE); System.out.println("IOException : "+e.getMessage()); } } /** * file save operation * just tell the editor object to save to the file. If no file selected, then do * the saveAs operation instead. */ void save(){ if(fileName == null || fileName.length() == 0) { // has a file been specified saveAs(); // no do a saveAs return; } else try{ FileWriter output = new FileWriter(fileName); // create a file writer tEditor.write(output); // tell the editor to write to it output.close(); // close the writer } catch(IOException e) { JOptionPane.showMessageDialog(parent,e.getMessage(),"IOException",JOptionPane.ERROR_MESSAGE); System.out.println("IOException : "+e.getMessage()); } } }