|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
HTMLTextSelection.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
import java.awt.datatransfer.DataFlavor;
|
|
20 |
import java.awt.datatransfer.Transferable;
|
|
21 |
import java.awt.datatransfer.Clipboard;
|
|
22 |
import java.awt.datatransfer.ClipboardOwner;
|
|
23 |
import java.awt.datatransfer.UnsupportedFlavorException;
|
|
24 |
import java.io.IOException;
|
|
25 |
import java.io.StringReader;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* A transferable for HTML text.
|
|
29 |
*
|
|
30 |
* <p>It can be used in drag and drop operations or in copy and paste
|
|
31 |
* operations. Additional to <code>HTMLText</code> it supports the
|
|
32 |
* <code>String</code> data flavor.</p>
|
|
33 |
*
|
|
34 |
* @author Ulrich Hilger
|
|
35 |
* @author Light Development
|
|
36 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
37 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
38 |
* @author published under the terms and conditions of the
|
|
39 |
* GNU General Public License,
|
|
40 |
* for details see file gpl.txt in the distribution
|
|
41 |
* package of this software
|
|
42 |
*
|
|
43 |
* @version stage 11, April 27, 2003
|
|
44 |
*
|
|
45 |
* @see java.awt.datatransfer.DataFlavor.stringFlavor
|
|
46 |
* @see java.awt.datatransfer.DataFlavor.plainTextFlavor
|
|
47 |
* @see com.lightdev.app.shtm.HTMLText
|
|
48 |
*/
|
|
49 |
|
|
50 |
public class HTMLTextSelection implements Transferable, ClipboardOwner |
|
51 |
{ |
|
52 |
|
|
53 |
/** index of HTML text data flavor */
|
|
54 |
private static final int HTML_TEXT = 0; |
|
55 |
|
|
56 |
/** index of String data flavor */
|
|
57 |
private static final int STRING = 1; |
|
58 |
|
|
59 |
/** the data to transfer */
|
|
60 |
private HTMLText data;
|
|
61 |
|
|
62 |
/** the data flavor of this transferable */
|
|
63 |
private static final DataFlavor[] flavors = { |
|
64 |
new DataFlavor(HTMLText.class, "HTMLText"), |
|
65 |
DataFlavor.stringFlavor |
|
66 |
}; |
|
67 |
|
|
68 |
/**
|
|
69 |
* construct a <code>HTMLTextSelection</code> with a chunk
|
|
70 |
* of styled text.
|
|
71 |
*
|
|
72 |
* @param data - a HTMLText object
|
|
73 |
*
|
|
74 |
* @see com.lightdev.app.shtm.HTMLText
|
|
75 |
*/
|
|
76 | 0 |
public HTMLTextSelection(HTMLText data) {
|
77 | 0 |
this.data = data;
|
78 |
} |
|
79 |
|
|
80 |
/* ---- start of Transferable implementation ----------------------------*/
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Returns an array of DataFlavor objects indicating the flavors the data
|
|
84 |
* can be provided in. The array should be ordered according to preference
|
|
85 |
* for providing the data (from most richly descriptive to least descriptive).
|
|
86 |
* @return an array of data flavors in which this data can be transferred
|
|
87 |
*/
|
|
88 | 0 |
public DataFlavor[] getTransferDataFlavors() {
|
89 | 0 |
return (DataFlavor[])flavors.clone();
|
90 |
} |
|
91 |
|
|
92 |
/**
|
|
93 |
* Returns whether or not the specified data flavor is supported for
|
|
94 |
* this object.
|
|
95 |
* @param flavor the requested flavor for the data
|
|
96 |
* @return boolean indicating wjether or not the data flavor is supported
|
|
97 |
*/
|
|
98 | 0 |
public boolean isDataFlavorSupported(DataFlavor flavor) { |
99 | 0 |
for (int i = 0; i < flavors.length; i++) { |
100 | 0 |
if (flavors[i].equals(flavor)) {
|
101 | 0 |
return true; |
102 |
} |
|
103 |
} |
|
104 | 0 |
return false; |
105 |
} |
|
106 |
|
|
107 |
/**
|
|
108 |
* Returns an object which represents the data to be transferred. The class
|
|
109 |
* of the object returned is defined by the representation class of the flavor.
|
|
110 |
*
|
|
111 |
* @param flavor the requested flavor for the data
|
|
112 |
* @see DataFlavor#getRepresentationClass
|
|
113 |
* @exception IOException if the data is no longer available
|
|
114 |
* in the requested flavor.
|
|
115 |
* @exception UnsupportedFlavorException if the requested data flavor is
|
|
116 |
* not supported.
|
|
117 |
*/
|
|
118 | 0 |
public Object getTransferData(DataFlavor flavor) throws |
119 |
UnsupportedFlavorException, IOException |
|
120 |
{ |
|
121 | 0 |
if (flavor.equals(flavors[HTML_TEXT])) {
|
122 | 0 |
return (Object) data;
|
123 |
} |
|
124 | 0 |
else if (flavor.equals(flavors[STRING])) { |
125 | 0 |
return (Object) data.toString();
|
126 |
} |
|
127 |
else {
|
|
128 | 0 |
throw new UnsupportedFlavorException(flavor); |
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
/* ----------- end of Transferable implementation ------------------- */
|
|
133 |
|
|
134 |
/* ----------- start of ClipboardOwner implementation --------------- */
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Notifies this object that it is no longer the owner of
|
|
138 |
* the contents of the clipboard.
|
|
139 |
* @param clipboard the clipboard that is no longer owned
|
|
140 |
* @param contents the contents which this owner had placed on the clipboard
|
|
141 |
*/
|
|
142 | 0 |
public void lostOwnership(Clipboard clipboard, Transferable contents) { |
143 |
} |
|
144 |
|
|
145 |
/* ------------ end of ClipboardOwner implementation ---------------- */
|
|
146 |
} |
|