📄 item.java
字号:
scr = getScreen();
}
//#ifndef polish.skipArgumentCheck
if ( (!(scr instanceof Form)) || (scr == null)) {
//#ifdef polish.verboseDebug
//# throw new IllegalStateException("notifyStateChanged() is valid only for items in Forms.");
//#else
throw new IllegalStateException();
//#endif
}
//#endif
((Form) scr).addToStateNotifyQueue(this);
}
/**
* Paints this item on the screen.
* This method should normally not be overriden. Override it
* only when you know what you are doing!
*
* @param x the left start position of this item.
* @param y the top start position of this item.
* @param leftBorder the left border, nothing must be painted left of this position
* @param rightBorder the right border, nothing must be painted right of this position,
* rightBorder > x >= leftBorder
* @param g the Graphics on which this item should be painted.
*/
public void paint( int x, int y, int leftBorder, int rightBorder, Graphics g ) {
// initialise this item if necessary:
int availableWidth = rightBorder - leftBorder;
if (!this.isInitialised || (availableWidth < this.itemWidth )) {
//#if polish.debug.info
//# if (availableWidth < this.itemWidth ) {
//#debug info
//# System.out.println("re-initializing item " + this + " for availableWidth=" + availableWidth + ", itemWidth=" + this.itemWidth);
//# }
//#endif
init( rightBorder - x, availableWidth );
}
boolean isLayoutShrink = (this.layout & LAYOUT_SHRINK) == LAYOUT_SHRINK;
// set coordinates of this item:
this.xLeftPos = leftBorder;
// this.yTopPos = y;
this.xRightPos = rightBorder; // contentX + this.contentWidth; //x + this.itemWidth; //TODO rob: Item.xRightPos might differ when this item contains line breaks
// this.yBottomPos = y + this.itemHeight;
// paint background and border when the label should be included in this:
//#if polish.css.include-label
//# if (this.includeLabel) {
//# int width = this.itemWidth - this.marginLeft - this.marginRight;
//# int height = this.itemHeight - this.marginTop - this.marginBottom;
//# int bX = x + this.marginLeft;
//# int bY = y + this.marginTop + this.backgroundYOffset;
//# if ( this.background != null ) {
//# this.background.paint(bX, bY, width, height, g);
//# }
//# if ( this.border != null ) {
//# this.border.paint(bX, bY, width, height, g);
//# }
//# }
//#endif
// paint label:
if (this.label != null) {
if (this.useSingleRow) {
this.label.paint( x, y, leftBorder, rightBorder - (this.contentWidth + this.paddingHorizontal), g );
x += this.label.itemWidth;
leftBorder += this.label.itemWidth;
} else {
this.label.paint( x, y, leftBorder, rightBorder, g );
y += this.label.itemHeight;
}
}
leftBorder += (this.marginLeft + this.borderWidth + this.paddingLeft);
//#ifdef polish.css.before
//# leftBorder += this.beforeWidth;
//#endif
//System.out.println( this.style.name + ": increasing leftBorder by " + (this.marginLeft + this.borderWidth + this.paddingLeft));
rightBorder -= (this.marginRight + this.borderWidth + this.paddingRight);
//#ifdef polish.css.after
//# rightBorder -= this.afterWidth;
//#endif
//System.out.println( this.style.name + ": decreasing rightBorder by " + (this.marginRight + this.borderWidth + this.paddingRight));
if ( this.isLayoutCenter && availableWidth > this.itemWidth) {
int difference = (availableWidth - this.itemWidth) / 2;
x += difference;
if (isLayoutShrink) {
leftBorder += difference;
rightBorder -= difference;
//System.out.println("item " + this + ": (center) shrinking left border to " + leftBorder + ", right border to " + rightBorder);
}
} else if ( this.isLayoutRight && availableWidth > this.itemWidth) {
// adjust the x-position so that the item is painted up to
// the right border (when it starts at x):
x += availableWidth - this.itemWidth;
if (isLayoutShrink) {
leftBorder += availableWidth - this.itemWidth;
//System.out.println("item " + this + ": (right) shrinking left border to " + leftBorder);
}
} else if (isLayoutShrink && availableWidth > this.itemWidth) {
rightBorder -= availableWidth - this.itemWidth;
//System.out.println("item " + this + ": (left) shrinking right border to " + rightBorder);
}
// paint background:
x += this.marginLeft;
y += this.marginTop;
//#if polish.css.include-label
//# if (!this.includeLabel) {
//#endif
if (this.background != null) {
this.background.paint(x, y + this.backgroundYOffset, this.backgroundWidth, this.backgroundHeight, g);
}
// paint border:
if (this.border != null) {
this.border.paint(x, y + this.backgroundYOffset, this.backgroundWidth, this.backgroundHeight, g);
}
//#if polish.css.include-label
//# }
//#endif
x += this.borderWidth + this.paddingLeft;
y += this.borderWidth + this.paddingTop;
int originalContentY = y;
// paint before element:
//#if polish.css.before || polish.css.after || polish.css.min-height || polish.css.max-height
//# boolean isVerticalCenter = (this.layout & LAYOUT_VCENTER) == LAYOUT_VCENTER;
//# boolean isTop = !isVerticalCenter && (this.layout & LAYOUT_TOP) == LAYOUT_TOP;
//# boolean isBottom = !isVerticalCenter && (this.layout & LAYOUT_BOTTOM) == LAYOUT_BOTTOM;
//#endif
//#if polish.css.min-height
//# if (this.minimumHeight != 0) {
//# int minHeight = this.minimumHeight - ( 2 * this.borderWidth + this.marginTop + this.marginBottom + this.paddingTop + this.paddingBottom);
//# if ( isVerticalCenter ) {
//# // System.out.println("vertical: adjusting contY by " + ((this.minimumHeight - this.contentHeight) / 2)
//# // + ", contentHeight=" + this.contentHeight + ", minHeight=" + this.minimumHeight );
//# y += (minHeight - this.contentHeight) / 2;
//# } else if ( isBottom ) {
//# //System.out.println("bottom: adjusting contY by " + (this.minimumHeight - this.contentHeight) );
//# y += (minHeight - this.contentHeight);
//# }
//# }
//#endif
//#ifdef polish.css.before
//# if (this.beforeImage != null) {
//# int beforeY = y;
//# int yAdjustment = this.beforeHeight - this.contentHeight;
//# if ( this.beforeHeight < this.contentHeight) {
//# if (isTop) {
//# beforeY -= yAdjustment;
//# } else if (isBottom) {
//# beforeY += yAdjustment;
//# } else {
//# beforeY -= yAdjustment / 2;
//# }
//# } else {
//# if (isTop) {
//# // keep contY
//# } else if (isBottom) {
//# y += yAdjustment;
//# } else {
//# y += yAdjustment / 2;
//# }
//# //contY += (this.beforeHeight - this.contentHeight) / 2;
//# }
//# g.drawImage(this.beforeImage, x, beforeY, Graphics.TOP | Graphics.LEFT );
//# x += this.beforeWidth;
//# }
//#endif
// paint after element:
//#ifdef polish.css.after
//# if (this.afterImage != null) {
//# int afterY = originalContentY;
//# int yAdjustment = this.afterHeight - this.contentHeight;
//# if ( this.afterHeight < this.contentHeight) {
//# if (isTop) {
//# afterY -= yAdjustment;
//# } else if (isBottom) {
//# afterY += yAdjustment;
//# } else {
//# afterY -= yAdjustment / 2;
//# }
//# //afterY += (this.contentHeight - this.afterHeight) / 2;
//# } else {
//#ifdef polish.css.before
//# if (this.afterHeight > this.beforeHeight) {
//#endif
//# if (isTop) {
//# // keep contY
//# } else if (isBottom) {
//# y = originalContentY + yAdjustment;
//# } else {
//# y = originalContentY + yAdjustment / 2;
//# }
//# //contY = originalContentY + (this.afterHeight - this.contentHeight) / 2;
//#ifdef polish.css.before
//# }
//#endif
//# }
//# g.drawImage(this.afterImage, rightBorder, afterY, Graphics.TOP | Graphics.LEFT );
//# }
//#endif
// paint content:
this.contentX = x;
this.contentY = y;
paintContent( x, y, leftBorder, rightBorder, g );
}
/**
* Initialises this item.
*
* @param firstLineWidth the maximum width of the first line
* @param lineWidth the maximum width of any following lines
*/
protected void init( int firstLineWidth, int lineWidth ) {
//#debug
//# System.out.println("intialising item " + this.getClass().getName() + " (" + this + ")");
if (this.style != null && !this.isStyleInitialised) {
setStyle( this.style );
}
//#ifdef polish.useDynamicStyles
else if (this.style == null) {
initStyle();
}
//#else
//# else if (this.style == null && !this.isStyleInitialised) {
//#debug
//# System.out.println("Setting default style for item " + getClass().getName() );
//# setStyle( StyleSheet.defaultStyle );
//# }
//#endif
int labelWidth = 0;
int labelHeight = 0;
if (this.label != null) {
if (!this.label.isInitialised) {
this.label.init( firstLineWidth, lineWidth );
}
labelWidth = this.label.itemWidth;
labelHeight = this.label.itemHeight;
}
// calculate content width and content height:
int noneContentWidth =
this.marginLeft + this.borderWidth + this.paddingLeft
+ this.paddingRight + this.borderWidth + this.marginRight;
//#ifdef polish.css.before
//# noneContentWidth += this.beforeWidth;
//#endif
//#ifdef polish.css.after
//# noneContentWidth += this.afterWidth;
//#endif
/*
if (noneContentWidth >= firstLineWidth) {
System.out.println("INVALID NONE CONTENT WIDTH=" + noneContentWidth);
}
*/
//#ifdef polish.css.max-width
//# int firstLineAdjustedWidth = firstLineWidth;
//# int lineAdjustedWidth = lineWidth;
//# if (this.maximumWidth != 0 ) {
//# if (firstLineAdjustedWidth > this.maximumWidth ) {
//# firstLineAdjustedWidth = this.maximumWidth;
//# }
//# if (lineAdjustedWidth > this.maximumWidth ) {
//# lineAdjustedWidth = this.maximumWidth;
//# }
//# }
//# int firstLineContentWidth = firstLineAdjustedWidth - noneContentWidth;
//# int availableContentWidth = lineAdjustedWidth - noneContentWidth;
//#else
int firstLineContentWidth = firstLineWidth - noneContentWidth;
int availableContentWidth = lineWidth - noneContentWidth;
//#endif
// initialise content by subclass:
initContent( firstLineContentWidth, availableContentWidth );
if (this.contentWidth == 0 && this.contentHeight == 0) {
this.itemWidth = labelWidth;
this.itemHeight = labelHeight;
this.backgroundHeight = 0;
this.backgroundWidth = 0;
this.isInitialised = true;
return;
}
this.itemWidth = noneContentWidth + this.contentWidth;
//#ifdef polish.css.min-width
//# if (this.itemWidth < this.minimumWidth ) {
//# this.itemWidth = this.minimumWidth;
//# }
//#endif
int cHeight = this.contentHeight;
//#ifdef polish.css.before
//# if (this.contentHeight < this.beforeHeight) {
//# cHeight = this.beforeHeight;
//# }
//#endif
//#ifdef polish.css.after
//# if (this.contentHeight < this.afterHeight) {
//# cHeight = this.afterHeight;
//# }
//#endif
int noneContentHeight = this.marginTop + this.borderWidth + this.paddingTop
+ this.paddingBottom + this.borderWidth + this.marginBottom;
if (this.itemWidth + labelWidth <= lineWidth) {
// label and content fit on one row:
this.itemWidth += labelWidth;
this.useSingleRow = true;
if (this.label != null) {
if ( (this.label.layout & LAYOUT_NEWLINE_AFTER) != 0 || ((this.layout & LAYOUT_NEWLINE_BEFORE) == LAYOUT_NEWLINE_BEFORE )) {
this.useSingleRow = false;
cHeight += labelHeight;
}
}
if ( cHeight + noneContentHeight < labelHeight ) {
cHeight = labelHeight - noneContentHeight;
}
} else {
this.useSingleRow = false;
cHeight += labelHeight;
}
if ( this.isLayoutExpand ) {
this.itemWidth = lineWidth;
//#ifdef polish.css.max-width
//# if (this.maximumWidth != 0 && lineWidth > this.maximumWidth ) {
//# this.itemWidth = this.maximumWidth;
//# }
//#endif
} else if (this.itemWidth > lineWidth) {
this.itemWidth = lineWidth;
}
if (cHeight + noneContentHeight < this.minimumHeight) {
cHeight = this.minimumHeight - noneContentHeight;
}
//#if polish.css.max-height
//# if (this.maximumHeight != 0 && cHeight + noneContentHeight > this.maximumHeight) {
//# cHeight = this.maximumHeight - noneContentHeight;
//# }
//#endif
this.itemHeight = cHeight + noneContentHeight;
if (this.useSingleRow) {
this.backgroundWidth = this.itemWidth - this.marginLeft - this.marginRight - labelWidth;
this.backgroundHeight = cHeight
+ noneContentHeight
- this.marginTop
- this.marginBottom;
} else {
this.backgroundWidth = this.ite
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -