📄 choiceitem.java
字号:
//#condition polish.usePolishGui
/*
* Created on 05-May-2004 at 15:11:20.
*
* 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.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* <p>Paints a single item of a choice group.</p>
*
* As for <code>IconItem</code>,
* <code>ChoiceItem</code> is a <i>de.enough.polish</i> extension to group
* in a same Object text and image, <code>ChoiceItem</code>s being aimed at
* choice items of lists, e.g. <code>List</code> or <code>ChoiceGroup</code>.
* Hence, arguments of type <code>ChoiceItem</code> are found in the constructors
* of these lists, and in all methods for adding items to them, or retrieving
* items from them.
* <p>
* This is a much better alternative to methods passing separately string and
* image parts, for (at least) 2 reasons:
* <ol>
* <li>for methods taking an array of <code>String</code>s and <code>Image</code>s,
* such as:</li>
* <ul>
* <li><code>
* List(String title, int listType, String[] stringElements, Image[] imageElements)
* </code></li>
* <li><code>
* ChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements)
* </code></li>
* </ul>
* the caller <u>must</u> ensure that both arrays have the same length.
* Providing an unique array of <code>ChoiceItem</code>, each of them containing
* its image and text, solves this error-prone constraint.
* <li>there is no easy way to associate data to a given choice. One must create
* a parallel array containing the associated data and ensure the synchronism between
* the data array and the displayed choices array.
* Extending <code>ChoiceItem</code> is a much
* robust approach, as shown in this example:
* </ol>
* <PRE>
* import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import de.enough.polish.ui.*;
* import de.enough.polish.ui.ChoiceItem;
*
* class ListExample extends List implements CommandListener
* {
* static class Entry extends ChoiceItem {
* String data;
* Entry(String text, Image img, String data) {
* //#style default
* super(text, img, List.IMPLICIT);
* this.data = data;
* }
* }
*
* static final Entry[] entries = {
* new Entry("Item #1", null, "data 1"),
* new Entry("Item #2", null, "data 2"),
* new Entry("Item #3", null, "data 3")
* };
*
* ListExample() {
* //#style default
* super("A List", List.IMPLICIT, entries);
* setCommandListener(this);
* display.setCurrent(this);
* }
*
* public void commandAction(Command c, Displayable d)
* {
* int sel;
* if ((c == List.SELECT_COMMAND) && (-1 != (sel = getSelectedIndex()))) {
* System.out.println("Selected data: " + ((Entry)getItem(sel)).data);
* }
* }
* }
* </PRE>
* <b><i>(Example and JavaDoc provided by Pierre G. Richard)</i></b>
* <p>Copyright Enough Software 2004, 2005</p>
* <pre>
* history
* 05-May-2004 - rob creation
* </pre>
* @author Robert Virkus, robert@enough.de
*/
public class ChoiceItem extends IconItem
//#ifdef polish.images.backgroundLoad
//# implements ImageConsumer
//#endif
{
private static Image defaultRadioSelected;
private static Image defaultRadioPlain;
private static Image defaultCheckSelected;
private static Image defaultCheckPlain;
private boolean isMultiple;
private Image selected;
private Image plain;
/** determines whether this item is currently selected */
public boolean isSelected;
private Image boxImage;
private int boxWidth;
private int yAdjust;
/** determines whether an image is drawn before the item */
public boolean drawBox;
private int choiceType;
protected Font preferredFont;
private int boxColor;
/** defines whether the plain image should be drawn at all */
private boolean drawNoPlain;
//#ifdef polish.images.backgroundLoad
//# private String selectedImgName;
//# private String plainImgName;
//#endif
private int yBoxAdjust;
/**
* Creates a new ChoiceItem.
*
* @param text the text of the item
* @param image the image of the item
* @param choiceType the type of this item, either
* Choice.EXCLUSIVE, Choice.MULTIPLE, Choice.IMPLICIT or Choice.POPUP.
* When IMPLICIT or POPUP is used, no selection-box will be drawn.
*/
public ChoiceItem(String text, Image image, int choiceType ) {
this( text, image, choiceType, null );
}
/**
* Creates a new ChoiceItem.
*
* @param text the text of the item
* @param image the image of the item
* @param choiceType the type of this item, either
* Choice.EXCLUSIVE, Choice.MULTIPLE, Choice.IMPLICIT or Choice.POPUP.
* When IMPLICIT or POPUP is used, no selection-box will be drawn.
* @param style the CSS style of this item
*/
public ChoiceItem(String text, Image image, int choiceType, Style style) {
super(text, image, style);
this.drawBox = (choiceType == Choice.EXCLUSIVE) || (choiceType == Choice.MULTIPLE);
this.choiceType = choiceType;
this.isMultiple = (choiceType == Choice.MULTIPLE);
this.appearanceMode = INTERACTIVE;
}
//#ifdef polish.useDynamicStyles
//# /* (non-Javadoc)
//# * @see de.enough.polish.ui.Item#createCssSelector()
//# */
//# protected String createCssSelector() {
//# if (this.choiceType == Choice.EXCLUSIVE) {
//# return "radiobox";
//# } else if (this.choiceType == Choice.MULTIPLE) {
//# return "checkbox";
//# } else if (this.choiceType == Choice.IMPLICIT){
//# return "listitem";
//# } else {
//# return "popup";
//# }
//# }
//#endif
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#initContent(int, int)
*/
protected void initContent(int firstLineWidth, int lineWidth) {
if (!this.drawBox) {
super.initContent(firstLineWidth, lineWidth);
return;
}
if (this.selected == null) {
this.selected = createSelected();
}
if (this.plain == null && !this.drawNoPlain) {
this.plain = createPlain();
}
int maxWidth = this.selected.getWidth();
if (!this.drawNoPlain && this.plain.getWidth() > maxWidth ) {
maxWidth = this.plain.getWidth();
}
maxWidth += this.paddingHorizontal;
this.boxWidth = maxWidth;
int maxHeight = this.selected.getHeight();
if ( !this.drawNoPlain && this.plain.getHeight() > maxHeight ) {
maxHeight = this.plain.getHeight();
}
firstLineWidth -= maxWidth;
lineWidth -= maxWidth;
super.initContent(firstLineWidth, lineWidth);
this.contentWidth += maxWidth;
if (this.contentHeight < maxHeight) {
this.yAdjust = maxHeight - this.contentHeight;
this.contentHeight = maxHeight;
this.yBoxAdjust = 0;
} else {
if ((this.layout & LAYOUT_TOP) == LAYOUT_TOP) {
this.yBoxAdjust = 0;
} else if ((this.layout & LAYOUT_BOTTOM) == LAYOUT_BOTTOM) {
this.yBoxAdjust = this.contentHeight - maxHeight;
} else {
this.yBoxAdjust = (this.contentHeight - maxHeight)/2;
}
this.yAdjust = 0;
}
if (this.isSelected) {
this.boxImage = this.selected;
} else {
this.boxImage = this.plain;
}
}
/**
* Creates a selected image.
*
* @return the image for a selected item
*/
private Image createSelected() {
if (this.isMultiple) {
if (defaultCheckSelected == null ) {
defaultCheckSelected = Image.createImage(12, 12);
Graphics g = defaultCheckSelected.getGraphics();
g.setColor( this.boxColor );
g.drawRect(0, 0, 11, 11 );
g.drawRect(1, 1, 9, 9 );
g.drawLine(0, 0, 11, 11 );
g.drawLine( 11, 0, 0, 11 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -