📄 iconitem.java
字号:
//#condition polish.usePolishGui
/*
* Created on 04-Apr-2004 at 21:30:32.
*
* 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 javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import de.enough.polish.util.ImageUtil;
/**
* <p>Shows a string with an optional image attached to it.</p>
* <p>The dynamic CSS selector of the IconItem is "icon".</p>
* <p>
* Following CSS attributes can be set:
* <ul>
* <li><b>icon-image-align</b>: The position of the icon-image relative to the text,
* either "right", "left", "top" or "bottom". Default is "left".</li>
* <li><b>icon-image</b>: The name of the image for this icon. The name can
* include the index of this item relative to the parent-container:
* The icon-image "%INDEX%icon.png" would be renamed to "0icon.png" when
* this icon would be the first one in a list.</li>
* <li><b></b>: </li>
* </ul>
* </p>
*
* <p>Copyright Enough Software 2004, 2005</p>
* <pre>
* history
* 04-Apr-2004 - rob creation
* </pre>
* @author Robert Virkus, robert@enough.de
*/
public class IconItem extends StringItem
//#ifdef polish.images.backgroundLoad
//# implements ImageConsumer
//#endif
{
Image image;
private int imageAlign = Graphics.LEFT;
private int imageHeight;
private int imageWidth;
private int yAdjustText;
//#if polish.midp2 && polish.css.scale-factor
//# private int scaleFactor;
//# private int scaleSteps;
//# private int currentStep;
//# private int[] rgbData;
//# private boolean scaleDown;
//# private boolean scaleFinished;
//# // a short array, so that we don't need to use synchronization.
//# // scaleData[0] = scaled RGB Data
//# // scaleData[1] = width of RGB Data
//# private Object[] scaleData;
//#endif
//#if polish.css.icon-vertical-adjustment
//# private int verticalAdjustment;
//#endif
//#if polish.css.icon-horizontal-adjustment
//# private int horizontalAdjustment;
//#endif
private int yAdjustImage;
/**
* Creates a new icon.
*
* @param text the text of this item
* @param image the image of this item, null when no image should be displayed
*/
public IconItem( String text, Image image ) {
this( null, text, image, null );
}
/**
* Creates a new icon.
*
* @param text the text of this item
* @param image the image of this item, null when no image should be displayed
* @param style the style of this item
*/
public IconItem( String text, Image image, Style style) {
this( null, text, image, style );
}
/**
* Creates a new icon.
*
* @param label the label of this item
* @param text the text of this item
* @param image the image of this item, null when no image should be displayed
* @param style the style of this item
*/
public IconItem( String label, String text, Image image, Style style) {
super(label, text, Item.HYPERLINK, style);
if (image != null) {
setImage( image );
}
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#createCssSelector()
*/
protected String createCssSelector() {
return "icon";
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#initItem()
*/
protected void initContent(int firstLineWidth, int lineWidth) {
if (this.image == null) {
super.initContent(firstLineWidth, lineWidth);
return;
}
this.imageWidth = this.image.getWidth();
this.imageHeight = this.image.getHeight();
if (this.imageAlign == Graphics.LEFT || this.imageAlign == Graphics.RIGHT ) {
this.imageWidth += this.paddingHorizontal;
firstLineWidth -= this.imageWidth;
lineWidth -= this.imageWidth;
super.initContent(firstLineWidth, lineWidth);
if (this.imageHeight > this.contentHeight) {
this.yAdjustImage = 0;
int verticalAlign = this.layout & LAYOUT_VCENTER;
if ( verticalAlign == LAYOUT_VCENTER || verticalAlign == 0 ) {
this.yAdjustText = (this.imageHeight - this.contentHeight) / 2;
} else if ( verticalAlign == LAYOUT_BOTTOM ) {
this.yAdjustText = this.imageHeight - this.contentHeight;
} else {
// top layout:
this.yAdjustText = 0;
}
this.contentHeight = this.imageHeight;
} else if (this.contentHeight > this.imageHeight) {
this.yAdjustText = 0;
int verticalAlign = this.layout & LAYOUT_VCENTER;
if ( verticalAlign == LAYOUT_VCENTER || verticalAlign == 0 ) {
this.yAdjustImage = (this.contentHeight - this.imageHeight) / 2;
} else if ( verticalAlign == LAYOUT_BOTTOM ) {
this.yAdjustImage = (this.contentHeight - this.imageHeight);
} else {
// top layout:
this.yAdjustImage = 0;
}
} else {
this.yAdjustImage = 0;
this.yAdjustText = 0;
}
if (this.isLayoutExpand && this.imageAlign == Graphics.RIGHT) {
this.contentWidth = firstLineWidth;
}
this.contentWidth += this.imageWidth;
} else {
this.imageHeight += this.paddingVertical;
super.initContent(firstLineWidth, lineWidth);
this.contentHeight += this.imageHeight;
if (this.imageWidth > this.contentWidth) {
this.contentWidth = this.imageWidth;
}
}
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#paintContent(int, int, javax.microedition.lcdui.Graphics)
*/
public void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
if (this.image != null) {
//#if polish.css.icon-horizontal-adjustment
//# x += this.horizontalAdjustment;
//#endif
//#if polish.css.icon-vertical-adjustment
//# y += this.verticalAdjustment;
//#endif
//#if polish.midp2 && polish.css.scale-factor
//# Object[] localeScaleData = this.scaleData;
//# int[] sData = null;
//# int sWidth = 0;
//# int sHeight = 0;
//# int scaleX = x;
//# int scaleY = y;
//# boolean useScaledImage = this.isFocused && (localeScaleData != null) && !this.scaleFinished;
//# if (useScaledImage) {
//# sData = (int[]) localeScaleData[0];
//# sWidth = ((Integer) localeScaleData[1]).intValue();
//# sHeight = sData.length / sWidth;
//# }
//#endif
if (this.imageAlign == Graphics.LEFT ) {
//#if polish.midp2 && polish.css.scale-factor
//# if (useScaledImage) {
//# scaleX = x - ((sWidth - this.image.getWidth()) / 2);
//# scaleY = y - ((sHeight - this.image.getHeight()) / 2) + this.yAdjustImage;
//# } else {
//#endif
g.drawImage(this.image, x, y + this.yAdjustImage, Graphics.TOP | Graphics.LEFT );
//#if polish.midp2 && polish.css.scale-factor
//# }
//#endif
x += this.imageWidth;
leftBorder += this.imageWidth;
y += this.yAdjustText;
} else if (this.imageAlign == Graphics.RIGHT ) {
//#if polish.midp2 && polish.css.scale-factor
//# if (useScaledImage) {
//# scaleX = rightBorder - ((this.image.getWidth() + sWidth) / 2);
//# scaleY = y - ((sHeight - this.image.getHeight()) / 2) + this.yAdjustImage;
//# } else {
//#endif
g.drawImage(this.image, x + this.contentWidth, y + this.yAdjustImage, Graphics.TOP | Graphics.RIGHT );
//#if polish.midp2 && polish.css.scale-factor
//# }
//#endif
rightBorder -= this.imageWidth;
y += this.yAdjustText;
} else if (this.imageAlign == Graphics.TOP ) {
int centerX = leftBorder + ((rightBorder - leftBorder) / 2);
//System.out.println("left: " + leftBorder + " right: " + rightBorder + " contentWidth: " + this.contentWidth);
//System.out.println("x: " + x + " centerX: " + centerX );
//#if polish.midp2 && polish.css.scale-factor
//# if (useScaledImage) {
//# scaleX = centerX - (sWidth / 2);
//# scaleY = y - ((sHeight - this.image.getHeight()) / 2);
//# } else {
//#endif
g.drawImage(this.image, centerX, y, Graphics.TOP | Graphics.HCENTER );
//#if polish.midp2 && polish.css.scale-factor
//# }
//#endif
y += this.imageHeight;
} else if (this.imageAlign == Graphics.BOTTOM ){
int centerX = leftBorder + ((rightBorder - leftBorder) / 2);
int bottomY = y + this.contentHeight;
//#if polish.midp2 && polish.css.scale-factor
//# if (useScaledImage) {
//# scaleX = centerX - (sWidth / 2);
//# scaleY = bottomY - sHeight;
//# } else {
//#endif
g.drawImage(this.image, centerX, bottomY, Graphics.BOTTOM | Graphics.HCENTER );
//#if polish.midp2 && polish.css.scale-factor
//# }
//#endif
} else {
// imageAlign == Graphics.HCENTER | Graphics.VCENTER
int centerX = leftBorder + ((rightBorder - leftBorder) / 2);
int centerY = y + this.contentHeight / 2;
//#if polish.midp2 && polish.css.scale-factor
//# if (useScaledImage) {
//# scaleX = centerX - (sWidth / 2);
//# scaleY = centerY - (sHeight / 2);
//# } else {
//#endif
g.drawImage(this.image, centerX, centerY, Graphics.HCENTER | Graphics.VCENTER);
//#if polish.midp2 && polish.css.scale-factor
//# }
//#endif
}
//#if polish.midp2 && polish.css.scale-factor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -