2 * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 package com.ibm.richtext.textpanel;
\r
15 import java.awt.datatransfer.Clipboard;
\r
16 import java.awt.datatransfer.ClipboardOwner;
\r
17 import java.awt.datatransfer.Transferable;
\r
18 import java.awt.datatransfer.DataFlavor;
\r
19 import java.awt.datatransfer.UnsupportedFlavorException;
\r
20 import java.io.IOException;
\r
21 import java.io.InputStream;
\r
23 import java.awt.Toolkit;
\r
25 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
27 import com.ibm.richtext.styledtext.MConstText;
\r
28 import com.ibm.richtext.styledtext.StyledText;
\r
31 * Wrapper for java.awt.datatransfer.Clipboard
\r
32 * Packages an MConstText in a transferable, and puts it on the clipboard.
\r
35 class StyledTextClipboard implements ClipboardOwner {
\r
37 static final String COPYRIGHT =
\r
38 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
39 // This class has a workaround for a bug in the Windows system clipboard.
\r
40 // The system clipboard will only return String content, even
\r
41 // though it has a reference to the contents. So if our
\r
42 // clipboard is the system clipboard, we'll keep a reference
\r
43 // to the content and use that instead of what the Clipboard returns.
\r
45 private static Clipboard SYSTEM = null;
\r
48 SYSTEM = Toolkit.getDefaultToolkit().getSystemClipboard();
\r
50 catch(Throwable th) {
\r
54 private static StyledTextClipboard fgSystemClipboard = null;
\r
56 public static StyledTextClipboard getClipboardFor(Clipboard clipboard) {
\r
58 if (clipboard == SYSTEM && SYSTEM != null) {
\r
59 synchronized(SYSTEM) {
\r
60 if (fgSystemClipboard == null) {
\r
61 fgSystemClipboard = new StyledTextClipboard(SYSTEM, true);
\r
64 return fgSystemClipboard;
\r
67 return new StyledTextClipboard(clipboard, false);
\r
71 private Clipboard fClipboard;
\r
72 private boolean fUseLocalContents;
\r
73 private Transferable fContents = null;
\r
75 private StyledTextClipboard(Clipboard clipboard, boolean useLocalContents) {
\r
77 if (clipboard == null) {
\r
78 fClipboard = new Clipboard("TextPanel clipboard");
\r
81 fClipboard = clipboard;
\r
84 fUseLocalContents = useLocalContents;
\r
87 public void lostOwnership(Clipboard clipboard,
\r
88 Transferable contents) {
\r
89 if (contents == fContents) {
\r
90 this.fContents = null;
\r
94 public void setContents(MConstText newContents) {
\r
96 TransferableText contents = new TransferableText(newContents);
\r
97 if (fClipboard == SYSTEM) {
\r
98 fContents = contents;
\r
100 fClipboard.setContents(contents, this);
\r
103 private Transferable getClipboardContents() {
\r
105 if (fUseLocalContents && fContents != null) {
\r
109 return fClipboard.getContents(this);
\r
113 * Has contents - faster than getContents for finding out whether the
\r
114 * clipboard has text.
\r
116 public boolean hasContents() {
\r
118 Transferable contents = getClipboardContents();
\r
120 if (contents == null) {
\r
124 return contents.isDataFlavorSupported(MConstText.styledTextFlavor) ||
\r
125 contents.isDataFlavorSupported(DataFlavor.stringFlavor) ||
\r
126 contents.isDataFlavorSupported(DataFlavor.plainTextFlavor);
\r
129 private String getString(InputStream inStream) throws IOException {
\r
131 String value = new String();
\r
135 byte inBytes[] = new byte[inStream.available()];
\r
136 bytesRead = inStream.read(inBytes);
\r
138 if (bytesRead != -1)
\r
139 value = value + new String(inBytes);
\r
141 } while (bytesRead != -1);
\r
147 * If the Clipboard has text content, return it as an
\r
148 * MConstText. Otherwise return null.
\r
149 * @param defaultStyle the style to apply to unstyled
\r
150 * text (such as a String). If the clipboard
\r
151 * has styled text this parameter is not used.
\r
153 public MConstText getContents(AttributeMap defaultStyle) {
\r
155 Transferable contents = getClipboardContents();
\r
157 if (contents == null) {
\r
161 DataFlavor flavors[] = contents.getTransferDataFlavors();
\r
163 // search flavors for our flavor, String flavor and raw text flavor
\r
165 Exception ex = null;
\r
170 for (i=0; i < flavors.length; i++) {
\r
171 if (flavors[i].equals(MConstText.styledTextFlavor))
\r
175 if (i < flavors.length) {
\r
177 Object data = contents.getTransferData(MConstText.styledTextFlavor);
\r
179 System.out.println("Data is null.");
\r
180 return (MConstText) data;
\r
183 for (i=0; i < flavors.length; i++) {
\r
184 if (flavors[i].equals(DataFlavor.stringFlavor))
\r
188 if (i < flavors.length) {
\r
190 Object data = contents.getTransferData(DataFlavor.stringFlavor);
\r
191 return new StyledText((String) data, defaultStyle);
\r
194 for (i=0; i < flavors.length; i++) {
\r
195 if (flavors[i].equals(DataFlavor.plainTextFlavor))
\r
199 if (i < flavors.length) {
\r
201 Object data = contents.getTransferData(DataFlavor.plainTextFlavor);
\r
203 String textString = getString((InputStream) data);
\r
204 return new StyledText(textString, defaultStyle);
\r
207 catch(UnsupportedFlavorException e) {
\r
210 catch(IOException e) {
\r
214 System.out.println("Exception when retrieving data. Exception:" + ex);
\r