1 30 31 package com.jgoodies.looks.demo; 32 33 import java.awt.event.ActionListener ; 34 import java.net.URL ; 35 36 import javax.swing.*; 37 import javax.swing.event.ChangeEvent ; 38 import javax.swing.event.ChangeListener ; 39 40 import com.jgoodies.looks.Options; 41 import com.jgoodies.looks.plastic.PlasticLookAndFeel; 42 import com.jgoodies.looks.windows.WindowsLookAndFeel; 43 44 57 58 public class MenuBuilder { 59 60 private static final String HTML_TEXT = 61 "<html><b>Bold</b>, <i>Italics</i>, <tt>Typewriter</tt></html>"; 62 63 67 JMenuBar buildMenuBar( 68 Settings settings, 69 ActionListener helpActionListener, 70 ActionListener 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 95 private JMenu buildFileMenu() { 96 JMenuItem item; 97 98 JMenu menu = createMenu("File", 'F'); 99 100 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 149 private JMenu buildRadioMenu() { 150 JRadioButtonMenuItem item; 151 152 JMenu menu = createMenu("Radio", 'R'); 153 154 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 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 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 201 private JMenu buildCheckMenu() { 202 JCheckBoxMenuItem item; 203 204 JMenu menu = createMenu("Check", 'C'); 205 206 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 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 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 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 280 private JMenu buildHelpMenu( 281 ActionListener helpActionListener, 282 ActionListener 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 304 protected JMenu createMenu(String text, char mnemonic) { 305 JMenu menu = new JMenu(text); 306 menu.setMnemonic(mnemonic); 307 return menu; 308 } 309 310 protected JMenuItem createMenuItem(String text) { 311 return new JMenuItem(text); 312 } 313 314 protected JMenuItem createMenuItem(String text, char mnemonic) { 315 return new JMenuItem(text, mnemonic); 316 } 317 318 protected JMenuItem createMenuItem(String 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 text, Icon icon) { 325 return new JMenuItem(text, icon); 326 } 327 328 protected JMenuItem createMenuItem(String 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 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 text, boolean selected) { 341 return new JRadioButtonMenuItem(text, selected); 342 } 343 344 protected JCheckBoxMenuItem createCheckBoxMenuItem(String text, boolean selected) { 345 return new JCheckBoxMenuItem(text, selected); 346 } 347 348 349 351 357 protected boolean isQuitInOSMenu() { 358 return false; 359 } 360 361 362 368 protected boolean isAboutInOSMenu() { 369 return false; 370 } 371 372 373 375 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 () { 385 public void stateChanged(ChangeEvent e) { 386 JRadioButtonMenuItem source = (JRadioButtonMenuItem) e.getSource(); 387 source.setText(getToggleLabel(source.isEnabled(), source.isSelected())); 388 } 389 }); 390 return item; 391 } 392 393 394 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 () { 404 public void stateChanged(ChangeEvent e) { 405 JCheckBoxMenuItem source = (JCheckBoxMenuItem) e.getSource(); 406 source.setText(getToggleLabel(source.isEnabled(), source.isSelected())); 407 } 408 }); 409 return item; 410 } 411 412 413 416 protected String getToggleLabel(boolean enabled, boolean selected) { 417 String prefix = enabled ? "Enabled" : "Disabled"; 418 String suffix = selected ? "Selected" : "Deselected"; 419 return prefix + " and " + suffix; 420 } 421 422 424 427 private ImageIcon readImageIcon(String filename) { 428 URL url = getClass().getClassLoader().getResource("images/" + filename); 429 return new ImageIcon(url); 430 } 431 432 435 private JMenu createSubmenu(String 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 |