001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.chameleon.layers;
028:
029: import com.sun.midp.chameleon.skins.SoftButtonSkin;
030: import com.sun.midp.chameleon.skins.ScreenSkin;
031:
032: import com.sun.midp.chameleon.*;
033: import javax.microedition.lcdui.*;
034: import com.sun.midp.chameleon.skins.ScrollIndSkin;
035: import com.sun.midp.lcdui.EventConstants;
036:
037: /**
038: * A ScrollArrowLayer is a region of the display used for showing scroll indicator
039: * status arrows.
040: *
041: */
042: public class ScrollArrowLayer extends ScrollIndLayer {
043: /**
044: * True if up arrow is visible
045: */
046: protected boolean upViz;
047:
048: /**
049: * True if down arrow is visible
050: */
051: protected boolean downViz;
052:
053: /**
054: * Construct a new ScrollIndLayer, visible, but transparent :)
055: */
056: public ScrollArrowLayer(CLayer layer) {
057: this (layer, null);
058: }
059:
060: /**
061: * Additional constructor.
062: * @param layer the scrollable controlling the scrolling layer
063: * @param listener the scrolling listener
064: */
065: public ScrollArrowLayer(CLayer layer, ScrollListener listener) {
066: super (layer, listener);
067: }
068:
069: /**
070: * Calculate layer bounds depending on the scrollable
071: */
072: public void setBounds() {
073: bounds[H] = SoftButtonSkin.HEIGHT;
074: if (ScrollIndSkin.IMAGE_UP != null) {
075: bounds[W] = ScrollIndSkin.IMAGE_UP.getWidth();
076: bounds[H] = (2 * ScrollIndSkin.IMAGE_UP.getHeight());
077: bounds[Y] = (SoftButtonSkin.HEIGHT - bounds[H]) / 3;
078: bounds[H] += bounds[Y];
079: bounds[Y] = ScreenSkin.HEIGHT - SoftButtonSkin.HEIGHT
080: + bounds[Y];
081: } else {
082: bounds[W] = ScrollIndSkin.WIDTH;
083: bounds[Y] = 3;
084: }
085: bounds[X] = (ScreenSkin.WIDTH - bounds[W]) / 2;
086: }
087:
088: /**
089: * Set the current vertical scroll position and proportion.
090: *
091: * @param scrollPosition vertical scroll position.
092: * @param scrollProportion vertical scroll proportion.
093: */
094: public void setVerticalScroll(int scrollPosition,
095: int scrollProportion) {
096: boolean up = upViz;
097: boolean dn = downViz;
098: if ((scrollable != null) && (scrollProportion < 100)) {
099: upViz = (scrollPosition > 0);
100: downViz = (scrollPosition < 100);
101: } else {
102: upViz = downViz = false;
103: }
104: setVisible(upViz || downViz);
105: if (up != upViz || dn != downViz) {
106: requestRepaint();
107: }
108: }
109:
110: /**
111: * Paint the scroll indicator. The indicator arrows may be appear
112: * individually
113: * or together, and may vary in appearance based on whether they appear
114: * in the normalsoft button region or an alert's softbutton region.
115: * The visible state is based on the state of the <code>alertMode</code>,
116: * <code>upViz</code>, and <code>downViz</code> variables set by the
117: * <code>setVerticalScroll</code> method.
118: * @param g the graphics context to paint in
119: */
120: protected void paintBody(Graphics g) {
121: if (upViz) {
122: Image i = ScrollIndSkin.IMAGE_UP;
123: if (alertMode && ScrollIndSkin.IMAGE_AU_UP != null) {
124: i = ScrollIndSkin.IMAGE_AU_UP;
125: }
126: if (i != null) {
127: g.drawImage(i, 0, 0, Graphics.LEFT | Graphics.TOP);
128: }
129: }
130:
131: if (downViz) {
132: Image i = ScrollIndSkin.IMAGE_DN;
133: if (alertMode && ScrollIndSkin.IMAGE_AU_DN != null) {
134: i = ScrollIndSkin.IMAGE_AU_DN;
135: }
136: if (i != null) {
137: g.drawImage(i, 0, bounds[H]
138: - ScrollIndSkin.IMAGE_DN.getHeight(),
139: Graphics.LEFT | Graphics.TOP);
140: }
141: }
142: }
143:
144: /**
145: * Handle input from a pen tap. Parameters describe
146: * the type of pen event and the x,y location in the
147: * layer at which the event occurred. Important : the
148: * x,y location of the pen tap will already be translated
149: * into the coordinate space of the layer.
150: *
151: * @param type the type of pen event
152: * @param x the x coordinate of the event
153: * @param y the y coordinate of the event
154: */
155: public boolean pointerInput(int type, int x, int y) {
156: switch (type) {
157: case EventConstants.PRESSED:
158: // case EventConstants.HOLD:
159: // no action for tap-and-hold in scrollbar
160: // cancel timer for any press.
161:
162: int scrollType = getScrollType(x, y);
163: if (scrollType == SCROLL_LINEDOWN
164: || scrollType == SCROLL_LINEUP) {
165: listener.scrollContent(scrollType, 0);
166: }
167: break;
168: case EventConstants.RELEASED:
169: // do nothing
170: break;
171: default:
172: break;
173: }
174:
175: /* we should process all of the pointer event inside scroll layer
176: and don't pass it to underlying layer */
177: return true;
178: }
179:
180: /**
181: * Determine he scroll type basing on the pointer coordinates
182: * @param x - x coordinate
183: * @param y - y coordinate
184: * @return the scroll type
185: * The possible types of scrolling are
186: * @see #SCROLL_NONE
187: * @see #SCROLL_LINEUP
188: * @see #SCROLL_LINEDOWN
189: */
190: private int getScrollType(int x, int y) {
191: int ret = SCROLL_NONE;
192: if (x >= 0 && x <= bounds[W] && y >= 0 && y <= bounds[H]) {
193: if (upViz && y < bounds[H] / 2) {
194: ret = SCROLL_LINEUP;
195: } else if (downViz && y > bounds[H] / 2) {
196: ret = SCROLL_LINEDOWN;
197: }
198: }
199: return ret;
200: }
201:
202: /**
203: * Set new scrollable
204: * @param layer new scrollable controlling the scrolling layer
205: * @return true if the scrollable is changed, false - otherwise
206: */
207: public boolean setScrollable(CLayer layer) {
208: boolean ret = super .setScrollable(layer);
209: if (ret) {
210: alertMode |= scrollable instanceof MenuLayer;
211: }
212: upViz = downViz = false;
213: return ret;
214: }
215: }
|