Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

LookAndFeelSelector.java

Go to the documentation of this file.
00001 
00058 package com.arcle.rmt.j2se.swing.ui;
00059 
00060 import javax.swing.*;
00061 import java.lang.ref.SoftReference;
00062 import java.awt.event.ActionEvent;
00063 import java.awt.event.KeyEvent;
00064 import java.awt.Component;
00065 import java.awt.Window;
00066 
00074 public final class LookAndFeelSelector {
00075     private LookAndFeelSelector() {
00076     }
00077 
00078     //-----------------------------------------------------------------------
00079     // Accessor Methods
00080 
00084     public static final LookAndFeelSelector getInstance() {
00085         LookAndFeelSelector lf = (LookAndFeelSelector) _instance.get();
00086         if (lf == null) {
00087             lf = new LookAndFeelSelector();
00088             _instance = new SoftReference(lf);
00089         }
00090         return lf;
00091     }
00092 
00093     public JMenu getSelectorMenu() {
00094         if (_selectorMenu == null) {
00095             _selectorMenu = createSelectorMenu();
00096         }
00097         return _selectorMenu;
00098     }
00099 
00100 
00101     //-----------------------------------------------------------------------
00102     // Factory methods
00103 
00104     protected JMenu createSelectorMenu() {
00105         JMenu menu = new JMenu("Look and Feel");
00106         menu.setMnemonic(KeyEvent.VK_L);
00107 
00108         LookAndFeel curLF = UIManager.getLookAndFeel();
00109         String curLFClass = curLF != null ? curLF.getClass().getName() : null;
00110 
00111         ButtonGroup group = new ButtonGroup();
00112         UIManager.LookAndFeelInfo[] lfInfo = UIManager.getInstalledLookAndFeels();
00113         for (int i=0; i<lfInfo.length; i++) {
00114             UIManager.LookAndFeelInfo curInfo = lfInfo[i];
00115             JRadioButtonMenuItem item = new JRadioButtonMenuItem(
00116                 createLookAndFeelSelectAction(curInfo));
00117             group.add(item);
00118             if (curInfo.getClassName().equals(curLFClass)) {
00119                 group.setSelected(item.getModel(), true);
00120 
00121             }
00122             menu.add(item);
00123         }
00124         return menu;
00125     }
00126 
00127     protected Action createLookAndFeelSelectAction(UIManager.LookAndFeelInfo lf) {
00128         return new LookAndFeelSelectAction(lf);
00129     }
00130 
00131 
00132     //-----------------------------------------------------------------------
00133     // Member variables
00134 
00138     private JMenu _selectorMenu = null;
00139 
00143     private static SoftReference _instance = new SoftReference(null);
00144 
00145     //-----------------------------------------------------------------------
00146     // InnerClasses
00147 
00148 
00149     protected static class LookAndFeelSelectAction extends AbstractAction {
00150         public LookAndFeelSelectAction(UIManager.LookAndFeelInfo lf) {
00151             theLF = lf;
00152             putValue(NAME, theLF.getName());
00153             //putValue(SHORT_DESCRIPTION, theLF.getDescription());
00154             //setEnabled(theLF.isSupportedLookAndFeel());
00155         }
00156 
00157         public void actionPerformed(ActionEvent e) {
00158             try {
00159                 UIManager.setLookAndFeel(theLF.getClassName());
00160                 /*
00161                 @todo update all swing components
00162                 @note
00163                 Failed attempts to get the topmost Swing component to update:
00164                     - SwingUtilities.getRoot(c)
00165                     - SwingUtilities.getRootPane(c);
00166                     - SwingUtilities.windowForComponent(c);
00167                     - SwingUtilities.getWindowAncestor(c);
00168                 They all returned null.
00169                 */
00170 
00171             } catch(Exception ex) {
00172                 showException(e.getSource(), ex);
00173             }
00174         }
00175 
00176         protected void showException(Object source, Exception e) {
00177             Component c = source instanceof Component ?
00178                             (Component) source : null;
00179             MoreSwingUtilities.showException(c, e);
00180         }
00181 
00185         private UIManager.LookAndFeelInfo theLF;
00186     }
00187 }

Generated on Fri Jun 18 19:51:22 2004 for Arcle Rambutan by doxygen 1.3.5