1 19 20 21 package org.openide.text; 22 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.Position ; 28 import javax.swing.text.StyledDocument ; 29 30 import junit.textui.TestRunner; 31 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.junit.NbTestSuite; 34 import org.openide.actions.*; 35 import org.openide.cookies.CloseCookie; 36 import org.openide.cookies.EditCookie; 37 38 import org.openide.cookies.EditorCookie; 39 import org.openide.cookies.OpenCookie; 40 import org.openide.cookies.PrintCookie; 41 import org.openide.cookies.SaveCookie; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileStateInvalidException; 45 import org.openide.filesystems.Repository; 46 import org.openide.loaders.DataNode; 47 import org.openide.loaders.DataObject; 48 import org.openide.loaders.DataObjectExistsException; 49 import org.openide.loaders.ExtensionList; 50 import org.openide.loaders.MultiDataObject; 51 import org.openide.loaders.MultiFileLoader; 52 import org.openide.loaders.UniFileLoader; 53 import org.openide.nodes.Children; 54 import org.openide.nodes.CookieSet; 55 import org.openide.nodes.Node; 56 import org.openide.text.CloneableEditorSupport; 57 import org.openide.util.HelpCtx; 58 import org.openide.util.NbBundle; 59 import org.openide.util.actions.SystemAction; 60 import org.openide.windows.CloneableOpenSupport; 61 62 63 77 public class SaveDocumentTest extends NbTestCase { 78 79 80 public SaveDocumentTest(String s) { 81 super(s); 82 } 83 84 protected void setUp() throws Exception { 85 TXTDataLoader loader = (TXTDataLoader)TXTDataLoader.getLoader (TXTDataLoader.class); 86 org.openide.loaders.AddLoaderManuallyHid.addRemoveLoader (loader, true); 87 } 88 89 protected void tearDown() throws Exception { 90 TXTDataLoader loader = (TXTDataLoader)TXTDataLoader.getLoader (TXTDataLoader.class); 91 org.openide.loaders.AddLoaderManuallyHid.addRemoveLoader (loader, false); 92 } 93 94 95 96 public void testSaveDocument() throws Exception { 97 System.err.println("Test Save Document"); 98 99 FileObject fo = Repository.getDefault().getDefaultFileSystem() 100 .getRoot().createData("test", "txt"); 101 102 DataObject data = DataObject.find(fo); 103 104 EditorCookie ec = (EditorCookie)data.getCookie(EditorCookie.class); 105 106 if(!(ec instanceof CloneableEditorSupport)) { 107 throw new IllegalStateException ("Bad editor cookie type"); 108 } 109 110 CloneableEditorSupport ces = (CloneableEditorSupport)ec; 111 System.err.println("CloneableEditorSupport="+ces); 112 113 if(ces.isModified()) { 114 throw new IllegalStateException ("Cloneable editor support should be marked as unmodified!"); 115 } 116 System.err.println("Saving unmodified document"); 117 ces.saveDocument(); 118 119 final StyledDocument doc = ces.openDocument(); 120 121 NbDocument.runAtomicAsUser(doc, new Runnable () { 122 public void run() { 123 try { 124 doc.insertString(0, "Inserted string", null); 125 } catch(BadLocationException ble) { 126 ble.printStackTrace(); 127 } 128 } 129 }); 130 131 System.err.println("doc="+ces.getDocument()); 132 133 System.err.println("ec isModified="+ces.isModified()); 134 if(!ec.isModified()) { 135 throw new IllegalStateException ("CloneableEditorSupport should be marked as modified already!"); 136 } 137 System.err.println("Saving modified document"); 138 139 ces.saveDocument(); 140 } 141 142 143 147 148 public static final class TXTDataLoader extends UniFileLoader { 149 150 151 static final long serialVersionUID =-3658061894653334886L; 152 153 154 static final String ATTR_IS_TEXT_FILE = "org.netbeans.modules.text.IsTextFile"; 156 157 158 public TXTDataLoader() { 159 super("org.netbeans.modules.text.TXTDataObject"); } 161 162 163 protected void initialize () { 164 super.initialize(); 165 166 ExtensionList ext = new ExtensionList(); 167 ext.addExtension("txt"); ext.addExtension("doc"); ext.addExtension("me"); ext.addExtension("policy"); ext.addExtension("mf"); ext.addExtension("MF"); ext.addExtension("log"); setExtensions(ext); 175 } 176 177 178 protected String defaultDisplayName() { 179 return NbBundle.getBundle(TXTDataLoader.class).getString("PROP_TXTLoader_Name"); 180 } 181 182 183 protected SystemAction[] defaultActions() { 184 return new SystemAction[] { 185 SystemAction.get(OpenAction.class), 186 SystemAction.get (FileSystemAction.class), 187 null, 188 SystemAction.get(CutAction.class), 189 SystemAction.get(CopyAction.class), 190 SystemAction.get(PasteAction.class), 191 null, 192 SystemAction.get(DeleteAction.class), 193 SystemAction.get(RenameAction.class), 194 null, 195 SystemAction.get(SaveAsTemplateAction.class), 196 null, 197 SystemAction.get(ToolsAction.class), 198 SystemAction.get(PropertiesAction.class), 199 }; 200 } 201 202 204 protected FileObject findPrimaryFile (FileObject fo) { 205 boolean isSysFile; 206 try { 207 isSysFile = fo.getFileSystem () == Repository.getDefault ().getDefaultFileSystem (); 208 } catch (FileStateInvalidException fsie) { 209 isSysFile = false; 211 } 212 if (! isSysFile && Boolean.TRUE.equals (fo.getAttribute (ATTR_IS_TEXT_FILE))) 213 return fo; 214 return super.findPrimaryFile (fo); 215 } 216 217 221 protected MultiDataObject createMultiObject(final FileObject fo) 222 throws IOException { 223 return new TXTDataObject(fo, this); 224 } 225 226 } 228 229 public static final class TXTDataObject extends MultiDataObject implements CookieSet.Factory { 230 231 232 static final long serialVersionUID = 4795737295255253334L; 233 234 235 private transient TXTEditorSupport editorSupport; 236 237 238 239 public TXTDataObject(final FileObject obj, final MultiFileLoader loader) throws DataObjectExistsException { 240 super(obj, loader); 241 242 getCookieSet().add(TXTEditorSupport.class, this); 243 } 244 245 246 247 public Node.Cookie createCookie(Class clazz) { 248 if(clazz.isAssignableFrom(TXTEditorSupport.class)) 249 return getEditorSupport(); 250 else 251 return null; 252 } 253 254 CookieSet getCookieSet0() { 256 return getCookieSet(); 257 } 258 259 260 private TXTEditorSupport getEditorSupport() { 261 if(editorSupport == null) { 262 synchronized(this) { 263 if(editorSupport == null) 264 editorSupport = new TXTEditorSupport(this); 265 } 266 } 267 268 return editorSupport; 269 } 270 271 280 protected Node createNodeDelegate () { 281 return new TXTNode(this); 282 } 283 284 287 public HelpCtx getHelpCtx () { 288 return new HelpCtx (TXTDataObject.class); 289 } 290 291 292 296 public static final class TXTNode extends DataNode { 297 298 private static final String TXT_ICON_BASE = "org/netbeans/modules/text/txtObject"; 300 301 public TXTNode (final DataObject dataObject) { 302 super(dataObject, Children.LEAF); 303 setIconBase(TXT_ICON_BASE); 304 } 305 306 307 public SystemAction getDefaultAction () { 308 SystemAction result = super.getDefaultAction(); 309 return result == null ? SystemAction.get(OpenAction.class) : result; 310 } 311 } 313 } 315 316 public static final class TXTEditorSupport extends DataEditorSupport 317 implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie { 318 319 321 private final SaveCookie saveCookie = new SaveCookie() { 322 323 public void save() throws IOException { 324 TXTEditorSupport.this.saveDocument(); 325 TXTEditorSupport.this.getDataObject().setModified(false); 326 } 327 }; 328 329 330 331 TXTEditorSupport(TXTDataObject obj) { 332 super(obj, new Environment(obj)); 333 334 setMIMEType("text/plain"); } 336 337 342 protected boolean notifyModified () { 343 if (!super.notifyModified()) 344 return false; 345 346 addSaveCookie(); 347 348 return true; 349 } 350 351 352 protected void notifyUnmodified () { 353 super.notifyUnmodified(); 354 355 removeSaveCookie(); 356 } 357 358 359 private void addSaveCookie() { 360 TXTDataObject obj = (TXTDataObject)getDataObject(); 361 362 if(obj.getCookie(SaveCookie.class) == null) { 364 obj.getCookieSet0().add(saveCookie); 365 obj.setModified(true); 366 } 367 } 368 369 370 private void removeSaveCookie() { 371 TXTDataObject obj = (TXTDataObject)getDataObject(); 372 373 Node.Cookie cookie = obj.getCookie(SaveCookie.class); 375 376 if(cookie != null && cookie.equals(saveCookie)) { 377 obj.getCookieSet0().remove(saveCookie); 378 obj.setModified(false); 379 } 380 } 381 382 383 386 387 private static class Environment extends DataEditorSupport.Env 388 { 389 private static final long serialVersionUID = 3499855082262173256L; 390 391 392 public Environment(TXTDataObject obj) { 393 super(obj); 394 } 395 396 397 398 protected FileObject getFile() { 399 return getDataObject().getPrimaryFile(); 400 } 401 402 403 protected FileLock takeLock() throws IOException { 404 return ((TXTDataObject)getDataObject()).getPrimaryEntry().takeLock(); 405 } 406 407 411 public CloneableOpenSupport findCloneableOpenSupport() { 412 return (TXTEditorSupport)getDataObject().getCookie(TXTEditorSupport.class); 413 } 414 } 416 } 418 } 419 | Popular Tags |