Java Code Examples for org.openide.cookies.EditorCookie#close()

The following examples show how to use org.openide.cookies.EditorCookie#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartialReparseTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void doVerifyFullReparse(String code, String inject) throws Exception {
    FileObject srcDir = FileUtil.createMemoryFileSystem().getRoot();
    FileObject src = srcDir.createData("Test.java");
    try (Writer out = new OutputStreamWriter(src.getOutputStream())) {
        out.write(code.replaceFirst("^", "").replaceFirst("^", ""));
    }
    EditorCookie ec = src.getLookup().lookup(EditorCookie.class);
    Document doc = ec.openDocument();
    JavaSource source = JavaSource.forFileObject(src);
    Object[] topLevel = new Object[1];
    source.runUserActionTask(cc -> {
        cc.toPhase(Phase.RESOLVED);
         topLevel[0] = cc.getCompilationUnit();
    }, true);
    int startReplace = code.indexOf('^');
    int endReplace = code.indexOf('^', startReplace + 1) - 1;
    doc.remove(startReplace, endReplace - startReplace);
    doc.insertString(startReplace, inject, null);
    source.runUserActionTask(cc -> {
        cc.toPhase(Phase.RESOLVED);
        assertNotSame(topLevel[0], cc.getCompilationUnit());
    }, true);
    ec.saveDocument();
    ec.close();
}
 
Example 2
Source File: JShellEnvironment.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void postCloseCleanup() {
    try {
        // try to close the dataobject
        DataObject d = DataObject.find(getConsoleFile());
        EditorCookie cake = d.getLookup().lookup(EditorCookie.class);
        cake.close();
        // discard the dataobject
        synchronized (this) {
            if (document == null) {
                return;
            }
            document = null;
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    if (controlsIO) {
        inputOutput.closeInputOutput();
    }
    ShellRegistry.get().closed(this);
}
 
Example 3
Source File: DataEditorSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Tests that charset of saving document is not removed from cache by
 * concurrent openDocument() call (see #160784). */
public void testSaveOpenConcurrent() throws Exception {
    obj = DataObject.find(fileObject);
    DES sup = support();
    assertFalse("It is closed now", sup.isDocumentLoaded());
    assertNotNull("DataObject found", obj);

    Document doc = sup.openDocument();
    assertTrue("It is open now", support().isDocumentLoaded());
    doc.insertString(0, "Ahoj", null);
    EditorCookie s = (EditorCookie) sup;
    assertNotNull("Modified, so it has cookie", s);
    assertEquals(sup, s);

    Logger.getLogger(DataEditorSupport.class.getName()).setLevel(Level.FINEST);
    Logger.getLogger(DataEditorSupport.class.getName()).addHandler(new OpeningHandler(sup));
    s.saveDocument();
    
    assertLockFree(obj.getPrimaryFile());
    s.close();
    CloseCookie c = (CloseCookie) sup;
    assertNotNull("Has close", c);
    assertTrue("Close ok", c.close());
    assertLockFree(obj.getPrimaryFile());
}
 
Example 4
Source File: PerfWatchProjects.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void closeTopComponents() {
    for (TopComponent tc : new ArrayList<TopComponent>(TopComponent.getRegistry().getOpened())) {
        final EditorCookie ec = tc.getLookup().lookup(EditorCookie.class);
        if (ec != null) {
            ec.close();
        }
    }
    System.out.println("closed all ... hopefully");
}
 
Example 5
Source File: JShellEnvironment.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean closeEditor() {
    EditorCookie cake = getConsoleFile().getLookup().lookup(EditorCookie.class);
    if (cake == null) {
        return true;
    }
    return cake.close();
}
 
Example 6
Source File: AnnotationProviderTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAnnotationProviderIsCalledCorrectly() throws Exception {              
    Object o = Lookup.getDefault().lookup(AnnotationProvider.class);
    if(o == null) {
        fail("No  annotation provider found");
    }
    
    FileObject fo = fs.getRoot().createData("test", "txt");

    DataObject data = DataObject.find(fo);
    
    EditorCookie ec = data.getCookie(EditorCookie.class);
    
    ConsistencyCheckProvider.setCalled(0);
    ec.open();
    
    CloneableEditorSupport ces = (CloneableEditorSupport)ec;
    
    assertEquals("Provider called exactly once", 1,ConsistencyCheckProvider.getCalled());
    assertEquals("Consistent lookup content", data.getPrimaryFile(),ConsistencyCheckProvider.getInLkp());

    Line l1 = ces.getLineSet().getCurrent(0);
    assertEquals("Exactly one annotation attached", 1, l1.getAnnotationCount());
    
    ec.close();
    // XXX
    Line l2 = ces.getLineSet().getCurrent(0);
    assertEquals ("Lines are the same", l1, l2);
    assertEquals("Exactly one annotation attached after close", 1, l2.getAnnotationCount());

    ConsistencyCheckProvider.setCalled(0);
    ec.open();
    // XXX
    assertEquals("Provider not called during reopen", 0,ConsistencyCheckProvider.getCalled());
    assertEquals("Exactly one annotation attached after reopen", 1, ces.getLineSet().getCurrent(0).getAnnotationCount());
}
 
Example 7
Source File: CatalogFileWrapperDOMImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void finalize() throws Throwable {
    try {
        DataObject dobj = DataObject.find(backendCatalogFileObj);
        EditorCookie thisDocumentEditorCookie = (EditorCookie)dobj.getCookie(EditorCookie.class);
        backendCatalogSwingDocument.removeDocumentListener(this);
        thisDocumentEditorCookie.close();
    } finally {
        super.finalize();
    }
}
 
Example 8
Source File: FileObjectWriter.java    From editorconfig-netbeans with MIT License 5 votes vote down vote up
public static synchronized void closeDocumentInEditor(DataObject dataObject) {
  final OpenCookie oc = dataObject.getLookup().lookup(OpenCookie.class);
  if (oc != null) {
    EditorCookie ec = dataObject.getLookup().lookup(EditorCookie.class);
    if (ec != null) {
      ec.close();
    }
  }
}
 
Example 9
Source File: FileObjectWriter.java    From editorconfig-netbeans with MIT License 5 votes vote down vote up
public static synchronized void reopenDocumentInEditor(DataObject dataObject) {
  final OpenCookie oc = dataObject.getLookup().lookup(OpenCookie.class);
  if (oc != null) {
    EditorCookie ec = dataObject.getLookup().lookup(EditorCookie.class);
    if (ec != null) {
      ec.close();
    }
    oc.open();
  }
}
 
Example 10
Source File: PartialReparseTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doRunTest(String code, String inject, Consumer<CompilationInfo> callback) throws Exception {
    FileObject srcDir = FileUtil.createMemoryFileSystem().getRoot();
    FileObject src = srcDir.createData("Test.java");
    try (Writer out = new OutputStreamWriter(src.getOutputStream())) {
        out.write(code.replaceFirst("^", "").replaceFirst("^", ""));
    }
    EditorCookie ec = src.getLookup().lookup(EditorCookie.class);
    Document doc = ec.openDocument();
    JavaSource source = JavaSource.forFileObject(src);
    Object[] topLevel = new Object[1];
    source.runUserActionTask(cc -> {
        cc.toPhase(Phase.RESOLVED);
        topLevel[0] = cc.getCompilationUnit();
        callback.accept(cc);
    }, true);
    int startReplace = code.indexOf('^');
    int endReplace = code.indexOf('^', startReplace + 1) + 1;
    doc.remove(startReplace, endReplace - startReplace);
    doc.insertString(startReplace, inject, null);
    AtomicReference<List<TreeDescription>> actualTree = new AtomicReference<>();
    AtomicReference<List<DiagnosticDescription>> actualDiagnostics = new AtomicReference<>();
    AtomicReference<List<Long>> actualLineMap = new AtomicReference<>();
    source.runUserActionTask(cc -> {
        cc.toPhase(Phase.RESOLVED);
        assertSame(topLevel[0], cc.getCompilationUnit());
        actualTree.set(dumpTree(cc));
        actualDiagnostics.set(dumpDiagnostics(cc));
        actualLineMap.set(dumpLineMap(cc));
        callback.accept(cc);
    }, true);
    ec.saveDocument();
    ec.close();
    AtomicReference<List<TreeDescription>> expectedTree = new AtomicReference<>();
    AtomicReference<List<DiagnosticDescription>> expectedDiagnostics = new AtomicReference<>();
    AtomicReference<List<Long>> expectedLineMap = new AtomicReference<>();
    source.runUserActionTask(cc -> {
        cc.toPhase(Phase.RESOLVED);
        assertNotSame(topLevel[0], cc.getCompilationUnit());
        expectedTree.set(dumpTree(cc));
        expectedDiagnostics.set(dumpDiagnostics(cc));
        expectedLineMap.set(dumpLineMap(cc));
        callback.accept(cc);
    }, true);
    assertEquals(expectedTree.get(), actualTree.get());
    assertEquals(expectedDiagnostics.get(), actualDiagnostics.get());
    assertEquals(expectedLineMap.get(), actualLineMap.get());
}
 
Example 11
Source File: MarkOccurrencesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void closeFile() {
    EditorCookie ec = null;
    if(dataObject!=null) ec = dataObject.getCookie(EditorCookie.class);
    if(ec != null) ec.close();
}