Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/zazof/jteg/gui/MenuBar.java


1   package org.zazof.jteg.gui;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.event.*;
6   import java.util.Vector;
7   import java.util.Hashtable;
8   import org.zazof.jteg.*;
9   
10  
11  /**
12   *  This class represents the Menubar of the main GUI window.
13   *
14   *  Implements: Languagelistener for adapting to current Language<br>
15   *  Implements: StateListener for enabling/disabling MenuItems to current State<br>
16   *
17   *  @author Yves Vandewoude (stcham)
18   *    @author Jef De Geeter (zazof)
19   *
20   *  @date Januari 2001
21   */
22  
23  
24  public class MenuBar extends JMenuBar implements LanguageListener, StateListener{
25  
26    public MenuBar(MainFrame frame){
27      super();
28      $mainFrame = frame;
29  
30      // Create menuItems
31      createMenuItems();
32      
33      // Initialise the hashtable that contains the state of all the menuitems for the different states
34      initStateTables();
35  
36      // Construct MenuBar itself
37  
38      add($gameMenu);
39      add($actionsMenu);
40      add($viewMenu);
41      add($languagesMenu);
42      add($settingsMenu);
43      add($helpMenu);
44      
45      // Set the correct enable/disable state for all buttons
46      enableMenuItemsForState(JTEGStateMachine.getInstance().getCurrentState());
47      
48    }
49  
50  
51     /*
52      *  Initializes the different statetable of the menubar. Each Menu has a statetable for its items.<br>
53      *  This table contains the state of the items for all the states in the statemachine.
54      */
55    private void initStateTables()
56      {
57      initGameMenuStateTable();
58      initActionMenuStateTable();
59      initViewMenuStateTable();
60      // LanguagesMenu, SettingsMenu and HelpMenu doen't need a state, since they are always fully enabled
61    }
62    
63    private void initGameMenuStateTable()
64      {
65        // {Connect, Disconnect, Exit}
66        boolean[] disconnected = { true, false, true};
67        boolean[] connecting = {false, false, true};
68        boolean[] choosingcolors = {false, false, true};
69        boolean[] connected = {false, true,  true};
70        boolean[] observing = {false, true,  true};
71        boolean[] gamestarted = {false, true,  true};
72        boolean[] exchangingcards = {false, true,  true};
73        boolean[] placingarmies = {false, true,  true};
74        boolean[] armiesplaced = {false, true,  true};
75        boolean[] attacking = {false, true,  true};
76        boolean[] movingafterattack = {false, true,  true};
77        boolean[] regrouping = {false, true,  true};
78        boolean[] armiesregrouped = {false, true,  true};
79        boolean[] gettingcard = {false, true,  true};
80        boolean[] waitingforotherplayers = {false, true,  true};
81        
82        $gameMenuStateTable.put(new Integer(JTEGStateMachine.DISCONNECTED), disconnected);
83        $gameMenuStateTable.put(new Integer(JTEGStateMachine.CONNECTING), connecting);
84        $gameMenuStateTable.put(new Integer(JTEGStateMachine.CHOOSING_COLORS), choosingcolors);      
85        $gameMenuStateTable.put(new Integer(JTEGStateMachine.CONNECTED), connected);
86        $gameMenuStateTable.put(new Integer(JTEGStateMachine.OBSERVING), observing);
87        $gameMenuStateTable.put(new Integer(JTEGStateMachine.GAME_STARTED), gamestarted);      
88        $gameMenuStateTable.put(new Integer(JTEGStateMachine.EXCHANGING_CARDS), exchangingcards);
89        $gameMenuStateTable.put(new Integer(JTEGStateMachine.PLACING_ARMIES), placingarmies);
90        $gameMenuStateTable.put(new Integer(JTEGStateMachine.ARMIES_PLACED), armiesplaced);  
91        $gameMenuStateTable.put(new Integer(JTEGStateMachine.ATTACKING), attacking);
92        $gameMenuStateTable.put(new Integer(JTEGStateMachine.MOVING_AFTER_ATTACK), movingafterattack);
93        $gameMenuStateTable.put(new Integer(JTEGStateMachine.REGROUPING_ARMIES), regrouping);  
94        $gameMenuStateTable.put(new Integer(JTEGStateMachine.ARMIES_REGROUPED), armiesregrouped);
95        $gameMenuStateTable.put(new Integer(JTEGStateMachine.GETTING_CARD), gettingcard);
96        $gameMenuStateTable.put(new Integer(JTEGStateMachine.WAITING_FOR_OTHER_PLAYERS), waitingforotherplayers);  
97      }
98  
99  
100   private void initActionMenuStateTable()
101     {
102       // {Start, SendArmies, UpdateMap, Reattack, ResetAttack, RegroupArmies, GetCard, EndTurn, Surrender}
103       
104       // Note, some of those items have not yet been implemented, they are always disabled for obvious reasons.
105       
106       boolean[] disconnected = {false, false, false, false, false,false, false, false, false};
107       boolean[] connecting = {false, false, false, false, false,false, false, false, false};
108       boolean[] choosingcolors = {false, false, false, false, false,false, false, false, false};
109       boolean[] connected = {true,  false,false, false, false, false, false, false, false};
110       boolean[] observing = {false, false, false, false, false,false, false, false, false};
111       boolean[] gamestarted = {false, false, false, false, false,false, false, false, false};    
112       boolean[] exchangingcards = {false, false,false, false, false, false, false, false, false};
113       boolean[] placingarmies = {false, false,false, false, false, false, false, false, false};      
114       boolean[] armiesplaced = {false, true,false, false, false, false, false, false, false};
115       boolean[] attacking = {false, false, false, false, false,true, true, true, false};
116       boolean[] movingafterattack = {false, false, false, false, false,true, true, true, false};
117       boolean[] regrouping = {false, false, false, false, false,false, true, true, false};
118       boolean[] armiesregrouped = {false, false,false, false, false, false, true, true, false};
119       boolean[] gettingcard = {false, false,false, false, false, false, false, true, false};
120       boolean[] waitingforotherplayers = {false, false,false, false, false, false, false, false, false};
121       
122       $actionMenuStateTable.put(new Integer(JTEGStateMachine.DISCONNECTED), disconnected);
123       $actionMenuStateTable.put(new Integer(JTEGStateMachine.CONNECTING), connecting);
124       $actionMenuStateTable.put(new Integer(JTEGStateMachine.CHOOSING_COLORS), choosingcolors);      
125       $actionMenuStateTable.put(new Integer(JTEGStateMachine.CONNECTED), connected);
126       $actionMenuStateTable.put(new Integer(JTEGStateMachine.OBSERVING), observing);
127       $actionMenuStateTable.put(new Integer(JTEGStateMachine.GAME_STARTED), gamestarted);      
128       $actionMenuStateTable.put(new Integer(JTEGStateMachine.EXCHANGING_CARDS), exchangingcards);
129       $actionMenuStateTable.put(new Integer(JTEGStateMachine.PLACING_ARMIES), placingarmies);
130       $actionMenuStateTable.put(new Integer(JTEGStateMachine.ARMIES_PLACED), armiesplaced);  
131       $actionMenuStateTable.put(new Integer(JTEGStateMachine.ATTACKING), attacking);
132       $actionMenuStateTable.put(new Integer(JTEGStateMachine.MOVING_AFTER_ATTACK), movingafterattack);
133       $actionMenuStateTable.put(new Integer(JTEGStateMachine.REGROUPING_ARMIES), regrouping);  
134       $actionMenuStateTable.put(new Integer(JTEGStateMachine.ARMIES_REGROUPED), armiesregrouped);
135       $actionMenuStateTable.put(new Integer(JTEGStateMachine.GETTING_CARD), gettingcard);
136       $actionMenuStateTable.put(new Integer(JTEGStateMachine.WAITING_FOR_OTHER_PLAYERS), waitingforotherplayers);  
137     }
138 
139   private void initViewMenuStateTable()
140     {
141       // {Cards, Mission}
142 
143       boolean[] disconnected = {false, false};
144       boolean[] connecting = {false, false};
145       boolean[] choosingcolors = {false, false};
146       boolean[] connected = {true, false};
147       boolean[] observing = {false, true};
148       boolean[] gamestarted = {true, true};
149       boolean[] exchangingcards = {true, true};
150       boolean[] placingarmies = {true, true};
151       boolean[] armiesplaced = {true, true};
152       boolean[] attacking = {true, true};
153       boolean[] movingafterattack = {true, true};
154       boolean[] regrouping = {true, true};
155       boolean[] armiesregrouped = {true, true};
156       boolean[] gettingcard = {true, true};
157       boolean[] waitingforotherplayers = {true, true};
158       
159       $viewMenuStateTable.put(new Integer(JTEGStateMachine.DISCONNECTED), disconnected);
160       $viewMenuStateTable.put(new Integer(JTEGStateMachine.CONNECTING), connecting);
161       $viewMenuStateTable.put(new Integer(JTEGStateMachine.CHOOSING_COLORS), choosingcolors);      
162       $viewMenuStateTable.put(new Integer(JTEGStateMachine.CONNECTED), connected);
163       $viewMenuStateTable.put(new Integer(JTEGStateMachine.OBSERVING), observing);
164       $viewMenuStateTable.put(new Integer(JTEGStateMachine.GAME_STARTED), gamestarted);      
165       $viewMenuStateTable.put(new Integer(JTEGStateMachine.EXCHANGING_CARDS), exchangingcards);
166       $viewMenuStateTable.put(new Integer(JTEGStateMachine.PLACING_ARMIES), placingarmies);
167       $viewMenuStateTable.put(new Integer(JTEGStateMachine.ARMIES_PLACED), armiesplaced);  
168       $viewMenuStateTable.put(new Integer(JTEGStateMachine.ATTACKING), attacking);
169       $viewMenuStateTable.put(new Integer(JTEGStateMachine.MOVING_AFTER_ATTACK), movingafterattack);
170       $viewMenuStateTable.put(new Integer(JTEGStateMachine.REGROUPING_ARMIES), regrouping);  
171       $viewMenuStateTable.put(new Integer(JTEGStateMachine.ARMIES_REGROUPED), armiesregrouped);
172       $viewMenuStateTable.put(new Integer(JTEGStateMachine.GETTING_CARD), gettingcard);
173       $viewMenuStateTable.put(new Integer(JTEGStateMachine.WAITING_FOR_OTHER_PLAYERS), waitingforotherplayers);  
174     }
175 
176 
177    /*
178     *  Sets the state of the buttons according to the current state.
179     *  
180     *  @param stateToSet The current state (see constants in JTEGStateMachine)
181     */
182   private void enableMenuItemsForState(int stateToSet)
183     {
184     // Game Menu
185     boolean[] gameMenuItemStates = (boolean[]) $gameMenuStateTable.get(new Integer(stateToSet));
186     boolean[] actionMenuItemStates = (boolean[]) $actionMenuStateTable.get(new Integer(stateToSet));
187     boolean[] viewMenuItemStates = (boolean[]) $viewMenuStateTable.get(new Integer(stateToSet));
188     
189     // GameMenu Items
190     // {Connect, Disconnect, Exit}
191     $gameMenuConnectItem.setEnabled(gameMenuItemStates[0]);
192     $gameMenuDisconnectItem.setEnabled(gameMenuItemStates[1]);
193     $gameMenuExitItem.setEnabled(gameMenuItemStates[2]);
194     
195     // ActionMenu Items
196     // {Start, SendArmies, UpdateMap, Reattack, ResetAttack, RegroupArmies, GetCard, EndTurn, Surrender}
197     $actionsMenuStartItem.setEnabled(actionMenuItemStates[0]);
198     $actionsMenuSendarmiesItem.setEnabled(actionMenuItemStates[1]);
199     $actionsMenuUpdatemapItem.setEnabled(actionMenuItemStates[2]);
200     $actionsMenuReattackItem.setEnabled(actionMenuItemStates[3]);
201     $actionsMenuResetattackItem.setEnabled(actionMenuItemStates[4]);
202     $actionsMenuRegrouparmiesItem.setEnabled(actionMenuItemStates[5]);
203     $actionsMenuGetcardItem.setEnabled(actionMenuItemStates[6]);
204     $actionsMenuEndturnItem.setEnabled(actionMenuItemStates[7]);
205     $actionsMenuSurrenderItem.setEnabled(actionMenuItemStates[8]);
206 
207     // ViewMenu Items
208     // {Cards}
209     $viewMenuCardsItem.setEnabled(viewMenuItemStates[0]);
210     $viewMenuMissionItem.setEnabled(viewMenuItemStates[1]);
211     }
212 
213 
214 
215   private void createMenuItems()
216     {
217           // ***********************************************
218           //      The Game Menu
219           // ***********************************************
220 
221     $gameMenu = new JMenu(LanguageManager.getInstance().lookup("game"));
222     
223     $gameMenuConnectItem = new JMenuItem(LanguageManager.getInstance().lookup("connect"));
224     $gameMenuConnectItem.addActionListener(new ActionListener(){
225       public void actionPerformed(ActionEvent ae)
226       {
227         $mainFrame.showConnectionDialog();
228       }
229     });
230     
231     $gameMenuDisconnectItem = new JMenuItem(LanguageManager.getInstance().lookup("disconnect"));
232     $gameMenuDisconnectItem.addActionListener(new ActionListener(){    
233       public void actionPerformed(ActionEvent ae)
234       {
235         try
236         {
237           MessageManager.getInstance().disconnect();
238           JTEGStateMachine.getInstance().setCurrentState(JTEGStateMachine.DISCONNECTED);
239         }
240         catch (Exception e)
241         {
242           e.printStackTrace();
243         }
244       }
245     });
246     
247     $gameMenuExitItem = new JMenuItem(LanguageManager.getInstance().lookup("exit"));
248     $gameMenuExitItem.addActionListener(new ActionListener(){    
249       public void actionPerformed(ActionEvent ae)
250       {
251         if (DEBUG) System.out.println("JTEG is exiting");
252         System.exit(0);
253       }
254     });    
255 
256           $gameMenu.add($gameMenuConnectItem);
257     $gameMenu.add($gameMenuDisconnectItem);
258     $gameMenu.addSeparator();
259     $gameMenu.add($gameMenuExitItem);
260 
261 
262           // ***********************************************
263           //      The Action Menu
264           // ***********************************************
265   
266     $actionsMenu = new JMenu(LanguageManager.getInstance().lookup("actions"));
267 
268     $actionsMenuStartItem = new JMenuItem(LanguageManager.getInstance().lookup("start"));
269     $actionsMenuStartItem.addActionListener(new ActionListener(){
270       public void actionPerformed(ActionEvent ae)
271       {
272         try
273         {
274           // We request a MissionDialog, and leave the rest to that dialog :)
275           MissionDialog someDialog = new MissionDialog();
276         }
277         catch (Exception e)
278         {
279           e.printStackTrace();
280         }
281       }
282     });
283     
284     $actionsMenuSendarmiesItem = new JMenuItem(LanguageManager.getInstance().lookup("sendarmies"));
285     $actionsMenuSendarmiesItem.addActionListener(new ActionListener(){
286       public void actionPerformed(ActionEvent ae){
287         JTEGStateMachine.getInstance().setCurrentState(JTEGStateMachine.WAITING_FOR_OTHER_PLAYERS);
288         $mainFrame.sendArmies();
289       }
290     });
291         
292     $actionsMenuUpdatemapItem = new JMenuItem(LanguageManager.getInstance().lookup("updatemap"));
293     $actionsMenuReattackItem = new JMenuItem(LanguageManager.getInstance().lookup("reattack"));
294     $actionsMenuResetattackItem = new JMenuItem(LanguageManager.getInstance().lookup("resetattack"));
295     
296     $actionsMenuRegrouparmiesItem = new JMenuItem(LanguageManager.getInstance().lookup("regrouparmies"));
297     $actionsMenuRegrouparmiesItem.addActionListener(new ActionListener(){
298       public void actionPerformed(ActionEvent ae){
299         $mainFrame.prepareRegroup();
300       }
301     });
302     
303     $actionsMenuGetcardItem = new JMenuItem(LanguageManager.getInstance().lookup("getcard"));
304     $actionsMenuGetcardItem.addActionListener(new ActionListener(){
305       public void actionPerformed(ActionEvent ae){
306         if (JTEGStateMachine.getInstance().getCurrentState() == JTEGStateMachine.REGROUPING_ARMIES)
307           $mainFrame.removeRegroupMouseListeners();
308           MenuBar.this.$mainFrame.getCard();
309         
310       }
311     });
312     
313     $actionsMenuEndturnItem = new JMenuItem(LanguageManager.getInstance().lookup("endturn"));
314     $actionsMenuEndturnItem.addActionListener(new ActionListener(){
315       public void actionPerformed(ActionEvent ae){
316         try
317           {
318           if (JTEGStateMachine.getInstance().getCurrentState() == JTEGStateMachine.REGROUPING_ARMIES)
319             $mainFrame.removeRegroupMouseListeners();            
320           JTEGStateMachine.getInstance().setCurrentState(JTEGStateMachine.WAITING_FOR_OTHER_PLAYERS);
321           String[] param = {"endturn"};
322           MessageManager.getInstance().sendToServer(param);
323           }
324         catch (Exception e)
325           {
326             System.out.println(e.getMessage());
327             e.printStackTrace();
328           }
329       }
330     });
331     $actionsMenuSurrenderItem = new JMenuItem(LanguageManager.getInstance().lookup("surrender"));
332 
333     $actionsMenu.add($actionsMenuStartItem);
334     $actionsMenu.add($actionsMenuSendarmiesItem);
335     $actionsMenu.add($actionsMenuUpdatemapItem);
336     $actionsMenu.add($actionsMenuReattackItem);
337     $actionsMenu.add($actionsMenuResetattackItem);
338     $actionsMenu.add($actionsMenuRegrouparmiesItem);
339     $actionsMenu.add($actionsMenuGetcardItem);
340     $actionsMenu.add($actionsMenuEndturnItem);
341     $actionsMenu.addSeparator();
342     $actionsMenu.add($actionsMenuSurrenderItem);
343 
344 
345           // ***********************************************
346           //      The View Menu
347           // ***********************************************
348 
349     $viewMenu = new JMenu(LanguageManager.getInstance().lookup("view"));
350     $viewMenuCardsItem = new JMenuItem(LanguageManager.getInstance().lookup("viewcards"));
351     $viewMenuCardsItem.addActionListener(new ActionListener(){
352       public void actionPerformed(ActionEvent ae){
353         MenuBar.this.$mainFrame.showCardFrame();
354       }});
355 
356     $viewMenu.add($viewMenuCardsItem);
357 
358 
359     $viewMenuMissionItem = new JMenuItem(LanguageManager.getInstance().lookup("showmission"));
360     $viewMenuMissionItem.addActionListener(new ActionListener(){
361       public void actionPerformed(ActionEvent ae){
362         MenuBar.this.$mainFrame.showMissionFrame();
363       }});
364     $viewMenu.add($viewMenuMissionItem);
365 
366           // ***********************************************
367           //      The Language Menu
368           // ***********************************************
369 
370     // We lookup all the different languages, we add them to the menu, and attach an actionlistener to them.
371     $languagesMenu = new JMenu(LanguageManager.getInstance().lookup("language"));
372     Vector languages = LanguageManager.getInstance().getAvailableLanguages();
373     for (int index=0; index < languages.size(); index++)
374       {
375         JMenuItem anItem = new JMenuItem((String) languages.elementAt(index));
376         anItem.addActionListener(new ActionListener(){
377             public void actionPerformed(ActionEvent ae){
378               try
379               {
380                 LanguageManager.getInstance().setCurrentLanguage(ae.getActionCommand());
381                 if (DEBUG) System.out.println("I have said to change to : " + ae.getActionCommand());
382               }
383               catch (NoSuchLanguageException exc)
384               {  
385                 // Can never happen!!!
386                 System.out.println("An exception that should never be thrown is thrown!: " + exc.getMessage());
387                 exc.printStackTrace();
388               }
389               }});
390                 $languagesMenu.add(anItem);
391       }
392 
393     
394           // ***********************************************
395           //      The Settings Menu
396           // ***********************************************
397     
398     $settingsMenu = new JMenu(LanguageManager.getInstance().lookup("settings"));
399     
400           // ***********************************************
401           //      The Help Menu
402           // ***********************************************
403     
404     $helpMenu = new JMenu(LanguageManager.getInstance().lookup("help"));
405     }
406 
407 
408   // ***********************************
409   //   Listener-Methods
410   // ***********************************
411 
412   /**
413     *  Method for being a languageListener
414     */ 
415   public void languageChanged()
416     {
417       if (DEBUG) System.out.println("MENUBAR: I've been notified by the languagemanager!");
418       // Game Menu
419       $gameMenu.setText(LanguageManager.getInstance().lookup("game"));
420       $gameMenuConnectItem.setText(LanguageManager.getInstance().lookup("connect"));
421       $gameMenuDisconnectItem.setText(LanguageManager.getInstance().lookup("disconnect"));
422       $gameMenuExitItem.setText(LanguageManager.getInstance().lookup("exit"));
423       // Actions Menu
424       $actionsMenu.setText(LanguageManager.getInstance().lookup("actions"));
425       $actionsMenuStartItem.setText(LanguageManager.getInstance().lookup("start"));
426       $actionsMenuSendarmiesItem.setText(LanguageManager.getInstance().lookup("sendarmies"));
427       $actionsMenuUpdatemapItem.setText(LanguageManager.getInstance().lookup("updatemap"));
428       $actionsMenuReattackItem.setText(LanguageManager.getInstance().lookup("reattack"));
429       $actionsMenuResetattackItem.setText(LanguageManager.getInstance().lookup("resetattack"));
430       $actionsMenuRegrouparmiesItem.setText(LanguageManager.getInstance().lookup("regrouparmies"));
431       $actionsMenuGetcardItem.setText(LanguageManager.getInstance().lookup("getcard"));
432       $actionsMenuEndturnItem.setText(LanguageManager.getInstance().lookup("endturn"));
433       $actionsMenuSurrenderItem.setText(LanguageManager.getInstance().lookup("surrender"));
434       // View Menu
435       $viewMenu.setText(LanguageManager.getInstance().lookup("view"));
436       $viewMenuCardsItem.setText(LanguageManager.getInstance().lookup("viewcards"));
437       $viewMenuMissionItem.setText(LanguageManager.getInstance().lookup("showmission"));      
438       // Languages Menu
439       $languagesMenu.setText(LanguageManager.getInstance().lookup("language"));  
440       // Settings Menu
441       $settingsMenu.setText(LanguageManager.getInstance().lookup("settings"));
442       // Help Menu
443       $helpMenu.setText(LanguageManager.getInstance().lookup("help"));
444       
445       // A simple repaint does not seem to work :'(
446       setVisible(false);
447       setVisible(true);
448       if (DEBUG) System.out.println("MENUBAR: I have repainted!");      
449     }  
450 
451   /**
452     *  Method for being a stateListener
453     */ 
454   public void stateChanged(int newState)
455     {
456       enableMenuItemsForState(newState);
457     }
458 
459 
460   // *****************************
461   //   Instantiationvariables
462   // *****************************
463 
464 
465     // Other Private Datamembers
466     private MainFrame $mainFrame;
467     private static final boolean DEBUG = false;
468     
469     // The Hashtables
470     private Hashtable $gameMenuStateTable = new Hashtable(20);
471     private Hashtable $actionMenuStateTable = new Hashtable(20);
472     private Hashtable $viewMenuStateTable = new Hashtable(20);
473 
474     // Menu-Items
475     private JMenu $gameMenu; 
476     private JMenuItem $gameMenuConnectItem;
477     private JMenuItem $gameMenuDisconnectItem;
478     private JMenuItem $gameMenuExitItem; 
479     private JMenu $actionsMenu;
480     private JMenuItem $actionsMenuStartItem; 
481     private JMenuItem $actionsMenuSendarmiesItem;
482     private JMenuItem $actionsMenuUpdatemapItem;
483     private JMenuItem $actionsMenuReattackItem;
484     private JMenuItem $actionsMenuResetattackItem;
485     private JMenuItem $actionsMenuRegrouparmiesItem;
486     private JMenuItem $actionsMenuGetcardItem;
487     private JMenuItem $actionsMenuEndturnItem;
488     private JMenuItem $actionsMenuSurrenderItem;
489     private JMenu $viewMenu; 
490     private JMenuItem $viewMenuCardsItem;
491     private JMenuItem $viewMenuMissionItem;
492     private JMenu $settingsMenu;
493     private JMenu $helpMenu;
494     private JMenu $languagesMenu;
495 }