Application Techniques



Building a Text Editor with Menus

How to develop a menu-based text editor form.

About this technique

Details

Category

Java Client Techniques> Menus

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on advanced form topics in the Programmer's Guide

The example implements some basic notepad operations. It shows how to create menus and menu items, implement the menus as a menu bar and as a popup menu, set up the handling of menu item selections, and implement clipboard operations.

Creating menus   Top of page

The following code creates various menus and menu items. Some of the menu items can be accessed by shortcut key strokes. There are two implementations to the menus:

This is the code:

  protected void formActivate() 
  { 
   // Create menus and menuitems 
   Menu fileMenu = new Menu ("File"); 
   addMenuItem (fileMenu, "New", new MenuShortcut (KeyEvent.VK_N)); 
   addMenuItem (fileMenu, "Open", new MenuShortcut (KeyEvent.VK_O)); 
   addMenuItem (fileMenu, "Save", new MenuShortcut (KeyEvent.VK_S)); 
   addMenuItem (fileMenu, "Print", null); 
   fileMenu.addSeparator (); 
   addMenuItem (fileMenu, "Exit", new MenuShortcut (KeyEvent.VK_X)); 
   
   Menu editMenu = new Menu ("Edit"); 
   addMenuItem (editMenu, "Cut", null); 
   addMenuItem (editMenu, "Copy", null); 
   gPasteMenuItem = addMenuItem (editMenu, "Paste", null); 
   gPasteMenuItem.disable (); 
   
   Menu styleMenu = new Menu ("Style"); 
   Menu fontMenu = new Menu ("Font"); 
   // Init font with names of fonts on this platform 
   Toolkit t = Toolkit.getDefaultToolkit (); 
   String [] fontlist = t.getFontList (); 
   for (int i = 0; i < fontlist.length; i ++) 
   { 
      MenuItem fMenu = addMenuItem (fontMenu, fontlist [i], null); 
      fMenu.setActionCommand ("Fontname" + fontlist [i]); 
   } 
   int itemcount = fontMenu.getItemCount (); 
   
   addCheckboxMenuItem(styleMenu, "Bold"); 
   addCheckboxMenuItem(styleMenu, "Italic"); 
   
   Menu sizeMenu = new Menu ("Size"); 
   addMenuItem (sizeMenu, "10", null); 
   addMenuItem (sizeMenu, "12", null); 
   addMenuItem (sizeMenu, "14", null); 
   addMenuItem (sizeMenu, "16", null); 
   
   Menu helpMenu = new Menu ("Help"); 
   addMenuItem (helpMenu, "About", null); 
   
   // Create menus, change title and title bar icon,  
   // but only if we're running as a Java application 
   if (agGeneral.getEnvironment() == agGeneral.ENV_APPLICATION) 
   { 
      // Add the menu bar and set the title on the main form frame 
      Frame fr = getFrame(); 
      fr.setTitle ("SilverStream Notepad"); 
      agGeneral.setIconImage (getImage("Bluecube.gif")); 
   
      MenuBar mbar = new MenuBar (); 
      mbar.add (fileMenu); 
      mbar.add (editMenu); 
      mbar.add (fontMenu); 
      mbar.add (styleMenu); 
      mbar.add (sizeMenu); 
      mbar.add (helpMenu); 
      mbar.setHelpMenu (helpMenu); 
   
      fr.setMenuBar (mbar); 
      lblMenu.setVisible(false); 
   } 
   else 
   { 
      // Application or applet, add in popup menus 
      gPopMenu = new PopupMenu ("Actions"); 
      gPopMenu.add (fileMenu); 
      gPopMenu.add (editMenu); 
      gPopMenu.add (fontMenu); 
      gPopMenu.add (styleMenu); 
      gPopMenu.add (sizeMenu); 
      gPopMenu.add (helpMenu); 
      add (gPopMenu); 
   } 
  } 

Notes about the code

Displaying a popup menu for browser applications   Top of page

The following code allows a popup menu to appear at the location where the user clicks the mouse. This codes ensures that it runs only where the Notepad application runs as an applet in a browser.

  private void handle_frmNotepad_mousePressed(MouseEvent mouseEvent) 
  { 
   // Show popmenu only in applet. For application, use menubar 
   if (agGeneral.getEnvironment() != agGeneral.ENV_APPLICATION) 
   { 
      m_popMenu.show (this,  
          mouseEvent.getX(), mouseEvent.getY()); 
   } 
  } 

Notes about the code

Handling menu item selections   Top of page

The following method contains the code that, given a menu item selection, calls the appropriate operation.

  protected void menuItemSelected(MenuItem menuItem,  
        ActionEvent actionEvent) 
  { 
   String   scommand = actionEvent.getActionCommand(); 
   if (command.equals ("New")) 
      newNotepad(); 
   if (actionEvent.getActionCommand().equals ("Open")) 
      openNotepad(); 
   if (actionEvent.getActionCommand().equals ("Save")) 
      saveNotepad(); 
   if (command.equals ("Print")) 
      printNotepad(); 
   if (command.equals ("Exit")) 
      closeDialog(""); 
   
   if (command.equals ("Cut")) 
      cutNotepad (); 
   if (command.equals ("Copy")) 
      copyNotepad (); 
   if (command.equals ("Paste")) 
      pasteNotepad (); 
   
   if (command.equals ("10")) 
      setNotepadSize ("10"); 
   if (command.equals ("12")) 
      setNotepadSize ("12"); 
   if (command.equals ("14")) 
      setNotepadSize ("14"); 
   if (command.equals ("16")) 
      setNotepadSize ("16"); 
   
   if (command.length() > 8) 
      if (command.substring (0,8).equals ("Fontname")) 
         setNotepadFontname (command.substring (8)); 
   
   if (command.equals ("About")) 
      agDialog.showMessage("SilverStream Notepad Rel 2.0", 
         "Copyright 1998 SilverStream Software Inc."); 
  } 
   
  protected void menuItemSelected(MenuItem menuItem, ActionEvent actionEvent) 
    { 
      String sCommand = actionEvent.getActionCommand(); 
      if (sCommand.equals ("New")) 
        newNotepad(); 
      if (actionEvent.getActionCommand().equals ("Open")) 
        openNotepad(); 
      if (actionEvent.getActionCommand().equals ("Save")) 
        saveNotepad(); 
      if (sCommand.equals ("Print")) 
        printNotepad(); 
      if (sCommand.equals ("Exit")) 
        closeDialog(""); 
       
      if (sCommand.equals ("Cut")) 
        cutNotepad (); 
      if (sCommand.equals ("Copy")) 
        copyNotepad (); 
      if (sCommand.equals ("Paste")) 
        pasteNotepad (); 
         
      if (sCommand.equals ("10")) 
        setNotepadSize ("10"); 
      if (sCommand.equals ("12")) 
        setNotepadSize ("12"); 
      if (sCommand.equals ("14")) 
        setNotepadSize ("14"); 
      if (sCommand.equals ("16")) 
        setNotepadSize ("16"); 
         
      if (sCommand.length() > 8) 
        if (sCommand.substring (0,8).equals ("Fontname")) 
          setNotepadFontname (sCommand.substring (8)); 
         
      if (sCommand.equals ("About")) 
       // Previously declared String variables hold the info.  
        agDialog.showMessage(ABOUT_TITLE, ABOUT_MESSAGE); 
    } 

Notes about the code

Performing the Copy clipboard operation   Top of page

The following code shows the copy operation.

  public void copyNotepad() 
  { 
     // Get the clipboard for this system 
     Toolkit tk = Toolkit.getDefaultToolkit(); 
     Clipboard cl = tk.getSystemClipboard(); 
   
     // get the selected text and place it on the clipboard 
     String sText = taNotepad.getSelectedText(); 
     StringSelection sSelectedText = new StringSelection (sText); 
     cl.setContents (sSelectedText, sSelectedText); 
     m_menuItem.enable (); 
  } 

Notes about the code

Performing the Cut clipboard operation   Top of page

The following code shows the cut operation. The code copies the selected text to the clipboard, using the same technique as the copy operation. It then deletes the selected text from the text area by concatenating all unselected text in the text area, and resetting the text area to the resulting concatenation.

  public void cutNotepad() 
  { 
    // Get the clipboard for this system 
    Toolkit tk = Toolkit.getDefaultToolkit(); 
    Clipboard cl = tk.getSystemClipboard(); 
   
    // get the selected text and place it on the clipboard 
    String sText = taNotepad.getSelectedText(); 
    StringSelection sSelectedText = new StringSelection (sText); 
    cl.setContents (sSelectedText, sSelectedText); 
   
    // delete the text 
    int iStart = taNotepad.getSelectionStart(); 
    int iEnd = taNotepad.getSelectionEnd(); 
    String sWork = taNotepad.getText(); 
    sText = sWork.substring (0, iStart) + sWork.substring (iEnd); 
    taNotepad.setText(sText); 
    m_menuItem.enable (); 
  } 

Notes about the code

Perfoming the Paste clipboard operation   Top of page

The following code shows the paste operation. It pastes the contents of the clipboard to the text area, replacing the selected text (if text has been selected) or inserting the contents at the cursor's position (if no text has been selected). The code gets the start and end points of the selected text and uses them to determine the start and end points of the substrings that it creates and concatenates.

  public void pasteNotepad() 
  { 
    // Get the clipboard for this system 
    Toolkit tk = Toolkit.getDefaultToolkit(); 
    Clipboard cl = tk.getSystemClipboard(); 
         
    // get the selected text from the clipboard 
    try 
    { 
      StringSelection sSelectedText =  
         (StringSelection) cl.getContents (this); 
      String s = (String) sSelectedText.getTransferData 
         (DataFlavor.stringFlavor); 
      int iStart = taNotepad.getSelectionStart(); 
      int iEnd = taNotepad.getSelectionEnd(); 
      String sOriginalText = taNotepad.getText(); 
      String sResultText = sOriginalText.substring (0, iStart)  
         + s + sOriginalText.substring (iEnd); 
      taNotepad.setText(sResultText); 
      m_bNotepadModified = true; 
    } 
    catch (Exception e) 
    { 
      agDialog.displayError(e); 
    } 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.