1 19 package org.openide.util.datatransfer; 20 21 import java.awt.datatransfer.*; 22 23 import javax.swing.event.EventListenerList ; 24 25 26 28 33 public abstract class ExClipboard extends Clipboard { 34 35 private EventListenerList listeners = new EventListenerList (); 36 37 40 public ExClipboard(String name) { 41 super(name); 42 } 43 44 47 public final void addClipboardListener(ClipboardListener list) { 48 listeners.add(ClipboardListener.class, list); 49 } 50 51 54 public final void removeClipboardListener(ClipboardListener list) { 55 listeners.remove(ClipboardListener.class, list); 56 } 57 58 60 protected final void fireClipboardChange() { 61 Object [] l = listeners.getListenerList(); 62 ClipboardEvent ev = null; 63 64 for (int i = l.length - 2; i >= 0; i -= 2) { 65 ClipboardListener list = (ClipboardListener) l[i + 1]; 66 67 if (ev == null) { 68 ev = new ClipboardEvent(this); 69 } 70 71 list.clipboardChanged(ev); 72 } 73 } 74 75 79 protected abstract Convertor[] getConvertors(); 80 81 102 public Transferable convert(Transferable t) { 103 Convertor[] convertors = getConvertors(); 104 105 for (int i = 0; i < convertors.length; i++) { 106 if (t == null) { 107 return null; 108 } 109 110 t = convertors[i].convert(t); 111 } 112 113 return t; 114 } 115 116 123 public static void transferableAccepted(Transferable t, int action) { 124 if (t instanceof ExTransferable) { 125 ((ExTransferable) t).fireAccepted(action); 126 } else if (t.isDataFlavorSupported(ExTransferable.multiFlavor)) { 127 try { 128 MultiTransferObject mto = (MultiTransferObject) t.getTransferData(ExTransferable.multiFlavor); 129 int cnt = mto.getCount(); 130 131 for (int i = 0; i < cnt; i++) { 132 transferableAccepted(mto.getTransferableAt(i), action); 133 } 134 } catch (Exception e) { 135 } 137 } 138 } 139 140 146 public static void transferableRejected(Transferable t) { 147 if (t instanceof ExTransferable) { 148 ((ExTransferable) t).fireRejected(); 149 } else if (t.isDataFlavorSupported(ExTransferable.multiFlavor)) { 150 try { 151 MultiTransferObject mto = (MultiTransferObject) t.getTransferData(ExTransferable.multiFlavor); 152 int cnt = mto.getCount(); 153 154 for (int i = 0; i < cnt; i++) { 155 transferableRejected(mto.getTransferableAt(i)); 156 } 157 } catch (Exception e) { 158 } 160 } 161 } 162 163 public synchronized void setContents(Transferable contents, ClipboardOwner owner) { 164 if (this.contents != null) { 165 transferableOwnershipLost(this.contents); 166 } 167 168 super.setContents(contents, owner); 169 } 170 171 177 public static void transferableOwnershipLost(Transferable t) { 178 if (t instanceof ExTransferable) { 179 ((ExTransferable) t).fireOwnershipLost(); 180 } else if (t.isDataFlavorSupported(ExTransferable.multiFlavor)) { 181 try { 182 MultiTransferObject mto = (MultiTransferObject) t.getTransferData(ExTransferable.multiFlavor); 183 int cnt = mto.getCount(); 184 185 for (int i = 0; i < cnt; i++) { 186 transferableOwnershipLost(mto.getTransferableAt(i)); 187 } 188 } catch (Exception e) { 189 } 191 } 192 } 193 194 197 public interface Convertor { 198 258 public Transferable convert(Transferable t); 259 } 260 } 261 | Popular Tags |