Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Java - CUT&PASTE

// Cut&Paste Method
package system.clipboard;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;	 

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener
{
	private static final long serialVersionUID = 1L;
	
	JTextArea srcText, dstText;
	JButton copyButton, pasteButton;
	Clipboard clipboard = getToolkit().getSystemClipboard();

	public ClipBoard()
	{
		super("Clipboard Test");
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		setLayout(gridbag);
		
		srcText = new JTextArea(8, 32);
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.CENTER;
		gridbag.setConstraints(srcText, c);
		add(srcText);

		copyButton = new JButton("Copy Above");
		copyButton.setActionCommand("copy");
		copyButton.addActionListener(this);
		c.gridy = 1;
		c.gridwidth = 1;
		gridbag.setConstraints(copyButton, c);
		add(copyButton);
		
		pasteButton = new JButton("Paste Below");
		pasteButton.setActionCommand("paste");
		pasteButton.addActionListener(this);
		pasteButton.setEnabled(false);
		c.gridx = 1;
		gridbag.setConstraints(pasteButton, c);
		add(pasteButton);
		
		dstText = new JTextArea(8, 32);
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		gridbag.setConstraints(dstText, c);
		add(dstText);
		
		pack();
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		String cmd = evt.getActionCommand();
		
		if(cmd.equals("copy")) 
		{
			// Implement Copy operation
			String srcData = srcText.getText();
			
			if(srcData != null)
			{
				StringSelection contents = new StringSelection(srcData);
				clipboard.setContents(contents, this);
				pasteButton.setEnabled(true);
			}
		}
		else if(cmd.equals("paste"))
		{
			// Implement Paste operation
			Transferable content = clipboard.getContents(this);
			if(content != null) 
			{
				try
				{
					String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
					dstText.append(dstData);
				}
				catch(Exception e)
				{
					System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName());
				}
			}
		}
	}

	public void lostOwnership(Clipboard clipboard, Transferable contents)
	{
		System.out.println("Clipboard contents replaced");
	}
	
	public static void main(String[] args) 
	{
		ClipBoard test = new ClipBoard();
		test.setVisible(true);
	}
}

Getting HTML from Windows clipboard

Get the code for class HtmlClipboard() by Phillip Piper here.

Usage
>>> cb = HtmlClipboard()
>>> cb.GetAvailableFormats()
[49161, 49562, 49339, 49560, 49561, 13, 1, 49334, 49335, 49333, 49344, 49478, 49171, 16, 7]
>>> cb.HasHtmlFormat()
True
>>> cb.GetFromClipboard()
>>> cb.htmlClipboardVersion
'0.9'
>>> cb.GetFragment()
'<p>Writing to the clipboard is <strong>easy</strong> with this code.</p>'
>>> cb.GetSource()
'http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474121'
>>> 

You can also put a html fragment to the clipboard as well.
See more detials in the recipe.

clip function

    clip: func ["write to clipboard shortcut" target /local res] [
        write clipboard://
            res: either any [file? target url? target] [read target] [target]
        res
    ]

setClipboard for Firefox

post : 2006-01-10
update : 2006-01-10
maybe work : Firefox1.5 and Opera8.5
function setClipboard(text){
	var url = [
		'data:text/html;charset=utf-8;base64,PGJvZHk+PC9ib2',
		'R5PjxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij4KKGZ1',
		'bmN0aW9uKGVuY29kZWQpe3ZhciBzd2ZfZGF0YSA9IFsKICdkYX',
		'RhOmFwcGxpY2F0aW9uL3gtc2hvY2t3YXZlLWZsYXNoO2Jhc2U2',
		'NCxRMWRUQjJ3JywKICdBQUFCNG5EUGdZbGpBd01qSTRNejAlMk',
		'YlMkY5JTJGZTJaZkJnYUdhV3dNRE1uNUthJywKICdrTU10TjRH',
		'ZGdaZ1NJTXdaWEZKYW01UUFFJTJCQm9iaTFCTG5uTXlDcFB6RW',
		'9oU0dJJywKICdQRnAlMkZBeHNEREJRa3BGWkRGUUZGQ2d1eVM4',
		'QXlqSTRBRVVCaXkwVndBJTNEJTNEJwpdLmpvaW4oIiIpOwpkb2',
		'N1bWVudC5ib2R5LmlubmVySFRNTCA9IFsKICc8ZW1iZWQgc3Jj',
		'PSInLHN3Zl9kYXRhLCciICcsCiAnRmxhc2hWYXJzPSJjb2RlPS',
		'csZW5jb2RlZCwnIj4nLAogJzwvZW1iZWQ+JwpdLmpvaW4oIiIp',
		'Owp9KSgi',
		base64encode( encodeURIComponent(text) + '")</'+'script>')
	].join("");
	var tmp = document.createElement("div");
	tmp.innerHTML = [
		 '<iframe src="',url,'"'
		,' width="0" height="0">'
		,'</iframe>'
	].join("");
	with(tmp.style){
		position ="absolute";
		left = "-10px";
		top  = "-10px";
		visibility = "hidden";
	};
	document.body.appendChild(tmp);
	setTimeout(function(){document.body.removeChild(tmp)},1000);
	function base64encode(str){
		var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
		var c1, c2, c3;
		var buf = [];
		var len = str.length;
		var i = 0;
		while(i < len){
			c1 = str.charCodeAt(i) & 0xff;
			c2 = str.charCodeAt(i+1);
			c3 = str.charCodeAt(i+2);
			buf.push(Chars[(c1 >> 2)]);
			if(i+1 == len){
				buf.push(Chars[(c1 & 0x3) << 4],"==");
				break;
			}
			buf.push(Chars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]);
			if(i+2 == len){
				buf.push(Chars[(c2 & 0xF) << 2],"=");
				break;
			}
			buf.push(
				Chars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)],
				Chars[(c3 & 0x3F)]
			);
			i+=3;
		}
		return buf.join("")
	}
}


here is demo.

just a firefox only
http://la.ma.la/misc/js/setclipboard.txt

Get and set windows clipboard

From June Kim's recipe.
import win32clipboard as w 
import win32con

def getText(): 
    w.OpenClipboard() 
    d=w.GetClipboardData(win32con.CF_TEXT) 
    w.CloseClipboard() 
    return d 
 
def setText(aType,aString): 
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(aType,aString) 
    w.CloseClipboard()
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS