FavoritesGenerator.java Sample
This topic inludes the source code for the FavoritesGenerator.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.IActionContainer;
06 import com.bea.ide.actions.IActionProxy;
07 import com.bea.ide.actions.IGenerator;
08 import com.bea.ide.core.PreferencesSvc;
09
10 /**
11 * A generator class to generate "favorites" menu items. A generator
12 * class implements IGenerator to create menu items dynamically.
13 * This is in contrast to the menu commands created statically in
14 * an extension.xml file.
15 *
16 * The extension.xml file specifies this class as the generator to use
17 * for the "favorites" menu.
18 */
19 public class FavoritesGenerator implements IGenerator
20 {
21 /**
22 * Called by the IDE to discover whether this generator's actions
23 * should be visible. Some implementation may want to check
24 * the IDE's context to determine the value to return.
25 *
26 * @return <code>true</code> to make the action visible; otherwise,
27 * <code>false</code>.
28 */
29 public boolean isVisible()
30 {
31 return true;
32 }
33
34 /**
35 * Populates the menu items from the entries stored in the "favorites"
36 * node in the $USER_HOME/.workshop.pref file. The <em>menu</em>
37 * received here is specified in this extension's extension.xml. There,
38 * an <action-group> element's generator attribute assigns this class
39 * as the generator to use for a submenu of the "favorites" menu.
40 *
41 * @param menu The menu that should be populated.
42 * @return <code>true</code> if items were added; <code>false</code>
43 * if none were added.
44 */
45 public boolean populate(IActionContainer menu)
46 {
47 /**
48 * The preferences service provides easy access to the .workshop.pref
49 * file so that extensions can use it to store settings.
50 */
51 PreferencesSvc.IPreferencePkg prefs =
52 PreferencesSvc.get().getUserPreferences(Application.get(),
53 Application.class).getPackageForNode("favorites");
54
55 /**
56 * Iterate through the stored "favorites" preferences, adding each
57 * as a menu command.
58 */
59 int favNum = 0;
60 String url;
61 while ((url = prefs.get("favorites" + (++favNum))) != null)
62 {
63 // Create a new UI object to represent "favorites" menu command.
64 IActionProxy ap = new LaunchBrowserAction().getProxy();
65 // Assign a label and URL to the menu UI.
66 ap.putValue(IActionProxy.PROP_Label, url);
67 // Add the new menu to menu received from the IDE.
68 menu.add(ap);
69 }
70
71 /**
72 * Create a "Delete Favorite" menu command and enable it if
73 * there are any favorites.
74 */
75 IActionProxy toggle =
76 ActionSvc.get().getAction(DeleteFavoriteAction.class.getName());
77 if (favNum == 1)
78 {
79 toggle.setEnabled(false);
80 } else
81 {
82 toggle.setEnabled(true);
83 }
84
85 return true;
86 }
87 }
|