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.ActionListener;
11  import javax.swing.Box;
12  import javax.swing.ButtonGroup;
13  import javax.swing.JMenu;
14  import javax.swing.JMenuBar;
15  import javax.swing.JRadioButtonMenuItem;
16  
17  import net.curre.prefcount.PrefCountRegistry;
18  import net.curre.prefcount.bean.Settings;
19  import net.curre.prefcount.event.ChangeLanguageActionListener;
20  import net.curre.prefcount.event.LafMenuItemListener;
21  import net.curre.prefcount.gui.theme.skin.PrefSkin;
22  import static net.curre.prefcount.gui.type.WindowComponent.ABOUT_ACTION;
23  import static net.curre.prefcount.gui.type.WindowComponent.DIVISIBLE_BY_N;
24  import static net.curre.prefcount.gui.type.WindowComponent.DIVISIBLE_IGNORE;
25  import static net.curre.prefcount.gui.type.WindowComponent.HELP_COMMON_ACTION;
26  import static net.curre.prefcount.gui.type.WindowComponent.HELP_COUNT_ACTION;
27  import static net.curre.prefcount.gui.type.WindowComponent.HELP_PREF_ACTION;
28  import static net.curre.prefcount.gui.type.WindowComponent.LENINGRAD;
29  import static net.curre.prefcount.gui.type.WindowComponent.MAIN_3_PLAYERS;
30  import static net.curre.prefcount.gui.type.WindowComponent.MAIN_4_PLAYERS;
31  import static net.curre.prefcount.gui.type.WindowComponent.PRINT_SCORES_ACTION;
32  import static net.curre.prefcount.gui.type.WindowComponent.PRINT_TEMPLATE3_ACTION;
33  import static net.curre.prefcount.gui.type.WindowComponent.PRINT_TEMPLATE4_ACTION;
34  import static net.curre.prefcount.gui.type.WindowComponent.QUIT_ACTION;
35  import static net.curre.prefcount.gui.type.WindowComponent.RESET_SETTINGS_ACTION;
36  import static net.curre.prefcount.gui.type.WindowComponent.SAVE_SETTINGS_ACTION;
37  import static net.curre.prefcount.gui.type.WindowComponent.SOCHINKA;
38  import net.curre.prefcount.service.LafThemeService;
39  import net.curre.prefcount.service.SettingsService;
40  import net.curre.prefcount.util.LocaleExt;
41  import net.curre.prefcount.util.Utilities;
42  
43  /**
44   * Object of this class represents a menu bar that for
45   * a non-Mac OS platform. For the reason of why we can't
46   * use the same menu bar for Mac OS, please, read the
47   * <code>net.curre.prefcount.gui.menu</code> package
48   * description and the <code>AwtMenuBar</code> javadoc.
49   * <p/>
50   * Created date: Jul 28, 2007
51   *
52   * @author Yevgeny Nyden
53   * @see net.curre.prefcount.gui.menu
54   * @see AwtMenuBar
55   */
56  public class SwingMenuBar extends JMenuBar implements PrefCountMenuBar {
57  
58    /** Reference to the language menu. */
59    private JMenu languageMenu;
60  
61    /** Reference to the dialog frame menu item. */
62    private JRadioButtonMenuItem dialogFrameItem;
63  
64    /**
65     * Constructor that initializes necessary
66     * data structures, and creates the menus.
67     *
68     * @throws UnsupportedOperationException When running on Mac OS.
69     */
70    public SwingMenuBar() {
71      if (Utilities.isMacOs()) {
72        throw new UnsupportedOperationException("SwingMenuBar should not be created for Mac OS");
73      }
74  
75      createMenus();
76    }
77  
78    /** {@inheritDoc} */
79    public void refreshLanguageIcon() {
80      languageMenu.setIcon(PrefCountRegistry.getCurrentLocale().getLocaleIcon());
81    }
82  
83    /**
84     * Does nothing.
85     *
86     * @param enabled Enabled value.
87     */
88    public void toggleNextAction(boolean enabled) {
89    }
90  
91    /**
92     * Does nothing.
93     *
94     * @param enabled Enabled value.
95     */
96    public void toggleBackAction(boolean enabled) {
97    }
98  
99    /**
100    * Does nothing.
101    *
102    * @param enabled Enabled value.
103    */
104   public void toggleComputeAction(boolean enabled) {
105   }
106 
107   /** {@inheritDoc} */
108   public void setDialogFrameItemState(boolean isSelected) {
109     this.dialogFrameItem.setSelected(isSelected);
110   }
111 
112   /** Private methods ***********************/
113 
114   /** Creates all necessary menus and menu items. */
115   private void createMenus() {
116 
117     // creating the main menu
118     super.add(createMainMenu());
119 
120     // creating the action menu
121     super.add(createActionMenu());
122 
123     // creating the window menu
124     super.add(createWindowMenu());
125 
126     // creating the help menu
127     super.add(createHelpMenu());
128 
129     // creating the language menu
130     this.languageMenu = createLanguageMenu();
131     super.add(Box.createHorizontalGlue());
132     super.add(this.languageMenu);
133   }
134 
135   /**
136    * Creates the main menu.
137    *
138    * @return created main menu.
139    */
140   private JMenu createMainMenu() {
141 
142     JMenu mainMenu = new JMenu(LocaleExt.getString("pref.mainMenu.main"));
143     LocaleExt.registerComponent(mainMenu, "pref.mainMenu.main");
144 
145     // ..... creating Look and Feel submenu
146     Settings settings = SettingsService.getSettings();
147     final JMenu lafMenu = new JMenu(LocaleExt.getString("pref.mainMenu.look"));
148     LocaleExt.registerComponent(lafMenu, "pref.mainMenu.look");
149 
150     ButtonGroup group = new ButtonGroup();
151     for (final PrefSkin skin : LafThemeService.AVAILABLE_SKINS) {
152       String name = LocaleExt.getString(skin.getNameResourceKey());
153       JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(name);
154       LocaleExt.registerComponent(lafItem, skin.getNameResourceKey());
155       group.add(lafItem);
156       if (skin.getNameResourceKey().equals(settings.getLafSkinId())) {
157         lafItem.setSelected(true);
158       }
159       lafItem.addActionListener(new LafMenuItemListener(skin));
160       lafMenu.add(lafItem);
161     }
162     mainMenu.add(lafMenu);
163 
164     // .....  creating other menu items on the main menu
165     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
166     mainMenu.add(menuItemsBean.getJMenuItem(PRINT_SCORES_ACTION));
167 
168     final JMenu printMenu = new JMenu(LocaleExt.getString("pref.mainMenu.print.templates"));
169     LocaleExt.registerComponent(printMenu, "pref.mainMenu.print.templates");
170     printMenu.add(menuItemsBean.getJMenuItem(PRINT_TEMPLATE3_ACTION));
171     printMenu.add(menuItemsBean.getJMenuItem(PRINT_TEMPLATE4_ACTION));
172     mainMenu.add(printMenu);
173 
174     mainMenu.addSeparator();
175 
176     mainMenu.add(menuItemsBean.getJMenuItem(SAVE_SETTINGS_ACTION));
177     mainMenu.add(menuItemsBean.getJMenuItem(RESET_SETTINGS_ACTION));
178     mainMenu.addSeparator();
179 
180     mainMenu.add(menuItemsBean.getJMenuItem(QUIT_ACTION));
181 
182     return mainMenu;
183   }
184 
185   /**
186    * Creates the action menu.
187    *
188    * @return created action menu.
189    */
190   private JMenu createActionMenu() {
191 
192     JMenu menu = new JMenu(LocaleExt.getString("pref.actionMenu.name"));
193     LocaleExt.registerComponent(menu, "pref.actionMenu.name");
194     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
195 
196     menu.add(menuItemsBean.getJRadioButtonMenuItem(LENINGRAD));
197     menu.add(menuItemsBean.getJRadioButtonMenuItem(SOCHINKA));
198     menu.addSeparator();
199 
200     menu.add(menuItemsBean.getJRadioButtonMenuItem(MAIN_3_PLAYERS));
201     menu.add(menuItemsBean.getJRadioButtonMenuItem(MAIN_4_PLAYERS));
202     menu.addSeparator();
203 
204     menu.add(menuItemsBean.getJRadioButtonMenuItem(DIVISIBLE_IGNORE));
205     menu.add(menuItemsBean.getJRadioButtonMenuItem(DIVISIBLE_BY_N));
206 
207     return menu;
208   }
209 
210   /**
211    * Creates the window menu.
212    *
213    * @return created window menu.
214    */
215   private JMenu createWindowMenu() {
216     final JMenu winMenu = new JMenu(LocaleExt.getString("pref.windowMenu.name"));
217     LocaleExt.registerComponent(winMenu, "pref.windowMenu.name");
218 
219     final JRadioButtonMenuItem scoreboardItem = new JRadioButtonMenuItem(LocaleExt.getString("pref.windowMenu.scoreboard"));
220     scoreboardItem.setSelected(true);
221     scoreboardItem.setEnabled(false);
222     LocaleExt.registerComponent(scoreboardItem, "pref.windowMenu.scoreboard");
223     winMenu.add(scoreboardItem);
224 
225     this.dialogFrameItem = new JRadioButtonMenuItem(LocaleExt.getString("pref.windowMenu.dialog"));
226     this.dialogFrameItem.setSelected(true);
227     LocaleExt.registerComponent(this.dialogFrameItem, "pref.windowMenu.dialog");
228     this.dialogFrameItem.addActionListener(new ActionListener() {
229       public void actionPerformed(ActionEvent event) {
230         boolean selected = SwingMenuBar.this.dialogFrameItem.isSelected();
231         PrefCountRegistry.getInstance().getPlayerDialogFrame().setVisible(selected);
232       }
233     });
234     winMenu.add(this.dialogFrameItem);
235     return winMenu;
236   }
237 
238   /**
239    * Creates the help menu.
240    *
241    * @return created help menu.
242    */
243   private JMenu createHelpMenu() {
244     final JMenu helpMenu = new JMenu(LocaleExt.getString("pref.helpMenu.name"));
245     LocaleExt.registerComponent(helpMenu, "pref.helpMenu.name");
246 
247     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
248     helpMenu.add(menuItemsBean.getJMenuItem(HELP_COUNT_ACTION));
249     helpMenu.add(menuItemsBean.getJMenuItem(HELP_PREF_ACTION));
250     helpMenu.add(menuItemsBean.getJMenuItem(HELP_COMMON_ACTION));
251     helpMenu.add(menuItemsBean.getJMenuItem(ABOUT_ACTION));
252 
253     return helpMenu;
254   }
255 
256   /**
257    * Creates the language menu.
258    *
259    * @return created language menu.
260    */
261   private JMenu createLanguageMenu() {
262     LocaleExt loc = PrefCountRegistry.getCurrentLocale();
263     JMenu menu = new JMenu();
264     menu.setIcon(loc.getLocaleIcon());
265     ButtonGroup group = new ButtonGroup();
266     for (final LocaleExt currLoc : PrefCountRegistry.AVAILABLE_LOCALES) {
267       JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem(currLoc.getDisplayLanguage());
268       rbMenuItem.setIcon(currLoc.getLocaleIcon());
269       if (currLoc.equals(loc)) {
270         rbMenuItem.setSelected(true);
271       }
272       group.add(rbMenuItem);
273       rbMenuItem.addActionListener(
274           new ChangeLanguageActionListener(currLoc.getLocale().getLanguage()));
275       menu.add(rbMenuItem);
276     }
277     return menu;
278   }
279 
280 }