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 as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.gui.menu;
16  
17  import java.awt.CheckboxMenuItem;
18  import java.awt.Menu;
19  import java.awt.MenuBar;
20  import java.awt.MenuItem;
21  import java.awt.event.ItemEvent;
22  import java.awt.event.ItemListener;
23  
24  import net.curre.prefcount.PrefCountRegistry;
25  import net.curre.prefcount.bean.Settings;
26  import net.curre.prefcount.event.ChangeLanguageActionListener;
27  import net.curre.prefcount.event.LafMenuItemListener;
28  import static net.curre.prefcount.gui.menu.PrefCountMenuBar.MenuBarType.MAIN_WINDOW;
29  import static net.curre.prefcount.gui.menu.PrefCountMenuBar.MenuBarType.PLAYER_DIALOG;
30  import net.curre.prefcount.gui.theme.skin.PrefSkin;
31  import static net.curre.prefcount.gui.type.WindowComponent.*;
32  import net.curre.prefcount.service.LafThemeService;
33  import net.curre.prefcount.service.SettingsService;
34  import net.curre.prefcount.util.LocaleExt;
35  import net.curre.prefcount.util.Utilities;
36  
37  /**
38   * Object of this class represents a menu bar for Mac OS.
39   * This menu bar is assumed to be  placed at the top fo the
40   * screen. Other platforms are not supported by this class
41   * just because swing menu bar is a better option when not
42   * on Mac OS.
43   * <p/>
44   * Note that we are using awt menu bar because swing menu bar
45   * is not compatible with the substance look-and-feel library
46   * when running on Mac.
47   * <p/>
48   * Created date: Jan 24, 2008
49   *
50   * @author Yevgeny Nyden
51   */
52  public class AwtMenuBar extends MenuBar implements PrefCountMenuBar {
53  
54    /** <code>serialVersionUID</code> value for serialization. */
55    private static final long serialVersionUID = 9188705200836157181L;
56  
57    /** Menu item 'next' on the action menu. */
58    private MenuItem actonNextMenuItem;
59  
60    /** Menu item 'back' on the action menu. */
61    private MenuItem actonBackMenuItem;
62  
63    /** Reference to the dialog frame menu item. */
64    private CheckboxMenuItem dialogFrameItem;
65  
66    /**
67     * Constructor that sets the type of this menu bar,
68     * initializes necessary data structures, and creates the menus.
69     *
70     * @param menuBarType Type of this menu bar (for which frame it was created).
71     * @throws IllegalArgumentException      If menu bar type is not supported.
72     * @throws UnsupportedOperationException When running on not Mac OS.
73     */
74    public AwtMenuBar(MenuBarType menuBarType) {
75  
76      if (menuBarType != MAIN_WINDOW && menuBarType != PLAYER_DIALOG) {
77        throw new IllegalArgumentException("Unsupported menu bar type: " + menuBarType);
78      }
79      if (Utilities.isMacOs() == false) {
80        throw new UnsupportedOperationException("AwtMenuBar should only be created for Mac OS");
81      }
82  
83      createMenus(menuBarType);
84    }
85  
86    /** Does nothing. */
87    public void refreshLanguageIcon() {
88    }
89  
90    /** {@inheritDoc} */
91    public void toggleNextAction(boolean enabled) {
92      actonNextMenuItem.setEnabled(enabled);
93    }
94  
95    /** {@inheritDoc} */
96    public void toggleBackAction(boolean enabled) {
97      actonBackMenuItem.setEnabled(enabled);
98    }
99  
100   /** {@inheritDoc} */
101   public void toggleComputeAction(boolean enabled) {
102   }
103 
104   /** {@inheritDoc} */
105   public void setDialogFrameItemState(boolean isSelected) {
106     this.dialogFrameItem.setState(isSelected);
107   }
108 
109   /** Private methods ***********************/
110 
111   /**
112    * Creates all necessary menus and menu items.
113    *
114    * @param menuBarType Type of the menu bar to create
115    *                    (indicates for which window).
116    */
117   private void createMenus(MenuBarType menuBarType) {
118 
119     // creating the main menu
120     if (menuBarType == MAIN_WINDOW) {
121       super.add(createMainMenu());
122     }
123 
124     // creating the action menu
125     super.add(createActionMenu(menuBarType));
126 
127     // creating the window menu
128     super.add(createWindowMenu());
129 
130     // creating the help menu
131     Menu languageMenu = createLanguageMenu(menuBarType);
132     if (languageMenu != null) {
133       super.add(languageMenu);
134     }
135 
136     // creating the help menu
137     super.add(createHelpMenu(menuBarType));
138   }
139 
140   /**
141    * Creates the main menu.
142    *
143    * @return created main menu.
144    */
145   private Menu createMainMenu() {
146 
147     Menu mainMenu = new Menu(LocaleExt.getString("pref.mainMenu.main"));
148     LocaleExt.registerComponent(mainMenu, "pref.mainMenu.main");
149 
150     // ..... creating Look and Feel submenu
151     Settings settings = SettingsService.getSettings();
152     Menu lafMenu = new Menu(LocaleExt.getString("pref.mainMenu.look"));
153     LocaleExt.registerComponent(lafMenu, "pref.mainMenu.look");
154 
155     AwtCheckboxMenuGroup group = new AwtCheckboxMenuGroup();
156     for (final PrefSkin skin : LafThemeService.AVAILABLE_SKINS) {
157       CheckboxMenuItem lafItem = new CheckboxMenuItem(LocaleExt.getString(skin.getNameResourceKey()));
158       LocaleExt.registerComponent(lafItem, skin.getNameResourceKey());
159       if (skin.getNameResourceKey().equals(settings.getLafSkinId())) {
160         lafItem.setState(true);
161       }
162       lafItem.addItemListener(new LafMenuItemListener(skin));
163       group.addItemToGroup(lafItem);
164       lafMenu.add(lafItem);
165     }
166     mainMenu.add(lafMenu);
167 
168     // .....  creating other menu items on the main menu
169     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
170     mainMenu.add(menuItemsBean.getMenuItem(PRINT_SCORES_ACTION));
171 
172     final Menu printMenu = new Menu(LocaleExt.getString("pref.mainMenu.print.templates"));
173     LocaleExt.registerComponent(printMenu, "pref.mainMenu.print.templates");
174     printMenu.add(menuItemsBean.getMenuItem(PRINT_TEMPLATE3_ACTION));
175     printMenu.add(menuItemsBean.getMenuItem(PRINT_TEMPLATE4_ACTION));
176     mainMenu.add(printMenu);
177 
178     mainMenu.addSeparator();
179 
180     mainMenu.add(menuItemsBean.getMenuItem(SAVE_SETTINGS_ACTION));
181     mainMenu.add(menuItemsBean.getMenuItem(RESET_SETTINGS_ACTION));
182 
183     return mainMenu;
184   }
185 
186   /**
187    * Creates the action menu.
188    *
189    * @param menuBarType Type of the menu bar to create
190    *                    (indicates for which window).
191    * @return created action menu.
192    */
193   private Menu createActionMenu(MenuBarType menuBarType) {
194 
195     Menu actionMenu = new Menu(LocaleExt.getString("pref.actionMenu.name"));
196     LocaleExt.registerComponent(actionMenu, "pref.actionMenu.name");
197     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
198 
199     // action menu is different depending on the window
200     switch (menuBarType) {
201 
202       case MAIN_WINDOW:
203 
204         actionMenu.add(menuItemsBean.getRadioButtonMenuItem(LENINGRAD));
205         actionMenu.add(menuItemsBean.getRadioButtonMenuItem(SOCHINKA));
206         actionMenu.addSeparator();
207 
208         actionMenu.add(menuItemsBean.getRadioButtonMenuItem(MAIN_3_PLAYERS));
209         actionMenu.add(menuItemsBean.getRadioButtonMenuItem(MAIN_4_PLAYERS));
210         actionMenu.addSeparator();
211 
212         actionMenu.add(menuItemsBean.getRadioButtonMenuItem(DIVISIBLE_IGNORE));
213         actionMenu.add(menuItemsBean.getRadioButtonMenuItem(DIVISIBLE_BY_N));
214         break;
215 
216       case PLAYER_DIALOG:
217 
218         this.actonNextMenuItem = menuItemsBean.getMenuItem(DIALOG_FORWARD);
219         actionMenu.add(this.actonNextMenuItem);
220 
221         this.actonBackMenuItem = menuItemsBean.getMenuItem(DIALOG_BACK);
222         this.actonBackMenuItem.setEnabled(false);
223         actionMenu.add(this.actonBackMenuItem);
224         break;
225     }
226 
227     return actionMenu;
228   }
229 
230   /**
231    * Creates the window menu.
232    *
233    * @return created window menu.
234    */
235   private Menu createWindowMenu() {
236     final Menu winMenu = new Menu(LocaleExt.getString("pref.windowMenu.name"));
237     LocaleExt.registerComponent(winMenu, "pref.windowMenu.name");
238 
239     final CheckboxMenuItem scoreboardItem = new CheckboxMenuItem(LocaleExt.getString("pref.windowMenu.scoreboard"));
240     scoreboardItem.setState(true);
241     scoreboardItem.setEnabled(false);
242     LocaleExt.registerComponent(scoreboardItem, "pref.windowMenu.scoreboard");
243     winMenu.add(scoreboardItem);
244 
245     this.dialogFrameItem = new CheckboxMenuItem(LocaleExt.getString("pref.windowMenu.dialog"));
246     this.dialogFrameItem.setState(true);
247     LocaleExt.registerComponent(this.dialogFrameItem, "pref.windowMenu.dialog");
248     this.dialogFrameItem.addItemListener(new ItemListener() {
249       public void itemStateChanged(ItemEvent event) {
250         boolean selected = AwtMenuBar.this.dialogFrameItem.getState();
251         PrefCountRegistry registry = PrefCountRegistry.getInstance();
252         registry.getPlayerDialogFrame().setVisible(selected);
253         registry.getMainWindow().getPrefCountMenuBar().setDialogFrameItemState(selected);
254         PrefCountMenuBar menuBar = registry.getPlayerDialogFrame().getPrefCountMenuBar();
255         if (menuBar != null) {
256           menuBar.setDialogFrameItemState(selected);
257         }
258       }
259     });
260     winMenu.add(this.dialogFrameItem);
261     return winMenu;
262   }
263 
264   /**
265    * Creates the language menu.
266    *
267    * @param menuBarType Type of the menu bar to create
268    *                    (indicates for which window).
269    * @return created language menu.
270    */
271   private Menu createLanguageMenu(MenuBarType menuBarType) {
272     if (menuBarType == MAIN_WINDOW) {
273       Menu languageMenu = new Menu(LocaleExt.getString("pref.langMenu.name"));
274       LocaleExt.registerComponent(languageMenu, "pref.langMenu.name");
275       LocaleExt loc = PrefCountRegistry.getCurrentLocale();
276       AwtCheckboxMenuGroup group = new AwtCheckboxMenuGroup();
277       for (final LocaleExt currLoc : PrefCountRegistry.AVAILABLE_LOCALES) {
278         CheckboxMenuItem rbMenuItem = new CheckboxMenuItem(currLoc.getDisplayLanguage());
279         if (currLoc.equals(loc)) {
280           rbMenuItem.setState(true);
281         }
282         rbMenuItem.addItemListener(new ChangeLanguageActionListener(currLoc.getLocale().getLanguage()));
283         group.addItemToGroup(rbMenuItem);
284         languageMenu.add(rbMenuItem);
285       }
286       return languageMenu;
287     }
288     return null;
289   }
290 
291   /**
292    * Creates the help menu.
293    *
294    * @param menuBarType type of the menu.
295    * @return created help menu.
296    */
297   private Menu createHelpMenu(MenuBarType menuBarType) {
298 
299     Menu helpMenu = new Menu(LocaleExt.getString("pref.helpMenu.name"));
300     LocaleExt.registerComponent(helpMenu, "pref.helpMenu.name");
301     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
302 
303     if (menuBarType == MAIN_WINDOW) {
304       helpMenu.add(menuItemsBean.getMenuItem(HELP_COUNT_ACTION));
305       helpMenu.add(menuItemsBean.getMenuItem(HELP_PREF_ACTION));
306       helpMenu.add(menuItemsBean.getMenuItem(HELP_COMMON_ACTION));
307       helpMenu.add(menuItemsBean.getMenuItem(ABOUT_ACTION));
308 
309     } else {
310       helpMenu.add(menuItemsBean.getMenuItem(HELP_COUNT_ACTION2));
311       helpMenu.add(menuItemsBean.getMenuItem(HELP_PREF_ACTION2));
312       helpMenu.add(menuItemsBean.getMenuItem(HELP_COMMON_ACTION2));
313       helpMenu.add(menuItemsBean.getMenuItem(ABOUT_ACTION2));
314 
315     }
316 
317     return helpMenu;
318   }
319 
320 }