1 30 31 package com.jgoodies.looks.demo; 32 33 import java.awt.*; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.net.URL ; 37 38 import javax.swing.*; 39 import javax.swing.border.Border ; 40 import javax.swing.border.EmptyBorder ; 41 import javax.swing.plaf.UIResource ; 42 import javax.swing.plaf.metal.DefaultMetalTheme ; 43 import javax.swing.plaf.metal.MetalLookAndFeel ; 44 45 import com.jgoodies.looks.LookUtils; 46 import com.jgoodies.looks.Options; 47 import com.jgoodies.looks.plastic.PlasticLookAndFeel; 48 import com.jgoodies.looks.windows.WindowsLookAndFeel; 49 50 65 public class DemoFrame extends JFrame { 66 67 protected static final Dimension PREFERRED_SIZE = 68 LookUtils.IS_LOW_RESOLUTION ? new Dimension(650, 510) : new Dimension(730, 560); 69 70 71 private final Settings settings; 72 73 77 protected DemoFrame(Settings settings) { 78 this.settings = settings; 79 configureUI(); 80 build(); 81 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 82 } 83 84 public static void main(String [] args) { 85 DemoFrame instance = new DemoFrame(createSettings()); 86 instance.setSize(PREFERRED_SIZE); 87 instance.locateOnScreen(instance); 88 instance.setVisible(true); 89 } 90 91 private static Settings createSettings() { 92 Settings settings = Settings.createDefault(); 93 94 96 return settings; 97 } 98 99 100 104 private void configureUI() { 105 Options.setDefaultIconSize(new Dimension(18, 18)); 106 107 UIManager.put( 109 Options.USE_SYSTEM_FONTS_APP_KEY, 110 settings.isUseSystemFonts()); 111 Options.setGlobalFontSizeHints(settings.getFontSizeHints()); 112 Options.setUseNarrowButtons(settings.isUseNarrowButtons()); 113 114 Options.setTabIconsEnabled(settings.isTabIconsEnabled()); 116 UIManager.put(Options.POPUP_DROP_SHADOW_ENABLED_KEY, 117 settings.isPopupDropShadowEnabled()); 118 119 LookAndFeel selectedLaf = settings.getSelectedLookAndFeel(); 121 if (selectedLaf instanceof PlasticLookAndFeel) { 122 PlasticLookAndFeel.setMyCurrentTheme(settings.getSelectedTheme()); 123 PlasticLookAndFeel.setTabStyle(settings.getPlasticTabStyle()); 124 PlasticLookAndFeel.setHighContrastFocusColorsEnabled( 125 settings.isPlasticHighContrastFocusEnabled()); 126 } else if (selectedLaf.getClass() == MetalLookAndFeel .class) { 127 MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme ()); 128 } 129 130 JRadioButton radio = new JRadioButton(); 132 radio.getUI().uninstallUI(radio); 133 JCheckBox checkBox = new JCheckBox(); 134 checkBox.getUI().uninstallUI(checkBox); 135 136 try { 137 UIManager.setLookAndFeel(selectedLaf); 138 } catch (Exception e) { 139 System.out.println("Can't change L&F: " + e); 140 } 141 142 } 143 144 147 private void build() { 148 setContentPane(buildContentPane()); 149 setTitle(getWindowTitle()); 150 setJMenuBar( 151 createMenuBuilder().buildMenuBar( 152 settings, 153 createHelpActionListener(), 154 createAboutActionListener())); 155 setIconImage(readImageIcon("eye_16x16.gif").getImage()); 156 } 157 158 159 167 protected MenuBuilder createMenuBuilder() { 168 return new MenuBuilder(); 169 } 170 171 174 private JComponent buildContentPane() { 175 JPanel panel = new JPanel(new BorderLayout()); 176 panel.add(buildToolBar(), BorderLayout.NORTH); 177 panel.add(buildMainPanel(), BorderLayout.CENTER); 178 return panel; 179 } 180 181 183 188 private Component buildToolBar() { 189 JToolBar toolBar = new JToolBar(); 190 toolBar.setFloatable(false); 191 toolBar.putClientProperty("JToolBar.isRollover", Boolean.TRUE); 192 toolBar.putClientProperty( 194 Options.HEADER_STYLE_KEY, 195 settings.getToolBarHeaderStyle()); 196 toolBar.putClientProperty( 197 PlasticLookAndFeel.BORDER_STYLE_KEY, 198 settings.getToolBarPlasticBorderStyle()); 199 toolBar.putClientProperty( 200 WindowsLookAndFeel.BORDER_STYLE_KEY, 201 settings.getToolBarWindowsBorderStyle()); 202 toolBar.putClientProperty( 203 PlasticLookAndFeel.IS_3D_KEY, 204 settings.getToolBar3DHint()); 205 206 AbstractButton button; 207 208 toolBar.add(createToolBarButton("backward.gif")); 209 button = createToolBarButton("forward.gif"); 210 button.setEnabled(false); 211 toolBar.add(button); 212 toolBar.add(createToolBarButton("home.gif")); 213 toolBar.addSeparator(); 214 toolBar.add(createOpenButton()); 215 toolBar.add(createToolBarButton("print.gif")); 216 toolBar.add(createToolBarButton("refresh.gif")); 217 toolBar.addSeparator(); 218 219 ButtonGroup group = new ButtonGroup(); 220 button = createToolBarRadioButton("pie_mode.png"); 221 button.setSelectedIcon(readImageIcon("pie_mode_selected.gif")); 222 group.add(button); 223 button.setSelected(true); 224 toolBar.add(button); 225 226 button = createToolBarRadioButton("bar_mode.png"); 227 button.setSelectedIcon(readImageIcon("bar_mode_selected.gif")); 228 group.add(button); 229 toolBar.add(button); 230 231 button = createToolBarRadioButton("table_mode.png"); 232 button.setSelectedIcon(readImageIcon("table_mode_selected.gif")); 233 group.add(button); 234 toolBar.add(button); 235 toolBar.addSeparator(); 236 237 button = createToolBarButton("help.gif"); 238 button.addActionListener(createHelpActionListener()); 239 toolBar.add(button); 240 241 toolBar.add(Box.createGlue()); 242 243 button = new RolloverCheckButton(); 244 button.setToolTipText("Shall show border when mouse is over"); 245 button.setMargin(new Insets(0, 0, 0, 0)); 246 toolBar.add(button); 247 return toolBar; 248 } 249 250 private AbstractButton createOpenButton() { 251 AbstractButton button = createToolBarButton("open.gif"); 252 button.addActionListener(new ActionListener () { 253 public void actionPerformed(ActionEvent e) { 254 new JFileChooser().showOpenDialog(DemoFrame.this); 255 } 256 257 }); 258 return button; 259 } 260 261 269 protected AbstractButton createToolBarButton(String iconName) { 270 JButton button = new JButton(readImageIcon(iconName)); 271 button.setFocusable(false); 272 return button; 273 } 274 275 283 protected AbstractButton createToolBarRadioButton(String iconName) { 284 JToggleButton button = new JToggleButton(readImageIcon(iconName)); 285 button.setFocusable(false); 286 return button; 287 } 288 289 291 294 private Component buildMainPanel() { 295 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP); 296 298 addTabs(tabbedPane); 299 300 tabbedPane.setBorder(new EmptyBorder (10, 10, 10, 10)); 301 return tabbedPane; 302 } 303 304 private void addTabs(JTabbedPane tabbedPane) { 305 tabbedPane.addTab("State", new StatesTab().build()); 306 tabbedPane.addTab("Align", new AlignmentTab().build()); 307 tabbedPane.addTab("Tab", new TabTestTab().build()); 308 tabbedPane.addTab("Split", new SplitTab().build()); 309 tabbedPane.addTab("HTML", new HtmlTab().build()); 310 tabbedPane.addTab("Dialog", new DialogsTab().build(tabbedPane)); 311 tabbedPane.addTab("Desktop", new DesktopTab().build()); 312 tabbedPane.addTab("Narrow", new NarrowTab().build()); 313 } 314 315 protected String getWindowTitle() { 316 return "Simple Looks Demo"; 317 } 318 319 320 322 325 protected static ImageIcon readImageIcon(String filename) { 326 URL url = 327 DemoFrame.class.getClassLoader().getResource("images/" + filename); 328 return new ImageIcon(url); 329 } 330 331 334 protected void locateOnScreen(Component component) { 335 Dimension paneSize = component.getSize(); 336 Dimension screenSize = component.getToolkit().getScreenSize(); 337 component.setLocation( 338 (screenSize.width - paneSize.width) / 2, 339 (screenSize.height - paneSize.height) / 2); 340 } 341 342 345 protected ActionListener createHelpActionListener() { 346 return null; 347 } 348 349 352 protected ActionListener createAboutActionListener() { 353 return new ActionListener () { 354 public void actionPerformed(ActionEvent e) { 355 JOptionPane.showMessageDialog( 356 DemoFrame.this, 357 "The simple Looks Demo Application\n" 358 + "\n\u00a9 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.\n\n"); 359 } 360 }; 361 } 362 363 private static class RolloverCheckButton extends JButton { 365 366 private boolean checked = false; 367 368 public void paint(Graphics g) { 369 if (!checked) { 370 checkAndSetResult(); 371 } 372 super.paint(g); 373 } 374 375 private void checkAndSetResult() { 376 Icon passedIcon = readImageIcon("passed.gif"); 377 Icon failedIcon = readImageIcon("failed.gif"); 378 379 boolean passed = allButtonBordersAreUIResources(); 380 setIcon(passed ? passedIcon : failedIcon); 381 setText(passed ? "Can Swap L&F" : "Can't Swap L&F"); 382 383 checked = true; 384 } 385 386 389 private boolean allButtonBordersAreUIResources() { 390 JToolBar bar = (JToolBar) getParent(); 391 for (int i = bar.getComponentCount() - 1; i >= 0; i--) { 392 Component child = bar.getComponent(i); 393 if (child instanceof JButton) { 394 Border b = ((JButton) child).getBorder(); 395 if (!(b instanceof UIResource )) 396 return false; 397 } 398 } 399 return true; 400 } 401 402 } 403 404 } | Popular Tags |