📄 scrollbar.java
字号:
//#condition polish.usePolishGui
/*
* Created on 22-Feb-2006 at 19:09:37.
*
* Copyright (c) 2006 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.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* <p>Realizes a scrollbar for any J2ME Polish screens.</p>
* <p>
* The scrollbar needs to be activated using the "polish.useScrollBar"
* variable:
* <pre>
* <variable name="polish.useScrollBar" value="true" />
* </pre>
* </p>
* <p>Design the scrollbar using the predefined "scrollbar" style. You
* can also specify a screen specific style by defining the "scrollbar-style" CSS attribute.
* <br />
* Scrollbars support following additional attributes (apart from the background etc):
* </p>
* <ul>
* <li><b>scrollbar-slider-width</b>: The width of the slider in pixels, is ignored when a slider-image is defined.</li>
* <li><b>scrollbar-slider-color</b>: The color of the slider, is ignored when a slider-image is defined.</li>
* <li><b>scrollbar-slider-image</b>: The image used for the slider.</li>
* <li><b>scrollbar-slider-image-repeat</b>: Either "true" or "false". When "true" is
* given, the slider image is repeated vertically, if necessary.</li>
* <li><b>scrollbar-slider-mode</b>: The mode of the slider, either "area",
* "item" or "page". In the "area" mode the slider represents the
* position of the current selection relativ to the complete width and height.
* In the "item" mode the index of currently selected item
* is taken into account relative to the number of items.
* In the "page" mode the position and height of the currently visible area is
* shown relative to the complete height of that screen.
* The default mode is "page".
* </li>
* <li><b>scrollbar-position</b>: either "left" or "right" (not yet supported).</li>
* <li><b></b>: </li>
* </ul>
* <p>You can exchange the default implementation with the "polish.classes.ScrollBar"
* variable.
* </p>
*
* <p>Copyright Enough Software 2006</p>
* <pre>
* history
* 22-Feb-2006 - rob creation
* </pre>
* @author Robert Virkus, j2mepolish@enough.de
*/
public class ScrollBar extends Item {
private static final int MODE_AREA = 0;
private static final int MODE_ITEM = 1;
private static final int MODE_PAGE = 2;
protected int sliderColor;
protected int sliderWidth = 2;
//#if polish.css.scrollbar-slider-image
//# protected Image sliderImage;
//#if polish.css.scrollbar-slider-image-repeat
//# protected boolean repeatSliderImage;
//# protected int repeatSliderNumber;
//#endif
//#endif
//#if polish.css.scrollbar-slider-mode
//# protected int sliderMode = MODE_PAGE;
//#endif
protected int sliderY;
protected int sliderHeight;
protected int scrollBarHeight;
private boolean isVisible;
/**
* Creates a new default scrollbar
*/
public ScrollBar() {
super();
}
/**
* Creates a new styled scrollbar
*
* @param style the style
*/
public ScrollBar(Style style) {
super(style);
}
/**
* Initializes this scrollbar.
* @param screenWidth the width of the screen
* @param screenAvailableHeight the height available for the content within the screen
* @param screenContentHeight the height of the content area of the screen
* @param contentYOffset the y offset for the content in the range of [-(screenContentHeight-screenAvailableHeight)...0]
* @param selectionStart the start of the selection relative to the screenContentHeight
* @param selectionHeight the height of the current selection
* @param focusedIndex the index of currently focused item
* @param numberOfItems the number of available items
* @return the item width of the scroll bar.
*/
public int initScrollBar( int screenWidth, int screenAvailableHeight, int screenContentHeight, int contentYOffset, int selectionStart, int selectionHeight, int focusedIndex, int numberOfItems ) {
//#debug
//# System.out.println("initScrollBar( screenWidth=" + screenWidth + ", screenAvailableHeight=" + screenAvailableHeight + ", screenContentHeight=" + screenContentHeight + ", contentYOffset=" + contentYOffset + ", selectionStart=" + selectionStart + ", selectionHeight=" + selectionHeight + ", focusedIndex=" + focusedIndex + ", numberOfItems=" + numberOfItems + ")");
if ( screenAvailableHeight >= screenContentHeight ) {
this.isVisible = false;
return 0;
}
this.isVisible = true;
this.scrollBarHeight = screenAvailableHeight;
//#if polish.css.scrollbar-slider-mode
//# if ( this.sliderMode == MODE_ITEM && focusedIndex != -1) {
//# //System.out.println("using item");
//# int chunkPerSlider = (screenAvailableHeight << 8) / numberOfItems;
//# this.sliderY = (chunkPerSlider * focusedIndex) >>> 8;
//# this.sliderHeight = chunkPerSlider >>> 8;
//# } else if ( this.sliderMode == MODE_AREA && selectionHeight != 0 ) {
//# // take the currently selected area
//# //System.out.println("using area");
//# this.sliderY = ( (selectionStart - contentYOffset) * screenAvailableHeight) / screenContentHeight;
//# this.sliderHeight = (selectionHeight * screenAvailableHeight) / screenContentHeight;
//# } else {
//#endif
// use the page dimensions:
//System.out.println("using page");
this.sliderY = (-contentYOffset * screenAvailableHeight) / screenContentHeight;
this.sliderHeight = (screenAvailableHeight * screenAvailableHeight) / screenContentHeight;
//#if polish.css.scrollbar-slider-mode
//# }
//#endif
//#if polish.css.scrollbar-slider-image && polish.css.scrollbar-slider-image-repeat
//# if (this.repeatSliderImage && this.sliderImage != null ) {
//# if (this.sliderHeight > this.sliderImage.getHeight()) {
//# this.repeatSliderNumber = this.sliderHeight / this.sliderImage.getHeight();
//# } else {
//# this.repeatSliderNumber = 1;
//# }
//# }
//#endif
//#debug
//# System.out.println("sliderY=" + this.sliderY + ", sliderHeight=" + this.sliderHeight);
if (!this.isInitialized) {
init( screenWidth, screenWidth );
}
return this.itemWidth;
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#initContent(int, int)
*/
protected void initContent(int firstLineWidth, int lineWidth) {
//#if polish.css.scrollbar-slider-image
//# if (this.sliderImage != null) {
//# this.contentWidth = this.sliderImage.getWidth();
//# } else {
//#endif
this.contentWidth = this.sliderWidth;
//#if polish.css.scrollbar-slider-image
//# }
//#endif
this.contentHeight = this.scrollBarHeight - ( this.paddingTop + this.paddingBottom + this.marginTop + this.marginBottom );
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#paint(int, int, int, int, javax.microedition.lcdui.Graphics)
*/
public void paint(int x, int y, int leftBorder, int rightBorder, Graphics g) {
if (!this.isVisible) {
return;
}
super.paint(x, y, leftBorder, rightBorder, g);
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#paintContent(int, int, int, int, javax.microedition.lcdui.Graphics)
*/
protected void paintContent(int x, int y, int leftBorder, int rightBorder,
Graphics g)
{
//#if polish.css.scrollbar-slider-image
//# if (this.sliderImage != null) {
//#if polish.css.scrollbar-slider-image-repeat
//# int imageHeight = this.sliderImage.getHeight();
//# y = this.sliderY;
//# for (int i=this.repeatSliderNumber; --i >= 0; ) {
//# g.drawImage(this.sliderImage, x, this.sliderY, Graphics.TOP | Graphics.LEFT );
//# y += imageHeight;
//# }
//#else
//# g.drawImage(this.sliderImage, x, this.sliderY, Graphics.TOP | Graphics.LEFT );
//#endif
//# } else {
//#endif
g.setColor( this.sliderColor );
g.fillRect(x, y + this.sliderY, this.contentWidth, this.sliderHeight);
//#if polish.css.scrollbar-slider-image
//# }
//#endif
}
//#ifdef polish.useDynamicStyles
//# /* (non-Javadoc)
//# * @see de.enough.polish.ui.Item#createCssSelector()
//# */
//# protected String createCssSelector() {
//# return "scrollbar";
//# }
//#endif
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#setStyle(de.enough.polish.ui.Style)
*/
public void setStyle(Style style) {
super.setStyle(style);
//#if polish.css.scrollbar-slider-image
//# String url = style.getProperty(121);
//# if (url != null) {
//# try {
//# this.sliderImage = StyleSheet.getImage(url, url, false);
//# } catch (IOException e) {
//#debug error
//# System.out.println("Unable to load scrollbar slider image " + url + e );
//# }
//# }
//#if polish.css.scrollbar-slider-image-repeat
//# Boolean repeatSliderImageBool = style.getBooleanProperty(122);
//# if (repeatSliderImageBool != null) {
//# this.repeatSliderImage = repeatSliderImageBool.booleanValue();
//# }
//#endif
//#endif
//#if polish.css.scrollbar-slider-width
//# Integer sliderWidthInt = style.getIntProperty(119);
//# if (sliderWidthInt != null) {
//# this.sliderWidth = sliderWidthInt.intValue();
//# }
//#endif
//#if polish.css.scrollbar-slider-color
//# Integer sliderColorInt = style.getIntProperty(120);
//# if (sliderColorInt != null) {
//# this.sliderColor = sliderColorInt.intValue();
//# }
//#endif
//#if polish.css.scrollbar-slider-mode
//# Integer sliderModeInt = style.getIntProperty(123);
//# if (sliderModeInt != null) {
//# this.sliderMode = sliderModeInt.intValue();
//# }
//#endif
}
//#ifdef polish.hasPointerEvents
//# /**
//# * Handles the event when a pointer has been pressed at the specified position.
//# * The default method discards this event when relX/relY is outside of the item's area.
//# * When the event took place inside of the content area, the pointer-event is translated into an artificial
//# * FIRE game-action keyPressed event, which is subsequently handled
//# * bu the handleKeyPressed(-1, Canvas.FIRE) method.
//# * This method needs should be overwritten only when the "polish.hasPointerEvents"
//# * preprocessing symbol is defined: "//#ifdef polish.hasPointerEvents".
//# *
//# * @param relX the x position of the pointer pressing relative to this item's left position
//# * @param relY the y position of the pointer pressing relative to this item's top position
//# * @return true when the pressing of the pointer was actually handled by this item.
//# * @see #isInItemArea(int, int) this method is used for determining whether the event belongs to this item
//# * @see #isInContentArea(int, int) for a helper method for determining whether the event took place into the actual content area
//# * @see #handleKeyPressed(int, int)
//# */
//# protected boolean handlePointerPressed( int relX, int relY ) {
//# if ( isInItemArea(relX, relY) ) {
//# int diff = 0;
//# if (relY < this.sliderY) {
//# // scroll up
//# diff = 30;
//# } else if (relY > this.sliderY + this.sliderHeight){
//# // scroll down
//# diff = -30;
//# }
//# if (diff != 0) {
//# this.screen.scrollRelative( diff );
//# }
//#
//# }
//# return false;
//# }
//#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -