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: package java.awt.dnd;
019:
020: import java.awt.Component;
021: import java.awt.datatransfer.DataFlavor;
022: import java.awt.datatransfer.Transferable;
023: import java.awt.datatransfer.UnsupportedFlavorException;
024: import java.awt.dnd.peer.DropTargetContextPeer;
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030: import java.io.Serializable;
031: import java.util.ArrayList;
032: import java.util.Arrays;
033: import java.util.List;
034:
035: import org.apache.harmony.awt.internal.nls.Messages;
036:
037: public class DropTargetContext implements Serializable {
038:
039: private static final long serialVersionUID = -634158968993743371L;
040:
041: protected class TransferableProxy implements Transferable {
042:
043: protected boolean isLocal;
044:
045: protected Transferable transferable;
046:
047: TransferableProxy(boolean isLocal, Transferable transferable) {
048: this .isLocal = isLocal;
049: this .transferable = transferable;
050: }
051:
052: public Object getTransferData(DataFlavor flavor)
053: throws UnsupportedFlavorException, IOException {
054:
055: if (isLocal && flavor.isMimeTypeSerializedObject()) {
056: Object data = transferable.getTransferData(flavor);
057: if (data instanceof Serializable) {
058: return getSerializedCopy((Serializable) data);
059: }
060: }
061:
062: return transferable.getTransferData(flavor);
063: }
064:
065: private Object getSerializedCopy(Serializable data) {
066: try {
067: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
068: ObjectOutputStream objOut = new ObjectOutputStream(
069: byteOut);
070: objOut.writeObject(data);
071: ByteArrayInputStream byteIn = new ByteArrayInputStream(
072: byteOut.toByteArray());
073: ObjectInputStream objIn = new ObjectInputStream(byteIn);
074: return objIn.readObject();
075: } catch (Exception e) {
076: return data;
077: }
078: }
079:
080: public boolean isDataFlavorSupported(DataFlavor flavor) {
081: return transferable.isDataFlavorSupported(flavor);
082: }
083:
084: public DataFlavor[] getTransferDataFlavors() {
085: return transferable.getTransferDataFlavors();
086: }
087: }
088:
089: final DropTarget target;
090:
091: DropTargetContextPeer peer;
092:
093: DropTargetContext(DropTarget target) {
094: this .target = target;
095: }
096:
097: protected Transferable createTransferableProxy(Transferable t,
098: boolean local) {
099: return new TransferableProxy(local, t);
100: }
101:
102: protected List<DataFlavor> getCurrentDataFlavorsAsList() {
103: if (peer != null) {
104: return Arrays.asList(peer.getTransferDataFlavors());
105: }
106: return new ArrayList<DataFlavor>();
107: }
108:
109: public void addNotify(DropTargetContextPeer peer) {
110: this .peer = peer;
111: }
112:
113: public DropTarget getDropTarget() {
114: return target;
115: }
116:
117: protected Transferable getTransferable()
118: throws InvalidDnDOperationException {
119: if (peer == null) {
120: // awt.07=Transfer data is not available
121: throw new InvalidDnDOperationException(Messages
122: .getString("awt.07")); //$NON-NLS-1$
123: }
124: return new TransferableProxy(peer.isTransferableJVMLocal(),
125: peer.getTransferable());
126: }
127:
128: protected boolean isDataFlavorSupported(DataFlavor flavor) {
129: if (peer != null) {
130: DataFlavor[] df = peer.getTransferDataFlavors();
131: for (DataFlavor element : df) {
132: if (element.equals(flavor)) {
133: return true;
134: }
135: }
136: }
137: return false;
138: }
139:
140: protected DataFlavor[] getCurrentDataFlavors() {
141: if (peer != null) {
142: return peer.getTransferDataFlavors();
143: }
144: return new DataFlavor[0];
145: }
146:
147: public Component getComponent() {
148: return target.getComponent();
149: }
150:
151: public void dropComplete(boolean success)
152: throws InvalidDnDOperationException {
153: if (peer != null) {
154: peer.dropComplete(success);
155: }
156: }
157:
158: protected void setTargetActions(int actions) {
159: if (peer != null) {
160: peer.setTargetActions(actions);
161: }
162: target.setDefaultActions(actions);
163: }
164:
165: protected void acceptDrop(int dragOperation) {
166: if (peer != null) {
167: peer.acceptDrop(dragOperation);
168: }
169: }
170:
171: protected void acceptDrag(int dragOperation) {
172: if (peer != null) {
173: peer.acceptDrag(dragOperation);
174: }
175: }
176:
177: public void removeNotify() {
178: peer = null;
179: }
180:
181: protected void rejectDrop() {
182: if (peer != null) {
183: peer.rejectDrop();
184: }
185: }
186:
187: protected void rejectDrag() {
188: if (peer != null) {
189: peer.rejectDrag();
190: }
191: }
192:
193: protected int getTargetActions() {
194: if (peer != null) {
195: return peer.getTargetActions();
196: }
197: return target.getDefaultActions();
198: }
199: }
|