/*
* Created on 13-6-20
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* Copyright @2013 the original author or authors.
*/
package ch22swing.table.button;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
/**
* Description of this file.
*
* @author XiongNeng
* @version 1.0
* @since 13-6-20
*/
public class UIUtils {
private static final String FALLBACK_FONT_FAMILY_NAME = Font.SANS_SERIF;
private static final Map<String, String> FONT_FAMILY_NAMES = new HashMap<String, String>();
private static final String[] BEST_FONT_FAMILIES = {
"微软雅黑", "arial", "sans-serif"
};
private static final int BEST_FONT_SIZE = 12; // 12px
static {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFamilyNames = env.getAvailableFontFamilyNames();
for (String fontFamilyName : fontFamilyNames) {
FONT_FAMILY_NAMES.put(fontFamilyName.toLowerCase(), fontFamilyName);
}
if (!FONT_FAMILY_NAMES.containsKey("serif")) {
FONT_FAMILY_NAMES.put("serif", Font.SERIF);
}
if (!FONT_FAMILY_NAMES.containsKey("sans-serif")) {
FONT_FAMILY_NAMES.put("sans-serif", Font.SANS_SERIF);
}
}
public static void enableAntiAliasing() {
System.setProperty("awt.useSystemAAFontSettings", "on");
System.setProperty("swing.aatext", "true");
}
public static String getLookAndFeel() {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
return info.getClassName();
}
}
} catch (Exception ignore) {
}
return UIManager.getCrossPlatformLookAndFeelClassName();
}
public static String getFontFamily(String[] fontFamilies) {
for (String fontFamily : fontFamilies) {
fontFamily = fontFamily.toLowerCase();
if (FONT_FAMILY_NAMES.containsKey(fontFamily)) {
return FONT_FAMILY_NAMES.get(fontFamily);
}
}
return FALLBACK_FONT_FAMILY_NAME;
}
public static String[] getBestFontFamilies() {
return BEST_FONT_FAMILIES;
}
public static int getBestFontSize() {
return BEST_FONT_SIZE;
}
/*########################################*/
public static void setUI() {
enableAntiAliasing();
// set LookAndFeel
try {
UIManager.setLookAndFeel(getLookAndFeel());
} catch (Exception ignore) {
}
// set DefaultFont
String bestFontFamily = getFontFamily(getBestFontFamilies());
for (Map.Entry<Object, Object> entry : UIManager.getDefaults().entrySet()) {
if (entry.getValue() instanceof FontUIResource) {
FontUIResource fontUIRes = (FontUIResource) entry.getValue();
entry.setValue(new FontUIResource(
bestFontFamily,
fontUIRes.getStyle(),
getBestFontSize() > fontUIRes.getSize() ?
getBestFontSize() : fontUIRes.getSize()
));
}
}
}
}