iqq.app.core.service.impl.SkinServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for iqq.app.core.service.impl.SkinServiceImpl.java

Source

package iqq.app.core.service.impl;

import com.alee.extended.painter.NinePatchIconPainter;
import com.alee.extended.painter.Painter;
import com.alee.laf.WebLookAndFeel;
import com.alee.laf.tree.WebTreeUI;
import iqq.app.core.service.SkinService;
import iqq.app.util.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.nutz.ioc.loader.annotation.IocBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.Locale;

/**
 * ???????
 * ???
 * ??
 *
 * 
 *
 * Project  : iqq-projects
 * Author   :  < 6208317@qq.com >
 * Created  : 14-4-15
 * License  : Apache License 2.0
 */
@IocBean(create = "init")
public class SkinServiceImpl implements SkinService {
    private static final Logger LOG = LoggerFactory.getLogger(SkinServiceImpl.class);

    /**
     * ?
     */
    public static final String SKIN_CONFIG_FILE = "skin.xml";

    /**
     * 
     */
    public static final String DEFAULT_SKIN_DIR = System.getProperty("app.dir", System.getProperty("user.dir"))
            + File.separator + "skin-default" + File.separator;

    /**
     * ??
     */
    public void init() {
        // ?
        loadDefaultFont();
        LOG.debug("skin init...");
    }

    /**
     * ?
     *
     * @param key
     * @param def
     * @return
     */
    public Color getColorByKey(String key, Color def) {
        Color color = getColorByKey(key);
        if (color == null) {
            color = def;
        }
        return color;
    }

    /**
     * ?
     *
     * @param key
     * @return
     */
    @Override
    public Color getColorByKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            try {
                // ???
                String color = XmlUtils.getNodeText(getSkinConfig(), key);
                return Color.decode(color);
            } catch (NullPointerException e) {
                // ????
                String color = null;
                try {
                    // getDefaultConfig()??
                    color = XmlUtils.getNodeText(getDefaultConfig(), key);
                } catch (NullPointerException e1) {
                    LOG.error("?, key=" + key, e1);
                } catch (DocumentException e2) {
                    LOG.error("?, key=" + key, e2);
                }
                return Color.decode(color);
            } catch (DocumentException e) {
                LOG.error("?, key=" + key, e);
            }
        }
        return null;
    }

    /**
     * ?
     *
     * @param key
     * @return
     */
    @Override
    public ImageIcon getIconByKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            try {
                // ???
                String iconPath = getDirectory();
                iconPath += XmlUtils.getNodeText(getSkinConfig(), key);
                return new ImageIcon(iconPath);
            } catch (NullPointerException ex) {
                // ???
                String iconPath = DEFAULT_SKIN_DIR;
                try {
                    // getDefaultConfig()??
                    iconPath += XmlUtils.getNodeText(getDefaultConfig(), key);
                } catch (NullPointerException e1) {
                    LOG.error("?, key=" + key, e1);
                } catch (DocumentException e) {
                    LOG.error("?, key=" + key, e);
                }
                return new ImageIcon(iconPath);
            } catch (DocumentException e) {
                LOG.error("?, key=" + key, e);
            }
        }
        return null;
    }

    @Override
    public ImageIcon getIconByKey(String key, int width, int height) {
        return new ImageIcon(getIconByKey(key).getImage().getScaledInstance(width, height, 100));
    }

    /**
     * ?9Painter
     *
     * @param key
     * @return
     */
    @Override
    public Painter getPainterByKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            try {
                // ???
                String iconPath = getDirectory();
                iconPath += XmlUtils.getNodeText(getSkinConfig(), key);
                return new NinePatchIconPainter(iconPath);
            } catch (NullPointerException ex) {
                // ??
                //
                // ?
                String iconPath = DEFAULT_SKIN_DIR;
                try {
                    // getDefaultConfig()??
                    iconPath += XmlUtils.getNodeText(getDefaultConfig(), key);
                } catch (NullPointerException e1) {
                    LOG.error("?, key=" + key, e1);
                } catch (DocumentException e) {
                    LOG.error("?, key=" + key, e);
                }
                LOG.debug(iconPath);
                return new NinePatchIconPainter(iconPath);
            } catch (DocumentException e) {
                LOG.error("?, key=" + key, e);
            }
        }
        return null;
    }

    /**
     * ??
     *
     * @return
     */
    @Override
    public String getDefaultConfig() {
        return DEFAULT_SKIN_DIR + SKIN_CONFIG_FILE;
    }

    /**
     * ?????
     *
     * @return
     */
    public String getSkinConfig() {
        return getDirectory() + SKIN_CONFIG_FILE;
    }

    /**
     * ?
     *
     * @return
     */
    @Override
    public String getDirectory() {
        if (isEnabledCustom()) {
            try {
                // ??
                return XmlUtils.getNodeText(getDefaultConfig(), "config/customDir");
            } catch (DocumentException e) {
                LOG.error("??", e);
                return DEFAULT_SKIN_DIR;
            }
        }
        return DEFAULT_SKIN_DIR;
    }

    /**
     * 
     *
     * @param path
     */
    @Override
    public void setDirectory(String path) {
        if (StringUtils.isNotEmpty(path)) {
            try {
                // ??
                XmlUtils.setNodeText(getDefaultConfig(), "config/customDir", path);
            } catch (DocumentException e) {
                LOG.error("?Document path=" + path, e);
            } catch (IOException e) {
                LOG.error("?IO path=" + path, e);
            }
        }
    }

    /**
     * ??
     *
     * @return
     */
    @Override
    public boolean isEnabledCustom() {
        String isEnable = "";
        try {
            // ??
            isEnable = XmlUtils.getNodeText(getDefaultConfig(), "config/enable");
            // LOG.debug("?" + isEnable);
        } catch (DocumentException e) {
            LOG.error("???", e);
            return false;
        }
        return isEnable.equalsIgnoreCase("true");
    }

    /**
     * ?
     *
     * @param enable
     */
    @Override
    public void setEnableCustom(boolean enable) {
        try {
            // ??
            XmlUtils.setNodeText(getDefaultConfig(), "config/enable", enable ? "true" : "false");
        } catch (DocumentException e) {
            LOG.error("?Document", e);
        } catch (IOException e) {
            LOG.error("?IO", e);
        }
    }

    /**
     * 
     */
    private void loadDefaultFont() {
        Font globalFont = null;
        try {
            String fontName = XmlUtils.getNodeText(getDefaultConfig(), "font/name");
            String fontSource = XmlUtils.getNodeText(getDefaultConfig(), "font/source");
            String fontFile = XmlUtils.getNodeText(getDefaultConfig(), "font/file");
            int fontSize = Integer.parseInt(XmlUtils.getNodeText(getSkinConfig(), "font/size"));
            if (fontSource.equals("file")) {
                Font font = loadFontFromFile(new File(DEFAULT_SKIN_DIR + fontFile));
                if (font != null) {
                    globalFont = font.deriveFont((float) fontSize);
                    setDefaultFont(globalFont);
                }
            } else if (fontSource.equals("system")) {
                globalFont = new Font(fontName, Font.PLAIN, fontSize);
                setDefaultFont(globalFont);
            } else {
                LOG.warn("unknown font source :" + fontSource);
            }
        } catch (DocumentException e) {
            LOG.warn("?", e);
        }
    }

    private Font loadFontFromFile(File file) {
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
            Font font = Font.createFont(Font.TRUETYPE_FONT, in);
            return font;
        } catch (FileNotFoundException e) {
            LOG.warn("font " + file + "not found!", e);
        } catch (FontFormatException e) {
            LOG.warn("invalid font file!", e);
        } catch (IOException e) {
            LOG.warn("read font error!", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    LOG.warn("close font file error!", e);
                }
            }
        }
        return null;
    }

    @Override
    public void setDefaultFont(Font vFont) {
        UIManager.put("ToolTip.font", vFont);
        UIManager.put("Table.font", vFont);
        UIManager.put("TableHeader.font", vFont);
        UIManager.put("TextField.font", vFont);
        UIManager.put("ComboBox.font", vFont);
        UIManager.put("TextField.font", vFont);
        UIManager.put("PasswordField.font", vFont);
        UIManager.put("TextArea.font", vFont);
        UIManager.put("TextPane.font", vFont);
        UIManager.put("EditorPane.font", vFont);
        UIManager.put("FormattedTextField.font", vFont);
        UIManager.put("Button.font", vFont);
        UIManager.put("CheckBox.font", vFont);
        UIManager.put("RadioButton.font", vFont);
        UIManager.put("ToggleButton.font", vFont);
        UIManager.put("ProgressBar.font", vFont);
        UIManager.put("DesktopIcon.font", vFont);
        UIManager.put("TitledBorder.font", vFont);
        UIManager.put("Label.font", vFont);
        UIManager.put("List.font", vFont);
        UIManager.put("TabbedPane.font", vFont);
        UIManager.put("MenuBar.font", vFont);
        UIManager.put("Menu.font", vFont);
        UIManager.put("MenuItem.font", vFont);
        UIManager.put("PopupMenu.font", vFont);
        UIManager.put("CheckBoxMenuItem.font", vFont);
        UIManager.put("RadioButtonMenuItem.font", vFont);
        UIManager.put("Spinner.font", vFont);
        UIManager.put("Tree.font", vFont);
        UIManager.put("ToolBar.font", vFont);
        UIManager.put("OptionPane.messageFont", vFont);
        UIManager.put("OptionPane.buttonFont", vFont);

        UIManager.put("Tree.paintLines", false); // Tree
        UIManager.put("Tree.leftChildIndent", 0); //  

        UIManager.put("Tree.arrowDown", getIconByKey("main/arrowDown")); // Tree
        UIManager.put("Tree.arrowLeft", getIconByKey("main/arrowLeft")); //  

        WebLookAndFeel.globalControlFont = vFont;
        WebLookAndFeel.globalTextFont = vFont;
        WebLookAndFeel.globalTitleFont = vFont;
        WebLookAndFeel.globalTooltipFont = vFont;
        WebLookAndFeel.globalAcceleratorFont = vFont;
        WebLookAndFeel.menuItemAcceleratorFont = vFont;
        WebLookAndFeel.globalAlertFont = vFont;
        WebLookAndFeel.globalMenuFont = vFont;

        // ?weblaf
        WebLookAndFeel.setDecorateAllWindows(false);
        WebLookAndFeel.install();
        Locale.setDefault(Locale.CHINA);
    }
}