if (!fTextWidget.getBlockSelection()) {
fTextWidget.paste();
} else {
wrapCompoundChange(new Runnable(){
public void run() {
SelectionProcessor processor= new SelectionProcessor(TextViewer.this);
Clipboard clipboard= new Clipboard(getDisplay());
try {
/*
* Paste in block selection mode. If the pasted text is not a multi-line
* text, pasting behaves like typing, i.e. the pasted text replaces
* the selection on each line. If the pasted text is multi-line (e.g. from
* copying a column selection), the selection is replaced, line-by-line, by
* the corresponding contents of the pasted text. If the selection touches
* more lines than the pasted text, the selection on the remaining lines
* is deleted (assuming an empty text being pasted). If the pasted
* text contains more lines than the selection, the selection is extended
* to the succeeding lines, or more lines are added to accommodate the
* paste operation.
*/
ISelection selection= getSelection();
TextTransfer plainTextTransfer = TextTransfer.getInstance();
String contents= (String)clipboard.getContents(plainTextTransfer, DND.CLIPBOARD);
String toInsert;
if (TextUtilities.indexOf(fDocument.getLegalLineDelimiters(), contents, 0)[0] != -1) {
// multi-line insertion
toInsert= contents;
} else {
// single-line insertion
int length= contents.length();
int lines= processor.getCoveredLines(selection);
String delim= fDocument.getLegalLineDelimiters()[0];
StringBuffer text= new StringBuffer(lines * length + (lines - 1) * delim.length());
text.append(contents);
for (int i= 0; i < lines - 1; i++) {
text.append(delim);
text.append(contents);
}
toInsert= text.toString();
}
processor.doReplace(selection, toInsert);
} catch (BadLocationException x) {
if (TRACE_ERRORS)
System.out.println(JFaceTextMessages.getString("TextViewer.error.bad_location.paste")); //$NON-NLS-1$
} finally {
clipboard.dispose();