View Javadoc

1   /**
2    * This program is free software; you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License version 2 as 
4    * published by the Free Software Foundation;
5    */
6   
7   package net.curre.prefcount.gui.menu;
8   
9   import java.awt.event.ActionEvent;
10  import java.awt.event.KeyEvent;
11  import java.awt.event.ActionListener;
12  import java.awt.Component;
13  import java.util.HashMap;
14  import java.util.ResourceBundle;
15  import java.util.Map;
16  import javax.swing.*;
17  
18  import net.curre.prefcount.gui.theme.skin.PrefSkin;
19  import net.curre.prefcount.util.Utilities;
20  import net.curre.prefcount.util.LocaleExt;
21  import net.curre.prefcount.bean.Settings;
22  import net.curre.prefcount.service.SettingsService;
23  import net.curre.prefcount.service.LafThemeService;
24  import net.curre.prefcount.event.SaveResetSettingsActionListener;
25  import net.curre.prefcount.event.ChangeLanguageActionListener;
26  import net.curre.prefcount.event.AboutActionListener;
27  import net.curre.prefcount.event.StartAskingQuestionsListener;
28  import net.curre.prefcount.event.QuitActionListener;
29  import net.curre.prefcount.event.LafMenuItemListener;
30  import net.curre.prefcount.PrefCountRegistry;
31  
32  /**
33   * Object of this class represents a menu bar that for
34   * a non-Mac OS platform. For the reason of why we can't
35   * use the same menu bar for Mac OS, please, read the
36   * <code>net.curre.prefcount.gui.menu</code> package
37   * description and the <code>AwtMenuBar</code> javadoc.
38   * <p/>
39   * Created date: Jul 28, 2007
40   *
41   * @author Yevgeny Nyden
42   * @see net.curre.prefcount.gui.menu
43   * @see AwtMenuBar
44   */
45  public class SwingMenuBar extends JMenuBar implements PrefCountMenuBar {
46  
47    /**
48     * Map that holds menu items and buttons label resource
49     * keys to button mappings for all menu items and buttons.
50     */
51    private Map<String, AbstractButton> itemsResouceKeysMap;
52  
53    /**
54     * Map that holds menu accelerator resource keys to menu items
55     * mappings for all menu items that have locale specific shortcuts.
56     */
57    private Map<String, JMenuItem> menuAcceleratorsMap;
58  
59    /** Reference to the action menu. */
60    private JMenu actionMenu;
61  
62    /** Reference to the language menu. */
63    private JMenu languageMenu;
64  
65    /**
66     * Constructor that initializes necessary
67     * data structures, and creates the menus.
68     *
69     * @throws UnsupportedOperationException When running on Mac OS.
70     */
71    public SwingMenuBar() {
72      if (Utilities.isMacOs()) {
73        throw new UnsupportedOperationException("SwingMenuBar should not be created for Mac OS");
74      }
75      itemsResouceKeysMap = new HashMap<String, AbstractButton>();
76      menuAcceleratorsMap = new HashMap<String, JMenuItem>();
77      createMenus();
78    }
79  
80    /** {@inheritDoc} */
81    public void refreshMenuItemsLabels() {
82      ResourceBundle bundle = ResourceBundle.getBundle("default");
83      // refreshing the menu and menu items labels
84      for (Map.Entry<String, AbstractButton> entry : itemsResouceKeysMap.entrySet()) {
85        entry.getValue().setText(bundle.getString(entry.getKey()));
86      }
87  
88      // refreshing the menu items accelerators
89      for (Map.Entry<String, JMenuItem> entry : menuAcceleratorsMap.entrySet()) {
90        entry.getValue().setAccelerator(KeyStroke.getKeyStroke(
91            bundle.getString(entry.getKey()).charAt(0), ActionEvent.CTRL_MASK));
92      }
93  
94      // refreshing the language icon
95      languageMenu.setIcon(PrefCountRegistry.getCurrentLocale().getEnabledIcon());
96    }
97  
98    /** {@inheritDoc} */
99    public void disableActionMenu() {
100     actionMenu.setEnabled(false);
101     for (Component item : actionMenu.getMenuComponents()) {
102       item.setEnabled(false);
103     }
104     menuAcceleratorsMap.remove("pref.actionMenu.player3.shortcut");
105     menuAcceleratorsMap.remove("pref.actionMenu.player4.shortcut");
106   }
107 
108   /** {@inheritDoc} */
109   public void disableLanguageMenu() {
110     for (Component item : languageMenu.getMenuComponents()) {
111       item.setEnabled(false);
112     }
113     languageMenu.setIcon(PrefCountRegistry.getCurrentLocale().getDisabledIcon());
114   }
115 
116   /**
117    * Does nothing.
118    *
119    * @param enabled Enabled value.
120    */
121   public void toggleNextAction(boolean enabled) {
122   }
123 
124   /**
125    * Does nothing.
126    *
127    * @param enabled Enabled value.
128    */
129   public void toggleBackAction(boolean enabled) {
130   }
131 
132   /**
133    * Does nothing.
134    *
135    * @param enabled Enabled value.
136    */
137   public void toggleComputeAction(boolean enabled) {
138   }
139 
140   /** Private methods ***********************/
141 
142   /** Creates all necessary menus and menu items. */
143   private void createMenus() {
144     ResourceBundle bundle = ResourceBundle.getBundle("default");
145 
146     // CREATING MAIN MENU
147     final JMenu mainMenu = new JMenu(bundle.getString("pref.mainMenu.main"));
148     itemsResouceKeysMap.put("pref.mainMenu.main", mainMenu);
149 
150     // ..... creating Look and Feel submenu
151     Settings settings = SettingsService.getSettings();
152     final JMenu lafMenu = new JMenu(bundle.getString("pref.mainMenu.look"));
153     itemsResouceKeysMap.put("pref.mainMenu.look", lafMenu);
154     ButtonGroup group = new ButtonGroup();
155     for (final PrefSkin skin : LafThemeService.AVAILABLE_SKINS) {
156       String name = bundle.getString(skin.getNameResourceKey());
157       JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(name);
158       itemsResouceKeysMap.put(skin.getNameResourceKey(), lafItem);
159       group.add(lafItem);
160       if (skin.getNameResourceKey().equals(settings.getLafSkinId())) {
161         lafItem.setSelected(true);
162       }
163       lafItem.addActionListener(new LafMenuItemListener(skin));
164       lafMenu.add(lafItem);
165     }
166     mainMenu.add(lafMenu);
167 
168     // .....  creating other menu items on the main menu
169     JMenuItem menuItem = new JMenuItem(bundle.getString("pref.mainMenu.print"));
170     itemsResouceKeysMap.put("pref.mainMenu.print", menuItem);
171     menuItem.setEnabled(false); // TODO - implement printing
172     mainMenu.add(menuItem);
173     mainMenu.addSeparator();
174     menuItem = new JMenuItem(bundle.getString("pref.mainMenu.settings.save"));
175     itemsResouceKeysMap.put("pref.mainMenu.settings.save", menuItem);
176     menuItem.addActionListener(new SaveResetSettingsActionListener(true));
177     menuItem.setAccelerator(KeyStroke.getKeyStroke(
178         bundle.getString("pref.mainMenu.settings.save.shortcut").charAt(0),
179         ActionEvent.CTRL_MASK));
180     menuAcceleratorsMap.put("pref.mainMenu.settings.save.shortcut", menuItem);
181     mainMenu.add(menuItem);
182 
183     menuItem = new JMenuItem(bundle.getString("pref.mainMenu.settings.reset"));
184     itemsResouceKeysMap.put("pref.mainMenu.settings.reset", menuItem);
185     menuItem.addActionListener(new SaveResetSettingsActionListener(false));
186     menuItem.setAccelerator(KeyStroke.getKeyStroke(
187         bundle.getString("pref.mainMenu.settings.reset.shortcut").charAt(0),
188         ActionEvent.CTRL_MASK));
189     menuAcceleratorsMap.put("pref.mainMenu.settings.reset.shortcut", menuItem);
190     mainMenu.add(menuItem);
191 
192     mainMenu.addSeparator();
193     JMenuItem menuItemQuit = new JMenuItem(bundle.getString("pref.mainMenu.quit"));
194     itemsResouceKeysMap.put("pref.mainMenu.quit", menuItemQuit);
195     menuItemQuit.addActionListener(new QuitActionListener());
196     menuItemQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
197                                                        ActionEvent.CTRL_MASK));
198     mainMenu.add(menuItemQuit);
199     super.add(mainMenu);
200 
201     // CREATING ACTION MENU
202     actionMenu = new JMenu(bundle.getString("pref.actionMenu.name"));
203     itemsResouceKeysMap.put("pref.actionMenu.name", actionMenu);
204     JMenuItem acton3PlayersMenuItem = new JMenuItem(bundle.getString("pref.actionMenu.player3"));
205     ActionListener eventListener = new StartAskingQuestionsListener(3);
206     acton3PlayersMenuItem.addActionListener(eventListener);
207     itemsResouceKeysMap.put("pref.actionMenu.player3", acton3PlayersMenuItem);
208     acton3PlayersMenuItem.setAccelerator(KeyStroke.getKeyStroke(
209         bundle.getString("pref.actionMenu.player3.shortcut").charAt(0),
210         ActionEvent.CTRL_MASK));
211     menuAcceleratorsMap.put("pref.actionMenu.player3.shortcut",
212                             acton3PlayersMenuItem);
213     actionMenu.add(acton3PlayersMenuItem);
214 
215     JMenuItem acton4PlayersMenuItem = new JMenuItem(bundle.getString("pref.actionMenu.player4"));
216     eventListener = new StartAskingQuestionsListener(4);
217     acton4PlayersMenuItem.addActionListener(eventListener);
218     itemsResouceKeysMap.put("pref.actionMenu.player4", acton4PlayersMenuItem);
219     acton4PlayersMenuItem.setAccelerator(KeyStroke.getKeyStroke(
220         bundle.getString("pref.actionMenu.player4.shortcut").charAt(0),
221         ActionEvent.CTRL_MASK));
222     menuAcceleratorsMap.put("pref.actionMenu.player4.shortcut",
223                             acton4PlayersMenuItem);
224     actionMenu.add(acton4PlayersMenuItem);
225     super.add(actionMenu);
226 
227     // CREATING HELP MENU
228     final JMenu helpMenu = new JMenu(bundle.getString("pref.helpMenu.name"));
229     itemsResouceKeysMap.put("pref.helpMenu.name", helpMenu);
230     menuItem = new JMenuItem(bundle.getString("pref.helpMenu.count"));
231     itemsResouceKeysMap.put("pref.helpMenu.count", menuItem);
232     menuItem.setEnabled(false); // TODO: implement counting info
233     helpMenu.add(menuItem);
234     menuItem = new JMenuItem(bundle.getString("pref.helpMenu.about"));
235     itemsResouceKeysMap.put("pref.helpMenu.about", menuItem);
236     menuItem.addActionListener(new AboutActionListener());
237     helpMenu.add(menuItem);
238     super.add(helpMenu);
239 
240     // CREATING LANGUAGE MENU
241     LocaleExt loc = PrefCountRegistry.getCurrentLocale();
242     languageMenu = new JMenu();
243     languageMenu.setIcon(loc.getEnabledIcon());
244     group = new ButtonGroup();
245     for (final LocaleExt currLoc : PrefCountRegistry.AVAILABLE_LOCALES) {
246       JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem(currLoc.getDisplayLanguage());
247       rbMenuItem.setIcon(currLoc.getEnabledIcon());
248       if (currLoc.equals(loc)) {
249         rbMenuItem.setSelected(true);
250       }
251       group.add(rbMenuItem);
252       rbMenuItem.addActionListener(
253           new ChangeLanguageActionListener(currLoc.getLocale().getLanguage()));
254       languageMenu.add(rbMenuItem);
255     }
256 
257     super.add(Box.createHorizontalGlue());
258     super.add(languageMenu);
259   }
260 
261 }