001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package customitem;
033:
034: import javax.microedition.lcdui.*;
035:
036: /**
037: *
038: * @version 2.0
039: */
040: public class Table extends CustomItem implements ItemCommandListener {
041: private static final Command CMD_EDIT = new Command("Edit",
042: Command.ITEM, 1);
043: private static final int UPPER = 0;
044: private static final int IN = 1;
045: private static final int LOWER = 2;
046: private Display display;
047: private int rows = 5;
048: private int cols = 3;
049: private int dx = 50;
050: private int dy = 20;
051: private int location = UPPER;
052: private int currentX = 0;
053: private int currentY = 0;
054: private String[][] data = new String[rows][cols];
055:
056: // Traversal stuff
057: // indicating support of horizontal traversal internal to the CustomItem
058: boolean horz;
059:
060: // indicating support for vertical traversal internal to the CustomItem.
061: boolean vert;
062:
063: public Table(String title, Display d) {
064: super (title);
065: display = d;
066: setDefaultCommand(CMD_EDIT);
067: setItemCommandListener(this );
068:
069: int interactionMode = getInteractionModes();
070: horz = ((interactionMode & CustomItem.TRAVERSE_HORIZONTAL) != 0);
071: vert = ((interactionMode & CustomItem.TRAVERSE_VERTICAL) != 0);
072: }
073:
074: protected int getMinContentHeight() {
075: return (rows * dy) + 1;
076: }
077:
078: protected int getMinContentWidth() {
079: return (cols * dx) + 1;
080: }
081:
082: protected int getPrefContentHeight(int width) {
083: return (rows * dy) + 1;
084: }
085:
086: protected int getPrefContentWidth(int height) {
087: return (cols * dx) + 1;
088: }
089:
090: protected void paint(Graphics g, int w, int h) {
091: for (int i = 0; i <= rows; i++) {
092: g.drawLine(0, i * dy, cols * dx, i * dy);
093: }
094:
095: for (int i = 0; i <= cols; i++) {
096: g.drawLine(i * dx, 0, i * dx, rows * dy);
097: }
098:
099: int oldColor = g.getColor();
100: g.setColor(0x00D0D0D0);
101: g.fillRect((currentX * dx) + 1, (currentY * dy) + 1, dx - 1,
102: dy - 1);
103: g.setColor(oldColor);
104:
105: for (int i = 0; i < rows; i++) {
106: for (int j = 0; j < cols; j++) {
107: if (data[i][j] != null) {
108: // store clipping properties
109: int oldClipX = g.getClipX();
110: int oldClipY = g.getClipY();
111: int oldClipWidth = g.getClipWidth();
112: int oldClipHeight = g.getClipHeight();
113: g.setClip((j * dx) + 1, i * dy, dx - 1, dy - 1);
114: g.drawString(data[i][j], (j * dx) + 2,
115: ((i + 1) * dy) - 2, Graphics.BOTTOM
116: | Graphics.LEFT);
117:
118: // restore clipping properties
119: g.setClip(oldClipX, oldClipY, oldClipWidth,
120: oldClipHeight);
121: }
122: }
123: }
124: }
125:
126: protected boolean traverse(int dir, int viewportWidth,
127: int viewportHeight, int[] visRect_inout) {
128: if (horz && vert) {
129: switch (dir) {
130: case Canvas.DOWN:
131:
132: if (location == UPPER) {
133: location = IN;
134: } else {
135: if (currentY < (rows - 1)) {
136: currentY++;
137: repaint(currentX * dx, (currentY - 1) * dy, dx,
138: dy);
139: repaint(currentX * dx, currentY * dy, dx, dy);
140: } else {
141: location = LOWER;
142:
143: return false;
144: }
145: }
146:
147: break;
148:
149: case Canvas.UP:
150:
151: if (location == LOWER) {
152: location = IN;
153: } else {
154: if (currentY > 0) {
155: currentY--;
156: repaint(currentX * dx, (currentY + 1) * dy, dx,
157: dy);
158: repaint(currentX * dx, currentY * dy, dx, dy);
159: } else {
160: location = UPPER;
161:
162: return false;
163: }
164: }
165:
166: break;
167:
168: case Canvas.LEFT:
169:
170: if (currentX > 0) {
171: currentX--;
172: repaint((currentX + 1) * dx, currentY * dy, dx, dy);
173: repaint(currentX * dx, currentY * dy, dx, dy);
174: }
175:
176: break;
177:
178: case Canvas.RIGHT:
179:
180: if (currentX < (cols - 1)) {
181: currentX++;
182: repaint((currentX - 1) * dx, currentY * dy, dx, dy);
183: repaint(currentX * dx, currentY * dy, dx, dy);
184: }
185: }
186: } else if (horz || vert) {
187: switch (dir) {
188: case Canvas.UP:
189: case Canvas.LEFT:
190:
191: if (location == LOWER) {
192: location = IN;
193: } else {
194: if (currentX > 0) {
195: currentX--;
196: repaint((currentX + 1) * dx, currentY * dy, dx,
197: dy);
198: repaint(currentX * dx, currentY * dy, dx, dy);
199: } else if (currentY > 0) {
200: currentY--;
201: repaint(currentX * dx, (currentY + 1) * dy, dx,
202: dy);
203: currentX = cols - 1;
204: repaint(currentX * dx, currentY * dy, dx, dy);
205: } else {
206: location = UPPER;
207:
208: return false;
209: }
210: }
211:
212: break;
213:
214: case Canvas.DOWN:
215: case Canvas.RIGHT:
216:
217: if (location == UPPER) {
218: location = IN;
219: } else {
220: if (currentX < (cols - 1)) {
221: currentX++;
222: repaint((currentX - 1) * dx, currentY * dy, dx,
223: dy);
224: repaint(currentX * dx, currentY * dy, dx, dy);
225: } else if (currentY < (rows - 1)) {
226: currentY++;
227: repaint(currentX * dx, (currentY - 1) * dy, dx,
228: dy);
229: currentX = 0;
230: repaint(currentX * dx, currentY * dy, dx, dy);
231: } else {
232: location = LOWER;
233:
234: return false;
235: }
236: }
237: }
238: } else {
239: // In case of no Traversal at all: (horz|vert) == 0
240: }
241:
242: visRect_inout[0] = currentX;
243: visRect_inout[1] = currentY;
244: visRect_inout[2] = dx;
245: visRect_inout[3] = dy;
246:
247: return true;
248: }
249:
250: public void setText(String text) {
251: data[currentY][currentX] = text;
252: repaint(currentY * dx, currentX * dy, dx, dy);
253: }
254:
255: public void commandAction(Command c, Item i) {
256: if (c == CMD_EDIT) {
257: TextInput textInput = new TextInput(
258: data[currentY][currentX], this, display);
259: display.setCurrent(textInput);
260: }
261: }
262: }
|