Testing the Action
Let us now see how the copy and paste functions work. For
copying text, we need to select the Copy command on
ClipboardDemo (see Figure 4).

Figure 4. ClipboardDemo screen with text and pop-up
menu
This displays the Copyboard, which has the text that
was on ClipboardDemo. This can be seen in Figure 5.
Note that the command that we'll need next--"Start
selection"--is available onscreen.

Figure 5. Copyboard with the contents of
ClipboardDemo
Next we need to move the caret to the left of the first
character of the part to be copied and select the "Start
selection" command, as shown in Figure 6.

Figure 6. Start selecting text to be copied
We can see the result in Figure 7. This command is processed by
ClipboardCommandProcessor and we see an alert that
tells us where the start position is.

Figure 7. Alert showing where selection has been
started
The alert is displayed for two seconds, after which
Copyboard is brought to the foreground. At this time,
the "Start selection" command is removed from the screen and
the Copy command takes its place. This is illustrated in
Figure 8.

Figure 8. Copyboard after starting
selection
Then we have to position the caret at the right of the last
character to be copied and select the Copy command. Again
ClipboardCommandProcessor takes over and shows the
string that has been selected. But before that, it checks to see if the
selection was done backwards and adjusts the copying action
accordingly:
if(endposition<startposition)
{
//selection has been made backwards
//so simply interchange start and end positions
int temp = endposition;
endposition = startposition;
startposition = temp;
}
//make the selection
displaystring = text.substring(startposition, endposition);
//show the selected string
selection.setText(displaystring);
CopyForm.setTitle("Selected text");
CopyForm.addCommand(CMD_COPY);
display.setCurrent(CopyForm);
Figure 9 shows the screen for displaying the selected text.

Figure 9. Screen showing the selected text
Once we approve the text selection through the OK
command, the saveCopiedInfo method of
ClipboardHandler class is invoked to save the copied
text into clipboard. Of course, we have to the option to
cancel the copying process at each step.
Similarly, we initiate pasting by selecting the Paste
command on ClipboardDemo. This brings up the
Pasteboard with the contents of
ClipboardDemo. Again we position the caret where we
want the saved text to be pasted and select Paste (Figure
10).

Figure 10. Caret positioned for pasting
We get an alert to inform us that the pasting operation has been
done and then we see the result as in Figure 11. At this point, we
can either accept or reject the pasting action.

Figure 11. Pasteboard after pasting
The one thing that we have not talked about is how the demo
MIDlets get to access the common clipboard. The trick lies in
knowing how to address this common record store. We have seen
earlier how clipboard was created with universal access
authorization by the Clipboard MIDlet. While setting
up the Clipboard project on WTK, I had entered the
name of a fictitious vendor in the required field of project
settings. Record store names are internally constructed from the
name of the MIDlet suite that create them, the name of the vendor of
the MIDlet, and the name given to the record store by the MIDlet. So
the full name of clipboard is a combination of "clipboard"
(record store name), "V. Endor" (vendor name), and "Clipboard" (with
a capital "C"--the name of the creator). Any other MIDlet can access
clipboard if all these parameters are specified. This can be
seen in the openRecord method of
ClipboardHandler:
private void openRecord() throws Exception
{
clipboard = RecordStore.openRecordStore("clipboard",
"V. Endor", "Clipboard");
//clipboard = RecordStore.openRecordStore("newclipboard",
true);
}
Note that the significance of the commented-out line of code is
explained in the concluding section of this article.
There is one feature that is definitely desirable, if not
essential: clearing clipboard. In desktop applications, this
usually happens automatically when we close the source.
Applications on mobile phones, however, run one at a time, which
means the source must always be closed before the destination can
be opened. So automatic deletion is not a good option here. On the
other hand, the user may not like to leave the copied matter on the
clipboard if the content is confidential. That is why we display a
reminder alert when the Exit command is selected. If the
user doesn't want to clear the clipboard, the application is closed. If,
on the other hand, she decides to delete whatever had been saved,
the clearClipboard method of
ClipboardCompatible is called. This call ripples
through to ClipboardHandler, which saves an empty
string thus deleting whatever was on clipboard. The
application is then closed.
With Clipboard, ClipDemo1, and
ClipDemo2 installed on a phone, you have to run
Clipboard first to create clipboard. Thereafter, you
can exchange text between the two demo applications by copying from
one and pasting into the other.
One Final Issue
A close look at Scratchpad and
ClipboardCommandProcessor will reveal that as a
command gets removed and another is added to the instances of
Scratchpad, there is always another screen that is
sandwiched between the two actions. For example, when we select the
starting position on Copyboard, the "Start
selection" command is removed, the Copy command is added,
and an alert is shown for two seconds, after which the
Copyboard is displayed once more. While these
intervening displays keep the user informed of what is happening,
they do serve another purpose as well.
When a command is added to a Displayable that is
"actually visible on the display, and this call affects the set of
visible commands, the implementation should update the display as
soon as it is feasible to do so." The quote is from ME
documentation. In some devices the visual updating does not
occur, while in some of them the added command does not become
functional until the screen is moved to background and then made
visible again. Thus the alerts and other screens that keep popping
up during copying and pasting ensure smooth functioning of newly
added commands by temporarily hiding Copyboard and
Pasteboard.
Conclusion
We have seen how we can incorporate copy and paste functions
into an ME TextBox. Along the way, we have also seen
that ME implementations can vary subtly but significantly from one
device to another and considerable effort is required to write,
test, and debug applications that work on a wide range of devices.
The application described here has been tested on the WTK22 devices
(with the modification mentioned below), and also on a Nokia 6101
and on a Samsung 309. On WTK, the universal access mechanism for
record stores does not seem to work. So if you want to test the
basic performance on WTK, you'll need to comment out the first line
of the openRecord method of
ClipboardHandler shown above and uncomment the second.
However, in that case, each MIDlet will have its own clipboard and
copying from one to the other will not be possible. On the Nokia
and Samsung phones the application works just as expected.
Along with Copy and Paste, there is another function that is used
widely--Cut. This third function is actually a combination
of Copy and Delete. I have not shown its implementation here
because I believe it'll be an enjoyable experience for you to
enhance this application by incorporating this missing
function.
Resources
Biswajit Sarkar is an electrical engineer with a specialization in programmable industrial automation.