/**
* Copyright 2011 multibit.org
*
* Licensed under the MIT license (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.bither.viewsystem.action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
public final class TextTransfer implements ClipboardOwner {
private final static Logger log = LoggerFactory.getLogger(TextTransfer.class);
public static void main(String... aArguments) {
TextTransfer textTransfer = new TextTransfer();
// display what is currently on the clipboard
log.debug("Clipboard contains: {}", textTransfer.getClipboardContents());
// change the contents and then re-display
textTransfer.setClipboardContents("blah, blah, blah");
log.debug("Clipboard contains: {}", textTransfer.getClipboardContents());
}
/**
* Empty implementation of the ClipboardOwner interface.
*/
@Override
public void lostOwnership(Clipboard aClipboard, Transferable aContents) {
// do nothing
}
/**
* Place a String on the clipboard, and make this class the owner of the
* Clipboard's contents.
*/
public void setClipboardContents(String aString) {
StringSelection stringSelection = new StringSelection(aString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);
}
/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an empty
* String.
*/
public String getClipboardContents() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText = (contents != null)
&& contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if (hasTransferableText) {
try {
result = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
// highly unlikely since we are using a standard DataFlavor
log.error("UnsupportedFlavorException: {}", e.getMessage(), e);
} catch (IOException e) {
log.error("IOException: {}", e.getMessage(), e);
}
}
return result;
}
}