View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui;
21  
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.KeyEvent;
25  
26  import javax.help.CSH;
27  import javax.help.HelpBroker;
28  import javax.swing.Action;
29  import javax.swing.ButtonGroup;
30  import javax.swing.JCheckBoxMenuItem;
31  import javax.swing.JComponent;
32  import javax.swing.JMenu;
33  import javax.swing.JMenuBar;
34  import javax.swing.JMenuItem;
35  import javax.swing.KeyStroke;
36  import javax.swing.UIManager;
37  
38  import org.xnap.XNap;
39  import org.xnap.action.AbstractXNapAction;
40  import org.xnap.gui.action.AboutDialogAction;
41  import org.xnap.gui.action.ActionHelper;
42  import org.xnap.gui.action.ClearHistoriesAction;
43  import org.xnap.gui.action.ConsoleDialogAction;
44  import org.xnap.gui.action.ContextHelpAction;
45  import org.xnap.gui.action.ErrorAction;
46  import org.xnap.gui.action.ExitAction;
47  import org.xnap.gui.action.FeedbackDialogAction;
48  import org.xnap.gui.action.FocusAction;
49  import org.xnap.gui.action.HelpDialogAction;
50  import org.xnap.gui.action.PluginDialogAction;
51  import org.xnap.gui.action.PreferencesDialogAction;
52  import org.xnap.gui.action.ResetPreferencesAction;
53  import org.xnap.gui.action.ShowChatPanelAction;
54  import org.xnap.gui.action.ShowHotlistAction;
55  import org.xnap.gui.action.ShowLibraryPanelAction;
56  import org.xnap.gui.action.ShowMainToolBarAction;
57  import org.xnap.gui.action.StartupWizardDialogAction;
58  import org.xnap.gui.action.UpdateLibraryAction;
59  import org.xnap.gui.action.UseTabbedPaneAction;
60  import org.xnap.gui.action.UseTabbedTransferPaneAction;
61  import org.xnap.gui.component.XNapCheckBoxMenuItem;
62  import org.xnap.gui.component.XNapMenu;
63  import org.xnap.gui.component.XNapMenuItem;
64  import org.xnap.gui.menu.EnablePluginsMenu;
65  import org.xnap.gui.shortcut.ShortcutManager;
66  import org.xnap.gui.theme.ThemeManager;
67  import org.xnap.gui.util.GUIHelper;
68  import org.xnap.gui.util.HelpManager;
69  import org.xnap.gui.util.IconHelper;
70  import org.xnap.util.Preferences;
71  import org.xnap.util.SystemHelper;
72  
73  
74  /***
75   * The MainToolBar is shown at the top of the main application window.
76   */
77  public class MainMenuBar extends JMenuBar
78  {
79      //--- Constant(s) ---
80      
81      //--- Data field(s) ---
82  
83      private JMenu editMenu;
84      private JMenu fileMenu;
85      private JMenu helpMenu;
86      private JMenu pluginsMenu;
87      private JMenu settingsMenu;
88      private JMenu viewMenu;
89  	private JMenu lookAndFeelMenu;
90  
91      private JMenu visibleColumnsMenu;
92  
93  	private int pluginMenuInitialItemCount;
94  
95  	private static Preferences prefs = Preferences.getInstance();
96  
97      //--- Constructor(s) ---
98  
99      public MainMenuBar()
100     {
101         initializeFileMenu();
102 		initializeEditMenu();
103 		initializeViewMenu();
104         initializePluginsMenu();
105 		initializeSettingsMenu();
106 		initializeHelpMenu();
107 
108 		updateMnemonics();
109     }
110 
111     //--- Method(s) ---
112 
113 	/***
114 	 * Adds a plugins menu.
115 	 */
116 	public void addPluginMenu(JMenu menu)
117 	{
118 		if (Preferences.getInstance().getUseSubMenuForPluginMenus()) {
119 			if (pluginsMenu.getItemCount() == pluginMenuInitialItemCount) {
120 				pluginsMenu.addSeparator();
121 			}
122 			pluginsMenu.add(menu);
123 		}
124 		else {
125 			menu.setIcon(null);
126 			add(menu, 4);
127 			updateUI();
128 		}
129 		updateMnemonics();
130 	}
131 
132     /***
133      * Adds a table column menu.
134      */
135     public void addTableColumnsMenu(JMenu menu)
136     {
137 		visibleColumnsMenu.add(menu);
138 		updateMnemonics();
139     }
140 
141     /***
142      * Adds a panel to the view menu.
143      */
144     public JMenuItem addViewItem(FocusAction action, int keyCode)
145     {
146 		JMenuItem jmi = new XNapMenuItem(action);
147 		GUIHelper.setAccelerator(jmi, keyCode);
148 		getViewMenu().add(jmi);
149 		updateMnemonics();
150 
151 		return jmi;
152     }
153 
154     /***
155      * Returns the file menu.
156      */
157     public JMenu getFileMenu()
158     {
159 		return fileMenu;
160     }
161 
162     /***
163      * Returns the help menu.
164      */
165     public JMenu getHelpMenu()
166     {
167 	return helpMenu;
168     }
169 
170     public JMenu getPluginsMenu1()
171     {
172 		return pluginsMenu;
173     }
174 
175     /***
176      * Returns the view menu.
177      */
178     public JMenu getViewMenu()
179     {
180 		return viewMenu;
181     }
182 
183 	/***
184 	 * Removes a plugins menu.
185 	 */
186 	public void removePluginMenu(JMenu menu)
187 	{
188 		if (pluginsMenu.isMenuComponent(menu)) {
189 			pluginsMenu.remove(menu);
190 			if (pluginsMenu.getItemCount() == pluginMenuInitialItemCount) {
191 				// remove separator
192 				pluginsMenu.remove(pluginMenuInitialItemCount);
193 			}
194 		}
195 		else {
196 			remove(menu);
197 			updateUI();
198 		}
199 	}
200 
201     public void removeTableColumnsMenu(JMenu menu)
202     {
203 		visibleColumnsMenu.remove(menu);
204     }
205 
206     public void removeViewItem(JMenuItem menuItem)
207     {
208 		getViewMenu().remove(menuItem);
209     }
210 
211 	public void updateMnemonics()
212 	{
213 		// mnemomics are a foreign concept to Mac OS X, see
214 		// http://developer.apple.com/techpubs/macosx/Java/JavaDevelopment/ \
215 		// JavaDevonMacOSX.pdf for more information
216 		if (!SystemHelper.isMacOSX) {
217 			GUIHelper.setMnemonics(this);
218 		}
219 	}
220 
221     private void initializeFileMenu()
222     {
223         fileMenu = new JMenu(XNap.tr("File"));
224 		CSH.setHelpIDString(fileMenu, "file-menu");
225 		add(fileMenu);
226 
227 		fileMenu.add(new XNapMenuItem(new UpdateLibraryAction()));
228         fileMenu.addSeparator();
229 
230  		if (XNap.isRunFromCvs()) {
231  			fileMenu.add(new XNapMenuItem(new ErrorAction()));
232 			fileMenu.addSeparator();
233  		}
234 
235 		ExitAction exitAction = new ExitAction();
236 		ShortcutManager.getInstance().add(exitAction);
237         fileMenu.add(new XNapMenuItem(exitAction));
238     }
239 
240     private void initializeEditMenu()
241     {
242 		editMenu = new JMenu(XNap.tr("Edit"));
243 		CSH.setHelpIDString(editMenu, "edit-menu");
244 		add(editMenu);
245 	
246 		editMenu.add(new XNapMenuItem(ActionHelper.cutAction));
247 		editMenu.add(new XNapMenuItem(ActionHelper.copyAction));
248 		editMenu.add(new XNapMenuItem(ActionHelper.pasteAction));
249 		editMenu.addSeparator();
250 		editMenu.add(new XNapMenuItem(new ClearHistoriesAction()));
251     }
252 
253     private void initializePluginsMenu()
254     {	
255 		pluginsMenu = new JMenu(XNap.tr("Plugins"));
256 		CSH.setHelpIDString(pluginsMenu, "plugin-menu");
257 		add(pluginsMenu);
258 
259 		pluginsMenu.add(new XNapMenuItem(new PluginDialogAction()));
260 		pluginsMenu.add(new EnablePluginsMenu());
261 
262 		pluginMenuInitialItemCount = pluginsMenu.getItemCount();
263 	}
264 	
265     private void initializeViewMenu()
266     {
267 		viewMenu = new JMenu(XNap.tr("View"));
268 		CSH.setHelpIDString(viewMenu, "view-menu");
269 		add(viewMenu);
270 
271 		FocusAction action = new FocusAction();
272 		action.putValue(Action.NAME, XNap.tr("Search"));
273 		action.putValue(Action.SHORT_DESCRIPTION, 
274 						XNap.tr("Switches to search panel."));
275 		action.putValue(Action.ACTION_COMMAND_KEY, "search");
276 		action.putValue(IconHelper.XNAP_ICON, "find.png");
277 		action.putValue(AbstractXNapAction.DEFAULT_KEYSTROKE,
278 						GUIHelper.getMenuKeyStroke(KeyEvent.VK_S));
279 		viewMenu.add(new XNapMenuItem(action));
280 		ShortcutManager.getInstance().add(action);
281 
282 		action = new FocusAction();
283 		action.putValue(Action.NAME, XNap.tr("Transfer"));
284 		action.putValue(Action.SHORT_DESCRIPTION, 
285 						XNap.tr("Switches to transfer panel."));
286 		action.putValue(Action.ACTION_COMMAND_KEY, "transfer");
287 		action.putValue(IconHelper.XNAP_ICON, "connect_established.png");
288 		action.putValue(AbstractXNapAction.DEFAULT_KEYSTROKE,
289 						GUIHelper.getMenuKeyStroke(KeyEvent.VK_T));
290 		viewMenu.add(new XNapMenuItem(action));
291 		ShortcutManager.getInstance().add(action);
292 
293 		action = new FocusAction();
294 		action.putValue(Action.NAME, XNap.tr("Chat"));
295 		action.putValue(Action.SHORT_DESCRIPTION, 
296 						XNap.tr("Switches to chat panel."));
297 		action.putValue(Action.ACTION_COMMAND_KEY, "chat");
298 		action.putValue(IconHelper.XNAP_ICON, "mail_generic2.png");
299 		action.putValue(AbstractXNapAction.DEFAULT_KEYSTROKE,
300 						GUIHelper.getMenuKeyStroke(KeyEvent.VK_H));
301 		viewMenu.add(new XNapMenuItem(action));
302 		ShortcutManager.getInstance().add
303 			(action, (JComponent) XNapFrame.getInstance().getContentPane());
304 
305 		action = new FocusAction();
306 		action.putValue(Action.NAME, XNap.tr("Library"));
307 		action.putValue(Action.SHORT_DESCRIPTION,
308 						XNap.tr("Switches to library panel."));
309 		action.putValue(Action.ACTION_COMMAND_KEY, "library");
310 		action.putValue(IconHelper.XNAP_ICON, "contents.png");
311 		action.putValue(AbstractXNapAction.DEFAULT_KEYSTROKE,
312 						GUIHelper.getMenuKeyStroke(KeyEvent.VK_L));
313 		viewMenu.add(new XNapMenuItem(action));
314 		ShortcutManager.getInstance().add
315 			(action, (JComponent) XNapFrame.getInstance().getContentPane());
316 
317 		viewMenu.addSeparator();
318 				
319 		viewMenu.add(new XNapMenuItem(new ConsoleDialogAction()));
320     }
321 
322     private void initializeSettingsMenu()
323     {
324 		settingsMenu = new JMenu(XNap.tr("Settings"));
325 		CSH.setHelpIDString(settingsMenu, "settings-menu");
326 		add(settingsMenu);
327 
328 		settingsMenu.add
329 			(new XNapCheckBoxMenuItem(new ShowMainToolBarAction()));
330 		XNapCheckBoxMenuItem jmiHotlist
331 			= new XNapCheckBoxMenuItem(new ShowHotlistAction());
332 		jmiHotlist.setAccelerator
333 			(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_MASK));
334 		settingsMenu.add(jmiHotlist);
335 
336 		JMenu jmPanels = new XNapMenu(XNap.tr("Show Panels"));
337 		settingsMenu.add(jmPanels);
338 		jmPanels.add
339 			(new XNapCheckBoxMenuItem(new ShowChatPanelAction()));
340 		jmPanels.add
341 			(new XNapCheckBoxMenuItem(new ShowLibraryPanelAction()));
342 
343 		settingsMenu.addSeparator();
344 		settingsMenu.add(new XNapCheckBoxMenuItem(new UseTabbedPaneAction()));
345 		settingsMenu.add
346 			(new XNapCheckBoxMenuItem(new UseTabbedTransferPaneAction()));
347 		settingsMenu.addSeparator();
348 
349 		/* who needs to switch that on and off quickly? */
350 		//settingsMenu.add(new XNapCheckBoxMenuItem(new UseSocksProxyAction()));
351 		//settingsMenu.addSeparator();
352 	
353 		visibleColumnsMenu
354 			= new XNapMenu(XNap.tr("Visible Columns"), "view_detailed.png");
355 		//settingsMenu.add(jmVisibleColumns);
356 		//settingsMenu.addSeparator();
357 	
358 		lookAndFeelMenu = new XNapMenu(XNap.tr("Look & Feel"));
359 		lookAndFeelMenu.setIcon(IconHelper.getMenuIcon("list.png"));
360 		settingsMenu.add(lookAndFeelMenu);
361 		updateLookAndFeels();
362 
363 		settingsMenu.addSeparator();
364 
365 		JMenu maintananceMenu = new JMenu(XNap.tr("Maintenance"));
366 		settingsMenu.add(maintananceMenu);
367 
368 		maintananceMenu.add(new XNapMenuItem(new ResetPreferencesAction()));
369 
370 		settingsMenu.add(new XNapMenuItem(new StartupWizardDialogAction()));
371 
372 		settingsMenu.add
373 			(new XNapMenuItem(ActionHelper.pluginPreferencesDialogAction));
374         XNapMenuItem jmiPreferences 
375 			= new XNapMenuItem(new PreferencesDialogAction());
376 		jmiPreferences.setAccelerator(prefs.getShortcut("preferencesDialog"));
377         settingsMenu.add(jmiPreferences);
378     }
379 
380     private void initializeHelpMenu()
381     {
382         helpMenu = new JMenu (XNap.tr("Help"));
383 		CSH.setHelpIDString(helpMenu, "help-menu");
384 		add(helpMenu);
385 		helpMenu.add
386 			(new XNapMenuItem(new HelpDialogAction(XNap.tr("XNap Handbook"),
387 												   HelpManager.getMainHelpSet())));
388 		JMenuItem item = new JMenuItem(XNap.tr("XNap FAQ"),
389 									   IconHelper.getMenuIcon("contents.png"));
390 
391 		HelpBroker hb = HelpManager.getMainHelpBroker();
392 		if (hb != null) {
393 			hb.enableHelpOnButton(item, "frequently-asked-questions", 
394 								  HelpManager.getMainHelpSet());
395 		}
396 		else {
397 			item.setEnabled(false);
398 		}
399 		helpMenu.add(item);
400 						   
401 		JMenuItem jm = new XNapMenuItem(new ContextHelpAction());
402 		jm.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,
403 												 KeyEvent.SHIFT_MASK));
404 		helpMenu.add(jm);
405 		helpMenu.addSeparator();
406 		helpMenu.add(new XNapMenuItem(new FeedbackDialogAction()));
407     	helpMenu.addSeparator();
408 		helpMenu.add(new XNapMenuItem(new AboutDialogAction()));
409     }
410 
411 	public void updateLookAndFeels()
412 	{
413 		lookAndFeelMenu.removeAll();
414 
415 		UIManager.LookAndFeelInfo[] info 
416 			= ThemeManager.getInstalledLookAndFeels();
417 		ButtonGroup group = new ButtonGroup();
418 		ActionListener lafListener = new LookAndFeelActionListener();
419 		String currentLaf = UIManager.getLookAndFeel().getClass().getName();
420 		for (int i = 0; i < info.length; i++) {
421 			JCheckBoxMenuItem item = new JCheckBoxMenuItem(info[i].getName());
422 		
423 			item.setActionCommand(info[i].getClassName());
424 			item.addActionListener(lafListener);
425 
426 			if (currentLaf.equals(info[i].getClassName())) {
427 				item.setSelected(true);
428 			}
429 
430 			group.add(item);
431 			lookAndFeelMenu.add(item);
432 		}
433 	}
434 
435     private class LookAndFeelActionListener implements ActionListener 
436     {
437 
438 		public void actionPerformed(ActionEvent e) 
439 		{
440 			Preferences.getInstance().setLookAndFeel(e.getActionCommand());
441 			Preferences.getInstance().write();
442 
443 			//Dialogs.showRestartNotification(XNapFrame.getInstance());
444 		}
445 
446     }
447 
448 }