001/* DropTargetContext.java -- 002 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038package java.awt.dnd; 039 040import java.awt.Component; 041import java.awt.datatransfer.DataFlavor; 042import java.awt.datatransfer.Transferable; 043import java.awt.datatransfer.UnsupportedFlavorException; 044import java.awt.dnd.peer.DropTargetContextPeer; 045import java.io.IOException; 046import java.io.Serializable; 047import java.util.Arrays; 048import java.util.List; 049 050/** 051 * @author Michael Koch (konqueror@gmx.de) 052 * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 053 * @since 1.2 054 */ 055public class DropTargetContext implements Serializable 056{ 057 static final long serialVersionUID = -634158968993743371L; 058 059 protected class TransferableProxy implements Transferable 060 { 061 protected boolean isLocal; 062 protected Transferable transferable; 063 064 TransferableProxy(Transferable t, boolean local) 065 { 066 this.transferable = t; 067 this.isLocal = local; 068 } 069 070 public DataFlavor[] getTransferDataFlavors() 071 { 072 return transferable.getTransferDataFlavors(); 073 } 074 075 public boolean isDataFlavorSupported(DataFlavor flavor) 076 { 077 return transferable.isDataFlavorSupported(flavor); 078 } 079 080 public Object getTransferData(DataFlavor flavor) 081 throws UnsupportedFlavorException, IOException 082 { 083 return transferable.getTransferData (flavor); 084 } 085 } 086 087 private DropTarget dropTarget; 088 private int targetActions; 089 private DropTargetContextPeer dtcp; 090 091 // package private 092 DropTargetContext(DropTarget dropTarget) 093 { 094 this.dropTarget = dropTarget; 095 } 096 097 public DropTarget getDropTarget() 098 { 099 return dropTarget; 100 } 101 102 public Component getComponent() 103 { 104 return dropTarget.getComponent(); 105 } 106 107 public void addNotify(DropTargetContextPeer dtcp) 108 { 109 this.dtcp = dtcp; 110 } 111 112 public void removeNotify() 113 { 114 this.dtcp = null; 115 } 116 117 protected void setTargetActions(int actions) 118 { 119 targetActions = actions; 120 } 121 122 protected int getTargetActions() 123 { 124 return targetActions; 125 } 126 127 /** 128 * Signals that the drop is completed. 129 * 130 * @exception InvalidDnDOperationException If a drop is not outstanding. 131 */ 132 public void dropComplete (boolean success) 133 { 134 if (dtcp != null) 135 dtcp.dropComplete(success); 136 } 137 138 protected void acceptDrag (int dragOperation) 139 { 140 if (dtcp != null) 141 dtcp.acceptDrag(dragOperation); 142 } 143 144 protected void rejectDrag () 145 { 146 if (dtcp != null) 147 dtcp.rejectDrag(); 148 } 149 150 protected void acceptDrop (int dropOperation) 151 { 152 if (dtcp != null) 153 dtcp.acceptDrop(dropOperation); 154 } 155 156 protected void rejectDrop () 157 { 158 if (dtcp != null) 159 dtcp.rejectDrop(); 160 } 161 162 protected DataFlavor[] getCurrentDataFlavors () 163 { 164 if (dtcp != null) 165 dtcp.getTransferDataFlavors(); 166 return null; 167 } 168 169 protected List<DataFlavor> getCurrentDataFlavorsAsList () 170 { 171 return Arrays.asList(getCurrentDataFlavors ()); 172 } 173 174 protected boolean isDataFlavorSupported (DataFlavor flavor) 175 { 176 return getCurrentDataFlavorsAsList().contains (flavor); 177 } 178 179 /** 180 * Return the <code>Transferable</code> operandof this operation. 181 * 182 * @exception InvalidDnDOperationException If a drag is not outstanding. 183 */ 184 protected Transferable getTransferable() 185 throws InvalidDnDOperationException 186 { 187 // FIXME: Implement this 188 if (dtcp != null) 189 return dtcp.getTransferable(); 190 return null; 191 } 192 193 protected Transferable createTransferableProxy(Transferable t, boolean local) 194 { 195 return new TransferableProxy(t, local); 196 } 197} // class DropTargetContext