📄 item.java
字号:
//}
Style focused = (Style) style.getObjectProperty(1);
if (focused != null) {
this.focusedStyle = focused;
// if (this instanceof ChoiceGroup) {
// System.out.println("Setting focused style for choicegroup!");
// }
}
//#endif
//#if polish.css.colspan
//# Integer colSpanInt = style.getIntProperty(106);
//# if ( colSpanInt != null ) {
//# this.colSpan = colSpanInt.intValue();
//# }
//#endif
//#if polish.css.include-label
//# Boolean includeLabelBool = style.getBooleanProperty(132);
//# if (includeLabelBool != null) {
//# this.includeLabel = includeLabelBool.booleanValue();
//# }
//#endif
//#ifdef polish.css.view-type
ItemView viewType = (ItemView) style.getObjectProperty(39);
// if (this instanceof ChoiceGroup) {
// System.out.println("SET.STYLE / CHOICEGROUP: found view-type (1): " + (viewType != null) + " for " + this);
// }
if (viewType != null && viewType.isValid(this, style)) {
if (this.view == null || this.view.getClass() != viewType.getClass()) {
try {
if (viewType.parentItem != null) {
viewType = (ItemView) viewType.getClass().newInstance();
}
viewType.parentItem = this;
this.view = viewType;
} catch (Exception e) {
//#debug error
//# System.out.println("Container: Unable to init view-type " + e );
viewType = null;
}
}
}
//#endif
//#ifdef polish.css.view-type
if (this.view != null) {
this.view.setStyle(style);
}
//#endif
}
/**
* Retrieves the complete width of this item.
* Note that the width can dynamically change,
* e.g. when a StringItem gets a new text.
*
* @param firstLineWidth the maximum width of the first line
* @param lineWidth the maximum width of any following lines
* @return the complete width of this item.
*/
public int getItemWidth( int firstLineWidth, int lineWidth ) {
if (!this.isInitialized || this.itemWidth > lineWidth) {
init( firstLineWidth, lineWidth );
}
return this.itemWidth;
}
/**
* Retrieves the complete height of this item.
* Note that the width can dynamically change,
* e.g. when a new style is set.
*
* @param firstLineWidth the maximum width of the first line
* @param lineWidth the maximum width of any following lines
* @return the complete heigth of this item.
*/
public int getItemHeight( int firstLineWidth, int lineWidth ) {
if (!this.isInitialized || this.itemWidth > lineWidth) {
init( firstLineWidth, lineWidth );
}
return this.itemHeight;
}
/**
* Adds a context sensitive <code>Command</code> to the item.
* The semantic type of
* <code>Command</code> should be <code>ITEM</code>. The implementation
* will present the command
* only when the item is active, for example, highlighted.
* <p>
* If the added command is already in the item (tested by comparing the
* object references), the method has no effect. If the item is
* actually visible on the display, and this call affects the set of
* visible commands, the implementation should update the display as soon
* as it is feasible to do so.
*
* <p>It is illegal to call this method if this <code>Item</code>
* is contained within an <code>Alert</code>.</p>
*
* @param cmd the command to be added
* @throws IllegalStateException if this Item is contained within an Alert
* @throws NullPointerException if cmd is null
* @since MIDP 2.0
*/
public void addCommand( Command cmd)
{
if (this.commands == null) {
this.commands = new ArrayList();
}
if (!this.commands.contains( cmd )) {
this.commands.add(cmd);
if (this.appearanceMode == PLAIN) {
this.appearanceMode = HYPERLINK;
}
if (this.isFocused) {
Screen scr = getScreen();
if (scr != null) {
scr.addCommand( cmd );
}
}
if (this.isInitialized) {
repaint();
}
}
}
/**
* Repaints the complete screen to which this item belongs to.
* Subclasses can call this method whenever their contents
* have changed and they need an immediate refresh.
*
* @see #repaint()
* @see #repaint(int, int, int, int)
*/
protected void repaintFully() {
repaint( this.relativeX, this.relativeY, this.itemWidth, this.itemHeight );
if (this.parent instanceof Container) {
((Container) this.parent).isInitialized = false;
}
Screen scr = getScreen();
if (scr != null && scr == StyleSheet.currentScreen) {
scr.repaint( );
}
}
/**
* Repaints the screen to which this item belongs to depending on the isInitialized field
* When this item is initialized, only the area covered by this item is repainted (unless other repaint requests are queued).
* When this item is not initialized (isInitialized == false), a repaint for the complete screen is triggered, as there might be
* a size change involved.
* Subclasses can call this method whenever their contents have changed and they need an immediate refresh.
*
* @see #isInitialized
* @see #repaintFully()
* @see #repaint(int, int, int, int)
*/
protected void repaint() {
//System.out.println("repaint(): " + this.relativeX + ", " + this.relativeY + ", " + this.itemWidth + ", " + this.itemHeight);
if (this.isInitialized) {
// note: -contentX, -contentY fails for right or center layouts
repaint( - (this.paddingLeft + this.marginLeft + this.borderWidth), -(this.paddingTop + this.marginTop + this.borderWidth), this.itemWidth, this.itemHeight );
} else {
repaintFully();
}
}
/**
* Repaints the specified relative area of this item.
* The area is specified relative to the <code>Item's</code>
* content area.
*
* @param relX horizontal start position relative to this item's content area
* @param relY vertical start position relative to this item's content area
* @param width the width of the area
* @param height the height of the area
*
* @see #repaint()
* @see #repaintFully()
*/
protected void repaint( int relX, int relY, int width, int height ) {
//System.out.println("repaint called by class " + getClass().getName() );
if (this.parent instanceof Container) {
((Container) this.parent).isInitialized = false;
}
Screen scr = getScreen();
if (scr != null && scr == StyleSheet.currentScreen) {
relX += getAbsoluteX();
relY += getAbsoluteY();
//System.out.println("item.repaint(" + relX + ", " + relY+ ", " + width + ", " + height + ") for " + this );
scr.repaint( relX, relY, width, height );
}
}
/**
* Requests that this item and all its parents are to be re-initialised at the next repainting.
* All parents of this item are notified, too.
* This method should be called when an item changes its size more than
* usual.
* When the item already has been initialised, a repaint() is requested, too.
*/
protected void requestInit() {
//System.out.println("requestInit called by class " + getClass().getName() + " - screen.class=" + getScreen().getClass().getName() );
Item p = this.parent;
while ( p != null) {
p.isInitialized = false;
p = p.parent;
}
if (this.isInitialized) {
this.isInitialized = false;
repaint();
}
}
/**
* Retrieves the screen to which this item belongs to.
*
* @return either the corresponding screen or null when no screen could be found
*/
public Screen getScreen() {
if (this.screen != null) {
return this.screen;
} else if (this.parent != null) {
Item p = this.parent;
while (p.parent != null) {
p = p.parent;
}
return p.screen;
} else {
return null;
}
}
/**
* Removes the context sensitive command from item. If the command is not
* in the <code>Item</code> (tested by comparing the object references),
* the method has
* no effect. If the <code>Item</code> is actually visible on the display,
* and this call
* affects the set of visible commands, the implementation should update
* the display as soon as it is feasible to do so.
*
*
* If the command to be removed happens to be the default command,
* the command is removed and the default command on this Item is
* set to <code>null</code>.
*
* The following code:
* <CODE> <pre>
* // Command c is the default command on Item item
* item.removeCommand(c);
* </pre> </CODE>
* is equivalent to the following code:
* <CODE> <pre>
* // Command c is the default command on Item item
* item.setDefaultCommand(null);
* item.removeCommand(c);
* </pre> </CODE>
*
* @param cmd - the command to be removed
* @since MIDP 2.0
*/
public void removeCommand( Command cmd ) {
if (this.commands != null) {
if (cmd == this.defaultCommand) {
this.defaultCommand = null;
}
if (this.commands.remove(cmd)) {
if (this.isFocused) {
Screen scr = getScreen();
if (scr != null) {
scr.removeCommand( cmd );
}
}
if (this.isInitialized) {
repaint();
}
}
}
}
/**
* Sets a listener for <code>Commands</code> to this <code>Item</code>,
* replacing any previous
* <code>ItemCommandListener</code>. A <code>null</code> reference
* is allowed and has the effect of removing any existing listener.
*
* When no listener is registered, J2ME Polish notifies the
* command-listener of the current screen, when an item command
* has been selected.
*
* <p>It is illegal to call this method if this <code>Item</code>
* is contained within an <code>Alert</code>.</p>
*
* @param l the new listener, or null.
* @throws IllegalStateException if this Item is contained within an Alert
* @since MIDP 2.0
*/
public void setItemCommandListener( ItemCommandListener l)
{
this.itemCommandListener = l;
}
/**
* Gets the preferred width of this <code>Item</code>.
* If the application has locked
* the width to a specific value, this method returns that value.
* Otherwise, the return value is computed based on the
* <code>Item's</code> contents,
* possibly with respect to the <code>Item's</code> preferred height
* if it is locked.
* See <a href="#sizes">Item Sizes</a> for a complete discussion.
*
* @return the preferred width of the Item
* @see #getPreferredHeight()
* @see #setPreferredSize(int, int)
* @since MIDP 2.0
*/
public int getPreferredWidth()
{
return this.preferredWidth;
}
/**
* Gets the preferred height of this <code>Item</code>.
* If the application has locked
* the height to a specific value, this method returns that value.
* Otherwise, the return value is computed based on the
* <code>Item's</code> contents,
* possibly with respect to the <code>Item's</code> preferred
* width if it is locked.
* See <a href="#sizes">Item Sizes</a> for a complete discussion.
*
* @return the preferred height of the Item
* @see #getPreferredWidth()
* @see #setPreferredSize(int, int)
* @since MIDP 2.0
*/
public int getPreferredHeight()
{
return this.preferredHeight;
}
/**
* Sets the preferred width and height for this <code>Item</code>.
* Values for width and height less than <code>-1</code> are illegal.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -