uimanager.java

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

JAVA
499
字号
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package com.sun.lwuit.plaf;

import com.sun.lwuit.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Central point singleton managing the look of the application, this class allows us to
 * customize the styles (themes) as well as the look instance.
 *
 * @author Chen Fishbein
 */
public class UIManager {
    private LookAndFeel current = new DefaultLookAndFeel();
    
    private Hashtable styles = new Hashtable();
    
    private Hashtable themeProps;
    
    private static UIManager instance = new UIManager();
    
    private Style defaultStyle = new Style();

    /**
     * This member is used by the resource editor
     */
    static boolean accessible = true;
    
    /**
     * Useful for caching theme images so they are not loaded twice in case 
     * an image reference is used it two places in the theme (e.g. same background
     * to title and menu bar).
     */
    private Hashtable imageCache = new Hashtable();
    
    /**
     * 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.
     */
    private Hashtable resourceBundle;
    
    private UIManager(){
        resetThemeProps();
    }
    
    /**
     * Singleton instance method
     * 
     * @return Instance of the ui manager
     */
    public static UIManager getInstance(){
        return instance;
    }
    
    /**
     * Returns the currently installed look and feel
     * 
     * @return the currently installed look and feel
     */
    public LookAndFeel getLookAndFeel(){
        return current;
    }
    
    /**
     * Sets the currently installed look and feel
     */    
    public void setLookAndFeel(LookAndFeel plaf){
        current.uninstall();
        current = plaf;
    }
    
    /**
     * Allows a developer to programmatically install a style into the UI manager
     * 
     * @param id the component id matching the given style
     * @param style the style object to install
     */
    public void setComponentStyle(String id, Style style) {
        if(id == null || id.length() == 0){
            //if no id return the default style
            id = "";
        } else {
            id = id + ".";
        }
        
        styles.put(id, style);
    }
    
    /**
     * Returns the style of the component with the given id or a <b>new instance</b> of the default
     * style.
     * This method will always return a new style instance to prevent modification of the global
     * style object.
     * 
     * @return the appropriate style (this method never returns null)
     */
    public Style getComponentStyle(String id){
        Style style = null;
        
        if(id == null || id.length() ==0){
            //if no id return the default style
            id = "";
        }else{
            id = id + ".";
        }
        
        style = (Style)styles.get(id);
        
        if(style == null){
            style = createStyle(id);
            styles.put(id, style);
        }
        
        return new Style(style);
    }
    
    /**
     * Loads a theme from the given input stream, the theme can be specified in any
     * stream using the following notation:
     * <pre>
     * Buttonfont=System{FACE_SYSTEM;STYLE_PLAIN;SIZE_SMALL}
     * LabelfgColor=333333
     * </pre>
     * @deprecated the system of loading a properties file is currently deprecated in favor
     * of the newer resource file format.
     */
    public void loadTheme(InputStream input) throws IOException {
        Hashtable theme = new Hashtable();
        byte[] buffer = new byte[1024];
        int size = input.read(buffer);
        StringBuffer stringBuffer = new StringBuffer();
        while(size > -1) {
            stringBuffer.append(new String(buffer));
            size = input.read(buffer);
        }        
        String file = stringBuffer.toString();
        stringBuffer = null;
        
        int previousOffset = 0;
        int fileOffset = nextNewline(file, 0);
        
        
        while(previousOffset < file.length() && previousOffset > -1) {
            String recordStr = file.substring(previousOffset, fileOffset);
            char currentChar = Character.toUpperCase(recordStr.charAt(0));
            while((currentChar < 'A' || currentChar > 'Z') && recordStr.length() > 1) {
                recordStr = recordStr.substring(1, recordStr.length());
                currentChar = Character.toUpperCase(recordStr.charAt(0));
            }
            previousOffset = fileOffset;
            fileOffset = nextNewline(file, previousOffset + 1);
            if(recordStr.indexOf("=") > 0 && !recordStr.startsWith("#")){
                //remove spaces
                int spacesLoc = -1;
                while((spacesLoc = recordStr.indexOf(" ")) > -1){
                    recordStr = recordStr.substring(0, spacesLoc) + recordStr.substring(spacesLoc + 1, recordStr.length());
                }
                //extract key and value
                String key = recordStr.substring(0, recordStr.indexOf("="));
                String value = recordStr.substring(recordStr.indexOf("=") + 1, recordStr.length());
                while(value.charAt(value.length() - 1) == '\n' || value.charAt(value.length() - 1) == '\r') {
                    value = value.substring(0, value.length() - 1);
                }
                theme.put(key, value);
            }
        }
        setThemeProps(theme);
    }
    
    private int nextNewline(String str, int offset) {
        int newline = str.indexOf('\n', offset);
        if(newline < 0) {
            return str.length();
        }
        return newline;
    }
    
    /**
     * @return the name of the current theme for theme switching UI's
     */
    public String getThemeName(){
        if(themeProps != null){
            return (String)themeProps.get("name");
        }
        return null;
    }
    
    /**
     * Initializes the theme properties with the current "defaults"
     */
    private void resetThemeProps() {
        themeProps = new Hashtable();
        themeProps.put("Button.border", Border.getDefaultBorder());
        themeProps.put("TextArea.border", Border.getDefaultBorder());
        themeProps.put("List.border", Border.getDefaultBorder());
        themeProps.put("TextField.border", Border.getDefaultBorder());
        themeProps.put("ComboBox.border", Border.getDefaultBorder());
        themeProps.put("ComboBoxPopup.border", Border.getDefaultBorder());
        themeProps.put("Title.margin", "0,0,0,0");
        themeProps.put("CommandList.margin", "0,0,0,0");
        themeProps.put("CommandList.transparency", "0");
        themeProps.put("Container.transparency", "0");
        themeProps.put("ComboBoxPopup.transparency", "0");
        themeProps.put("List.transparency", "0");
        themeProps.put("SoftButton.transparency", "255");
        themeProps.put("List.margin", "0,0,0,0");
        themeProps.put("SoftButton.margin", "0,0,0,0");
        themeProps.put("SoftButton.padding", "2,2,2,2");
        themeProps.put("Container.margin", "0,0,0,0");
        themeProps.put("Container.padding", "0,0,0,0");
        themeProps.put("Title.transparency", "255");
        themeProps.put("TabbedPane.margin", "0,0,0,0");
        themeProps.put("TabbedPane.padding", "0,0,0,0");
        themeProps.put("ScrollThumb.padding", "0,0,0,0");
        themeProps.put("ScrollThumb.margin", "0,0,0,0");
        themeProps.put("Form.padding", "0,0,0,0");
        themeProps.put("Form.margin", "0,0,0,0");
        themeProps.put(Style.TRANSPARENCY, "255");
    }
    
    /**
     * Allows manual theme loading from a hashtable of key/value pairs

⌨️ 快捷键说明

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