1 package com.sshtools.ui.swing; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.swing.border.*; 8 import javax.swing.plaf.*; 9 import javax.swing.table.TableCellRenderer ; 10 11 public class ShowUIDefaults extends JFrame implements ActionListener 12 { 13 JFrame frame; 14 JTabbedPane tabbedPane; 15 SampleRenderer sampleRenderer; 16 17 public ShowUIDefaults(String title) 18 { 19 super(title); 20 frame = this; 21 22 sampleRenderer = new SampleRenderer(); 23 24 getContentPane().setLayout( new BorderLayout() ); 25 tabbedPane = getTabbedPane(); 26 getContentPane().add( tabbedPane ); 27 28 JPanel buttons = new JPanel(); 29 buttons.setLayout( new GridLayout( 1, 3) ); 30 getContentPane().add(buttons, BorderLayout.SOUTH); 31 32 JButton metal = new JButton( "Metal" ); 33 metal.setActionCommand( "javax.swing.plaf.metal.MetalLookAndFeel" ); 34 metal.addActionListener( this ); 35 buttons.add(metal); 36 37 JButton windows = new JButton( "Windows" ); 38 windows.setActionCommand( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ); 39 windows.addActionListener( this ); 40 buttons.add(windows); 41 42 JButton motif = new JButton( "Motif" ); 43 motif.setActionCommand( "com.sun.java.swing.plaf.motif.MotifLookAndFeel" ); 44 motif.addActionListener( this ); 45 buttons.add(motif); 46 47 } 48 49 public void actionPerformed(ActionEvent e) 50 { 51 try 52 { 53 UIManager.setLookAndFeel(e.getActionCommand()); 54 } 55 catch (Exception e2) 56 { 57 System.out.println(e2); 58 } 59 60 getContentPane().remove(tabbedPane); 61 tabbedPane = getTabbedPane(); 62 getContentPane().add( tabbedPane ); 63 SwingUtilities.updateComponentTreeUI(frame); 64 frame.pack(); 65 } 66 67 private JTabbedPane getTabbedPane() 68 { 69 Map components = new TreeMap(); 70 UIDefaults defaults = UIManager.getLookAndFeelDefaults(); 71 72 74 for ( Enumeration e = defaults.keys(); e.hasMoreElements(); ) 75 { 76 Object key = e.nextElement(); 77 Object value = defaults.get( key ); 78 79 Map componentMap = getComponentMap( components, key.toString() ); 80 81 if ( componentMap != null ) 82 componentMap.put( key, value ); 83 } 84 85 JTabbedPane pane = new JTabbedPane( SwingConstants.BOTTOM ); 86 pane.setPreferredSize( new Dimension( 800, 400 ) ); 87 addComponentTabs( pane, components ); 88 89 return pane; 90 } 91 92 private Map getComponentMap( Map components, String key ) 93 { 94 if ( key.startsWith("class") | key.startsWith("javax") ) 95 return null; 96 97 99 String componentName; 100 101 int pos = key.indexOf( "." ); 102 103 if (pos == -1) 104 if (key.endsWith( "UI" ) ) 105 componentName = key.substring( 0, key.length() - 2 ); 106 else 107 componentName = "System Colors"; 108 else 109 componentName = key.substring( 0, pos ); 110 111 113 if (componentName.equals("Checkbox")) 114 componentName = "CheckBox"; 115 116 118 Object componentMap = components.get( componentName ); 119 120 if (componentMap == null) 121 { 122 componentMap = new TreeMap(); 123 components.put( componentName, componentMap ); 124 } 125 126 return (Map)componentMap; 127 } 128 129 private void addComponentTabs(JTabbedPane pane, Map components) 130 { 131 String [ ] colName = {"Key", "Value", "Sample"}; 132 Set c = components.keySet(); 133 134 for (Iterator ci = c.iterator(); ci.hasNext();) 135 { 136 String component = (String )ci.next(); 137 Map attributes = (Map)components.get( component ); 138 139 Object [ ][ ] rowData = new Object [ attributes.size() ][ 3 ]; 140 int i = 0; 141 142 Set a = attributes.keySet(); 143 144 for(Iterator ai = a.iterator(); ai.hasNext(); i++) 145 146 { 147 String attribute = (String )ai.next(); 148 rowData[ i ] [ 0 ] = attribute; 149 Object o = attributes.get(attribute); 150 151 if (o != null) 152 { 153 rowData[ i ] [ 1 ] = o.toString(); 154 rowData[ i ] [ 2 ] = o; 155 } 156 else 157 { 158 rowData[ i ] [ 1 ] = "null"; 159 rowData[ i ] [ 2 ] = ""; 160 } 161 162 } 163 164 JTable table = new JTable(rowData, colName); 165 table.getColumnModel().getColumn(2).setCellRenderer( sampleRenderer ); 166 table.getColumnModel().getColumn(0).setPreferredWidth(250); 167 table.getColumnModel().getColumn(1).setPreferredWidth(500); 168 table.getColumnModel().getColumn(2).setPreferredWidth(50); 169 170 pane.addTab( component, new JScrollPane( table ) ); 171 } 172 } 173 174 class SampleRenderer extends JLabel implements TableCellRenderer 175 { 176 public SampleRenderer() 177 { 178 super(); 179 setHorizontalAlignment( SwingConstants.CENTER ); 180 setOpaque(true); } 182 183 public Component getTableCellRendererComponent(JTable table, 184 Object sample, 185 boolean isSelected, 186 boolean hasFocus, 187 int row, 188 int column) 189 { 190 setBackground( null ); 191 setBorder( null ); 192 setIcon( null ); 193 setText( "" ); 194 195 if ( sample instanceof Color ) 196 { 197 setBackground( (Color)sample ); 198 } 199 else if ( sample instanceof Border ) 200 { 201 setBorder( (Border)sample ); 202 } 203 else if ( sample instanceof Font ) 204 { 205 setText( "Sample" ); 206 setFont( (Font)sample ); 207 } 208 else if ( sample instanceof Icon ) 209 { 210 setIcon( (Icon)sample ); 211 } 212 213 return this; 214 } 215 216 public void paint(Graphics g) 217 { 218 try 219 { 220 super.paint(g); 221 } 222 catch(Exception e) 223 { 224 } 227 } 228 } 229 230 public static void main(String [] args) 231 { 232 JFrame f = new ShowUIDefaults( "UI Defaults" ); 233 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 234 f.pack(); 235 f.setLocationRelativeTo( null ); 236 f.setVisible(true); 237 } 238 239 } 240 | Popular Tags |