[Webfunds-commits] java/webfunds/utils ClipboardHelper.java
Jeroen C. van Gelderen
gelderen@cypherpunks.ai
Wed, 14 Mar 2001 13:44:32 -0400 (AST)
gelderen 01/03/14 13:44:32
Added: webfunds/utils ClipboardHelper.java
Log:
Initial version.
Revision Changes Path
1.1 java/webfunds/utils/ClipboardHelper.java
Index: ClipboardHelper.java
===================================================================
/* $Id: ClipboardHelper.java,v 1.1 2001/03/14 17:44:32 gelderen Exp $
*
* Copyright (c) Systemics Inc. 2001 on behalf of
* the WebFunds Development Team. All Rights Reserved.
*/
package webfunds.utils;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
/**
* Helper class for Java Clipboard related functionality.
*
* The Java clipboard implementation always expects LF line endings and
* blindly adds extra CRs on Windows, even when the text already has CR/LF
* line endings. This class helps convert text in a platform encoding to
* text with UNIX line endings so that the Clipboard can then reconvert to
* platform line endings. Thanks Sun!
*
* @version $Revision: 1.1 $
* @author Jeroen C. van Gelderen (gelderen@systemics.com)
*/
public final class ClipboardHelper {
private static final Clipboard clip;
/**
* char[] containing the platform-specific sequence of line ending
* characters. This is not final in order for the tests to work.
*/
private static char[] NEWLINECHARS =
System.getProperty("line.separator").toCharArray();
static {
Toolkit toolkit = Toolkit.getDefaultToolkit();
clip = toolkit.getSystemClipboard();
}
/**
* Put the given text (which must have platform line endings!) on the
* clipboard.
*/
public static void setClipboardText(String text) {
String unixText = toUnixText(text);
StringSelection sel = new StringSelection(unixText);
clip.setContents(sel, null);
}
/**
* Convert the given text from platform line endings to UNIX line endings.
*
* Currently only CR, LF and CR/LF are handled correctly and mixed line
* endings are not detected.
*/
public static String toUnixText(String text) {
StringBuffer buf = new StringBuffer( text.length() );
boolean lineFeedPending = false;
for(int i=0; i < text.length(); i++) {
char c = text.charAt(i);
if( lineFeedPending && (c != '\n') )
buf.append('\n');
lineFeedPending = (c == '\r');
if( !lineFeedPending )
buf.append(c);
}
if( lineFeedPending )
buf.append('\n');
return buf.toString();
}
// Test
//............................................................................
public static void main(String[] argv) {
NEWLINECHARS = "\n".toCharArray();
String unixText = makeText("\n");
if( !unixText.equals( toUnixText(unixText) ) )
throw new RuntimeException("unixText != unixText");
NEWLINECHARS = "\r\n".toCharArray();
String dosText = makeText("\r\n");
if( !unixText.equals( toUnixText(dosText) ) )
throw new RuntimeException("unixText != dosText");
NEWLINECHARS = "\r".toCharArray();
String macText = makeText("\r");
if( !unixText.equals( toUnixText(macText) ) )
throw new RuntimeException("unixText != macText");
/*
XXX: we need a scanner in order to handle this correctly
NEWLINECHARS = "\r\r".toCharArray();
String weirdText = makeText("\r\r");
if( !unixText.equals( toUnixText(weirdText) ) )
throw new RuntimeException("unixText != weirdText");
*/
System.out.println("Test completed successfully!");
System.exit(0);
}
private static String makeText(String lineFeed) {
return lineFeed +
"line1" + lineFeed +
"line2" + lineFeed +
lineFeed +
"line3" + lineFeed + lineFeed;
}
}