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
07 import java.awt.event.ActionEvent;
08 import java.util.prefs.Preferences;
09 import javax.swing.JComboBox;
10 import javax.swing.JLabel;
11 import javax.swing.JOptionPane;
12 import javax.swing.JPanel;
13
14 /**
15 * An action behind a "Delete Favorite" menu command. See the comments
16 * in AddFavoriteAction for more information on the action API.
17 */
18 public class DeleteFavoriteAction extends DefaultAction
19 {
20 /**
21 * Deletes an item from the "favorites" menu. This method is
22 * called by the IDE when the user clicks the "Delete Favorite"
23 * menu command.
24 *
25 * The extension.xml specifies this class as the logic for the "Delete
26 * Favorites" menu command.
27 *
28 * @param e The menu click event that occurred in the IDE.
29 */
30 public void actionPerformed(ActionEvent e)
31 {
32 // Start creation of a panel for display in a JOptionPane.
33 JPanel panel = new JPanel();
34 JLabel label = new JLabel("Yo. Which link gets dissed?");
35 JComboBox box = new JComboBox();
36 box.setEditable(false);
37
38 // Grab the node that contains all the user prefs for favorite links.
39 Preferences prefs =
40 Application.get().userNodeForPackage(Application.class).node("favorites");
41
42 // Populate the JComboBox with the individual links
43 int favnum = 0;
44 while (prefs.get("favorites" + (++favnum), null) != null)
45 {
46 box.addItem(prefs.get("favorites" + favnum, null));
47 }
48
49 // Finish creation of the panel for display in the following JOptionPane.
50 panel.add(label);
51 panel.add(box);
52
53 if (JOptionPane.showConfirmDialog(Application.getRootFrame(),
54 panel,
55 "Remove Favorite",
56 JOptionPane.OK_CANCEL_OPTION,
57 JOptionPane.PLAIN_MESSAGE,
58 null) == JOptionPane.OK_OPTION)
59 {
60 // Remove the selected link from the node.
61 int index = box.getSelectedIndex() + 1;
62 prefs.remove("favorites" + index);
63
64 // Shift any remaining links up one in the indexing.
65 while (prefs.get("favorites" + (++index), null) != null)
66 {
67 String value = prefs.get("favorites" + index, null);
68 prefs.remove("favorites" + index);
69 prefs.put("favorites" + (index - 1), value);
70 }
71
72 // Refresh the menu generator to remove the deleted entry.
73 ActionSvc.get().refreshGenerator(FavoritesGenerator.class.getName());
74 }
75 }
76 }
|