Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

Java, the OLPC, and community responsibility

The "One Laptop Per Child" project has a great device ready to ship, but there's no Java on there. Let's think about working together to put Java on OLPC!
Replies: 1 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Providing consistent clipboard behavior in form-based workbench parts

At 10:20 AM on Jan 20, 2006, Peter Nehrer DeveloperZone Top 100 wrote:

When you create a workbench part, such as a custom editor or a view, you can contribute your custom actions to your part's action bars -- toolbar, menubar, and the status line -- which the workbench then arranges and displays appropriately. You can also contribute your own implementations of global, retargetable actions, such as the familiar editing actions Cut, Copy, Paste, Delete, and Select All. For example, you may implement the Copy action for your table to copy the selected data to clipboard in application-specific format, or convert it to some standard format in application-specific way.

When creating more complex user interfaces, such as the form-like data entry editors, which often consist of several text controls, checkboxes, tables, and so on, you should remember that users will likely expect your text controls to behave in the standard way -- pressing Ctrl+C in Windows, for example, should copy the field's selected text to clipboard. After all, this is what they're familiar with in dialog windows (not to mention web pages).

However, text controls created in workbench parts do not, by default, get that behavior. This is because you as the creator of the workbench part get to make your own contributions to the action bars, whether or not you use text controls. What you need to do is make sure that whenever a text control has the focus, the global actions Cut, Copy, Paste, Delete, and Select All are delegated to its native clipboard methods.

Doesn't Eclipse already have a solution for this fequently encountered problem? Fortunately, it does -- class TextActionHandler allows you to provide your own, application-specific edit actions, but delegate to registered text controls' clipboard methods whenever one is active.

In the attached example, we create a simple, form-based view with a text control and a table. You can type arbitrary text into the text control and press the Add button to add it to the table. You can also delete selected table entries using the Delete button. Here's the relevant code snippet where we set up the TextActionHandler:

// create TextActionHandler for the view's action bars
IActionBars actionBars = getViewSite().getActionBars();
TextActionHandler textActionHandler = new TextActionHandler(actionBars);
 
// hook in the text control
textActionHandler.addText(text);
 
// set custom clipboard actions to delegate to when not in the text control
textActionHandler.setCutAction(cutAction);
textActionHandler.setCopyAction(copyAction);
textActionHandler.setDeleteAction(deleteAction);
textActionHandler.setPasteAction(pasteAction);
textActionHandler.setSelectAllAction(selectAllAction);


To see the full example, just import the attached archive into your workspace as an existing project. You will need Eclipse 3.1 to build and run it.

Note that the TextActionHandler class is not part of the generic workbench, but rather the IDE. This, in my mind, is unnecessary, as it has no strong dependencies on the IDE as such. If you believe this class could be of use to you in generic, non-IDE applications, please vote for the feature enhancement request https://bugs.eclipse.org/bugs/show_bug.cgi?id=124512.
  Click to reply to this thread Reply
1. At 11:23 AM on Sep 13, 2006, Jonas Rüttimann Javalobby Newcomers wrote:

Re: Providing consistent clipboard behavior in form-based workbench parts

This works very well, thanks a lot!
But if I try to run this example as an RCP Application this doesn't work. The system's action do their job instead.

Why is that?
How do I implement my own CUT, COPY or PASTE action?

thread.rss_message