package mil.navy.nps.logger; import java.awt.datatransfer.*; import mil.navy.nps.dis.*; import java.io.*; import java.util.*; /** * The clipboard needs some helper classes to transfer data * of an arbitrary type, and this is one such class. It * handles the selection and transfer of PDU objects. * when the user selects a range of PDUs and selects copy * or cut, an instance of this class is created. It manages * queries about what type of data it can provide to people * who do "paste", and holds data.

* * See http://www.vjinformant.com/features/1999/08/ji199908ps_f.asp * for details.

* * @author DMcG */ public class PduSelection extends Object implements Transferable, ClipboardOwner { public static DataFlavor[] flavors = {new DataFlavor(PduSelection.class, "PDUSelection")}; /** * Maintains a an array of byte arrays; each row of the array contains a DIS-format * array of bytes, exactly as we would have receieved from a Datagram packet */ private byte pduData[][]; /** * this constructor lists the PDUs we're going to be transfering. Note that * in general we have a list of PDUs; we transfer individual pdus by having a * list of one. this is a little bad in that when we create the selection we * go to the trouble of serializing the entire selection, which more or less * negates the advantages of lazy data copy. c'est la vie. */ public PduSelection(ProtocolDataUnit pdu[]) { pduData = new byte[pdu.length][1]; // The size of the row is irrelevant--we replace the array anyway for(int idx = 0; idx < pdu.length; idx++) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); pdu[idx].serialize(dos); pduData[idx] = bos.toByteArray(); } } /** * Constructor, takes vector of PDUs and adds them to our selection. */ public PduSelection(Vector pPdus) { pduData = new byte[pPdus.size()][1]; // The size of the row is irrelevant--we replace the array anyway for(int idx = 0; idx < pPdus.size(); idx++) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); ProtocolDataUnit aPdu = (ProtocolDataUnit)pPdus.elementAt(idx); aPdu.serialize(dos); pduData[idx] = bos.toByteArray(); } } /** * getTransferData returns an object that represents the * data placed on the clipboard. It takes as an argument * the data flavor (data format) it wants. */ public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if(!(flavor.equals(flavors[0]))) { System.out.println("cannot support flavor in gettranserData"); throw new UnsupportedFlavorException(flavor); } // Returns an array of pdu objects ProtocolDataUnit pdus[] = new ProtocolDataUnit[pduData.length]; for(int idx = 0; idx < pduData.length; idx++) { ProtocolDataUnit aPdu = ProtocolDataUnit.byteArrayToPdu(pduData[idx]); pdus[idx] = aPdu; } return pdus; } /** * getTransferDataFlavors returns a list of the data flavors * (aka data formats) that it can provide data in. This should * be ordered in preference, from most "rich" to least "rich", * for example you'd prefer RTF to plain text. In this case * we provide only one data flavor, PDUs, which is our own * extra-special data format. */ public DataFlavor[] getTransferDataFlavors() { return flavors; // Should return a clone so they can't modify us } /** * Returns true if the DataFlavor passed in is one of the ones we * support. As it turns out we have only one dataflavor to support. */ public boolean isDataFlavorSupported(DataFlavor flavor) { return (flavor.equals(flavors[0])); } /** * In general data transfer objects can implement "lazy transfer". The data * isn't actually moved anywhere until the user actually tries to paste. * So we need to keep the data around for a while. But what if the user copies * something else in the meantime? In that case we lose the requirement to * keep the data around, and we can ditch it. That's what this hook is for. * This method is part of the ClipboardOwner interface; we own the data, and * are notified if the data is no longer needed. */ public void lostOwnership(Clipboard clipboard, Transferable contents) { return; } }