Share Pastebin
Guest
Public paste!

Walter Laan

By: a guest | Nov 12th, 2008 | Syntax: Java | Size: 6.57 KB | Hits: 175 | Expires: Never
Copy text to clipboard
  1. package test;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.*;
  8. import java.util.List;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.swing.*;
  12. import javax.swing.border.Border;
  13. import javax.swing.table.TableCellRenderer;
  14.  
  15. public class ShowUIDefaults extends JFrame {
  16.     JFrame frame;
  17.     JTabbedPane tabbedPane;
  18.     SampleRenderer sampleRenderer;
  19.  
  20.     public ShowUIDefaults(String title) {
  21.         super(title);
  22.         frame = this;
  23.  
  24.         sampleRenderer = new SampleRenderer();
  25.  
  26.         getContentPane().setLayout(new BorderLayout());
  27.         tabbedPane = getTabbedPane();
  28.         getContentPane().add(tabbedPane);
  29.     }
  30.  
  31.     private JTabbedPane getTabbedPane() {
  32.         Map<String, Map<String, Object>> components = new HashMap<String, Map<String, Object>>();
  33.         UIDefaults defaults = UIManager.getLookAndFeelDefaults();
  34.  
  35.         //  Build of Map of attributes for each component
  36.  
  37.         for (Enumeration<Object> anEnum = defaults.keys(); anEnum.hasMoreElements();) {
  38.             String key = anEnum.nextElement().toString();
  39.             Object value = defaults.get(key);
  40.  
  41.             Map<String, Object> componentMap = getComponentMap(components, key);
  42.  
  43.             if (componentMap != null) {
  44.                 componentMap.put(key, value);
  45.             }
  46.         }
  47.  
  48.         JTabbedPane pane = new JTabbedPane(SwingConstants.BOTTOM);
  49.         pane.setPreferredSize(new Dimension(800, 400));
  50.         addComponentTabs(pane, components);
  51.  
  52.         return pane;
  53.     }
  54.  
  55.     private Map<String, Object> getComponentMap(Map<String, Map<String, Object>> components, String key) {
  56.         if (key.startsWith("class") || key.startsWith("javax")) {
  57.                         return null;
  58.                 }
  59.  
  60.         //  Component name is found before the first "."
  61.  
  62.         String componentName;
  63.  
  64.         int pos = key.indexOf(".");
  65.  
  66.         if (pos == -1) {
  67.                         if (key.endsWith("UI")) {
  68.                                 componentName = key.substring(0, key.length() - 2);
  69.                         }
  70.                         else {
  71.                                 componentName = "System Colors";
  72.                         }
  73.                 }
  74.                 else {
  75.                         componentName = key.substring(0, pos);
  76.                 }
  77.  
  78.         //  Fix inconsistency
  79.  
  80.         if (componentName.equals("Checkbox")) {
  81.                         componentName = "CheckBox";
  82.                 }
  83.  
  84.         //  Get the Map for this particular component
  85.         Map<String, Object> componentMap = components.get(componentName);
  86.  
  87.         if (componentMap == null) {
  88.             componentMap = new HashMap<String, Object>();
  89.             components.put(componentName, componentMap);
  90.         }
  91.  
  92.         return componentMap;
  93.     }
  94.  
  95.     private void addComponentTabs(JTabbedPane pane, Map<String, Map<String, Object>> components) {
  96.         String[] colName = { "Key", "Value", "Sample" };
  97.         List<String> c = new ArrayList<String>(components.keySet());
  98.         Collections.sort(c);
  99.         for (Iterator<String> ci = c.iterator(); ci.hasNext();) {
  100.             String component = ci.next();
  101.             Map<String, Object> attributes = components.get(component);
  102.  
  103.             Object[][] rowData = new Object[attributes.size()][3];
  104.             int i = 0;
  105.  
  106.             List<String> a = new ArrayList<String>(attributes.keySet());
  107.             Collections.sort(a);
  108.  
  109.             for (Iterator<String> ai = a.iterator(); ai.hasNext(); i++)
  110.  
  111.             {
  112.                 String attribute = ai.next();
  113.                 rowData[i][0] = attribute;
  114.                 Object o = attributes.get(attribute);
  115.  
  116.                 if (o != null) {
  117.                     rowData[i][1] = o.toString();
  118.                     rowData[i][2] = o;
  119.                 }
  120.                 else {
  121.                     rowData[i][1] = "null";
  122.                     rowData[i][2] = "";
  123.                 }
  124.  
  125.             }
  126.  
  127.             JTable table = new JTable(rowData, colName);
  128.             table.setCellSelectionEnabled(true);
  129.  
  130.             table.getColumnModel().getColumn(2).setCellRenderer(sampleRenderer);
  131.             table.getColumnModel().getColumn(0).setPreferredWidth(250);
  132.             table.getColumnModel().getColumn(1).setPreferredWidth(500);
  133.             table.getColumnModel().getColumn(2).setPreferredWidth(50);
  134.  
  135.             pane.addTab(component, new JScrollPane(table));
  136.         }
  137.     }
  138.  
  139.     private static class SampleRenderer extends JLabel implements TableCellRenderer {
  140.         public SampleRenderer() {
  141.             super();
  142.             setHorizontalAlignment(SwingConstants.CENTER);
  143.             setOpaque(true); //MUST do this for background to show up.
  144.         }
  145.  
  146.         public Component getTableCellRendererComponent(JTable table, Object sample, boolean isSelected,
  147.                 boolean hasFocus, int row, int column) {
  148.             setBackground(null);
  149.             setBorder(null);
  150.             setIcon(null);
  151.             setText("");
  152.  
  153.             if (sample instanceof Color) {
  154.                 setBackground((Color) sample);
  155.             }
  156.             else if (sample instanceof Border) {
  157.                 setBorder((Border) sample);
  158.             }
  159.             else if (sample instanceof Font) {
  160.                 setText("Sample");
  161.                 setFont((Font) sample);
  162.             }
  163.             else if (sample instanceof Icon) {
  164.                 setIcon((Icon) sample);
  165.             }
  166.  
  167.             return this;
  168.         }
  169.  
  170.         @Override
  171.                 public void paint(Graphics g) {
  172.             try {
  173.                 super.paint(g);
  174.             }
  175.             catch (Exception e) {
  176.                 // do nothing
  177.             }
  178.         }
  179.     }
  180.  
  181.     public static void main(String[] args) {
  182.         try {
  183. //              UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  184.                 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  185. //              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  186.                 } catch (ClassNotFoundException e) {
  187.                         e.printStackTrace();
  188.                 } catch (InstantiationException e) {
  189.                         e.printStackTrace();
  190.                 } catch (IllegalAccessException e) {
  191.                         e.printStackTrace();
  192.                 } catch (UnsupportedLookAndFeelException e) {
  193.                         e.printStackTrace();
  194.                 }
  195.  
  196.         JFrame f = new ShowUIDefaults("UI Defaults");
  197.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  198.         f.pack();
  199.         f.setLocationRelativeTo(null);
  200.         f.setVisible(true);
  201.     }
  202.  
  203.         public static void saveOptionPaneIcons() {
  204.                 UIDefaults defaults = UIManager.getDefaults();
  205.                 ImageIcon icon = (ImageIcon)defaults.getIcon("OptionPane.errorIcon");
  206.                 try {
  207.                         ImageIO.write((BufferedImage)icon.getImage(), "png", new File("C:/backup/error.png"));
  208.                 } catch (IOException e) {
  209.                         e.printStackTrace();
  210.                 }
  211.         }
  212. }