Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » A custom copy/paste operation
A custom copy/paste operation [message #647077] Tue, 04 January 2011 13:54 Go to next message
Roel De Nijs is currently offline Roel De NijsFriend
Messages: 28
Registered: March 2010
Junior Member
Hi,

I have to develop the following scenario: when a user copies a bunch of cells (each containing an email address) in Excel and he pastes this selection into a SWT text field, the text field has to contain all email addresses seperated by a comma.

Using a Clipboard instance and a KeyListener I was able to implement this scenario: when Ctrl+V is hit, I get the current content of the clipboard instance, replace every white space occurence with a comma and set this altered text into the text field.

When the user clicks the right mouse button and chooses 'Paste' nothing happens (of course), but it should behave in exactly the same way as using Ctrl+V to paste the email addresses.

How can I solve this problem? Maybe there is a better way of handling a custom copy/paste operation which will work in both cases.

Kind regards,
Roel

[Updated on: Tue, 04 January 2011 13:55]

Report message to a moderator

Re: A custom copy/paste operation [message #647161 is a reply to message #647077] Tue, 04 January 2011 19:51 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 8
Registered: January 2011
Junior Member
I don't have a correct answer for you (new to swt), after looking a bit I found this reply :

"Create your own text component based on Text or StyledText and override copy() and paste(). This can do what you want.

Don't forget to override checkSubclass method."

I think mouse menu with copy past option on text fields are handled in a layer below ours (or above, I'm tired sorry).
To intercept the call, I would look this way... good luck !
Re: A custom copy/paste operation [message #647166 is a reply to message #647077] Tue, 04 January 2011 20:01 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 8
Registered: January 2011
Junior Member
Another way to do the thing easily, may be just to put a listener on the text widget, like a ModifyListener.
He's called when user past text using ctrl+V or mouse menu, so it should be what you looking for.

But I havent try from other thing than text, you told about space you replace, so I suppose you get a string from excel, so I suppose it may works... At least, I suppose you can still check the clipboard from this listener and do the stuff.

[Updated on: Tue, 04 January 2011 20:08]

Report message to a moderator

Re: A custom copy/paste operation [message #647176 is a reply to message #647077] Tue, 04 January 2011 20:47 Go to previous messageGo to next message
Roel De Nijs is currently offline Roel De NijsFriend
Messages: 28
Registered: March 2010
Junior Member
I will comment on both alternatives you suggested

1/ overwriting paste-method
I think that's a good alternative (which I wanted to implement), but when I had a look at the paste-method I saw something like this:
public void paste () {
    checkWidget ();
    if ((style & SWT.READ_ONLY) != 0) return;
    OS.SendMessage (handle, OS.WM_PASTE, 0, 0);
}

That's Chinese for me, I don't have any idea how I could modify this code to get the desired outcome.

2/ using a ModifyListener
I thought about this one too, but I don't think this one is the road you want to follow, because:
a) how can you know the user selected 'Paste' in the context menu (and not 'Cut' or 'Copy' or just clicked in the text field)
b) with a ModifyListener the code will also be executed when the user enters something in the text field (regular data entry). And when the clipboard contains some copied text (from any application) you'll end up with unwanted data.

On the internet you'll find tons of examples about how to copy/paste text from the clipboard, but they all have the same problem: a seperate Paste-button which you have to click to paste the clipboard contents into the Text-field. So I think implementing a custom copy/paste operation without such a button is really hard to do.

As a side note: if you copy from Excel you'll get a String with the actual cell contents mixed with tabs (to seperate columns) and new lines (to seperate rows).

I'm most grateful if someone has any sample code or tips/hints to override the paste-method. Other suggestions or alternatives are also appreciated.

Kind regards,
Roel
Re: A custom copy/paste operation [message #647346 is a reply to message #647176] Wed, 05 January 2011 18:04 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 8
Registered: January 2011
Junior Member
Roel De Nijs wrote on Tue, 04 January 2011 15:47
I will comment on both alternatives you suggested
b) with a ModifyListener the code will also be executed when the user enters something in the text field (regular data entry). And when the clipboard contains some copied text (from any application) you'll end up with unwanted data.
...
As a side note: if you copy from Excel you'll get a String with the actual cell contents mixed with tabs (to seperate columns) and new lines (to seperate rows).

Kind regards,
Roel


For b : I personally don't use clipboard, but I don't need excel data too. In my listener, I get the string by doing text.getText();
Here is how I do to parse string user enter with keyboard, or ctrl+V or menu paste :
text.addModifyListener(new ModifyListener() {
	public void modifyText(ModifyEvent dummy) {
		int selection = text.getSelection().x;
		String checked = text.getText().trim().replaceAll("[\\\\/:*?\"<>|]", "");
		if(!text.getText().equals(checked)) {
			selection -= text.getText().length() - checked.length();
			text.setText(checked); // will call this listener again, and perform the 'else' branch
			text.setSelection(selection);
		} else {
			updateSomething();
		}
	}
});

There is certainly a much better way to do that, but it works.
What I want to suggest you (but may be that not a good idea), is to not try to know from where string come from, just parse it.
If the excel string contains some space and tab, you should be able to know it comes from excel.
But I think, is there is a real difference between what should stay in the text field depending on the source, may it's more an IHM bug.
Your user should be able to enter space and tab in that field ? That's maybe a bad idea...

Roel De Nijs wrote on Tue, 04 January 2011 15:47
I will comment on both alternatives you suggested

1/ overwriting paste-method
I think that's a good alternative (which I wanted to implement), but when I had a look at the paste-method I saw something like this:
public void paste () {
    checkWidget ();
    if ((style & SWT.READ_ONLY) != 0) return;
    OS.SendMessage (handle, OS.WM_PASTE, 0, 0);
}

a) how can you know the user selected 'Paste' in the context menu (and not 'Cut' or 'Copy' or just clicked in the text field)
Roel


This chinese just means the past is handled in another layer. Don't know more like u, and in that case the wiser should be to not change anything. Just use this place as a listener.

If this method is really called every time user performs mouse paste, then you can still do something using these two listeners (may be not very nice...) :

public String uglyHack = null;
public void paste () {
    if(you can know it's excell) {
          uglyHack = get the clipboard parsed;
          or just use boolean
    }
    super.paste();
}

....

text.addModifyListener(new ModifyListener() {
	public void modifyText(ModifyEvent dummy) {
		if(uglyHack != null / false) {
			uglyHack = null / false;
			text.setText(parsed excell string);
		} else {
			// do the thing with other text
		}
	}
});


May be you could try this way....
I suppose clipboard help for handling thing like everything that is not basic text. If user mouse-past text from notepad for example, clipboard should be able to know it's different.
good luck

[Updated on: Wed, 05 January 2011 19:35]

Report message to a moderator

Re: A custom copy/paste operation [message #647434 is a reply to message #647077] Thu, 06 January 2011 10:38 Go to previous messageGo to next message
Roel De Nijs is currently offline Roel De NijsFriend
Messages: 28
Registered: March 2010
Junior Member
When you want to use data from Excel you have to use a clipboard, because when you paste the content into a text field, only the content of the 1st cell is pasted (and that's not the desired behavior).

As you already indicated working with the paste-method is a possibility, but seems to be a bit of a hack, and so should be avoided. Because it's not a good programming practice and code like this which could fail after upgrading to the next version.

I guess we will use one of the following alternatives:
- disable mouse events in the text field
- use an extra button to perform the paste-operation from Excel
- use a multi-line text field (instead of a single line one), so you can just use regular paste (without any modifications). E-mail addresses will be seperated with white space (instead of a comma) and will not be on 1 line. So that's not the desired behavior but it's a rather small difference: main functionality (copy/paste from excel) is stiIl possible, but representation is a bit different. So I hope this alternative is satisfactory for the user (because from the developer's perspective it's the simplest and cleanest, no tweaks, solution).

Thanks for your ideas and suggestions!
Kind regards,
Roel

[Updated on: Thu, 06 January 2011 10:39]

Report message to a moderator

Re: A custom copy/paste operation [message #647515 is a reply to message #647434] Thu, 06 January 2011 18:17 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 8
Registered: January 2011
Junior Member
Or you may disable mouse event on Text, and implement your own menu. It need more works and still look like a hack, but "less uncontrolled" and your interface stay standard.

An idea that come to me using this way, is to try to replace only the listener used by the default menu.
As we can do Text.getMenu(), I suppose it returns the menu used by default on right click. Using it, you can retrieve the 'Paste' menu items. This point is less obvious to perform being smart... If you have an idea for that, it will be easy to just replace the 'paste' menu item selection listener.

[Updated on: Thu, 06 January 2011 18:55]

Report message to a moderator

Re: A custom copy/paste operation [message #647521 is a reply to message #647434] Thu, 06 January 2011 18:45 Go to previous messageGo to next message
Lakshmi ShanmugamFriend
Messages: 274
Registered: July 2009
Location: India
Senior Member
Hi,

I couldn't follow your question correctly, are you trying to override the default behavior when user selects paste from the context menu?
One way to do this is to show your own context menu (instead of the default menu) with the menu items you require such as cut, copy, paste. Then, you'll be able to control when the user selects paste from the menu.

Here is an example:
When the default context menu for theText is about to be shown your MenuDetect listener will be notified. You can create your own menu with menu items inside the listener.

text.addMenuDetectListener (new MenuDetectListener() {
public void menuDetected (MenuDetectEvent e) {
Menu menu = new Menu (shell, SWT.POP_UP);
MenuItem paste = new MenuItem (menu, SWT.PUSH);
paste.setText ("Paste");
text.setMenu (menu);
paste.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
//your paste logic
}
});
//create menu items for copy, cut
MenuItem copy = new MenuItem (menu, SWT.PUSH);
copy.setText ("Copy");
text.setMenu (menu);
copy.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
text.selectAll ();
text.copy ();
}
});
}
});

Also, if you do not want to show the context menu at all, you can set event.doit = false inside the listener.

HTH,


Lakshmi P Shanmugam
Re: A custom copy/paste operation [message #647522 is a reply to message #647434] Thu, 06 January 2011 18:45 Go to previous messageGo to next message
Lakshmi ShanmugamFriend
Messages: 274
Registered: July 2009
Location: India
Senior Member
Hi,

I couldn't follow your question correctly, are you trying to override the default behavior when user selects paste from the context menu?
One way to do this is to show your own context menu (instead of the default menu) with the menu items you require such as cut, copy, paste. Then, you'll be able to control when the user selects paste from the menu.

Here is an example:
When the default context menu for theText is about to be shown your MenuDetect listener will be notified. You can create your own menu with menu items inside the listener.

text.addMenuDetectListener (new MenuDetectListener() {
public void menuDetected (MenuDetectEvent e) {
Menu menu = new Menu (shell, SWT.POP_UP);
MenuItem paste = new MenuItem (menu, SWT.PUSH);
paste.setText ("Paste");
text.setMenu (menu);
paste.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
//your paste logic
}
});
//create menu items for copy, cut
MenuItem copy = new MenuItem (menu, SWT.PUSH);
copy.setText ("Copy");
text.setMenu (menu);
copy.addSelectionListener (new SelectionAdapter() {
public void widgetSelected (SelectionEvent e) {
text.selectAll ();
text.copy ();
}
});
}
});

Also, if you do not want to show the context menu at all, you can set event.doit = false inside the listener.

HTH,


Lakshmi P Shanmugam
Re: A custom copy/paste operation [message #647620 is a reply to message #647515] Fri, 07 January 2011 12:12 Go to previous messageGo to next message
Roel De Nijs is currently offline Roel De NijsFriend
Messages: 28
Registered: March 2010
Junior Member
Both disabling the context menu and creating our own context menu were alternatives I proposed to the responsible person. But he decided to use another alternative: using a seperate button for pasting the e-mail addresses from the clipboard.

Thanks for your help and suggestions, No Real Name.
Re: A custom copy/paste operation [message #647623 is a reply to message #647077] Fri, 07 January 2011 12:45 Go to previous message
Roel De Nijs is currently offline Roel De NijsFriend
Messages: 28
Registered: March 2010
Junior Member
Hi Lakshmi,

My initial question was: what's the best way/practice to customize the paste-operation. For example: people copy email addresses (1 per cell) from Excel and paste it into a swt single-line text field (comma seperated).

I was able to perform this behavior when the user uses Ctrl+V, but not when the user uses the default context menu. So I was looking for the best practice or alternatives.

So your suggestions of overriding the default context menu (with a code sample) or disabling it are highly appreciated (and were also suggested in the previous post).

I proposed a lot of alternatives (the ones you suggested were included) to the responsible person and he decided to use another alternative: using a seperate button for pasting the e-mail addresses from the clipboard. This alternative is more conform with the "philosophy" of our application (deliver a consistent look-and-feel to the user)

Kind regards,
Roel
Previous Topic:TreeViewers, Editing support and multiple Selection
Next Topic:Buttons in TreeEditor twinkle on item expand/collapse
Goto Forum:
  


Current Time: Wed Dec 17 20:02:02 GMT 2014

Powered by FUDForum. Page generated in 0.01884 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software