01: ///////////////////////////////////////////////////////////////////////////////
02: //
03: // This program is free software; you can redistribute it and/or modify
04: // it under the terms of the GNU General Public License and GNU Library
05: // General Public License as published by the Free Software Foundation;
06: // either version 2, or (at your option) any later version.
07: //
08: // This program is distributed in the hope that it will be useful,
09: // but WITHOUT ANY WARRANTY; without even the implied warranty of
10: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: // GNU General Public License and GNU Library General Public License
12: // for more details.
13: //
14: // You should have received a copy of the GNU General Public License
15: // and GNU Library General Public License along with this program; if
16: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
17: // MA 02139, USA.
18: //
19: //////////////////////////////////////////////////////////////////////////////
20:
21: package org.rdesktop.server.rdp.rdp5.cliprdr;
22:
23: import java.awt.datatransfer.*;
24:
25: import org.rdesktop.server.rdp.RdpPacket;
26:
27: public class UnicodeHandler extends TypeHandler {
28: public boolean formatValid(int format) {
29: return (format == CF_UNICODETEXT);
30: }
31:
32: public boolean mimeTypeValid(String mimeType) {
33: return mimeType.equals("text");
34: }
35:
36: public int preferredFormat() {
37: return CF_UNICODETEXT;
38: }
39:
40: public void handleData(RdpPacket data, int length,
41: ClipInterface clipInterface) {
42: String thingy = "";
43: for (int i = 0; i < length; i += 2) {
44: int aByte = data.getLittleEndian16();
45: if (aByte != 0) {
46: thingy += (char) (aByte);
47: }
48: }
49:
50: clipInterface.copyToClipboard(new StringSelection(thingy));
51: }
52:
53: public String name() {
54: return "CF_UNICODETEXT";
55: }
56:
57: public byte[] fromTransferable(Transferable in) {
58: String thingy;
59: if (in != null) {
60: try {
61: thingy = (String) in
62: .getTransferData(DataFlavor.stringFlavor);
63: } catch (Exception e) {
64: thingy = e.toString();
65: }
66:
67: thingy = thingy.replace('\n', (char) 0x0a);
68: thingy = thingy.replaceAll("" + (char) 0x0a, ""
69: + (char) 0x0d + (char) 0x0a);
70:
71: byte[] sBytes = thingy.getBytes();
72: int length = sBytes.length;
73: int lengthBy2 = length * 2;
74:
75: RdpPacket packet = new RdpPacket(lengthBy2);
76: for (int i = 0; i < sBytes.length; i++) {
77: packet.setLittleEndian16(sBytes[i]);
78: }
79:
80: sBytes = new byte[length * 2];
81: packet.copyToByteArray(sBytes, 0, 0, lengthBy2);
82: return sBytes;
83: }
84:
85: return null;
86: }
87:
88: public void send_data(Transferable in, ClipInterface clipInterface) {
89: byte[] data = fromTransferable(in);
90: clipInterface.send_data(data, data.length);
91: }
92: }
|