uimanager.java

来自「j2me设计的界面包」· Java 代码 · 共 499 行 · 第 1/2 页

JAVA
499
字号
     */
    public void setThemeProps(Hashtable themeProps) {
        if(accessible) {
            setThemePropsImpl(themeProps);
        }
    }
    
    void setThemePropsImpl(Hashtable themeProps) {
        resetThemeProps();
        Enumeration e = themeProps.keys();
        while(e.hasMoreElements()) {
            Object key = e.nextElement();
            this.themeProps.put(key, themeProps.get(key));
        }
        styles.clear();
        imageCache.clear();
        
        // necessary to clear up the style so we don't get resedue from the previous UI
        defaultStyle = new Style();
        
        //create's the default style
        defaultStyle = createStyle("");
    }
    
    private Style createStyle(String id) {
        Style style = new Style(defaultStyle);
        if(themeProps != null){
            String bgColor = (String)themeProps.get(id + Style.BG_COLOR);
            String fgColor = (String)themeProps.get(id + Style.FG_COLOR);
            Object bgImage = (Object)themeProps.get(id + Style.BG_IMAGE);
            String bgSelColor = (String)themeProps.get(id + Style.BG_SELECTION_COLOR);
            String fgSelColor = (String)themeProps.get(id + Style.FG_SELECTION_COLOR);
            String transperency = (String)themeProps.get(id + Style.TRANSPARENCY);
            String margin = (String)themeProps.get(id + Style.MARGIN);
            String padding = (String)themeProps.get(id + Style.PADDING);
            Object font = (Object)themeProps.get(id + Style.FONT);
            Object border = (Object)themeProps.get(id + Style.BORDER);
            Object scale = (Object)themeProps.get(id + Style.SCALED_IMAGE);
            if(bgColor != null){
                style.setBgColor(Integer.valueOf(bgColor, 16).intValue());
            }
            if(fgColor != null){
                style.setFgColor(Integer.valueOf(fgColor, 16).intValue());
            }
            if(bgSelColor != null){
                style.setBgSelectionColor(Integer.valueOf(bgSelColor, 16).intValue());
            }
            if(fgSelColor != null){
                style.setFgSelectionColor(Integer.valueOf(fgSelColor, 16).intValue());
            }
            if(transperency != null){
                style.setBgTransparency(Integer.valueOf(transperency).intValue());
            }
            if(margin != null){
                int [] marginArr = toIntArray(margin.trim());
                style.setMargin(marginArr[0], marginArr[1], marginArr[2], marginArr[3]);
            } 
            if(padding != null){
                int [] paddingArr = toIntArray(padding.trim());
                style.setPadding(paddingArr[0], paddingArr[1], paddingArr[2], paddingArr[3]);
            }
            if(scale != null){
                style.setScaleImage(!scale.equals("false"));
            }
            if(bgImage != null){
                Image im = null;
                if(bgImage instanceof String){
                    try {
                        String bgImageStr = (String)bgImage;
                        if(imageCache.contains(bgImageStr)) {
                            im = (Image)imageCache.get(bgImageStr);
                        } else { 
                            if(bgImageStr.startsWith("/")) {
                                im = Image.createImage(bgImageStr);
                            } else {
                                im = parseImage((String)bgImage);
                            }
                            imageCache.put(bgImageStr, im);
                        }
                        themeProps.put(id + Style.BG_IMAGE, im);
                    } catch (IOException ex) {
                        System.out.println("failed to parse image for id = "+id + Style.BG_IMAGE);
                    }
                }else{
                    im = (Image)bgImage;
                }
                if(id.indexOf("Form") > -1){
                    if((im.getWidth() != Display.getInstance().getDisplayWidth() || 
                       im.getHeight() != Display.getInstance().getDisplayHeight())
                       && style.isScaleImage()) {
                       im.scale(Display.getInstance().getDisplayWidth(), 
                               Display.getInstance().getDisplayHeight());
                    }
                }
                style.setBgImage(im);
            }
            if(font != null){
                if(font instanceof String){
                    style.setFont(parseFont((String)font));
                }else{
                    style.setFont((com.sun.lwuit.Font)font);
                }
            }
            
            style.setBorder((Border)border);
            style.resetModifiedFlag();
        } 
        
        return style;
    }
    
    /**
     * This method is used to parse the margin and the padding
     * @param str
     * @return
     */
    private int [] toIntArray(String str){
        int [] retVal = new int[4];
        str  = str + ",";
        for(int i=0; i< retVal.length; i++){
            retVal[i] = Integer.parseInt(str.substring(0, str.indexOf(",")));
            str = str.substring(str.indexOf(",") + 1, str.length());
        }
        return retVal;
    }
    
    
    private static Image parseImage(String value) throws IOException {
        int index = 0;
        byte [] imageData = new byte[value.length()/2];
        while(index < value.length()){
            String byteStr = value.substring(index, index + 2);
            imageData[index/2] = Integer.valueOf(byteStr, 16).byteValue();
            index += 2;
        }
        ByteArrayInputStream in = new ByteArrayInputStream(imageData);
        Image image = Image.createImage(in);
        in.close();
        return image;
    }
        
    private static com.sun.lwuit.Font parseFont(String fontStr) {
        if(fontStr.startsWith("System")){
            int face = 0;
            int style = 0;
            int size = 0;
            String faceStr, styleStr, sizeStr;
            String sysFont = fontStr.substring(fontStr.indexOf("{") + 1, fontStr.indexOf("}"));
            faceStr = sysFont.substring(0, sysFont.indexOf(";"));
            sysFont = sysFont.substring(sysFont.indexOf(";") + 1, sysFont.length());
            styleStr = sysFont.substring(0, sysFont.indexOf(";"));
            sizeStr = sysFont.substring(sysFont.indexOf(";") + 1, sysFont.length());
            
            if(faceStr.indexOf("FACE_SYSTEM") > -1){
                face = Font.FACE_SYSTEM;
            }else if(faceStr.indexOf("FACE_MONOSPACE") > -1){
                face = Font.FACE_MONOSPACE;
            }else if(faceStr.indexOf("FACE_PROPORTIONAL") > -1){
                face = Font.FACE_PROPORTIONAL;
            }
            
            if(styleStr.indexOf("STYLE_PLAIN") > -1){
                style = Font.STYLE_PLAIN;
            }else{
                if(styleStr.indexOf("STYLE_BOLD") > -1){
                    style = Font.STYLE_BOLD;
                }
                if(styleStr.indexOf("STYLE_ITALIC") > -1){
                    style = style | Font.STYLE_ITALIC;
                }
                if(styleStr.indexOf("STYLE_UNDERLINED") > -1){
                    style = style | Font.STYLE_UNDERLINED;
                }
            }
            
            if(sizeStr.indexOf("SIZE_SMALL") > -1){
                size = Font.SIZE_SMALL;
            }else if(sizeStr.indexOf("SIZE_MEDIUM") > -1){
                size = Font.SIZE_MEDIUM;
            }else if(sizeStr.indexOf("SIZE_LARGE") > -1){
                size = Font.SIZE_LARGE;
            }
            
            
            return com.sun.lwuit.Font.createSystemFont(face, style, size);            
        } else {
            if(fontStr.toLowerCase().startsWith("bitmap")) {
                try {
                    String bitmapFont = fontStr.substring(fontStr.indexOf("{") + 1, fontStr.indexOf("}"));
                    String nameStr;
                    nameStr = bitmapFont.substring(0, bitmapFont.length());

                    
                    if(nameStr.toLowerCase().startsWith("highcontrast")) {
                        nameStr = nameStr.substring(nameStr.indexOf(";") + 1, nameStr.length());
                        com.sun.lwuit.Font f = com.sun.lwuit.Font.getBitmapFont(nameStr);
                        f.addContrast((byte)30);
                        return f;
                    }
                    
                    return com.sun.lwuit.Font.getBitmapFont(nameStr);
                } catch (Exception ex) {
                    // illegal argument exception?
                    ex.printStackTrace();
                }
            }
        }
        // illegal argument?
        return null;
    }
    
    /**
     * The resource bundle allows us to implicitly localize the UI on the fly, once its
     * installed all internal application strings query the resource bundle and extract
     * their values from this table if applicable.
     */
    public Hashtable getResourceBundle() {
        return resourceBundle;
    }

    /**
     * The resource bundle allows us to implicitly localize the UI on the fly, once its
     * installed all internal application strings query the resource bundle and extract
     * their values from this table if applicable.
     */
    public void setResourceBundle(Hashtable resourceBundle) {
        this.resourceBundle = resourceBundle;
    }
    
    /**
     * Localizes the given string from the resource bundle if such a String exists in the
     * resource bundle. If no key exists in the bundle then or a bundle is not installed
     * the default value is returned.
     * 
     * @param key The key used to lookup in the resource bundle
     * @param defaultValue the value returned if no such key exists
     * @return either default value or the appropriate value
     */
    public String localize(String key, String defaultValue) {
        if(resourceBundle != null) {
            Object o = resourceBundle.get(key);
            if(o != null) {
                return (String)o;
            }
        }
        return defaultValue;
    }
}

⌨️ 快捷键说明

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