skinresourcesimpl.jpp
来自「This is a resource based on j2me embedde」· JPP 代码 · 共 702 行 · 第 1/2 页
JPP
702 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program 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. * * This program 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 at /legal/license.txt). * * 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 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.midp.chameleon.skins.resources;import com.sun.midp.security.*;import com.sun.midp.log.*;import com.sun.midp.util.ResourceHandler;import com.sun.midp.configurator.Constants;import com.sun.midp.chameleon.skins.ScreenSkin;import com.sun.midp.chameleon.skins.TickerSkin;import com.sun.midp.chameleon.skins.TitleSkin;import com.sun.midp.chameleon.skins.AlertSkin;import com.sun.midp.lcdui.DisplayAccess;import com.sun.midp.lcdui.GraphicsAccess;import com.sun.midp.io.j2me.storage.File;// #ifndef ENABLE_CDCimport com.sun.midp.main.MIDletSuiteUtils;// #endifimport javax.microedition.lcdui.Image;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import java.io.*;/** Utility class for skin resources. */public class SkinResourcesImpl extends SkinResources { /** Constant to distinguish image resources with no index */ private static final int NO_INDEX = -1; /** * Loaded skin resources. This object is used as resources * cache, so we don't have to load same resource again. In * MVM case this object is shared between Isolates, so all * Isolates can benefit from caching. However, only AMS * Isolate can put resources into the cache. */ private static LoadedSkinResources resources; /** * Loaded skin properties. In MVM case, this object is shared * between Isolates. */ private static LoadedSkinData skinData; /** * Inner class to request security token from SecurityInitializer. * SecurityInitializer should be able to check this inner class name. */ static private class SecurityTrusted implements ImplicitlyTrustedClass {}; /** A private internal reference to the system security token */ private static SecurityToken secureToken = SecurityInitializer.requestToken(new SecurityTrusted()); /** * True, if skin is being loaded in AMS Isolate in MVM case, * false otherwise. Always true in SVM case. */ private static boolean isAmsIsolate = true; /** * This class needs no real constructor, but its here as 'public' * so the SecurityIntializer can do a newInstance() on it and call * the initSecurityToken() method. */ public SkinResourcesImpl() { } /** * Load the skin, including all its properties and images. Some parts * of the skin may be lazily initialized, but this method starts the * process. If the flag to 'reload' is true, the method will ignore * all previously loaded resources and go through the process again. * * @param reload if true, ignore previously loaded resources and reload * @throws IOException if there was error reading skin data file * @throws IllegalStateException if skin data file is invalid */ public void loadSkin(boolean reload) throws IllegalStateException, IOException { resources = null; skinData = null;// #ifndef ENABLE_CDC isAmsIsolate = MIDletSuiteUtils.isAmsIsolate();// #endif if (reload) { if (Logging.REPORT_LEVEL <= Logging.CRITICAL) { Logging.report(Logging.CRITICAL, LogChannels.LC_HIGHUI, "Skin reloading isn't implemented yet"); } } if (!isAmsIsolate) { resources = (LoadedSkinResources)getSharedResourcePool(); skinData = (LoadedSkinData)getSharedSkinData(); } else { resources = new LoadedSkinResources(); skinData = new LoadedSkinData(); String fileName = File.getConfigRoot(Constants.INTERNAL_STORAGE_ID) + SkinResourcesConstants.SKIN_BINARY_FILE_NAME; skinData.readFromFile(fileName); resources.fonts = new Font[skinData.fontValues.length]; resources.images = new Image[skinData.imageValues.length]; // For MVM, share resources cache and loaded properties // between Isolates. For SVM, those methods do nothing. shareResourcePool(resources); shareSkinData(skinData); } // NOTE: The remaining properties classes need to be set and // possibly re-loaded lazily. // IMPL_NOTE if (reload), need to signal the LF classes in a way they // will re-load their skin resources on demand } /** * Utility method used by skin property classes to load * image resources. * * @param identifier a unique identifier for the image property * @return the Image if one is available, null otherwise */ public Image getImage(int identifier) { return getImage(identifier, NO_INDEX); } /** * Utility method used by skin property classes to load * image resources. * * @param identifier a unique identifier for the image property * @param index index of the image * * @return the Image if one is available, null otherwise */ public Image getImage(int identifier, int index) { int imageIdx = 0; String imageIdentifier = null; Image image = null; boolean isLoaded = false; // If we're set not to bother loading images, just return null if (!Constants.CHAM_USE_IMAGES) { return null; } try { imageIdx = skinData.properties[identifier]; if (index != NO_INDEX) { imageIdx += index; } // Check if this resource alread has been loaded image = resources.images[imageIdx]; if (image != null) { // image already has been loaded isLoaded = true; } else { byte[] imageData; imageIdentifier = skinData.imageValues[imageIdx]; if (imageIdentifier.length() == 0) { // image hasn't been specified return null; } if (imageIdentifier == null) { if (Logging.REPORT_LEVEL <= Logging.CRITICAL) { Logging.report(Logging.CRITICAL, LogChannels.LC_HIGHUI, "No value for skin property: id = " + identifier); } return null; } // Try to load the romized image if (Constants.CHAM_ROMIZED_IMAGES && graphicsAccess != null) { int romIndex = skinData.romizedImageIndexes[imageIdx]; if (romIndex != -1) { int imageDataPtr = getRomizedImageDataArrayPtr( romIndex); int imageDataLength = getRomizedImageDataArrayLength( romIndex); image = graphicsAccess.getRomizedImage(imageDataPtr, imageDataLength); } } // Image is not romized, load from filesystem if (image == null) { // Try to load the PNG image from the filesystem imageData = ResourceHandler.getSystemResource( secureToken, imageIdentifier + ".png"); // Try to load the raw image from the filesystem if (imageData == null) { imageData = ResourceHandler.getSystemResource( secureToken, imageIdentifier + ".raw"); } if (imageData != null) { image = Image.createImage( imageData, 0, imageData.length); } } } } catch (Throwable t) { if (Logging.REPORT_LEVEL <= Logging.CRITICAL) { Logging.report(Logging.CRITICAL, LogChannels.LC_HIGHUI, "Exception caught while loading Image resource " + imageIdentifier + ": " + t); } } if (image != null) { if (!isLoaded && isAmsIsolate) { // add resource to shared resources pool resources.images[imageIdx] = image; } } else { if (Logging.REPORT_LEVEL <= Logging.CRITICAL) { Logging.report(Logging.CRITICAL, LogChannels.LC_HIGHUI, "No resource found when loading Image resource: " + imageIdentifier); } } return image; } /** * Utility method used by skin property classes to load * composite image resources consisting of a few images. * * @param identifier a unique identifier for the composite image property * @param piecesNumber number of pieces consisting the composite image * * @return the Image[] with loaded image pieces, * or null if some of the pieces is not available */ public Image[] getCompositeImage( int identifier, int piecesNumber) { Image[] result = new Image[piecesNumber]; for (int i = 0; i < piecesNumber; i++) { result[i] = getImage(identifier, i); if (result[i] == null) { result = null; break; } } return result; } /** * Utility method used by skin property classes to load * Font resources. * * @param identifier a unique identifier for the Font property * @return the Font object or null in case of error */ public Font getFont(int identifier) { int fontIdx = 0; int fontIdentifier = 0; Font font = null; boolean isLoaded = false; try { fontIdx = skinData.properties[identifier]; // Check if this resource alread has been loaded font = resources.fonts[fontIdx]; if (font != null) { isLoaded = true; } else { fontIdentifier = skinData.fontValues[fontIdx]; font = FontResources.getFont(fontIdentifier); } } catch (Exception e) { if (Logging.REPORT_LEVEL <= Logging.CRITICAL) { Logging.report(Logging.CRITICAL, LogChannels.LC_HIGHUI, "Exception caught while loading Font resource: " + identifier + ": " + e); } } if (font != null) { if (!isLoaded && isAmsIsolate) { // add resource to shared resources pool resources.fonts[fontIdx] = font; } } else { if (Logging.REPORT_LEVEL <= Logging.CRITICAL) { Logging.report(Logging.CRITICAL, LogChannels.LC_HIGHUI, "No resource found when loading Font resource: " + fontIdentifier); } font = FontResources.getFont(500); } return font; } /** * Utility method used by skin property classes to load * String resources. * * @param identifier a unique identifier for the String property * @return the String object or null in case of error */ public String getString(int identifier) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?