KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > thinlet > drafts > SwingProperties


1 package thinlet.drafts;
2
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.util.*;
6 import javax.swing.*;
7 import thinlet.*;
8
9 /**
10  *
11  */

12 public class SwingProperties {
13     
14     /**
15      *
16      */

17     public void load(Thinlet thinlet, Object JavaDoc table) {
18         UIDefaults defaults = UIManager.getDefaults();
19         Hashtable iconcache = new Hashtable();
20         
21         for (Enumeration keys = defaults.keys(); keys.hasMoreElements();) {
22             String JavaDoc key = (String JavaDoc) keys.nextElement();
23             Object JavaDoc value = defaults.get(key);
24             
25             Object JavaDoc row = Thinlet.create("row");
26             Object JavaDoc keycell = Thinlet.create("cell");
27             thinlet.setString(keycell, "text", key);
28             thinlet.add(row, keycell);
29             Object JavaDoc valuecell = Thinlet.create("cell");
30             if (value instanceof Color) {
31                 Color color = (Color) value;
32                 Image icon = (Image) iconcache.get(color);
33                 if (icon == null) {
34                     icon = createIcon(thinlet, color);
35                     iconcache.put(color, icon);
36                 }
37                 thinlet.setIcon(valuecell, "icon", icon);
38                 thinlet.setString(valuecell, "text", "0x" +
39                     Integer.toHexString(0xff000000 | color.getRGB()).substring(2));
40             }
41             else if (value instanceof Font) {
42                 Font font = (Font) value;
43                 StringBuffer JavaDoc fonttext = new StringBuffer JavaDoc(font.getName());
44                 if (font.isBold()) { fonttext.append(" bold"); }
45                 if (font.isItalic()) { fonttext.append(" italic"); }
46                 fonttext.append(' '); fonttext.append(font.getSize());
47                 thinlet.setString(valuecell, "text", fonttext.toString());
48             }
49             else {
50                 thinlet.setString(valuecell, "text", value.toString());
51             }
52             thinlet.add(row, valuecell);
53             thinlet.add(table, row);
54         }
55     }
56             
57     /**
58      *
59      */

60     private static Image createIcon(Component component, Color color) {
61         int rgb = color.getRGB();
62         int[] pix = new int[12 * 12];
63         for (int x = 0; x < 12; x++) {
64             for (int y = 0; y < 12; y++) {
65                 pix[x + y * 12] = ((x > 0) && (x < 11) &&
66                     (y > 0) && (y < 11)) ? rgb : 0xff666666;
67             }
68         }
69         return component.createImage(
70             new MemoryImageSource(12, 12, pix, 0, 12));
71     }
72     
73     /**
74      *
75      */

76     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
77         Thinlet thinlet = new Thinlet();
78         thinlet.setFont(new java.awt.Font JavaDoc("Tahoma", java.awt.Font.PLAIN, 11));
79         thinlet.setColors(0xf2f1e4, 0x000000, 0xffffff,
80             0x909090, 0xb0b0b0, 0xededed, 0xc7c5b2, 0xe68b2c, 0xf2c977);
81         thinlet.add(thinlet.parse("/thinlet/drafts/swingproperties.xml", new SwingProperties()));
82         new FrameLauncher("Thinlet widgets", thinlet, 320, 270);
83     }
84 }
Popular Tags