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

📄 stylesheet.java

📁 j2me polish学习的经典代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Created on 05-Jan-2004 at 20:41:52. * * Copyright (c) 2004-2005 Robert Virkus / Enough Software * * This file is part of J2ME Polish. * * J2ME Polish is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. *  * J2ME Polish 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 for more details. *  * You should have received a copy of the GNU General Public License * along with J2ME Polish; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *  * Commercial licenses are also available, please * refer to the accompanying LICENSE.txt or visit * http://www.j2mepolish.org for details. */package de.enough.polish.ui;import java.io.IOException;import java.util.Enumeration;import java.util.Hashtable;import java.util.Timer;import javax.microedition.lcdui.*;import de.enough.polish.ui.tasks.ImageTask;import de.enough.polish.util.Locale;/** * <p>Manages all defined styles of a specific project.</p> * <p>This class is actually pre-processed to get the styles specific for the project and the device.</p> * * @author Robert Virkus, robert@enough.de * <pre> * history *        05-Jan-2004 - rob creation * </pre> */public final class StyleSheet {		protected static Hashtable imagesByName;	//#ifdef polish.images.backgroundLoad		private static Hashtable scheduledImagesByName;		//private static final Boolean TRUE = new Boolean( true );		private static Timer timer;	//#endif	//#ifdef false		public static Style defaultStyle = null;		public static Style focusedStyle = null;		public static Style labelStyle = null; 		public static Style menuStyle = null;		private static Hashtable stylesByName = new Hashtable();	//#endif	//#if polish.ScreenChangeAnimation.forward:defined		//#if false			private static ScreenChangeAnimation forwardAnimation;			private static ScreenChangeAnimation backwardAnimation;					//#endif		//#= private static ScreenChangeAnimation forwardAnimation = ${polish.ScreenChangeAnimation.forward};		//#if polish.ScreenChangeAnimation.back:defined			//#= private static ScreenChangeAnimation backwardAnimation = ${polish.ScreenChangeAnimation.back};		//#elif polish.ScreenChangeAnimation.backward:defined			//#= private static ScreenChangeAnimation backwardAnimation = ${polish.ScreenChangeAnimation.backward};		//#else			//#abort You need to define the polish.ScreenChangeAnimation.backward screen change animation as well, when you define the forward animation!		//#endif	//#endif			// do not change the following line!//$$IncludeStyleSheetDefinitionHere$$//			public static Screen currentScreen;		public static Display display;	public static AnimationThread animationThread;	//#ifdef polish.i18n.useDynamicTranslations		public static Command OK_CMD = new Command( Locale.get("polish.command.ok"), Command.OK, 2 );	//#elifdef polish.command.ok:defined		//#= public static final Command OK_CMD = new Command("${polish.command.ok}", Command.OK, 2 );	//#else		//# public static final Command OK_CMD = new Command("OK", Command.OK, 2 );	//#endif	//#ifdef polish.i18n.useDynamicTranslations		public static Command CANCEL_CMD = new Command(Locale.get("polish.command.cancel"), Command.CANCEL, 2 );	//#elifdef polish.command.cancel:defined		//#= public static final Command CANCEL_CMD = new Command("${polish.command.cancel}", Command.CANCEL, 3 );	//#else		//# public static final Command CANCEL_CMD = new Command("Cancel", Command.CANCEL, 3 );	//#endif	//#if polish.ScreenChangeAnimation.allowConfiguration == true		public static boolean enableScreenChangeAnimations = true;	//#endif	/**	 * Retrieves the image with the given name.	 * When the image has been cached before, it will be returned immediately.	 * When it has not been cached before, it either will be loaded directly	 * or in a background thread. This behaviour is set in the 	 * <a href="../../../../definitions/polish_xml.html">polish.xml</a> file.	 * 	 * @param url the URL of the Image, e.g. "/background.png"	 * @param parent the object which needs the image, when the image should be loaded	 * 		   		in the background, the parent need to implement	 * 				the ImageConsumer interface when it wants to be notified when	 * 				the picture has been loaded.	 * @param cache true when the image should be cached for later retrieval.	 *              This costs RAM obviously, so you should decide carefully if	 *              large images should be cached.	 * @return the image when it either was cached or is loaded directly. 	 *              When the should be loaded in the background, it will be later	 *              set via the ImageConsumer.setImage()-method.	 * @throws IOException when the image could not be loaded directly	 * @see ImageConsumer#setImage(String, Image)	 */	public static Image getImage( String url, Object parent, boolean cache )	throws IOException 	{		// check if the image has been cached before:		//#if polish.allowImageCaching != false			if ( imagesByName != null ) {				Image image = (Image) imagesByName.get( url );				if (image != null) {					return image;				}			}		//#endif		//#ifdef polish.images.directLoad			// when images should be loaded directly, try to do so now:			//#ifdef polish.classes.ImageLoader:defined				//#= Image image = ${ classname( polish.classes.ImageLoader ) }.loadImage( url );			//#else				Image image = Image.createImage( url );			//#endif			//#if polish.allowImageCaching != false				if (cache) {					if (imagesByName == null ) {						imagesByName = new Hashtable();					}					imagesByName.put( url, image );				}			//#endif			//# return image;		//#else			// when images should be loaded in the background, 			// tell the background-thread to do so now:					if ( ! (parent instanceof ImageConsumer)) {				//#debug error				System.out.println("StyleSheet.getImage(..) needs an ImageConsumer when images are loaded in the background!");				return null;			}			if (scheduledImagesByName == null ) {				scheduledImagesByName = new Hashtable();			}			ImageQueue queue = (ImageQueue) scheduledImagesByName.get(url);			if (queue != null) {				// this image is already scheduled to load:				queue.addConsumer((ImageConsumer) parent);				return null;			}			scheduledImagesByName.put( url, new ImageQueue( (ImageConsumer) parent, cache ) );			if (imagesByName == null ) {				imagesByName = new Hashtable();			}			if (timer == null) {				timer = new Timer();			}			ImageTask task = new ImageTask( url );			timer.schedule( task, 10 );			return null;		//#endif	}		//#ifdef polish.images.backgroundLoad	/**	 * Notifies the GUI items which requested images about the successful loading of thoses images.	 * 	 * @param name the URL of the image	 * @param image the image 	 */	public static void notifyImageConsumers( String name, Image image ) {		ImageQueue queue = (ImageQueue) scheduledImagesByName.remove(name);		if (queue != null) {			if (queue.cache) {				imagesByName.put( name, image );			}			queue.notifyConsumers(name, image);			if (true) {				return;			}			if (currentScreen != null) {				currentScreen.repaint();			}		}	}	//#endif		/**	 * Gets the style with the specified name.	 * 	 * @param name the name of the style	 * @return the specified style or null when no style with the given 	 * 	       name has been defined.	 */	public static Style getStyle( String name ) {		return (Style) stylesByName.get( name );	}		//#ifdef polish.useDynamicStyles	/**	 * Retrieves the style for the given item.	 * This function is only available when the &lt;buildSetting&gt;-attribute	 * [useDynamicStyles] is enabled.	 * This function allows to set styles without actually using the preprocessing-	 * directive //#style. Beware that this dynamic style retrieval is not as performant	 * as the direct-style-setting with the //#style preprocessing directive.	 *  	 * @param item the item for which the style should be retrieved	 * @return the appropriate style. When no specific style is found,	 *         the default style is returned.	 */	public static Style getStyle( Item item ) {		if (item.screen == null) {			//#debug error			System.out.println("unable to retrieve style for item [" + item.getClass().getName() + "] without screen.");			return defaultStyle;		}		String itemCssSelector = item.cssSelector;		String screenCssSelector = item.screen.cssSelector;		Style style = null;		String fullStyleName;		StringBuffer buffer = new StringBuffer();		buffer.append( screenCssSelector );		if (item.parent == null) {			//#debug			System.out.println("item.parent == null");			buffer.append('>').append( itemCssSelector );			fullStyleName = buffer.toString();			style = (Style) stylesByName.get( fullStyleName );			if (style != null) {				return style;			}			style = (Style) stylesByName.get( screenCssSelector + " " + itemCssSelector );		} else if (item.parent.parent == null) {			//#debug			System.out.println("Item has one parent.");			// this item is propably in a form or list,			// typical hierarchy is for example "form>container>p"                                                 			Item parent = item.parent;			String parentCssSelector = parent.cssSelector;			if (parentCssSelector == null) {				parentCssSelector = parent.createCssSelector();			}			//#debug			System.out.println( parent.getClass().getName() + "-css-selector: " + parentCssSelector );			buffer.append('>').append( parentCssSelector )				  .append('>').append( itemCssSelector );

⌨️ 快捷键说明

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