1 7 8 package javax.swing.plaf.metal; 9 10 import javax.swing.plaf.*; 11 import javax.swing.*; 12 import java.awt.*; 13 14 import sun.security.action.GetPropertyAction; 15 16 31 public class DefaultMetalTheme extends MetalTheme { 32 36 private static final boolean PLAIN_FONTS; 37 38 41 private static final String [] fontNames = { 42 "Dialog", "Dialog", "Dialog", "Dialog", "Dialog", "Dialog" 43 }; 44 48 private static final int[] fontStyles = { 49 Font.BOLD, Font.PLAIN, Font.PLAIN, Font.BOLD, Font.BOLD, Font.PLAIN 50 }; 51 54 private static final int[] fontSizes = { 55 12, 12, 12, 12, 12, 10 56 }; 57 58 68 private static final String [] defaultNames = { 69 "swing.plaf.metal.controlFont", 70 "swing.plaf.metal.systemFont", 71 "swing.plaf.metal.userFont", 72 "swing.plaf.metal.controlFont", 73 "swing.plaf.metal.controlFont", 74 "swing.plaf.metal.smallFont" 75 }; 76 77 80 static String getDefaultFontName(int key) { 81 return fontNames[key]; 82 } 83 84 87 static int getDefaultFontSize(int key) { 88 return fontSizes[key]; 89 } 90 91 94 static int getDefaultFontStyle(int key) { 95 if (key != WINDOW_TITLE_FONT) { 96 Object boldMetal = UIManager.get("swing.boldMetal"); 97 if (boldMetal != null) { 98 if (Boolean.FALSE.equals(boldMetal)) { 99 return Font.PLAIN; 100 } 101 } 102 else if (PLAIN_FONTS) { 103 return Font.PLAIN; 104 } 105 } 106 return fontStyles[key]; 107 } 108 109 112 static String getDefaultPropertyName(int key) { 113 return defaultNames[key]; 114 } 115 116 static { 117 Object boldProperty = java.security.AccessController.doPrivileged( 118 new GetPropertyAction("swing.boldMetal")); 119 if (boldProperty == null || !"false".equals(boldProperty)) { 120 PLAIN_FONTS = false; 121 } 122 else { 123 PLAIN_FONTS = true; 124 } 125 } 126 127 private static final ColorUIResource primary1 = new ColorUIResource( 128 102, 102, 153); 129 private static final ColorUIResource primary2 = new ColorUIResource(153, 130 153, 204); 131 private static final ColorUIResource primary3 = new ColorUIResource( 132 204, 204, 255); 133 private static final ColorUIResource secondary1 = new ColorUIResource( 134 102, 102, 102); 135 private static final ColorUIResource secondary2 = new ColorUIResource( 136 153, 153, 153); 137 private static final ColorUIResource secondary3 = new ColorUIResource( 138 204, 204, 204); 139 140 private FontDelegate fontDelegate; 141 142 public String getName() { return "Steel"; } 143 144 public DefaultMetalTheme() { 145 install(); 146 } 147 148 protected ColorUIResource getPrimary1() { return primary1; } 150 protected ColorUIResource getPrimary2() { return primary2; } 151 protected ColorUIResource getPrimary3() { return primary3; } 152 153 protected ColorUIResource getSecondary1() { return secondary1; } 155 protected ColorUIResource getSecondary2() { return secondary2; } 156 protected ColorUIResource getSecondary3() { return secondary3; } 157 158 159 public FontUIResource getControlTextFont() { 160 return getFont(CONTROL_TEXT_FONT); 161 } 162 163 public FontUIResource getSystemTextFont() { 164 return getFont(SYSTEM_TEXT_FONT); 165 } 166 167 public FontUIResource getUserTextFont() { 168 return getFont(USER_TEXT_FONT); 169 } 170 171 public FontUIResource getMenuTextFont() { 172 return getFont(MENU_TEXT_FONT); 173 } 174 175 public FontUIResource getWindowTitleFont() { 176 return getFont(WINDOW_TITLE_FONT); 177 } 178 179 public FontUIResource getSubTextFont() { 180 return getFont(SUB_TEXT_FONT); 181 } 182 183 private FontUIResource getFont(int key) { 184 return fontDelegate.getFont(key); 185 } 186 187 void install() { 188 if (MetalLookAndFeel.isWindows() && 189 MetalLookAndFeel.useSystemFonts()) { 190 fontDelegate = new WindowsFontDelegate(); 191 } 192 else { 193 fontDelegate = new FontDelegate(); 194 } 195 } 196 197 200 boolean isSystemTheme() { 201 return (getClass() == DefaultMetalTheme .class); 202 } 203 204 207 private static class FontDelegate { 208 private static int[] defaultMapping = { 209 CONTROL_TEXT_FONT, SYSTEM_TEXT_FONT, 210 USER_TEXT_FONT, CONTROL_TEXT_FONT, 211 CONTROL_TEXT_FONT, SUB_TEXT_FONT 212 }; 213 FontUIResource fonts[]; 214 215 public FontDelegate() { 217 fonts = new FontUIResource[6]; 218 } 219 220 public FontUIResource getFont(int type) { 221 int mappedType = defaultMapping[type]; 222 if (fonts[type] == null) { 223 Font f = getPrivilegedFont(mappedType); 224 225 if (f == null) { 226 f = new Font(getDefaultFontName(type), 227 getDefaultFontStyle(type), 228 getDefaultFontSize(type)); 229 } 230 fonts[type] = new FontUIResource(f); 231 } 232 return fonts[type]; 233 } 234 235 240 protected Font getPrivilegedFont(final int key) { 241 return (Font)java.security.AccessController.doPrivileged( 242 new java.security.PrivilegedAction () { 243 public Object run() { 244 return Font.getFont(getDefaultPropertyName(key)); 245 } 246 } 247 ); 248 } 249 } 250 251 254 private static class WindowsFontDelegate extends FontDelegate { 255 private MetalFontDesktopProperty [] props; 256 private boolean[] checkedPriviledged; 257 258 public WindowsFontDelegate() { 259 props = new MetalFontDesktopProperty [6]; 260 checkedPriviledged = new boolean[6]; 261 } 262 263 public FontUIResource getFont(int type) { 264 if (fonts[type] != null) { 265 return fonts[type]; 266 } 267 if (!checkedPriviledged[type]) { 268 Font f = getPrivilegedFont(type); 269 270 checkedPriviledged[type] = true; 271 if (f != null) { 272 fonts[type] = new FontUIResource(f); 273 return fonts[type]; 274 } 275 } 276 if (props[type] == null) { 277 props[type] = new MetalFontDesktopProperty (type); 278 } 279 return (FontUIResource)props[type].createValue(null); 282 } 283 } 284 } 285 | Popular Tags |