0

How can I store clipboard information as data that is represented in a non-standard, custom-defined way?

My company has a custom-made SWT control for creating textboxes with masks, allowing a user to fill in a prompt for things such as phone numbers:

example1

example2

When data from this control is copied, I want a way that subsequent pastes of the data vary by context. For example, if the contexts are copied and pasted into a text file, I want the masked characters to be included. However, if it is pasted into other certain controls in our system, I want it to only paste the non-mask characters. Since any arbitrary characters can be used for the mask characters in the mask control, it is not possible for the controls receiving the pasted to be made to parse the pasted result. As such, I believe that the only way to accomplish this is to customize the way that the copied data is stored in clipboard. Is this plausible?

Southpaw Hare
  • 1,535
  • 3
  • 24
  • 50

1 Answers1

3

If you use a custom version of the Text control and override the copy() method you can use your own code to copy to the Clipboard object. The format of data in the clipboard is controlled by the Transfer object(s) you use. You can supply several different Transfer objects and data:

Clipboard clipboard = new Clipboard(display);

String maskedData = "*******";
String unmaskedData = "password";

TextTransfer textTransfer = TextTransfer.getInstance();
UnmaskedTransfer umaskedTransfer = UnmaskedTransfer.getInstance();

Transfer [] transfers = new Transfer [] {textTransfer, umaskedTransfer};
Object [] data = new Object [] {maskedData, unmaskedData};

clipboard.setContents(data, transfers);

clipboard.dispose();

UnmaskedTransfer is your own Transfer implementation which has the unmasked password. The controls in your system which need to get the unmasked text can ask the clipboard for data using that Transfer object (this assumes these controls are in your code and can be customized to do this).

Controls which do not know about the special Transfer object will use TextTransfer and receive the masked password.

The custom Text control might look something like this:

public class CustomText extends Text
{
  public CustomText(final Composite parent, final int style)
  {
    super(parent, style);
  }

  @Override
  public void copy()
  {
    // TODO your copy code
  }

  @Override
  public void paste()
  {
    // TODO your paste code
  }

  @Override
  protected void checkSubclass()
  {
    // Don't call super to stop subclassing exception
  }
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • I think this is exactly what I was looking for! I'll have to try it out next week! – Southpaw Hare Feb 22 '14 at 23:04
  • I am having some trouble understanding how to properly override the cut/copy/paste methods. These methods do not seem to get triggered by normal means (ctrl+X/C/V, context menu options). Can you elaborate on this part? – Southpaw Hare Feb 26 '14 at 18:44
  • Added outline of the custom text control. – greg-449 Feb 26 '14 at 18:56
  • That is indeed what I did, and it was unsuccessful. I have posted a new question on the matter: http://stackoverflow.com/q/22050899/1961815 – Southpaw Hare Feb 26 '14 at 19:09