1 19 24 25 package org.openide.loaders; 26 27 28 import java.awt.datatransfer.Transferable ; 29 import java.io.*; 30 import java.util.Arrays ; 31 import java.util.logging.*; 32 import org.openide.*; 33 import org.openide.cookies.InstanceCookie; 34 import org.openide.filesystems.*; 35 import org.openide.util.*; 36 import org.openide.util.datatransfer.*; 37 38 41 abstract class DataTransferSupport { 42 43 47 protected abstract PasteTypeExt [] definePasteTypes (int op); 48 51 protected abstract int [] defineOperations (); 52 56 protected void handleCreatePasteTypes (Transferable t, java.util.List <PasteType> s) { 57 } 58 63 public final void createPasteTypes (Transferable t, java.util.List <PasteType> s) { 64 65 int [] ops = defineOperations (); 66 67 for (int i = 0; i < ops.length; i++) { 68 DataObject objs [] = LoaderTransfer.getDataObjects (t, ops[i]); 69 PasteTypeExt pts []; 70 71 if (objs == null || objs.length == 0) 72 continue; 73 74 pts = definePasteTypes (ops[i]); 75 76 for (int j = 0; j < pts.length; j++) { 77 pts[j].setDataObjects (objs); 78 if (pts[j].canPaste ()) 79 s.add (pts[j]); 80 } 81 } 82 83 handleCreatePasteTypes (t, s); 84 } 85 86 private static final Logger err = Logger.getLogger("org.openide.loaders.DataTransferSupport"); 88 90 static abstract class PasteTypeExt extends PasteType { 91 92 private DataObject objs []; 93 94 public PasteTypeExt () { 95 } 96 100 protected abstract boolean handleCanPaste (DataObject obj); 101 104 protected abstract void handlePaste (DataObject obj) throws IOException; 105 109 protected boolean cleanClipboard () { 110 return false; 111 } 112 113 public final boolean canPaste () { 114 for (int i = 0; i < objs.length; i++) { 115 if (!handleCanPaste (objs[i])) 116 return false; 117 } 118 return true; 119 } 120 121 public final Transferable paste() throws IOException { 122 if (javax.swing.SwingUtilities.isEventDispatchThread()) { 123 org.openide.util.RequestProcessor.getDefault().post(new java.lang.Runnable () { 124 125 public void run() { 126 java.lang.String n = org.openide.awt.Actions.cutAmpersand(getName()); 127 org.netbeans.api.progress.ProgressHandle h = org.netbeans.api.progress.ProgressHandleFactory.createHandle(n); 128 129 h.start(); 130 h.switchToIndeterminate(); 131 try { 132 doPaste(); 133 } 134 catch (java.io.IOException ioe) { 135 Exceptions.printStackTrace(ioe); 136 } 137 finally { 138 h.finish(); 139 } 140 } 141 }); 142 } else { 143 doPaste(); 144 } 145 return cleanClipboard() ? ExTransferable.EMPTY : null; 147 } 148 149 private void doPaste () throws IOException { 150 if (err.isLoggable (Level.FINE)) { 151 err.log(Level.FINE, null, new Throwable ("Issue #58666: Called " + this + " doPaste() on objects " + Arrays.asList (objs))); } 153 for (int i = 0; i < objs.length; i++) 154 handlePaste (objs[i]); 155 } 156 157 public final void setDataObjects (DataObject objs []) { 158 this.objs = objs; 159 } 160 } 161 162 164 static class SerializePaste extends PasteType { 165 private InstanceCookie cookie; 166 private DataFolder target; 167 168 171 public SerializePaste (DataFolder target, InstanceCookie cookie) { 172 this.cookie = cookie; 173 this.target = target; 174 } 175 176 179 public String getName () { 180 return DataObject.getString ("PT_serialize"); 181 } 182 183 public HelpCtx getHelpCtx () { 184 return new HelpCtx (SerializePaste.class); 185 } 186 187 189 public final Transferable paste () throws IOException { 190 final DataFolder trg = getTargetFolder(); 191 String name = cookie.instanceName (); 192 int i = name.lastIndexOf ('.') + 1; 193 if (i != 0 && i != name.length ()) { 194 name = name.substring (i); 195 } 196 197 name = FileUtil.findFreeFileName (trg.getPrimaryFile (), name, "ser"); 199 200 final NotifyDescriptor.InputLine nd = new NotifyDescriptor.InputLine ( 201 DataObject.getString ("SerializeBean_Text"), 202 DataObject.getString ("SerializeBean_Title") 203 ); 204 nd.setInputText (name); 205 206 if (NotifyDescriptor.OK_OPTION == DialogDisplayer.getDefault ().notify (nd)) { 207 DataObjectPool.getPOOL().runAtomicAction (trg.getPrimaryFile (), new FileSystem.AtomicAction () { 208 public void run () throws IOException { 209 FileObject fo = trg.getPrimaryFile ().createData (nd.getInputText (), "ser"); FileLock lock = fo.lock (); 211 ObjectOutputStream oos = null; 212 try { 213 oos = new ObjectOutputStream ( 214 new java.io.BufferedOutputStream (fo.getOutputStream (lock)) 215 ); 216 oos.writeObject (cookie.instanceCreate ()); 217 } catch (ClassNotFoundException e) { 218 throw new IOException (e.getMessage ()); 219 } finally { 220 if (oos != null) oos.close (); 221 lock.releaseLock (); 222 } 223 } 224 }); 225 } 226 227 return null; 229 } 230 231 protected DataFolder getTargetFolder() throws IOException { 232 return target; 233 } 234 } 235 236 238 static class InstantiatePaste extends PasteType { 239 private InstanceCookie cookie; 240 private DataFolder target; 241 242 245 public InstantiatePaste (DataFolder target, InstanceCookie cookie) { 246 this.cookie = cookie; 247 this.target = target; 248 } 249 250 253 public String getName () { 254 return DataObject.getString ("PT_instance"); 255 } 256 257 public HelpCtx getHelpCtx () { 258 return new HelpCtx (InstantiatePaste.class); 259 } 260 261 263 public final Transferable paste () throws IOException { 264 try { 265 Class clazz = cookie.instanceClass (); 266 267 InstanceDataObject.create(getTargetFolder(), null, clazz); 269 } catch (ClassNotFoundException ex) { 270 throw new IOException (ex.getMessage ()); 271 } 272 273 return null; 275 } 276 277 protected DataFolder getTargetFolder() throws IOException { 278 return target; 279 } 280 } 281 } 282 | Popular Tags |