Programa Tabla Excel
import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet;
public class EjemploTabla extends MIDlet implements CommandListener { private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1); private Display display; private Form mainForm; public EjemploTabla() { mainForm = new Form("Tabla"); } protected void startApp() { display = Display.getDisplay(this); mainForm.append(new Tabla("Table", Display.getDisplay(this))); mainForm.addCommand(CMD_EXIT); mainForm.setCommandListener(this); display.setCurrent(mainForm); } public void commandAction(Command c, Displayable d) { if (c == CMD_EXIT) { destroyApp(false); notifyDestroyed(); } }
protected void destroyApp(boolean unconditional) { } protected void pauseApp() { } //Clase Tabla class Tabla extends CustomItem implements ItemCommandListener { private static final Command CMD_EDIT = new Command("Edit", Command.ITEM, 1); }
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 1
Programa Tabla Excel
private static final int UPPER = 0; private static final int IN = 1; private static final int LOWER = 2; private Display display; private int rows = 5; private int cols = 3; private int dx = 50; private int dy = 20; private int location = UPPER; private int currentX = 0; private int currentY = 0; private String[][] data = new String[rows][cols]; boolean horz; boolean vert; public Tabla(String title, Display d) { super(title); display = d; setDefaultCommand(CMD_EDIT); setItemCommandListener(this); int interactionMode = getInteractionModes(); horz = ((interactionMode & CustomItem.TRAVERSE_HORIZONTAL) != 0); vert = ((interactionMode & CustomItem.TRAVERSE_VERTICAL) != 0); } protected int getMinContentHeight() { return (rows * dy) + 1; }
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 2
Programa Tabla Excel
protected int getMinContentWidth() { return (cols * dx) + 1; } protected int getPrefContentHeight(int width) { return (rows * dy) + 1; } protected int getPrefContentWidth(int height) { return (cols * dx) + 1; } protected void paint(Graphics g, int w, int h) { for (int i = 0; i <= rows; i++) { g.drawLine(0, i * dy, cols * dx, i * dy); } for (int i = 0; i <= cols; i++) { g.drawLine(i * dx, 0, i * dx, rows * dy); } int oldColor = g.getColor(); g.setColor(0x00D0D0D0); g.fillRect((currentX * dx) + 1, (currentY * dy) + 1, dx - 1, dy - 1); g.setColor(oldColor); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (data[i][j] != null) { int oldClipX = g.getClipX(); int oldClipY = g.getClipY(); int oldClipWidth = g.getClipWidth(); int oldClipHeight = g.getClipHeight(); g.setClip((j * dx) + 1, i * dy, dx - 1, dy - 1);
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 3
Programa Tabla Excel
g.drawString(data[i][j], (j * dx) + 2, ((i + 1) * dy) - 2, Graphics.BOTTOM | Graphics.LEFT); g.setClip(oldClipX, oldClipY, oldClipWidth, oldClipHeight); } } } }
protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout) { if (horz && vert) { switch (dir) { case Canvas.DOWN: if (location == UPPER) { location = IN; } else { if (currentY < (rows - 1)) { currentY++; repaint(currentX * dx, (currentY - 1) * dy, dx, dy); repaint(currentX * dx, currentY * dy, dx, dy); } else { location = LOWER;
return false; } } break;
case Canvas.UP: if (location == LOWER) { location = IN; } else { if (currentY > 0) { currentY--;
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 4
Programa Tabla Excel
repaint(currentX * dx, (currentY + 1) * dy, dx, dy); repaint(currentX * dx, currentY * dy, dx, dy); } else { location = UPPER;
return false; } }
break;
case Canvas.LEFT:
if (currentX > 0) { currentX--; repaint((currentX + 1) * dx, currentY * dy, dx, dy); repaint(currentX * dx, currentY * dy, dx, dy); }
break;
case Canvas.RIGHT:
if (currentX < (cols - 1)) { currentX++;
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 5
Programa Tabla Excel
repaint((currentX - 1) * dx, currentY * dy, dx, dy); repaint(currentX * dx, currentY * dy, dx, dy); } } } else if (horz || vert) { switch (dir) { case Canvas.UP: case Canvas.LEFT:
if (location == LOWER) { location = IN; } else { if (currentX > 0) { currentX--; repaint((currentX + 1) * dx, currentY * dy, dx, dy); repaint(currentX * dx, currentY * dy, dx, dy); } else if (currentY > 0) { currentY--; repaint(currentX * dx, (currentY + 1) * dy, dx, dy); currentX = cols - 1; repaint(currentX * dx, currentY * dy, dx, dy); } else { location = UPPER;
return false;
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 6
Programa Tabla Excel
} }
break; case Canvas.DOWN: case Canvas.RIGHT:
if (location == UPPER) { location = IN; } else { if (currentX < (cols - 1)) { currentX++; repaint((currentX - 1) * dx, currentY * dy, dx, dy); repaint(currentX * dx, currentY * dy, dx, dy); } else if (currentY < (rows - 1)) { currentY++; repaint(currentX * dx, (currentY - 1) * dy, dx, dy); currentX = 0; repaint(currentX * dx, currentY * dy, dx, dy); } else { location = LOWER;
return false; } }
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 7
Programa Tabla Excel
} } else { } visRect_inout[0] = currentX; visRect_inout[1] = currentY; visRect_inout[2] = dx; visRect_inout[3] = dy; return true; } public void setText(String text) { data[currentY][currentX] = text; repaint(currentY * dx, currentX * dy, dx, dy); } public void commandAction(Command c, Item i) { if (c == CMD_EDIT) { TextInput textInput = new TextInput(data[currentY][currentX], this, display); display.setCurrent(textInput); } } } class TextInput extends TextBox implements CommandListener { private static final Command CMD_OK = new Command("OK", Command.OK, 1); private static final Command CMD_CANCEL = new Command("Cancel", Command.CANCEL, 1); private Tabla parent; private Display display; public TextInput(String text, Tabla parent, Display display) { super("Enter Text", text, 50, TextField.ANY); this.parent = parent; this.display = display;
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 8
Programa Tabla Excel
addCommand(CMD_OK); addCommand(CMD_CANCEL); setCommandListener(this); } public void commandAction(Command c, Displayable d) { if (c == CMD_OK) { parent.setText(getString()); display.setCurrentItem(parent); } else if (c == CMD_CANCEL) { display.setCurrentItem(parent); } }}
Mtro. en I.S. Miguel Angel Muoz Alvarado
Pgina 9