001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package org.apache.harmony.awt.datatransfer;
021:
022: import java.awt.datatransfer.DataFlavor;
023: import java.awt.datatransfer.SystemFlavorMap;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: /**
029: * Immutable copy of transferable data,
030: * safe for use across the threads
031: */
032: public class DataSnapshot implements DataProvider {
033:
034: private final String text;
035: private final String[] fileList;
036: private final String url;
037: private final String html;
038: private final RawBitmap rawBitmap;
039:
040: private final String[] nativeFormats;
041: /** Class -> byte[] */
042: private final Map<Class<?>, byte[]> serializedObjects;
043:
044: /**
045: * @param dataObject
046: */
047: public DataSnapshot(DataProvider data) {
048: nativeFormats = data.getNativeFormats();
049: text = data.getText();
050: fileList = data.getFileList();
051: url = data.getURL();
052: html = data.getHTML();
053: rawBitmap = data.getRawBitmap();
054:
055: serializedObjects = Collections
056: .synchronizedMap(new HashMap<Class<?>, byte[]>());
057:
058: for (int i = 0; i < nativeFormats.length; i++) {
059: DataFlavor df = null;
060: try {
061: df = SystemFlavorMap.decodeDataFlavor(nativeFormats[i]);
062: } catch (ClassNotFoundException e) {
063: }
064: if (df != null) {
065: Class<?> clazz = df.getRepresentationClass();
066: byte[] bytes = data.getSerializedObject(clazz);
067: if (bytes != null) {
068: serializedObjects.put(clazz, bytes);
069: }
070: }
071: }
072: // TODO: refine the list of native formats
073: }
074:
075: public boolean isNativeFormatAvailable(String nativeFormat) {
076: if (nativeFormat == null) {
077: return false;
078: }
079: if (nativeFormat.equals(FORMAT_TEXT)) {
080: return (text != null);
081: }
082: if (nativeFormat.equals(FORMAT_FILE_LIST)) {
083: return (fileList != null);
084: }
085: if (nativeFormat.equals(FORMAT_URL)) {
086: return (url != null);
087: }
088: if (nativeFormat.equals(FORMAT_HTML)) {
089: return (html != null);
090: }
091: if (nativeFormat.equals(FORMAT_IMAGE)) {
092: return (rawBitmap != null);
093: }
094: try {
095: DataFlavor df = SystemFlavorMap
096: .decodeDataFlavor(nativeFormat);
097: return serializedObjects.containsKey(df
098: .getRepresentationClass());
099: } catch (Exception e) {
100: return false;
101: }
102: }
103:
104: public String getText() {
105: return text;
106: }
107:
108: public String[] getFileList() {
109: return fileList;
110: }
111:
112: public String getURL() {
113: return url;
114: }
115:
116: public String getHTML() {
117: return html;
118: }
119:
120: public RawBitmap getRawBitmap() {
121: return rawBitmap;
122: }
123:
124: public int[] getRawBitmapHeader() {
125: return (rawBitmap != null) ? rawBitmap.getHeader() : null;
126: }
127:
128: public byte[] getRawBitmapBuffer8() {
129: return (rawBitmap != null)
130: && (rawBitmap.buffer instanceof byte[]) ? (byte[]) rawBitmap.buffer
131: : null;
132: }
133:
134: public short[] getRawBitmapBuffer16() {
135: return (rawBitmap != null)
136: && (rawBitmap.buffer instanceof short[]) ? (short[]) rawBitmap.buffer
137: : null;
138: }
139:
140: public int[] getRawBitmapBuffer32() {
141: return (rawBitmap != null)
142: && (rawBitmap.buffer instanceof int[]) ? (int[]) rawBitmap.buffer
143: : null;
144: }
145:
146: public byte[] getSerializedObject(Class<?> clazz) {
147: return serializedObjects.get(clazz);
148: }
149:
150: public byte[] getSerializedObject(String nativeFormat) {
151: try {
152: DataFlavor df = SystemFlavorMap
153: .decodeDataFlavor(nativeFormat);
154: return getSerializedObject(df.getRepresentationClass());
155: } catch (Exception e) {
156: return null;
157: }
158: }
159:
160: public String[] getNativeFormats() {
161: return nativeFormats;
162: }
163: }
|