NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

More fun with Nodes (accessing child nodes)

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users
View previous topic :: View next topic  
Author Message
Cush1978



Joined: 15 Sep 2008
Posts: 218

PostPosted: Wed Jun 06, 2012 1:15 pm    Post subject: More fun with Nodes (accessing child nodes) Reply with quote

In my previous topic, I outlined that my Explorer Tree represents a bunch of ZIP files and each ZIP file contains multiple files that can be edited.

So I have a ZIP node that iterates through all the files contained within and makes child nodes of those files.

During a session with my application, a user would open a ZIP file, then edit one or more files contained with it, then save the ZIP file (thus saving all the contents).

This is where I'm running into issues. The save cookie is based on the ZIP node, but the ZIP node can't access its child nodes. The ZIP contains a collection of files, but those files are what's loaded from the file system, not what's been manipulated in memory. I need a way to do the following:

- Iterate through all the ZIP node's child nodes and get the "in-memory", i.e. unsaved changes to each file.
- Save those changes into the ZIP file.

ZIP Node (Based on ZIP File)
|->File->Child Node (Based on File)->Unsaved Changes
|->etc.

I need a way to incorporate "Unsaved Changes" back to the ZIP when I "Save" the ZIP Node.
Back to top
Geertjan Wielenga
Posted via mailing list.





PostPosted: Wed Jun 06, 2012 1:32 pm    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

On 06/06/2012 03:15 PM, Cush1978 wrote:
Quote:
In my previous topic, I outlined that my Explorer Tree represents a bunch of ZIP files and each ZIP file contains multiple files that can be edited.

So I have a ZIP node that iterates through all the files contained within and makes child nodes of those files.

During a session with my application, a user would open a ZIP file, then edit one or more files contained with it, then save the ZIP file (thus saving all the contents).

This is where I'm running into issues. The save cookie is based on the ZIP node, but the ZIP node can't access its child nodes. The ZIP contains a collection of files, but those files are what's loaded from the file system, not what's been manipulated in memory. I need a way to do the following:

- Iterate through all the ZIP node's child nodes

Node.getChildren

http://bits.netbeans.org/dev/javadoc/org-openide-nodes/org/openide/nodes/Node.html#getChildren%28%29

Gj

Quote:
and get the "in-memory", i.e. unsaved changes to each file.
- Save those changes into the ZIP file.

ZIP Node (Based on ZIP File)
|->File->Child Node (Based on File)->Unsaved Changes
|->etc.

I need a way to incorporate "Unsaved Changes" back to the ZIP when I "Save" the ZIP Node.



Back to top
Cush1978



Joined: 15 Sep 2008
Posts: 218

PostPosted: Wed Jun 06, 2012 2:31 pm    Post subject: Reply with quote

Well, that's rather simple isn't it? I was so wrapped up in Lookups and the like, I didn't consider the easy solution.

Is it practical and/or common to have a Cookie directly working with nodes? I'm thinking something like this:

Code:
public class SaveZIP implements SaveCookie {

    private ZIPNode zip;

    SaveCookie(ZIPNode zip) {
        this.zip = zip;
    }

    public void save() {
        for (Child child : zip.getChildren) {
            // Extract content.
            // Add content to ZIP file.
        }

        // Save ZIP to file system.
    }
}


Is there a problem with doing it this way, or should I do all the content extraction elsewhere and have the Cookie be responsible for iterating a list of content to add to a ZIP file? This is more of a best practices question.

Thanks! The replies have been really helpful.
Back to top
Geertjan Wielenga
Posted via mailing list.





PostPosted: Wed Jun 06, 2012 2:40 pm    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

Note the richer features offered by Savable vs SaveCookie:

https://blogs.oracle.com/geertjan/entry/bye_savecookie_hello_org_netbeans

And, yes, you can put Savable or a SaveCookie into the Lookup of a Node.

Gj

On 06/06/2012 04:31 PM, Cush1978 wrote:
Quote:
Well, that's rather simple isn't it? I was so wrapped up in Lookups and the like, I didn't consider the easy solution.

Is it practical and/or common to have a Cookie directly working with nodes? I'm thinking something like this:


Code:
public class SaveZIP implements SaveCookie {

private ZIPNode zip;

SaveCookie(ZIPNode zip) {
this.zip = zip;
}

public void save() {
for (Child child : zip.getChildren) {
// Extract content.
// Add content to ZIP file.
}

// Save ZIP to file system.
}
}



Is there a problem with doing it this way, or should I do all the content extraction elsewhere and have the Cookie be responsible for iterating a list of content to add to a ZIP file? This is more of a best practices question.

Thanks! The replies have been really helpful.



Back to top
Cush1978



Joined: 15 Sep 2008
Posts: 218

PostPosted: Wed Jun 06, 2012 4:09 pm    Post subject: Reply with quote

Yeah, this looks great. If I'm reading the article right, I get the save functionality I'm looking for, plus a prompt if I close the application without saving unsaved changes?

It looks like that AbstractSavable objects know how to register/unregister themselves from a "global registry for Savables." When you modify your content, a Savable gets registered. When you save, it gets unregistered. I guess this registry is queried when you try to close the application (is this automatic for any platform application?) and if there are Savables, you're prompted to save.

I guess I can also tie into this with my "Close" action/cookie, by checking to see if the ZIP I'm closing has unsaved changes.

Have to start digging and get coding...always lots of questions for this platform, but almost always a good and logical answer.

Thanks!
Back to top
Cush1978



Joined: 15 Sep 2008
Posts: 218

PostPosted: Wed Jun 06, 2012 6:47 pm    Post subject: Reply with quote

OK, I'm still having a hell of a time with this.

Let me start by asking a simple question; are the DataNode (and thus DataObject) classes specifically used for interacting with files already on a file system?

I'm having an awful lot of trouble getting data from my nodes without a lot of casting and making a mess. I went the cookie route because that seemed to be the easiest way to tie to my Node tree:

Code:
public void SaveAction extends CookieAction {
...
...
    public void performAction(final Node[] activatedNodes) {
        for (Node : activatedNodes) {
            // I have to do a lot of instanceof and casting here to figure out
            // if it's a Node I can save and also cast to get methods specific
            // to retrieving Node data.
        }
    }
}


As pointed out earlier, it's pretty easy to traverse a Node tree, but I'm having a hard time extracting Node data. That's where I came across examples using DataObject and DataNode in my book, but they refer to files already on the file system and don't deal with saving modified data. I don't know if I'm on the right track, but what I want to do is:

Start with a Node representing a ZIP.
Retrieve data from all the child nodes. This is where I'm having trouble.
Save each child node's data as an entry in the ZIP file.

I just have a nagging feeling that there's a simpler solution where I can easily adjust my Node structure and/or actions/listeners to make this clean.

Thanks for any and all help.
Back to top
Geertjan Wielenga
Posted via mailing list.





PostPosted: Wed Jun 06, 2012 6:54 pm    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

On 06/06/2012 08:47 PM, Cush1978 wrote:
Quote:
OK, I'm still having a hell of a time with this.

Let me start by asking a simple question; are the DataNode (and thus DataObject) classes specifically used for interacting with files already on a file system?

I'm having an awful lot of trouble getting data from my nodes without a lot of casting and making a mess. I went the cookie route because that seemed to be the easiest way to tie to my Node tree:


Code:
public void SaveAction extends CookieAction {
...
...
public void performAction(final Node[] activatedNodes) {
for (Node : activatedNodes) {
// I have to do a lot of instanceof and casting here to figure out
// if it's a Node I can save and also cast to get methods specific
// to retrieving Node data.
}
}
}

Use Node.getLookup and then lookup some object in your Node. That means
you shouldn't be doing any casting.
Quote:



As pointed out earlier, it's pretty easy to traverse a Node tree, but I'm having a hard time extracting Node data.

Again, use Lookup. The Node is a visualizer for your data, which should
be found in the Lookup of the Node.

Gj

Quote:
That's where I came across examples using DataObject and DataNode in my book, but they refer to files already on the file system and don't deal with saving modified data. I don't know if I'm on the right track, but what I want to do is:

Start with a Node representing a ZIP.
Retrieve data from all the child nodes. This is where I'm having trouble.
Save each child node's data as an entry in the ZIP file.

I just have a nagging feeling that there's a simpler solution where I can easily adjust my Node structure and/or actions/listeners to make this clean.

Thanks for any and all help.



Back to top
Cush1978



Joined: 15 Sep 2008
Posts: 218

PostPosted: Wed Jun 06, 2012 8:03 pm    Post subject: Reply with quote

Of course. I'm getting the hang of it now. I was focusing on get/set methods of a particular Node, hence the messy casting. Lookup is precisely what'll solve that.

Seems simple from the 50,000ft view, but when I'm buried in code spread across various modules, it seems so complicated.

As always, thanks for a nudge on the right path and the excellent help provided. Half the problems I've solved in the past usually come from Google hits on your blog...Smile
Back to top
Geertjan Wielenga
Posted via mailing list.





PostPosted: Wed Jun 06, 2012 8:07 pm    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

On 06/06/2012 10:03 PM, Cush1978 wrote:
Quote:
Of course. I'm getting the hang of it now. I was focusing on get/set methods of a particular Node, hence the messy casting. Lookup is precisely what'll solve that.

Seems simple from the 50,000ft view, but when I'm buried in code spread across various modules, it seems so complicated.

As always, thanks for a nudge on the right path and the excellent help provided. Half the problems I've solved in the past usually come from Google hits on your blog...:)




Same goes for me. Smile When I'm stuck, I use Google and end up back in my
blog from three years ago.. and I'm normally surprised at the answer.

Gj
Back to top
Javier Ortiz
Posted via mailing list.





PostPosted: Thu Jun 07, 2012 6:24 pm    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

Remember to share your experience in the FAQ. This seems like a complex enough example of a real application that can come really handy.



Senior Software Quality Engineer
ArthroCare Corporation
7000 William Cannon Drive
Austin, TX 78735
Phone: 512-358-5996
email: address-removed

-----Original Message-----
From: Cush1978 [mailto:address-removed]
Sent: Wednesday, June 06, 2012 1:48 PM
To: address-removed
Subject: [platform-dev] More fun with Nodes (accessing child nodes)

OK, I'm still having a hell of a time with this.

Let me start by asking a simple question; are the DataNode (and thus DataObject) classes specifically used for interacting with files already on a file system?

I'm having an awful lot of trouble getting data from my nodes without a lot of casting and making a mess. I went the cookie route because that seemed to be the easiest way to tie to my Node tree:


Code:
public void SaveAction extends CookieAction { ...
...
public void performAction(final Node[] activatedNodes) {
for (Node : activatedNodes) {
// I have to do a lot of instanceof and casting here to figure out
// if it's a Node I can save and also cast to get methods specific
// to retrieving Node data.
}
}
}



As pointed out earlier, it's pretty easy to traverse a Node tree, but I'm having a hard time extracting Node data. That's where I came across examples using DataObject and DataNode in my book, but they refer to files already on the file system and don't deal with saving modified data. I don't know if I'm on the right track, but what I want to do is:

Start with a Node representing a ZIP.
Retrieve data from all the child nodes. This is where I'm having trouble.
Save each child node's data as an entry in the ZIP file.

I just have a nagging feeling that there's a simpler solution where I can easily adjust my Node structure and/or actions/listeners to make this clean.

Thanks for any and all help.




**********

The information contained in this e-mail message, together with any
attachments thereto, is intended only for the personal and confidential
use of the addressee named above. The message and the attachments
are or may be privileged or protected communication. If you are not the
intended recipient of this message, or authorized to receive it for the
intended recipient, you have received this message in error, and you
are not to review, use, disseminate, distribute or copy this message,
any attachments thereto, or their contents. If you have received this
message in error, please immediately notify us by return e-mail
message, and delete the original message.

Pursuant to Circular 230 issued by the United States Treasury
Department and relating to practice before the Internal Revenue
Services, any comment or opinion in this communication relating to a
federal tax issue is not intended to be used, and cannot be used, by a
taxpayer for the purpose of avoiding tax-related penalties that may be
imposed on the taxpayer.
Back to top
Cush1978



Joined: 15 Sep 2008
Posts: 218

PostPosted: Thu Jun 07, 2012 7:38 pm    Post subject: Reply with quote

I just took a look through the FAQ at http://wiki.netbeans.org/NetBeansDeveloperFAQ and found a section on Saving. Unfortunately, all the links are broken. All the entries on Saving seem to link to an outdated version of Geertjan's blog (blogs.sun.com).
Back to top
Maehem



Joined: 02 Jul 2009
Posts: 28
Location: Las Vegas, NV

PostPosted: Thu Jun 07, 2012 11:04 pm    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

Any of Geertjan's broken blog links can be worked around by substituting
'sun.com' for 'oracle.com' in most cases.


Mark


On 6/7/12 12:38 PM, Cush1978 wrote:
Quote:
I just took a look through the FAQ at http://wiki.netbeans.org/NetBeansDeveloperFAQ and found a section on Saving. Unfortunately, all the links are broken. All the entries on Saving seem to link to an outdated version of Geertjan's blog (blogs.sun.com).



Back to top
Geertjan Wielenga
Posted via mailing list.





PostPosted: Fri Jun 08, 2012 6:16 am    Post subject: [platform-dev] Re: More fun with Nodes (accessing child nodes) Reply with quote

On 06/07/2012 09:38 PM, Cush1978 wrote:
Quote:
I just took a look through the FAQ at http://wiki.netbeans.org/NetBeansDeveloperFAQ and found a section on Saving. Unfortunately, all the links are broken. All the entries on Saving seem to link to an outdated version of Geertjan's blog (blogs.sun.com).




That blog entry you already saw about Savable is the best reference for
current information on saving.

Will fix those links that are broken, just replace http://sun.com with
https://oracle.com, should be enough.

Gj
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo