org.openide.cookies.CloseCookie Java Examples

The following examples show how to use org.openide.cookies.CloseCookie. 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: GsfDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends Cookie> T createCookie(Class<T> klass) {
        if (
        klass.isAssignableFrom(DataEditorSupport.class) || 
        DataEditorSupport.class.isAssignableFrom(klass) || 
        klass.isAssignableFrom(Openable.class) || 
        klass.isAssignableFrom(Editable.class) || 
        klass.isAssignableFrom(EditorCookie.Observable.class) || 
        klass.isAssignableFrom(PrintCookie.class) || 
        klass.isAssignableFrom(CloseCookie.class) || 
        klass.isAssignableFrom(LineCookie.class)
    ) {
        return klass.cast(createEditorSupport());
    }
    return null;
}
 
Example #2
Source File: GsfDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public GsfDataObject(FileObject pf, MultiFileLoader loader, Language language) throws DataObjectExistsException {
    super(pf, loader);

    // If the user creates a file with a filename where we can't figure out the language
    // (e.g. the PHP New File wizard doesn't enforce a file extension, so if you create
    // a file named "pie.class" (issue 124044) the data loader doesn't know which language
    // to associate this with since it isn't a GSF file extension or mimetype). However
    // during template creation we know the language anyway so we can use it. On subsequent
    // IDE restarts the file won't be recognized so the user will have to rename or
    // add a new file extension to file type mapping.
    if (language == null) {
        language = templateLanguage;
    }
    this.language = language;
    getCookieSet().add(new Class[]{
            GenericEditorSupport.class, // NOI18N
            SaveAsCapable.class, Openable.class, EditorCookie.Observable.class, 
            PrintCookie.class, CloseCookie.class, Editable.class, LineCookie.class,
            DataEditorSupport.class, CloneableEditorSupport.class,
            CloneableOpenSupport.class
        }, new EditorSupportFactory());
}
 
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: XMLJ2eeDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public XMLJ2eeDataObject(FileObject pf, MultiFileLoader loader)
    throws org.openide.loaders.DataObjectExistsException {
    super(pf,loader);
    
    CookieSet cs = getCookieSet();
    cs.add(XMLJ2eeEditorSupport.class, this);
    cs.add(EditCookie.class, this);
    cs.add(EditorCookie.class, this);
    cs.add(LineCookie.class, this);
    cs.add(PrintCookie.class, this);
    cs.add(CloseCookie.class, this);
    // added CheckXMLCookie
    InputSource in = DataObjectAdapters.inputSource(this);
    CheckXMLCookie checkCookie = new CheckXMLSupport(in);
    cs.add(checkCookie);
}
 
Example #5
Source File: DefaultDataObjectHasOpenTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void tearDown() throws Exception {
    waitForAWT();
    DD.disableTest = true;

    super.tearDown();
    if (obj != null) {
        CloseCookie cc;
        cc = obj.getCookie(CloseCookie.class);
        if (cc != null) {
            DD.toReturn = NotifyDescriptor.NO_OPTION;
            cc.close();
        }
    }

    waitForAWT();
}
 
Example #6
Source File: XMLDataObjectMimeTypeTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected void tearDown() throws Exception {
    waitForAWT();
    DD.disableTest = true;

    super.tearDown();
    if (obj != null) {
        CloseCookie cc;
        cc = obj.getCookie(CloseCookie.class);
        if (cc != null) {
            DD.toReturn = NotifyDescriptor.NO_OPTION;
            cc.close();
        }
    }
    Repository.getDefault().removeFileSystem(lfs);

    waitForAWT();
}
 
Example #7
Source File: JShellDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public JShellDataObject(FileObject fo, MultiFileLoader loader) throws DataObjectExistsException {
    super(fo, loader);
    CookieSet cks = getCookieSet();
    cks.add(new Class[] {
            OpenCookie.class,
            EditorCookie.Observable.class,
            CloseCookie.class,
            LineCookie.class,
            SimpleES.class,
        }, new CookieSet.Factory() {
        private CloneableEditorSupport supp;
        public <T extends Node.Cookie> T createCookie(Class<T> klass) {
            if (supp != null) {
                return klass.cast(supp);
            }
            return klass.cast(
                    /*
                    supp = DataEditorSupport.create(JShellDataObject.this, 
                            getPrimaryEntry(), getCookieSet(), 
                            () -> createPane())
                    );*/
                    supp = new SimpleES(JShellDataObject.this, getPrimaryEntry())
            );
        }
    });
}
 
Example #8
Source File: DefaultDataObjectHasNodeInLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void tearDown() throws Exception {
    waitForAWT();

    super.tearDown();
    if (obj != null) {
        CloseCookie cc;
        cc = obj.getCookie(CloseCookie.class);
        if (cc != null) {
            cc.close();
        }
    }

    waitForAWT();
}
 
Example #9
Source File: CloseMergeViewAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void performAction(org.openide.nodes.Node[] node) {
    if (node.length == 0) return;
    CloseCookie cc = (CloseCookie) node[0].getCookie (CloseCookie.class);
    if (cc != null) {
        cc.close();
    }
}
 
Example #10
Source File: XMLDataObjectMimeTypeTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void tearDown() throws Exception {
    waitForAWT();

    super.tearDown();
    if (obj != null) {
        CloseCookie cc;
        cc = obj.getCookie(CloseCookie.class);
        if (cc != null) {
            cc.close();
        }
    }
    Repository.getDefault().removeFileSystem(lfs);

    waitForAWT();
}
 
Example #11
Source File: Group.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void closeDocuments(Set<DataObject> toCloseDocuments) {
    for (DataObject dobj : toCloseDocuments) {
        if (!dobj.isModified()) {
            //the modified files would force user to decide about saving..
            CloseCookie cook = dobj.getLookup().lookup(CloseCookie.class);
            if (cook != null) {
                cook.close();
            }
        }
    }   
}
 
Example #12
Source File: MultiDataObjectVersion1Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testEditorInLookup() throws Exception {
    assertTrue(this.one instanceof SimpleObject);
    SimpleObject s = (SimpleObject)this.one;

    LineCookie line = s.getLookup().lookup(LineCookie.class);
    assertNotNull("Line cookie is there", line);
    assertEquals("Edit cookie", line, s.getLookup().lookup(EditCookie.class));
    assertEquals("Editor cookie", line, s.getLookup().lookup(EditorCookie.class));
    assertEquals("Editor objservable cookie", line, s.getLookup().lookup(EditorCookie.Observable.class));
    assertEquals("SaveAsCapable", line, s.getLookup().lookup(SaveAsCapable.class));
    assertEquals("Open cookie", line, s.getLookup().lookup(OpenCookie.class));
    assertEquals("Close cookie", line, s.getLookup().lookup(CloseCookie.class));
    assertEquals("Print cookie", line, s.getLookup().lookup(PrintCookie.class));
}
 
Example #13
Source File: JWSProjectPropertiesUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean copyJnlpImplTemplate(Project proj) throws IOException {
    boolean res = false;
    final FileObject projDir = proj.getProjectDirectory();
    FileObject jnlpBuildFile = projDir.getFileObject(JNLP_BUILD_IMPL_PATH);
    if (jnlpBuildFile != null && !isJnlpImplUpToDate(proj)) {
        // try to close the file just in case the file is already opened in editor
        DataObject dobj = DataObject.find(jnlpBuildFile);
        CloseCookie closeCookie = dobj.getLookup().lookup(CloseCookie.class);
        if (closeCookie != null) {
            closeCookie.close();
        }
        final FileObject nbproject = projDir.getFileObject("nbproject");                    //NOI18N
        final FileObject backupFile = nbproject.getFileObject("jnlp-impl_backup", "xml");   //NOI18N
        if (backupFile != null) {
            backupFile.delete();
        }
        FileUtil.moveFile(jnlpBuildFile, nbproject, "jnlp-impl_backup");                    //NOI18N
        jnlpBuildFile = null;
    }
    if (jnlpBuildFile == null) {
        FileObject templateFO = FileUtil.getConfigFile(BUILD_TEMPLATE);
        if (templateFO != null) {
            FileUtil.copyFile(templateFO, projDir.getFileObject("nbproject"), "jnlp-impl"); // NOI18N
        }
        res = true;
    }
    return res;
}
 
Example #14
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NonNull
static FileObject copyBuildScript (@NonNull final Project project) throws IOException {
    final FileObject projDir = project.getProjectDirectory();
    FileObject rpBuildScript = projDir.getFileObject(BUILD_SCRIPT_PATH);
    if (rpBuildScript != null && !isBuildScriptUpToDate(project)) {
        // try to close the file just in case the file is already opened in editor
        DataObject dobj = DataObject.find(rpBuildScript);
        CloseCookie closeCookie = dobj.getLookup().lookup(CloseCookie.class);
        if (closeCookie != null) {
            closeCookie.close();
        }
        final FileObject nbproject = projDir.getFileObject("nbproject");                    //NOI18N
        final FileObject backupFile = nbproject.getFileObject(BUILD_SCRIPT_BACK_UP, "xml"); //NOI18N
        if (backupFile != null) {
            backupFile.delete();
        }
        FileUtil.moveFile(rpBuildScript, nbproject, BUILD_SCRIPT_BACK_UP);
        rpBuildScript = null;
    }
    if (rpBuildScript == null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(
                Level.FINE,
                "Updating remote build script in project {0} ({1})", //NOI18N
                new Object[]{
                    ProjectUtils.getInformation(project).getDisplayName(),
                    FileUtil.getFileDisplayName(projDir)
                });
        }
        rpBuildScript = FileUtil.createData(project.getProjectDirectory(), BUILD_SCRIPT_PATH);
        try(
            final InputStream in = new BufferedInputStream(RemotePlatformProjectSaver.class.getResourceAsStream(BUILD_SCRIPT_PROTOTYPE));
            final OutputStream out = new BufferedOutputStream(rpBuildScript.getOutputStream())) {
            FileUtil.copy(in, out);
        }
    }
    return rpBuildScript;
}
 
Example #15
Source File: DefaultDataObjectHasOpenTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doRegularCheck(final EditorCookie c) throws Exception {
    assertEquals(
            "Next questions results in the same cookie",
            c,
            obj.getCookie(EditorCookie.class)
            );
    assertEquals(
            "Next questions results in the same cookie",
            c,
            obj.getLookup().lookup(EditorCookie.class)
            );
    assertEquals(
            "Print cookie is provided",
            c,
            obj.getCookie(PrintCookie.class)
            );
    assertEquals(
            "CloseCookie as well",
            c,
            obj.getCookie(CloseCookie.class)
            );

    OpenCookie open = obj.getCookie(OpenCookie.class);
    open.open();


    Document d = null;
    for (int i = 0; i < 10; i++) {
        d = c.getDocument();
        if (d != null) {
            break;
        }
        Thread.sleep(1000);
    }
    assertNotNull(d);

    d.insertString(0, "Kuk", null);

    assertNotNull(
            "Now there is a save cookie",
            obj.getCookie(SaveCookie.class)
            );
}
 
Example #16
Source File: DocumentTitlePropertyTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
    super(folder, l);
    getCookieSet ().add (new Class[] { OpenCookie.class, CloseCookie.class, EditorCookie.class }, this);
}
 
Example #17
Source File: TopComponentGetLookupTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Check that even if a node has a <em>different</em> node in its lookup, a
 * query on Node.class will produce only the actual activated nodes.
 * Other queries may return the embedded node, but not duplicates.
 * @see "#36336"
 */
public void testForeignNodesInLookupIgnoredForNodeQuery() throws Exception {
    class CloseCookieNode extends AbstractNode implements CloseCookie {
        CloseCookieNode() {
            super(Children.LEAF);
            setName("n1");
        }
        public boolean close() {return true;}
    }
    Node n1 = new CloseCookieNode();
    Node n2 = new AbstractNode(Children.LEAF) {
        {
            setName("n2");
            class ViewCookieNode extends AbstractNode implements ViewCookie {
                ViewCookieNode() {
                    super(Children.LEAF);
                    setName("n3");
                }
                public void view() {}
            }
            getCookieSet().add(new ViewCookieNode());
            getCookieSet().add(new OpenCookie() {
                public void open() {}
            });
        }
    };
    Node[] sel = new Node[] {n1, n2};
    assertEquals("First node in selection has CloseCookie",
            1,
            n1.getLookup().lookup(new Lookup.Template<CloseCookie>(CloseCookie.class)).allInstances().size());
    assertEquals("Second node in selection has OpenCookie",
            1,
            n2.getLookup().lookup(new Lookup.Template<OpenCookie>(OpenCookie.class)).allInstances().size());
    assertEquals("Second node in selection has ViewCookie (actually a Node)",
            1,
            n2.getLookup().lookup(new Lookup.Template<ViewCookie>(ViewCookie.class)).allInstances().size());
    ViewCookie v = (ViewCookie)n2.getCookie(ViewCookie.class);
    assertNotNull(v);
    assertTrue(v instanceof Node);
    
    HashSet<Node> queryJustOnce = new HashSet<Node>(n2.getLookup().lookup(new Lookup.Template<Node>(Node.class)).allInstances());
    assertEquals("Second node in selection has two nodes in its own lookup",
            new HashSet<Object>(Arrays.asList(new Object[] {n2, v})), queryJustOnce
            );
    assertEquals(2, queryJustOnce.size());
    top.setActivatedNodes(sel);
    assertEquals("CloseCookie propagated from one member of node selection to TC lookup",
            1,
            lookup.lookup(new Lookup.Template<CloseCookie>(CloseCookie.class)).allInstances().size());
    assertEquals("OpenCookie propagated from one member of node selection to TC lookup",
            1,
            lookup.lookup(new Lookup.Template<OpenCookie>(OpenCookie.class)).allInstances().size());
    assertEquals("ViewCookie propagated from one member of node selection to TC lookup",
            1,
            lookup.lookup(new Lookup.Template<ViewCookie>(ViewCookie.class)).allInstances().size());
    assertEquals("But TC lookup query on Node gives only selection, not cookie node",
            new HashSet<Node>(Arrays.asList(sel)),
            new HashSet<Node>(lookup.lookup(new Lookup.Template<Node>(Node.class)).allInstances()));
    assertEquals(2, lookup.lookup(new Lookup.Template<Node>(Node.class)).allInstances().size());
    assertEquals("TC lookup query on FeatureDescriptor gives all three however",
            3,
            lookup.lookup(new Lookup.Template<FeatureDescriptor>(FeatureDescriptor.class)).allInstances().size());
    top.setActivatedNodes(new Node[] {n1});
    assertEquals("After setting node selection to one node, TC lookup has only that node",
            Collections.singleton(n1),
            new HashSet<Node>(lookup.lookup(new Lookup.Template<Node>(Node.class)).allInstances()));
    assertEquals(1, lookup.lookup(new Lookup.Template<Node>(Node.class)).allInstances().size());
    assertEquals("And the OpenCookie is gone",
            0,
            lookup.lookup(new Lookup.Template<OpenCookie>(OpenCookie.class)).allInstances().size());
    assertEquals("And the ViewCookie is gone",
            0,
            lookup.lookup(new Lookup.Template<ViewCookie>(ViewCookie.class)).allInstances().size());
    assertEquals("But the CloseCookie remains",
            1,
            lookup.lookup(new Lookup.Template<CloseCookie>(CloseCookie.class)).allInstances().size());
}
 
Example #18
Source File: DataEditorSupportTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
    super(folder, l);
    getCookieSet ().add (new Class[] { OpenCookie.class, CloseCookie.class, EditorCookie.class }, this);
}
 
Example #19
Source File: DataEditorReadOnlyTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
    super(folder, l);
    getCookieSet ().add (new Class[] { OpenCookie.class, CloseCookie.class, EditorCookie.class }, this);
}
 
Example #20
Source File: PropertyChangeTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException {
    super(folder, l);
    getCookieSet ().add (new Class[] { OpenCookie.class, CloseCookie.class, EditorCookie.class }, this);
}
 
Example #21
Source File: DefaultDataObject.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void fixCookieSet(Class<?> c, boolean force) {
    if ((cookieSetFixed && !force) || support != null) {
        return;
    }
    
    if (
        c.isAssignableFrom(EditCookie.class)
        ||
        c.isAssignableFrom(EditorCookie.Observable.class)
        ||
        c.isAssignableFrom(PrintCookie.class)
        ||
        c.isAssignableFrom(CloseCookie.class)
        ||
        c == DefaultES.class
    ) {
        try {
            if (!force) {
                // try to initialize the editor cookie set if the file 
                // seems editable
                byte[] arr = new byte[2048];
                InputStream is = getPrimaryFile().getInputStream();
                try {
                    int len = is.read (arr);
                    for (int i = 0; i < len; i++) {
                        if (arr[i] >= 0 && arr[i] <= 31 && arr[i] != '\n' && arr[i] != '\r' && arr[i] != '\t' && arr[i] != '\f') {
                            cookieSetFixed = true;
                            return;
                        }
                    }
                } finally {
                    is.close ();
                }
            }
            support = new DefaultES (
                this, getPrimaryEntry(), getCookieSet ()
            );
            getCookieSet().assign(DefaultES.class, support);
            cookieSetFixed = true;
        } catch (IOException ex) {
            LOG.log(Level.INFO, "Cannot read " + getPrimaryEntry(), ex); // NOI18N
        }
    }
}
 
Example #22
Source File: CloseMergeViewAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected Class[] cookieClasses() {
    return new Class[] { CloseCookie.class };
}