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.lcdui;
028:
029: /**
030: * Class that represents the line-wrapping and scroll position of
031: * text in a TextBox (editable) or Label, StringItem, or ListItem(uneditable)
032: *
033: * From this structure, <code>Text.paintText()</code> should be able to
034: * quickly render wrapped text
035: */
036: public class TextInfo {
037:
038: /** total number of lines */
039: public int numLines;
040:
041: /** number of visible lines */
042: public int visLines;
043:
044: /** first visible line */
045: public int topVis;
046:
047: /** the line where the cursor resides */
048: public int cursorLine;
049:
050: /** set to true to indicate this has been modified */
051: public boolean isModified;
052:
053: /** set to true if this has been scrolled in the Y direction */
054: public boolean scrollY;
055:
056: /** set to true if this has been scrolled in the X direction */
057: public boolean scrollX;
058:
059: /** starting offset of each line */
060: public int[] lineStart;
061:
062: /** offset of last character of each line */
063: public int[] lineEnd;
064:
065: /** the height of the block of text described by this object */
066: public int height;
067:
068: /** scroll up */
069: public final static int BACK = 1;
070:
071: /** scroll down */
072: public final static int FORWARD = 2;
073:
074: /**
075: * Construct a new TextInfo object with <code>size</code>
076: * lines initially
077: *
078: * @param size maximum number of lines this TextInfo
079: * struct can store without expanding
080: */
081: public TextInfo(int size) {
082: isModified = true;
083: scrollY = true;
084: scrollX = true;
085: lineStart = new int[size];
086: lineEnd = new int[size];
087: }
088:
089: /**
090: * Expand the capacity of this TextInfo structure by doubling the
091: * length of the lineStart and lineEnd arrays
092: */
093: public void expand() {
094: int[] tmpStart = new int[lineStart.length * 2];
095: int[] tmpEnd = new int[tmpStart.length];
096:
097: System.arraycopy(lineStart, 0, tmpStart, 0, lineStart.length);
098: System.arraycopy(lineEnd, 0, tmpEnd, 0, lineEnd.length);
099:
100: lineStart = tmpStart;
101: lineEnd = tmpEnd;
102: }
103:
104: /**
105: * Scroll Up or down by one line if possible
106: *
107: * @param dir direction of scroll, FORWARD or BACK
108: * @return true if scrolling happened, false if not
109: */
110: public boolean scroll(int dir) {
111: return scroll(dir, 1);
112: }
113:
114: /**
115: * Scroll Up or down by one line if possible
116: *
117: * @param dir direction of scroll, FORWARD or BACK
118: * @param length how many lines to scroll
119: * @return true if scrolling happened, false if not
120: */
121: public boolean scroll(int dir, int length) {
122: boolean rv = false;
123:
124: if (visLines < numLines) {
125: switch (dir) {
126: case FORWARD:
127: if (topVis + visLines < numLines) {
128: topVis += length;
129: if (topVis + visLines > numLines) {
130: topVis = numLines - visLines;
131: }
132: rv = true;
133: }
134: break;
135: case BACK:
136: if (topVis > 0) {
137: topVis -= length;
138: if (topVis < 0) {
139: topVis = 0;
140: }
141: rv = true;
142: }
143: break;
144: default:
145: // no-op
146: }
147: }
148: scrollY |= rv;
149: return rv;
150: }
151:
152: /**
153: * Scroll Up or down by page if possible
154: *
155: * @param dir direction of scroll, FORWARD or BACK
156: * @return number of scrolled lines
157: */
158: public int scrollByPage(int dir) {
159: int oldTopVis = topVis;
160:
161: if (visLines < numLines) {
162: switch (dir) {
163: case FORWARD:
164: if ((topVis + visLines) < numLines) {
165: topVis = numLines - (topVis + visLines - 1) < visLines ? numLines
166: - visLines
167: : topVis + visLines - 1;
168: }
169: break;
170: case BACK:
171: if (topVis > 0) {
172: topVis = (topVis - visLines + 1) < 0 ? 0 : topVis
173: - visLines + 1;
174: }
175: break;
176: default:
177: // no-op
178: }
179: }
180: scrollY |= (topVis != oldTopVis);
181: return topVis - oldTopVis;
182: }
183:
184: /**
185: * Returns scroll position from 0-100
186: * @return scroll position mapped to the range 0-100
187: */
188: public int getScrollPosition() {
189: // used to set scroll indicator visibility
190: if (numLines == 0 || numLines <= visLines) {
191: return 0;
192: } else {
193: return (topVis * 100) / (numLines - visLines);
194: }
195: }
196:
197: /**
198: * Returns scroll proportion from 0-100
199: * @return scroll proportion, as a percentage of the screen that
200: * is viewable.
201: */
202: public int getScrollProportion() {
203: // used to set scroll indicator visibility
204: if (visLines >= numLines || numLines == 0) {
205: return 100;
206: } else {
207: return (visLines * 100) / numLines;
208: }
209: }
210: }
|