01: /*
02: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
03: *
04: * The program is provided "as is" without any warranty express or
05: * implied, including the warranty of non-infringement and the implied
06: * warranties of merchantibility and fitness for a particular purpose.
07: * IBM will not be liable for any damages suffered by you as a result
08: * of using the Program. In no event will IBM be liable for any
09: * special, indirect or consequential damages or lost profits even if
10: * IBM has been advised of the possibility of their occurrence. IBM
11: * will not be liable for any third party claims against you.
12: */
13: package com.ibm.richtext.textpanel;
14:
15: import java.awt.datatransfer.StringSelection;
16: import java.awt.datatransfer.DataFlavor;
17: import java.awt.datatransfer.UnsupportedFlavorException;
18: import java.io.IOException;
19:
20: import com.ibm.richtext.styledtext.MConstText;
21:
22: /**
23: * This class allows MConstText instances to be the contents
24: * of a Clipboard. To store an MConstText on the clipboard,
25: * construct a TransferableText from the MConstText, and make
26: * the TransferableText the clipboard contents.
27: *
28: */
29: /*
30: * Note: this class inherits from StringSelection because of
31: * a bug in the 1.1.7 system clipboard implementation. The
32: * system clipboard won't put text on the OS clipboard unless
33: * the content is a StringSelection.
34: */
35: final class TransferableText extends StringSelection {
36: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
37: private MConstText fText;
38:
39: private static String textToString(MConstText text) {
40: char[] chars = new char[text.length()];
41: text.extractChars(0, chars.length, chars, 0);
42: return new String(chars);
43: }
44:
45: /**
46: * Create a TransferableText for the given text.
47: * @param text the text to go on the Clipboard. The text is
48: * adopted by this object.
49: */
50: public TransferableText(MConstText text) {
51:
52: super (textToString(text));
53:
54: fText = text;
55: }
56:
57: public DataFlavor[] getTransferDataFlavors() {
58:
59: DataFlavor[] flavors = super .getTransferDataFlavors();
60: DataFlavor[] result = new DataFlavor[flavors.length + 1];
61: result[0] = MConstText.styledTextFlavor;
62: System.arraycopy(flavors, 0, result, 1, flavors.length);
63: return result;
64: }
65:
66: public boolean isDataFlavorSupported(DataFlavor flavor) {
67:
68: if (flavor.equals(MConstText.styledTextFlavor)) {
69: return true;
70: } else {
71: return super .isDataFlavorSupported(flavor);
72: }
73: }
74:
75: public Object getTransferData(DataFlavor flavor)
76: throws UnsupportedFlavorException, IOException {
77:
78: if (flavor.equals(MConstText.styledTextFlavor)) {
79: return fText;
80: } else {
81: return super.getTransferData(flavor);
82: }
83: }
84: }
|