/* A simple clipboard with MARK, COPY and PASTE functions */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ClipBoard extends MIDlet implements CommandListener { // Refernce to objects private Display display; // reference to a Display object private TextBox tbClip; // the TextBox private Command cmExit; // Exit command private Command cmMark; // Command to start Marking a block of Characters private Command cmCopy; // copy Command private Command cmPaste; // Paste command private int StartIndex = 0; // the start Index private char[] clip_Board = null; // the clipboard private int clipboardchars = 0; // number of characters in clipboard // Constructor public ClipBoard(){ display = Display.getDisplay(this); // Create commands and set priority cmExit = new Command("Exit", Command.EXIT, 1); cmMark = new Command("Mark", Command.SCREEN, 2); cmCopy = new Command("Copy", Command.SCREEN, 3); cmPaste = new Command("Paste", Command.SCREEN, 4); tbClip = new TextBox("ClipBoard", "AaBbCcDd", 15, TextField.ANY); tbClip.addCommand(cmExit); tbClip.addCommand(cmMark); tbClip.addCommand(cmCopy); tbClip.addCommand(cmPaste); tbClip.setCommandListener(this); // allocate a clibboard big enough to hold the entire textbox clip_Board = new char[tbClip.getMaxSize()]; } // startApp() public void startApp(){ display.setCurrent(tbClip); } // pauseApp() public void pauseApp(){ } // destroyApp() public void destroyApp(boolean unconditional){ } public void commandAction(Command c, Displayable s){ if (c == cmMark){ StartIndex = tbClip.getCaretPosition(); // get Caret position and store it } else if (c == cmCopy && (tbClip.getCaretPosition() > StartIndex)) { // create an array to hold the current textbox contents char[] chr = new char[tbClip.size()]; // get the current textbox content tbClip.getChars(chr); // count the characters in the clipboard clipboardchars = tbClip.getCaretPosition() - StartIndex; // copy text into the clipboard // array (source, source index, dest, dest index, count) System.arraycopy(chr, StartIndex, clip_Board, 0, clipboardchars); } else if (c == cmPaste){ // Make sure the paste will not overrun the textbox length if((tbClip.size() + clipboardchars) <= tbClip.getMaxSize()) tbClip.insert(clip_Board, 0, clipboardchars, tbClip.getCaretPosition()); } else if (c == cmExit){ destroyApp(false); notifyDestroyed(); } } }