📄 resources.java
字号:
/* * 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.util;import com.sun.lwuit.Font;import com.sun.lwuit.Image;import com.sun.lwuit.IndexedImage;import com.sun.lwuit.StaticAnimation;import com.sun.lwuit.plaf.Border;import java.io.ByteArrayInputStream;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;/** * Loads resources from the binary resource file generated during the build process. * A resource is loaded entirely into memory since random file access is not supported * in Java ME, any other approach would be inefficient. This means that memory must * be made available to accomodate the resource file. * * @author Shai Almog */public class Resources { /** * Magic numbers to prevent data corruption */ static final byte MAGIC_IMAGE = (byte)0xF3; static final byte MAGIC_PACKED_IMAGE8 = (byte)0xF4; static final byte MAGIC_FONT = (byte)0xF6; static final byte MAGIC_THEME = (byte)0xF7; static final byte MAGIC_ANIMATION = (byte)0xF8; static final byte MAGIC_L10N = (byte)0xF9; static final byte MAGIC_DATA = (byte)0xFA; static final byte MAGIC_PACKED_FONT = (byte)0xFB; /** * Temporary member for compatibility with older versions, in future versions * this will superceed the MAGIC_THEME property */ static final byte MAGIC_THEME_2 = (byte)0xF2; static final int BORDER_TYPE_EMPTY = 0; static final int BORDER_TYPE_LINE = 1; static final int BORDER_TYPE_ROUNDED = 2; static final int BORDER_TYPE_ETCHED_LOWERED = 4; static final int BORDER_TYPE_ETCHED_RAISED = 5; static final int BORDER_TYPE_BEVEL_RAISED = 6; static final int BORDER_TYPE_BEVEL_LOWERED = 7; static final int BORDER_TYPE_IMAGE = 8; // for use by the resource editor private static Class classLoader = Resources.class; static void setClassLoader(Class cls) { classLoader = cls; } /** * Hashtable containing the mapping between element types and their names in the * resource hashtable */ private Hashtable resourceTypes = new Hashtable(); /** * A cache within the resource allowing us to preserve some resources in memory * so they can be utilized by a theme when it is loaded */ private Hashtable resources = new Hashtable(); private DataInputStream input; // for internal use by the resource editor, creates an empty resource Resources() { } Resources(InputStream input) throws IOException { openFile(input); } void clear() { resourceTypes.clear(); resources.clear(); input = null; } void openFile(InputStream input) throws IOException { clear(); this.input = new DataInputStream(input); int resourceCount = this.input.readShort(); for(int iter = 0 ; iter < resourceCount ; iter++) { byte magic = this.input.readByte(); String id = this.input.readUTF(); switch(magic) { case MAGIC_IMAGE: setResource(id, magic, createImage()); continue; case MAGIC_PACKED_IMAGE8: setResource(id, magic, createPackedImage8()); continue; case MAGIC_THEME: case MAGIC_THEME_2: setResource(id, MAGIC_THEME_2, loadTheme(id, magic == MAGIC_THEME_2)); continue; case MAGIC_FONT: setResource(id, magic, loadFont(id, false)); continue; case MAGIC_PACKED_FONT: setResource(id, magic, loadFont(id, true)); continue; case MAGIC_ANIMATION: setResource(id, magic, loadAnimation(id, this.input)); continue; case MAGIC_DATA: setResource(id, magic, createData()); continue; case MAGIC_L10N: setResource(id, magic, loadL10N()); continue; default: throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff)); } } } /** * Returns the names of the resources within this bundle * * @return array of names of all the resources in this bundle */ public String[] getResourceNames() { String[] arr = new String[resourceTypes.size()]; Enumeration e = resourceTypes.keys(); for(int iter = 0 ; iter < arr.length ; iter++) { arr[iter] = (String)e.nextElement(); } return arr; } /** * Returns the names of the data resources within this bundle * * @return array of names of the data resources in this bundle */ public String[] getDataResourceNames() { return getResourceTypeNames(MAGIC_DATA); } /** * For internal use only */ void setResource(String id, byte type, Object value) { if(value == null) { resources.remove(id); resourceTypes.remove(id); } else { resources.put(id, value); resourceTypes.put(id, new Byte(type)); } } /** * Returns the names of the localization bundles within this bundle * * @return array of names of the localization resources in this bundle */ public String[] getL10NResourceNames() { return getResourceTypeNames(MAGIC_L10N); } /** * Returns the names of the fonts within this bundle * * @return array of names of the font resources in this bundle */ public String[] getFontResourceNames() { Vector vec = new Vector(); Enumeration e = resourceTypes.keys(); while(e.hasMoreElements()) { String c = (String)e.nextElement(); if(isFont(c)) { vec.addElement(c); } } return toStringArray(vec); } /** * Returns the names of the images within this bundle * * @return array of names of the image resources in this bundle */ public String[] getThemeResourceNames() { Vector vec = new Vector(); Enumeration e = resourceTypes.keys(); while(e.hasMoreElements()) { String c = (String)e.nextElement(); if(isTheme(c)) { vec.addElement(c); } } return toStringArray(vec); } /** * Returns the names of the images within this bundle * * @return array of names of the image resources in this bundle */ public String[] getImageResourceNames() { Vector vec = new Vector(); Enumeration e = resourceTypes.keys(); while(e.hasMoreElements()) { String c = (String)e.nextElement(); if(isImage(c)) { vec.addElement(c); } } return toStringArray(vec); } /** * Returns the names of the animations within this bundle * * @return array of names of the animation resources in this bundle */ public String[] getAnimationResourceNames() { return getResourceTypeNames(MAGIC_ANIMATION); } /** * For internal use only */ byte getResourceType(String name) { return ((Byte)resourceTypes.get(name)).byteValue(); } private String[] getResourceTypeNames(byte b) { Vector vec = new Vector(); Enumeration e = resourceTypes.keys(); while(e.hasMoreElements()) { String c = (String)e.nextElement(); if(((Byte)resourceTypes.get(c)).byteValue() == b) { vec.addElement(c); } } return toStringArray(vec); } private static String[] toStringArray(Vector v) { String[] s = new String[v.size()]; for(int iter = 0 ; iter < s.length ; iter++) { s[iter] = (String)v.elementAt(iter); } return s; } /** * Returns true if this is a generic data resource * * @param name the name of the resource * @return true if the resource is a data resource * @throws NullPointerException if the resource doesn't exist */ public boolean isL10N(String name) { byte b = ((Byte)resourceTypes.get(name)).byteValue(); return b == MAGIC_L10N; } /** * Returns true if this is a theme resource * * @param name the name of the resource * @return true if the resource is a theme * @throws NullPointerException if the resource doesn't exist */ public boolean isTheme(String name) { byte b = ((Byte)resourceTypes.get(name)).byteValue(); return b == MAGIC_THEME || b == MAGIC_THEME_2; } /** * Returns true if this is a font resource * * @param name the name of the resource * @return true if the resource is a font * @throws NullPointerException if the resource doesn't exist */ public boolean isFont(String name) { byte b = ((Byte)resourceTypes.get(name)).byteValue(); return b == MAGIC_FONT || b == MAGIC_PACKED_FONT; } /** * Returns true if this is an animation resource * * @param name the name of the resource * @return true if the resource is an animation * @throws NullPointerException if the resource doesn't exist */ public boolean isAnimation(String name) { byte b = ((Byte)resourceTypes.get(name)).byteValue(); return b == MAGIC_ANIMATION; } /** * Returns true if this is a data resource * * @param name the name of the resource * @return true if the resource is a data resource * @throws NullPointerException if the resource doesn't exist */ public boolean isData(String name) { byte b = ((Byte)resourceTypes.get(name)).byteValue(); return b == MAGIC_DATA; } /** * Returns true if this is an image resource * * @param name the name of the resource * @return true if the resource is an image * @throws NullPointerException if the resource doesn't exist */ public boolean isImage(String name) { byte b = ((Byte)resourceTypes.get(name)).byteValue(); return b == MAGIC_IMAGE || b == MAGIC_PACKED_IMAGE8; } /** * Creates a resource object from the local JAR resource identifier * * @param resource a local reference to a resource using the syntax of Class.getResourceAsStream(String) * @return a resource object * @throws java.io.IOException if opening/reading the resource fails */ public static Resources open(String resource) throws IOException { try { return new Resources(classLoader.getResourceAsStream(resource)); } catch(RuntimeException err) { // intercept exceptions since user code might not deal well with runtime exceptions err.printStackTrace(); throw new IOException(err.getMessage()); } } StaticAnimation loadAnimation(String name, DataInputStream input) throws IOException { return StaticAnimation.createAnimation(input); } /** * Creates a resource object from the given input stream * * @param resource stream from which to read the resource * @return a resource object * @throws java.io.IOException if opening/reading the resource fails */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -