History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/javax/swing/plaf/basic/BasicTransferable.java (1 of 18352 files). Show all

Unified · Split

java/pkg/javax/swing/plaf/basic/BasicTransferable.java+282−0
@@ -0,0 +1,282 @@
1+/*
2+ * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+package javax.swing.plaf.basic;
26+
27+import java.io.*;
28+import java.awt.datatransfer.*;
29+import javax.swing.plaf.UIResource;
30+
31+/**
32+ * A transferable implementation for the default data transfer of some Swing
33+ * components.
34+ *
35+ * @author Timothy Prinzing
36+ */
37+class BasicTransferable implements Transferable, UIResource {
38+
39+ protected String plainData;
40+ protected String htmlData;
41+
42+ private static DataFlavor[] htmlFlavors;
43+ private static DataFlavor[] stringFlavors;
44+ private static DataFlavor[] plainFlavors;
45+
46+ static {
47+ try {
48+ htmlFlavors = new DataFlavor[3];
49+ htmlFlavors[0] = new DataFlavor("text/html;class=java.lang.String");
50+ htmlFlavors[1] = new DataFlavor("text/html;class=java.io.Reader");
51+ htmlFlavors[2] = new DataFlavor("text/html;charset=unicode;class=java.io.InputStream");
52+
53+ plainFlavors = new DataFlavor[3];
54+ plainFlavors[0] = new DataFlavor("text/plain;class=java.lang.String");
55+ plainFlavors[1] = new DataFlavor("text/plain;class=java.io.Reader");
56+ plainFlavors[2] = new DataFlavor("text/plain;charset=unicode;class=java.io.InputStream");
57+
58+ stringFlavors = new DataFlavor[2];
59+ stringFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=java.lang.String");
60+ stringFlavors[1] = DataFlavor.stringFlavor;
61+
62+ } catch (ClassNotFoundException cle) {
63+ System.err.println("error initializing javax.swing.plaf.basic.BasicTranserable");
64+ }
65+ }
66+
67+ public BasicTransferable(String plainData, String htmlData) {
68+ this.plainData = plainData;
69+ this.htmlData = htmlData;
70+ }
71+
72+
73+ /**
74+ * Returns an array of DataFlavor objects indicating the flavors the data
75+ * can be provided in. The array should be ordered according to preference
76+ * for providing the data (from most richly descriptive to least descriptive).
77+ * @return an array of data flavors in which this data can be transferred
78+ */
79+ public DataFlavor[] getTransferDataFlavors() {
80+ DataFlavor[] richerFlavors = getRicherFlavors();
81+ int nRicher = (richerFlavors != null) ? richerFlavors.length : 0;
82+ int nHTML = (isHTMLSupported()) ? htmlFlavors.length : 0;
83+ int nPlain = (isPlainSupported()) ? plainFlavors.length: 0;
84+ int nString = (isPlainSupported()) ? stringFlavors.length : 0;
85+ int nFlavors = nRicher + nHTML + nPlain + nString;
86+ DataFlavor[] flavors = new DataFlavor[nFlavors];
87+
88+ // fill in the array
89+ int nDone = 0;
90+ if (nRicher > 0) {
91+ System.arraycopy(richerFlavors, 0, flavors, nDone, nRicher);
92+ nDone += nRicher;
93+ }
94+ if (nHTML > 0) {
95+ System.arraycopy(htmlFlavors, 0, flavors, nDone, nHTML);
96+ nDone += nHTML;
97+ }
98+ if (nPlain > 0) {
99+ System.arraycopy(plainFlavors, 0, flavors, nDone, nPlain);
100+ nDone += nPlain;
101+ }
102+ if (nString > 0) {
103+ System.arraycopy(stringFlavors, 0, flavors, nDone, nString);
104+ nDone += nString;
105+ }
106+ return flavors;
107+ }
108+
109+ /**
110+ * Returns whether or not the specified data flavor is supported for
111+ * this object.
112+ * @param flavor the requested flavor for the data
113+ * @return boolean indicating whether or not the data flavor is supported
114+ */
115+ public boolean isDataFlavorSupported(DataFlavor flavor) {
116+ DataFlavor[] flavors = getTransferDataFlavors();
117+ for (int i = 0; i < flavors.length; i++) {
118+ if (flavors[i].equals(flavor)) {
119+ return true;
120+ }
121+ }
122+ return false;
123+ }
124+
125+ /**
126+ * Returns an object which represents the data to be transferred. The class
127+ * of the object returned is defined by the representation class of the flavor.
128+ *
129+ * @param flavor the requested flavor for the data
130+ * @see DataFlavor#getRepresentationClass
131+ * @exception IOException if the data is no longer available
132+ * in the requested flavor.
133+ * @exception UnsupportedFlavorException if the requested data flavor is
134+ * not supported.
135+ */
136+ public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
137+ DataFlavor[] richerFlavors = getRicherFlavors();
138+ if (isRicherFlavor(flavor)) {
139+ return getRicherData(flavor);
140+ } else if (isHTMLFlavor(flavor)) {
141+ String data = getHTMLData();
142+ data = (data == null) ? "" : data;
143+ if (String.class.equals(flavor.getRepresentationClass())) {
144+ return data;
145+ } else if (Reader.class.equals(flavor.getRepresentationClass())) {
146+ return new StringReader(data);
147+ } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
148+ return new StringBufferInputStream(data);
149+ }
150+ // fall through to unsupported
151+ } else if (isPlainFlavor(flavor)) {
152+ String data = getPlainData();
153+ data = (data == null) ? "" : data;
154+ if (String.class.equals(flavor.getRepresentationClass())) {
155+ return data;
156+ } else if (Reader.class.equals(flavor.getRepresentationClass())) {
157+ return new StringReader(data);
158+ } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
159+ return new StringBufferInputStream(data);
160+ }
161+ // fall through to unsupported
162+
163+ } else if (isStringFlavor(flavor)) {
164+ String data = getPlainData();
165+ data = (data == null) ? "" : data;
166+ return data;
167+ }
168+ throw new UnsupportedFlavorException(flavor);
169+ }
170+
171+ // --- richer subclass flavors ----------------------------------------------
172+
173+ protected boolean isRicherFlavor(DataFlavor flavor) {
174+ DataFlavor[] richerFlavors = getRicherFlavors();
175+ int nFlavors = (richerFlavors != null) ? richerFlavors.length : 0;
176+ for (int i = 0; i < nFlavors; i++) {
177+ if (richerFlavors[i].equals(flavor)) {
178+ return true;
179+ }
180+ }
181+ return false;
182+ }
183+
184+ /**
185+ * Some subclasses will have flavors that are more descriptive than HTML
186+ * or plain text. If this method returns a non-null value, it will be
187+ * placed at the start of the array of supported flavors.
188+ */
189+ protected DataFlavor[] getRicherFlavors() {
190+ return null;
191+ }
192+
193+ protected Object getRicherData(DataFlavor flavor) throws UnsupportedFlavorException {
194+ return null;
195+ }
196+
197+ // --- html flavors ----------------------------------------------------------
198+
199+ /**
200+ * Returns whether or not the specified data flavor is an HTML flavor that
201+ * is supported.
202+ * @param flavor the requested flavor for the data
203+ * @return boolean indicating whether or not the data flavor is supported
204+ */
205+ protected boolean isHTMLFlavor(DataFlavor flavor) {
206+ DataFlavor[] flavors = htmlFlavors;
207+ for (int i = 0; i < flavors.length; i++) {
208+ if (flavors[i].equals(flavor)) {
209+ return true;
210+ }
211+ }
212+ return false;
213+ }
214+
215+ /**
216+ * Should the HTML flavors be offered? If so, the method
217+ * getHTMLData should be implemented to provide something reasonable.
218+ */
219+ protected boolean isHTMLSupported() {
220+ return htmlData != null;
221+ }
222+
223+ /**
224+ * Fetch the data in a text/html format
225+ */
226+ protected String getHTMLData() {
227+ return htmlData;
228+ }
229+
230+ // --- plain text flavors ----------------------------------------------------
231+
232+ /**
233+ * Returns whether or not the specified data flavor is an plain flavor that
234+ * is supported.
235+ * @param flavor the requested flavor for the data
236+ * @return boolean indicating whether or not the data flavor is supported
237+ */
238+ protected boolean isPlainFlavor(DataFlavor flavor) {
239+ DataFlavor[] flavors = plainFlavors;
240+ for (int i = 0; i < flavors.length; i++) {
241+ if (flavors[i].equals(flavor)) {
242+ return true;
243+ }
244+ }
245+ return false;
246+ }
247+
248+ /**
249+ * Should the plain text flavors be offered? If so, the method
250+ * getPlainData should be implemented to provide something reasonable.
251+ */
252+ protected boolean isPlainSupported() {
253+ return plainData != null;
254+ }
255+
256+ /**
257+ * Fetch the data in a text/plain format.
258+ */
259+ protected String getPlainData() {
260+ return plainData;
261+ }
262+
263+ // --- string flavorss --------------------------------------------------------
264+
265+ /**
266+ * Returns whether or not the specified data flavor is a String flavor that
267+ * is supported.
268+ * @param flavor the requested flavor for the data
269+ * @return boolean indicating whether or not the data flavor is supported
270+ */
271+ protected boolean isStringFlavor(DataFlavor flavor) {
272+ DataFlavor[] flavors = stringFlavors;
273+ for (int i = 0; i < flavors.length; i++) {
274+ if (flavors[i].equals(flavor)) {
275+ return true;
276+ }
277+ }
278+ return false;
279+ }
280+
281+
282+}