KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > demo > MenuBuilder


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.demo;
32
33 import java.awt.event.ActionListener JavaDoc;
34 import java.net.URL JavaDoc;
35
36 import javax.swing.*;
37 import javax.swing.event.ChangeEvent JavaDoc;
38 import javax.swing.event.ChangeListener JavaDoc;
39
40 import com.jgoodies.looks.Options;
41 import com.jgoodies.looks.plastic.PlasticLookAndFeel;
42 import com.jgoodies.looks.windows.WindowsLookAndFeel;
43
44 /**
45  * Builds the menu bar and pull-down menus in the Simple Looks Demo.
46  * Demonstrates and tests different multi-platform issues.<p>
47  *
48  * This class provides a couple of factory methods that create
49  * menu items, check box menu items, and radio button menu items.
50  * The full JGoodies Looks Demo overrides these methods to vend
51  * components from the JGoodies UI framework that better handle
52  * different platforms.
53  *
54  * @author Karsten Lentzsch
55  * @version $Revision: 1.9 $
56  */

57
58 public class MenuBuilder {
59     
60     private static final String JavaDoc HTML_TEXT =
61         "<html><b>Bold</b>, <i>Italics</i>, <tt>Typewriter</tt></html>";
62     
63     /**
64      * Builds, configures, and answers the menubar. Requests HeaderStyle,
65      * look-specific BorderStyles, and Plastic 3D hint from Launcher.
66      */

67     JMenuBar buildMenuBar(
68         Settings settings,
69         ActionListener JavaDoc helpActionListener,
70         ActionListener JavaDoc aboutActionListener) {
71             
72         JMenuBar bar = new JMenuBar();
73         bar.putClientProperty(Options.HEADER_STYLE_KEY,
74                               settings.getMenuBarHeaderStyle());
75         bar.putClientProperty(PlasticLookAndFeel.BORDER_STYLE_KEY,
76                               settings.getMenuBarPlasticBorderStyle());
77         bar.putClientProperty(WindowsLookAndFeel.BORDER_STYLE_KEY,
78                               settings.getMenuBarWindowsBorderStyle());
79         bar.putClientProperty(PlasticLookAndFeel.IS_3D_KEY,
80                               settings.getMenuBar3DHint());
81
82         bar.add(buildFileMenu());
83         bar.add(buildRadioMenu());
84         bar.add(buildCheckMenu());
85         bar.add(buildHtmlMenu());
86         bar.add(buildAlignmentTestMenu());
87         bar.add(buildHelpMenu(helpActionListener, aboutActionListener));
88         return bar;
89     }
90     
91     
92     /**
93      * Builds and answers the file menu.
94      */

95     private JMenu buildFileMenu() {
96         JMenuItem item;
97         
98         JMenu menu = createMenu("File", 'F');
99         
100         // Build a submenu that has the noIcons hint set.
101
JMenu submenu = createMenu("New", 'N');
102         submenu.putClientProperty(Options.NO_ICONS_KEY, Boolean.TRUE);
103         submenu.add(createMenuItem("Project...", 'P'));
104         submenu.add(createMenuItem("Folder...", 'F'));
105         submenu.add(createMenuItem("Document...", 'D'));
106         submenu.addSeparator();
107         submenu.add(createMenuItem("No icon hint set"));
108         
109         menu.add(submenu);
110         menu.addSeparator();
111         item = createMenuItem("Close", 'C', KeyStroke.getKeyStroke("ctrl F4"));
112         menu.add(item);
113         item = createMenuItem("Close All", 'o', KeyStroke.getKeyStroke("ctrl shift F4"));
114         menu.add(item);
115         menu.addSeparator();
116         item = createMenuItem("Save description.txt",
117                               readImageIcon("save_edit.gif"),
118                               'd',
119                               KeyStroke.getKeyStroke("ctrl S"));
120         item.setEnabled(false);
121         menu.add(item);
122         item = createMenuItem("Save description.txt As...",
123                 readImageIcon("saveas_edit.gif"),
124                 'e');
125         menu.add(item);
126         item = createMenuItem("Save All", 'A', KeyStroke.getKeyStroke("ctrl shift S"));
127         item.setEnabled(false);
128         menu.add(item);
129         menu.addSeparator();
130         item = createMenuItem("Print", readImageIcon("print.gif"), 'P',
131                 KeyStroke.getKeyStroke("ctrl P"));
132         menu.add(item);
133         menu.addSeparator();
134         menu.add(createMenuItem("1 WinXPMenuItemUI.java", '1'));
135         menu.add(createMenuItem("2 WinXPUtils.java", '2'));
136         menu.add(createMenuItem("3 WinXPBorders.java", '3'));
137         menu.add(createMenuItem("4 WinXPLookAndFeel.java", '4'));
138         if (!isQuitInOSMenu()) {
139             menu.addSeparator();
140             menu.add(createMenuItem("Exit", 'E'));
141         }
142         return menu;
143     }
144     
145
146     /**
147      * Builds and answers a menu with different JRadioButtonMenuItems.
148      */

149     private JMenu buildRadioMenu() {
150         JRadioButtonMenuItem item;
151         
152         JMenu menu = createMenu("Radio", 'R');
153         
154         // Default icon
155
ButtonGroup group1 = new ButtonGroup();
156         item = createRadioItem(true, false);
157         group1.add(item);
158         menu.add(item);
159         item = createRadioItem(true, true);
160         group1.add(item);
161         menu.add(item);
162         
163         menu.addSeparator();
164
165         item = createRadioItem(false, false);
166         menu.add(item);
167         item = createRadioItem(false, true);
168         menu.add(item);
169         
170         menu.addSeparator();
171         
172         // Custom icon
173
ButtonGroup group2 = new ButtonGroup();
174         item = createRadioItem(true, false);
175         item.setIcon(readImageIcon("pie_mode.png"));
176         group2.add(item);
177         menu.add(item);
178         item = createRadioItem(true, true);
179         item.setIcon(readImageIcon("bar_mode.png"));
180         group2.add(item);
181         menu.add(item);
182
183         menu.addSeparator();
184
185         item = createRadioItem(false, false);
186         item.setIcon(readImageIcon("alphab_sort.png"));
187         //item.setDisabledIcon(Utils.getIcon("alphab_sort_gray.png"));
188
menu.add(item);
189         item = createRadioItem(false, true);
190         item.setIcon(readImageIcon("size_sort.png"));
191         item.setDisabledIcon(readImageIcon("size_sort_gray.png"));
192         menu.add(item);
193         
194         return menu;
195     }
196     
197     
198     /**
199      * Builds and answers a menu with different JCheckBoxMenuItems.
200      */

201     private JMenu buildCheckMenu() {
202         JCheckBoxMenuItem item;
203         
204         JMenu menu = createMenu("Check", 'C');
205         
206         // Default icon
207
menu.add(createCheckItem(true, false));
208         menu.add(createCheckItem(true, true));
209         menu.addSeparator();
210         menu.add(createCheckItem(false, false));
211         menu.add(createCheckItem(false, true));
212         
213         menu.addSeparator();
214         
215         // Custom icon
216
item = createCheckItem(true, false);
217         item.setIcon(readImageIcon("check.gif"));
218         item.setSelectedIcon(readImageIcon("check_selected.gif"));
219         menu.add(item);
220         item = createCheckItem(true, true);
221         item.setIcon(readImageIcon("check.gif"));
222         item.setSelectedIcon(readImageIcon("check_selected.gif"));
223         menu.add(item);
224
225         menu.addSeparator();
226         
227         item = createCheckItem(false, false);
228         item.setIcon(readImageIcon("check.gif"));
229         item.setSelectedIcon(readImageIcon("check_selected.gif"));
230         menu.add(item);
231         item = createCheckItem(false, true);
232         item.setIcon(readImageIcon("check.gif"));
233         item.setSelectedIcon(readImageIcon("check_selected.gif"));
234         item.setDisabledSelectedIcon(readImageIcon("check_disabled_selected.gif"));
235         menu.add(item);
236         
237         return menu;
238     }
239     
240     
241     /**
242      * Builds and answers a menu with items that use a HTML text.
243      */

244     private JMenu buildHtmlMenu() {
245         JMenu menu = createMenu("Styled", 'S');
246
247         menu.add(createSubmenu(HTML_TEXT));
248         menu.add(createMenuItem(HTML_TEXT));
249         menu.addSeparator();
250         menu.add(new JRadioButtonMenuItem(HTML_TEXT, false));
251         menu.add(new JRadioButtonMenuItem(HTML_TEXT, true));
252         menu.addSeparator();
253         menu.add(new JCheckBoxMenuItem(HTML_TEXT, true));
254         menu.add(new JCheckBoxMenuItem(HTML_TEXT, false));
255         return menu;
256     }
257         
258     /**
259      * Builds and answers a menu with items that use a HTML text.
260      */

261     private JMenu buildAlignmentTestMenu() {
262         JMenu menu = createMenu("Alignment", 'A');
263         
264         menu.add(createMenuItem("Menu item"));
265         menu.add(createMenuItem("Menu item with icon", readImageIcon("refresh.gif")));
266         menu.addSeparator();
267         JMenu submenu = createSubmenu("Submenu");
268         menu.add(submenu);
269
270         submenu = createSubmenu("Submenu with icon");
271         submenu.setIcon(readImageIcon("refresh.gif"));
272         menu.add(submenu);
273         
274         return menu;
275     }
276         
277     /**
278      * Builds and answers the help menu.
279      */

280     private JMenu buildHelpMenu(
281         ActionListener JavaDoc helpActionListener,
282         ActionListener JavaDoc aboutActionListener) {
283
284         JMenu menu = createMenu("Help", 'H');
285         
286         JMenuItem item;
287         item = createMenuItem("Help Contents", readImageIcon("help.gif"), 'H');
288         if (helpActionListener != null) {
289             item.addActionListener(helpActionListener);
290         }
291         menu.add(item);
292         if (!isAboutInOSMenu()) {
293             menu.addSeparator();
294             item = createMenuItem("About", 'a');
295             item.addActionListener(aboutActionListener);
296             menu.add(item);
297         }
298         
299         return menu;
300     }
301     
302     // Factory Methods ********************************************************
303

304     protected JMenu createMenu(String JavaDoc text, char mnemonic) {
305         JMenu menu = new JMenu(text);
306         menu.setMnemonic(mnemonic);
307         return menu;
308     }
309         
310     protected JMenuItem createMenuItem(String JavaDoc text) {
311         return new JMenuItem(text);
312     }
313     
314     protected JMenuItem createMenuItem(String JavaDoc text, char mnemonic) {
315         return new JMenuItem(text, mnemonic);
316     }
317     
318     protected JMenuItem createMenuItem(String JavaDoc text, char mnemonic, KeyStroke key) {
319         JMenuItem menuItem = new JMenuItem(text, mnemonic);
320         menuItem.setAccelerator(key);
321         return menuItem;
322     }
323     
324     protected JMenuItem createMenuItem(String JavaDoc text, Icon icon) {
325         return new JMenuItem(text, icon);
326     }
327     
328     protected JMenuItem createMenuItem(String JavaDoc text, Icon icon, char mnemonic) {
329         JMenuItem menuItem = new JMenuItem(text, icon);
330         menuItem.setMnemonic(mnemonic);
331         return menuItem;
332     }
333     
334     protected JMenuItem createMenuItem(String JavaDoc text, Icon icon, char mnemonic, KeyStroke key) {
335         JMenuItem menuItem = createMenuItem(text, icon, mnemonic);
336         menuItem.setAccelerator(key);
337         return menuItem;
338     }
339     
340     protected JRadioButtonMenuItem createRadioButtonMenuItem(String JavaDoc text, boolean selected) {
341         return new JRadioButtonMenuItem(text, selected);
342     }
343     
344     protected JCheckBoxMenuItem createCheckBoxMenuItem(String JavaDoc text, boolean selected) {
345         return new JCheckBoxMenuItem(text, selected);
346     }
347     
348
349     // Subclass will override the following methods ***************************
350

351     /**
352      * Checks and answers whether the quit action has been moved to an
353      * operating system specific menu, e.g. the OS X application menu.
354      *
355      * @return true if the quit action is in an OS-specific menu
356      */

357     protected boolean isQuitInOSMenu() {
358         return false;
359     }
360     
361
362     /**
363      * Checks and answers whether the about action has been moved to an
364      * operating system specific menu, e.g. the OS X application menu.
365      *
366      * @return true if the about action is in an OS-specific menu
367      */

368     protected boolean isAboutInOSMenu() {
369         return false;
370     }
371     
372     
373     // Higher Level Factory Methods *****************************************
374

375     /**
376      * Creates and answers a <code>JRadioButtonMenuItem</code>
377      * with the given enablement and selection state.
378      */

379     private JRadioButtonMenuItem createRadioItem(boolean enabled, boolean selected) {
380         JRadioButtonMenuItem item = createRadioButtonMenuItem(
381             getToggleLabel(enabled, selected),
382             selected);
383         item.setEnabled(enabled);
384         item.addChangeListener(new ChangeListener JavaDoc() {
385             public void stateChanged(ChangeEvent JavaDoc e) {
386                 JRadioButtonMenuItem source = (JRadioButtonMenuItem) e.getSource();
387                 source.setText(getToggleLabel(source.isEnabled(), source.isSelected()));
388             }
389         });
390         return item;
391     }
392     
393
394     /**
395      * Creates and answers a <code>JCheckBoxMenuItem</code>
396      * with the given enablement and selection state.
397      */

398     private JCheckBoxMenuItem createCheckItem(boolean enabled, boolean selected) {
399         JCheckBoxMenuItem item = createCheckBoxMenuItem(
400             getToggleLabel(enabled, selected),
401             selected);
402         item.setEnabled(enabled);
403         item.addChangeListener(new ChangeListener JavaDoc() {
404             public void stateChanged(ChangeEvent JavaDoc e) {
405                 JCheckBoxMenuItem source = (JCheckBoxMenuItem) e.getSource();
406                 source.setText(getToggleLabel(source.isEnabled(), source.isSelected()));
407             }
408         });
409         return item;
410     }
411     
412     
413     /**
414      * Answers an appropriate label for the given enablement and selection state.
415      */

416     protected String JavaDoc getToggleLabel(boolean enabled, boolean selected) {
417         String JavaDoc prefix = enabled ? "Enabled" : "Disabled";
418         String JavaDoc suffix = selected ? "Selected" : "Deselected";
419         return prefix + " and " + suffix;
420     }
421     
422     // Helper Code ************************************************************
423

424     /**
425      * Looks up and answers an icon for the specified filename suffix.
426      */

427     private ImageIcon readImageIcon(String JavaDoc filename) {
428         URL JavaDoc url = getClass().getClassLoader().getResource("images/" + filename);
429         return new ImageIcon(url);
430     }
431     
432     /**
433      * Creates and answers a submenu labeled with the given text.
434      */

435     private JMenu createSubmenu(String JavaDoc text) {
436         JMenu submenu = new JMenu(text);
437         submenu.add(new JMenuItem("Item 1"));
438         submenu.add(new JMenuItem("Item 2"));
439         submenu.add(new JMenuItem("Item 3"));
440         return submenu;
441     }
442     
443
444 }
Popular Tags