How to develop a menu-based text editor form.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
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.
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:
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); } }
formActivate()
creates the "File", "Edit", "Style", "Font", "Size" and "Help" menus. Each of the menus contains multiple selections, some of which can be accessed by shortcut key strokes. For example, the "File" menu has a "New" selection that can also be accessed by the shortcut key stroke: CTRL + N.
gPasteMenuItem.disable()
.
addMenuItem()
method to add a regular item, use the addCheckboxMenuItem()
to add a checkbox item.
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()); } }
if
statement checks the environment to ensure that only applications in a browser use this code.
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); }
if
statements to compare it with all possible actions. For each match, it calls an appropriate method. You must write the code for most of the called methods.
closeDialog("")
to close the window. Use agDialog.showMessage()
to display the About dialog box.
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 (); }
Clipboard
.
taNotepad.getSelectedText()
.
Clipboard
by calling cl.setContents()
.
m_menuItem.enable()
.
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 (); }
Clipboard
, obtain the start and end points of the selected text by calling getSelectionStart()
and getSelectionEnd()
respectively.
getText()
.
substring(0, iStart)
creates a substring from the start of the text-area text to the start of the selected text.
substring(iEnd)
creates a substring from the end of the selected text to the end of the text-area text.
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); } }
cl.getContents(this)
to get the contents of the Clipboard
.
getTransferData()
returns the String contents representing the selection.
getSelectionStart()
and getSelectionEnd()
gets the start and end points of the selected text in the text area.
Clipboard
. The following are the three Strings to concatenate: