⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swtfontfactory.java

📁 Java开发图文混排的编辑器
💻 JAVA
字号:
/*
 * Created on 2004-7-27
 * Author: Xuefeng, Copyright (C) 2004, Xuefeng.
 */
package jexi.ui.swt;

import java.util.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

import jexi.ui.*;

/**
 * The implementation of FontFactory. 
 * 
 * @author Xuefeng
 */
public class SWTFontFactory extends FontFactory {

    // store all fonts that is used now:
    private Hashtable fonts = new Hashtable();

    // store all fonts' name:
    private String[] fontNames = null;

    // we assume that the Display will always available:
    private Display display = null;

    /**
     * Create new font. 
     * 
     * @see jexi.ui.FontFactory#createFont(java.lang.String, int, boolean, boolean, boolean)
     */
    public jexi.ui.Font createFont(String name, int size, boolean bold, boolean italic, boolean underlined) {
        // first lookup if it already existed:
        String key = jexi.ui.swt.SWTFont.toKey(name, size, bold, italic, underlined);
        Object obj = fonts.get(key);
        if(obj!=null) {
            // found it!
            jexi.ui.swt.SWTFont _font = (jexi.ui.swt.SWTFont)obj;
            _font.addRef();
            return _font;
        }

        if(display==null) {
            SWTFrame frame = (SWTFrame)Application.instance().getFrame();
            display = frame.getDisplay();
        }
        // create real font resource:
        int style = ( bold ? SWT.BOLD : SWT.NORMAL ) | ( italic ? SWT.ITALIC : SWT.NORMAL );
        org.eclipse.swt.graphics.Font f = new org.eclipse.swt.graphics.Font(
            display, name, size, style);
        // wrap as jexi.ui.Font:
        jexi.ui.Font font = new jexi.ui.swt.SWTFont(name, size, bold, italic, underlined, f);
        // cache it:
        fonts.put(font.toString(), font);
        return font;
    }

    // enumerate all fonts installed in the system:
    public String[] enumerateFonts() {
        // TODO Auto-generated method stub
        if(fontNames==null) {
            fontNames = new String[3];
            fontNames[0] = "Arial";
            fontNames[1] = "System";
            fontNames[2] = "Tahoma";
        }
        return fontNames;
    }

    /**
     * dispose all fonts it used now. 
     * 
     * @see jexi.ui.FontFactory#clearAllFonts()
     */
    public void clearAllFonts() {
        Collection all_fonts = new ArrayList( fonts.values() );
        Iterator it = all_fonts.iterator();
        while(it.hasNext()) {
            jexi.ui.swt.SWTFont font = (jexi.ui.swt.SWTFont)it.next();
            while(font.refCount()>0) {
                System.out.println("WARNING: Some fonts are not released.");
                font.dispose();
            }
        }
        // clear hash table:
        fonts.clear();
    }

    /**
     * Remove the font from the cache. 
     * 
     */
    protected void remove(jexi.ui.Font font) {
        fonts.remove(font.toString());
    }

    /**
     * Get the count of current used fonts. 
     *  
     * @see jexi.ui.FontFactory#fontCount()
     */
    public int fontCount() {
        return fonts.size();
    }

    public void debug() {
        System.out.println("\n[SWTFontFactory] fonts=" + fonts.size());
        Collection all_fonts = fonts.values();
        Iterator it = all_fonts.iterator();
        while(it.hasNext()) {
            jexi.ui.swt.SWTFont font = (jexi.ui.swt.SWTFont)it.next();
            font.debug();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -