Listing 2. HighContrastLAF.java
1. import java.awt.*;
2. import java.io.*;
3. import java.util.Properties;
4. import javax.swing.*;
5. import javax.swing.plaf.metal.*;
6. import javax.swing.plaf.*;
7.
8. public class HighContrastLAF extends MetalLookAndFeel {
9. static Properties properties = new Properties() ;
10. // load the properties from the resource file
11. static {
12. try {
13. properties.load(HighContrastLAF.class.getResourceAsStream("HighContrastLAF.properties")) ;
14. } catch (IOException ioe) {System.err.println("Could not load properties.") ;}
15. MetalLookAndFeel.setCurrentTheme(new HighContrastTheme()) ;
16. }
17.
18. public String getName() { return properties.getProperty("name");}
19. public String getDescription() { return properties.getProperty("description");}
20. public String getID() {return getClass().getName() ;}
21.
22. protected void initComponentDefaults(UIDefaults table)
23. {
24. super.initComponentDefaults( table );
25. double mag = 1.0 ;
26. try {
27. mag = Double.parseDouble(properties.getProperty("magFactor")) ;
28. } catch (Exception exc) {}
29.
30. Object[] defaults = new Object[] {
31. "ComboBox.selectionForeground", getHighlightedTextColor(),
32. "Panel.font", getControlTextFont(),
33. "CheckBox.icon", new MagnifiedIcon(MetalIconFactory.getCheckBoxIcon(), mag),
34. "RadioButton.icon", new MagnifiedIcon(MetalIconFactory.getRadioButtonIcon(), mag),
35. "Menu.checkIcon", new MagnifiedIcon(MetalIconFactory.getMenuItemCheckIcon(), mag),
36. "Menu.arrowIcon", new MagnifiedIcon(MetalIconFactory.getMenuArrowIcon(), mag),
37. "CheckBoxMenuItem.checkIcon", new MagnifiedIcon(MetalIconFactory.getCheckBoxMenuItemIcon(), mag),
38. "CheckBoxMenuItem.arrowIcon", new MagnifiedIcon(MetalIconFactory.getMenuItemArrowIcon(), mag),
39. "RadioButtonMenuItem.checkIcon", new MagnifiedIcon(MetalIconFactory.getRadioButtonMenuItemIcon(), mag),
40. "RadioButtonMenuItem.arrowIcon", new MagnifiedIcon(MetalIconFactory.getMenuItemArrowIcon(), mag),
41. "Tree.openIcon", new MagnifiedIcon(MetalIconFactory.getTreeFolderIcon(), mag),
42. "Tree.closedIcon", new MagnifiedIcon(MetalIconFactory.getTreeFolderIcon(), mag),
43. "Tree.leafIcon", new MagnifiedIcon(MetalIconFactory.getTreeLeafIcon(), mag),
44. "Tree.expandedIcon", new MagnifiedIcon(MetalIconFactory.getTreeControlIcon(MetalIconFactory.DARK), mag),
45. "Tree.collapsedIcon", new MagnifiedIcon(MetalIconFactory.getTreeControlIcon(MetalIconFactory.LIGHT), mag),
46. "FileChooser.detailsViewIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserDetailViewIcon(), mag),
47. "FileChooser.homeFolderIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserHomeFolderIcon(), mag),
48. "FileChooser.listViewIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserListViewIcon(), mag),
49. "FileChooser.newFolderIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserNewFolderIcon(), mag),
50. "FileChooser.upFolderIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserUpFolderIcon(), mag),
51. } ;
52. table.putDefaults(defaults);
53. }
54.
55. /** A color theme that loads the main colors and fonts from an external properties file */
56. protected static class HighContrastTheme extends DefaultMetalTheme {
57. private FontUIResource font ;
58.
59. private static ColorUIResource getColor(String key) {
60. return new ColorUIResource(Integer.parseInt(properties.getProperty(key), 16));
61. }
62.
63. public HighContrastTheme() {
64. String fontName = properties.getProperty("fontName", "Dialog");
65. int fontSize = 12;
66. try {
67. fontSize = Integer.parseInt(properties.getProperty("fontSize"));
68. } catch (Exception exc) {}
69. font = new FontUIResource(fontName, Font.PLAIN, fontSize);
70. }
71.
72. protected ColorUIResource getWhite() { return getColor("backgroundColor"); }
73. protected ColorUIResource getBlack() { return getColor("foregroundColor"); }
74. protected ColorUIResource getPrimary1() { return getColor("primaryColor1"); }
75. protected ColorUIResource getPrimary2() { return getColor("primaryColor2"); }
76. protected ColorUIResource getPrimary3() { return getColor("primaryColor3"); }
77. protected ColorUIResource getSecondary1() { return getColor("secondaryColor1"); }
78. protected ColorUIResource getSecondary2() { return getColor("secondaryColor2"); }
79. protected ColorUIResource getSecondary3() { return getColor("secondaryColor3"); }
80. protected ColorUIResource getSelectionForeground() { return getColor("selectionForeground"); }
81. protected ColorUIResource getSelectionBackground() { return getColor("selectionBackground"); }
82. public ColorUIResource getMenuSelectedBackground() { return getSelectionBackground() ;}
83. public ColorUIResource getMenuSelectedForeground() { return getSelectionForeground() ;}
84. public ColorUIResource getTextHighlightColor() { return getSelectionBackground() ;}
85. public ColorUIResource getHighlightedTextColor() { return getSelectionForeground() ;}
86. public FontUIResource getControlTextFont(){ return font; }
87. public FontUIResource getMenuTextFont() { return font; }
88. public FontUIResource getSubTextFont() { return font; }
89. public FontUIResource getSystemTextFont() { return font; }
90. public FontUIResource getUserTextFont() { return font; }
91. public FontUIResource getWindowTitleFont(){ return font; }
92. }
93.
94. /** A class to create a magnified version of an existing icon */
95. protected class MagnifiedIcon implements Icon {
96. private Icon icon ;
97. private double factor ;
98.
99. public MagnifiedIcon(Icon icon, double factor) {
100. this.icon = icon ;
101. this.factor = factor ;
102. }
103. public int getIconWidth() { return (int)(icon.getIconWidth()*factor) ;}
104. public int getIconHeight() { return (int)(icon.getIconHeight()*factor) ;}
105.
106. public void paintIcon(Component c, Graphics g, int x, int y) {
107. Graphics2D g2d = (Graphics2D)g.create() ;
108. g2d.translate(x,y);
109. g2d.scale(factor, factor);
110. icon.paintIcon(c,g2d,0,0);
111. g2d.dispose();
112. }
113. }
114. }
|
