# AddFavoriteAction.java Sample

This topic inludes the source code for the AddFavoriteAction.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/IdeDevKit/MenuItems/src/ideExtensions/menuItems/

## Sample Source Code

`01 package ideExtensions.menuItems; 02 03 import com.bea.ide.Application; 04 import com.bea.ide.actions.ActionSvc; 05 import com.bea.ide.actions.DefaultAction; 06 import com.bea.ide.core.PreferencesSvc; 07 08 import java.awt.event.ActionEvent; 09 import javax.swing.JLabel; 10 import javax.swing.JOptionPane; 11 import javax.swing.JPanel; 12 import javax.swing.JTextField; 13 14 /** 15 * An action behind an "Add Favorite" menu command. The command 16 * adds a "favorite" URL menu command to a "Favorites" 17 * menu in the IDE. This class extends DefaultAction, which in turn 18 * implements IAction. Action extensions such as menu commands and 19 * toolbar buttons implement the IAction interface's actionPerformed 20 * method to provide logic for the action. The IDE calls actionPerformed 21 * when the command is selected by the IDE's user. 22 * 23 * The extension.xml file provided in the META-INF folder of the MenuItems 24 * sample specifies this class as the handler for the "Add Favorite" action. 25 */ 26 public class AddFavoriteAction extends DefaultAction 27 { 28 /** 29 * Adds a "favorite" URL to the "Favorites" menu provided by the 30 * MenuItems sample. This method is called by the IDE when the user 31 * clicks the "Favorites -> Add Favorite" menu. 32 * 33 * @param e The menu click event that occurred in the IDE. 34 */ 35 public void actionPerformed(ActionEvent e) 36 { 37 /** 38 * Start creating the user interface for a dialog to prompt the 39 * user for a URL to add. The panel contains the text box, then is 40 * added to a dialog below. 41 */ 42 JPanel panel = new JPanel(); 43 JTextField textField = new JTextField(25); 44 textField.setText("http://"); 45 textField.setCaretPosition(7); 46 JLabel label = new JLabel("Yo. Enter a favorite web link: "); 47 panel.add(label); 48 panel.add(textField); 49 50 /** 51 * Display a simple dialog that includes the panel created above. If 52 * the user clicks OK (rather than Cancel), then proceed with the 53 * following. 54 */ 55 if (JOptionPane.showConfirmDialog(Application.getRootFrame(), 56 panel, 57 "Add Favorite", 58 JOptionPane.OK_CANCEL_OPTION, 59 JOptionPane.PLAIN_MESSAGE, 60 null) == JOptionPane.OK_OPTION) 61 { 62 /** 63 * Use the preferences service to get any favorites already saved 64 * to $USER_HOME/.workshop.pref. 65 */ 66 PreferencesSvc.IPreferencePkg prefs = 67 PreferencesSvc.get().getUserPreferences(Application.get(), 68 Application.class).getPackageForNode("favorites"); 69 70 /** 71 * Favorites are stored in .workshop.pref as <option> elements 72 * whose name attribute is a value such as "favorites1" and value 73 * attribute is the URL. Find the end of the list and add a new entry 74 * with the text in the dialog's text box. 75 */ 76 int favNum = 0; 77 while (prefs.get("favorites" + (++favNum)) != null) ; 78 prefs.put("favorites" + favNum, textField.getText()); 79 80 /** 81 * Use the action service to refresh the menu with the complete list 82 * using the FavoritesGenerator class included in this extension. 83 * See the code in that class for details. 84 */ 85 ActionSvc.get().refreshGenerator(FavoritesGenerator.class.getName()); 86 } 87 } 88 }`