1. Building and Saving Treeviews

Hi All,
I asked this before but never really got the answers I wanted and I ran 
out of time. Now with time on my hands I'll try again.

I am looking at the example that comes with win32lib 57.9.
When a user creates a treeview as in the example titled tvedit.exw how 
can I save this TV to disk and rebuild it again later when I don't know 
how many branches/sub branches a user may have created.

Thanks
Tony Steward

Give your hardest tasks to your lasiest workers. 
They will always find the easiest way to complete it.

new topic     » topic index » view message » categorize

2. Re: Building and Saving Treeviews

Tony,

I have what *might* be the beginning of how to do it, but haven't finished
it yet.  Will tell you what I'm thinking might work in case it might help.

in the demo,  "folders" is a sequence of the created handles for each item
in the TV, starting with the root item;

1.  create another sequence that has empty sub-sequences for each element of
"folders";
2.  go thru "folders", and put the parents of the parents of the parents
(etc) for each element into each sub-sequence;
(that *could* look something like this:
{{1},{2,1},{3,1},{4,3,1},{5,4,3,1},{6,5,4,3,1},{7,2,1},{8,7,2,1},{9,8,7,2,1}
}  )

3.  Those numbers LOOK like what you would want, but they are handles which
just happen because of the order of their creation to be "correct"; I think
you would want to "adjust" them, in order to handle the general case, by
finding what *position* in that sequence each handle occupies (because
you're not interested in the actual handle, but rather its order of creation
& what item in that order was its parent, in order to recreate the tree from
what we're working toward); the result of "adjusting" the above sequence
would be an apparent *duplicate* of that sequence, but if the handles were
*different* (ie, if in your program some other controls were created "in
between" some of those TV items, the handles then wouldn't be in that
"simple" order to begin with), then you would have to get it to look like
that, eventually, but with each items text  associated with the appropriate
element in the final sequence.  I haven't worked out how to make that
adjustment yet, though.

4.  you would also want to associate each items text with its element in the
sequence, so you could save that sequence to a file, later read it in, & use
it to recreate the tree.
That final sequence might look like:
{{1,"root item"},{2,"first child",1,"root item"},{3,"second child",1,"root
item....etc}}
or something like that.

5.  Then after you read that sequence in from a file, you should be able to
use it to create a series of TV items in the order that the sequence is in,
knowing which item has which element as its parent by where it is in the
sequence, I think.

Can't finish it up right now, but maybe if that's not either obvious or just
off the wall, it might help?

Here's a code snippet for what I suggested, working off a menu item:

-- <CODE BEGINS>

-- attempts to save tree data to file:
procedure Click_MenuSave(integer self, integer event, sequence parms)
   sequence temp, temp2
   integer tempId
   temp = {}
   temp2 = {}

   handle = open( "Tree.dat", "w" )

-- for each element in folders, make an empty sequence in temp;
   for n = 1 to length(folders) do
     temp &= {{}}
   end for


-- look at each element in folder, one at a time;
-- put that into the sequence at same position in temp;
-- look for parent of item in temp;
-- if HAS parent other than root, place that id after item, then look
-- for ITS parent, continue till at root.


   for n = 1 to length( folders) do
     temp[n] &= folders[n]
     while 1 do
       if getTVParent(temp[n][length(temp[n])]) = 0 then
           exit
       else
           temp[n] &= {getTVParent(temp[n][length(temp[n])])}
       end if
      end while
   end for

   print(handle, temp)
   puts(handle, "\n")

-- the results of the above is SOMETHING like this:
-- {{1},{2,1},{3,1},{4,3,1},{5,4,3,1},{6,2,1},{7,6,2,1}}  ,
-- where the first number in each sub sequence is the handle of the item,
-- and each number after is the handle of the parent of the handle before it
-- note that those numbers would probably NOT be as simple 1,2,3 as above,
-- at this point, but you would want to TURN them into that to use to
-- regenerate the tree in order.  also want item names associated correctly.

-- go thru the sequence, replacing the FIRST number in each sequence with
-- the INDEX of that element

   for n = 1 to length(temp) do
      temp[n][1] = n
   end for

-- now do something like that for each other element in each sub-sequence.




   print(handle, temp)
   close(handle)

end procedure

<CODE ENDS>


Dan Moyer


----- Original Message -----
From: "Tony Steward" <lockmaster67 at aol.com>
To: "EUforum" <EUforum at topica.com>
Sent: Wednesday, October 16, 2002 11:56 PM
Subject: Building and Saving Treeviews


>
> Hi All,
> I asked this before but never really got the answers I wanted and I ran
> out of time. Now with time on my hands I'll try again.
>
> I am looking at the example that comes with win32lib 57.9.
> When a user creates a treeview as in the example titled tvedit.exw how
> can I save this TV to disk and rebuild it again later when I don't know
> how many branches/sub branches a user may have created.
>
> Thanks
> Tony Steward
>
> Give your hardest tasks to your lasiest workers.
> They will always find the easiest way to complete it.
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

3. Re: Building and Saving Treeviews

Tony,

My previous reply might (?) have been in the right direction, but wasn't
quite right; the following routines seem to do the job, though you might
want to do better file checking.  I've tested these routines in the demo,
and they seem to work fine, but I'm not 100% sure if they'll work elsewhere;
I've assumed that the id numbers in "folders" can be used "as-is", and that
might not be true in every case.

Dan Moyer


------------------------------------------------------------------------
-- saves tree data to file:
procedure Click_MenuSave(integer self, integer event, sequence parms)
   sequence temp, temp2
   integer tempId
   temp = {}
   temp2 = {}

   handle = open( "Tree.txt", "w" )

-- for each element in folders, make an empty sequence in temp;
   for n = 1 to length(folders) do
     temp &= {{}}
   end for


-- look at each element in folder, one at a time;
-- put that into the sequence at same position in temp;
-- look for parent of item in temp,  place that id after item,
-- then add the items text after that.

-- then write it to file.

   for n = 1 to length( folders) do
     temp[n] &= folders[n]
           temp[n] &= {getTVParent(temp[n][1])}
           temp[n] &= {getTVText(temp[n][1])}
   end for

   print(handle, temp)
   close(handle)

end procedure
-----------------------------------------------------------------

-- retrieves saved tree info, clears existing tree,
-- & rebuilds it with the saved info:
procedure Click_MenuGetTree(integer self, integer event, sequence parms)
sequence savedTree, result
savedTree = {}

-- first empty the tree:
  eraseItems(TV)

-- now get saved items:
   handle = open( "Tree.txt", "r" )
   result = get(handle)
   if result[1] = GET_SUCCESS then
      savedTree = result[2]
      folders = {}
      for n = 1 to length(savedTree) do
         folders &= addTVItem( TV, closefolder, openfolder, savedTree[n][3]
, savedTree[n][2] )
      end for
   end if
   close(handle)
end procedure



----- Original Message -----
From: "Tony Steward" <lockmaster67 at aol.com>
To: "EUforum" <EUforum at topica.com>
Sent: Wednesday, October 16, 2002 11:56 PM
Subject: Building and Saving Treeviews


>
> Hi All,
> I asked this before but never really got the answers I wanted and I ran
> out of time. Now with time on my hands I'll try again.
>
> I am looking at the example that comes with win32lib 57.9.
> When a user creates a treeview as in the example titled tvedit.exw how
> can I save this TV to disk and rebuild it again later when I don't know
> how many branches/sub branches a user may have created.
>
> Thanks
> Tony Steward
>
> Give your hardest tasks to your lasiest workers.
> They will always find the easiest way to complete it.
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

4. Re: Building and Saving Treeviews

Tony,

The routines I sent which worked don't seem to work if an item is *deleted*
from the tree  :(

Dan

----- Original Message -----
From: "Tony Steward" <lockmaster67 at aol.com>
To: "EUforum" <EUforum at topica.com>
Subject: Building and Saving Treeviews


>
> Hi All,
> I asked this before but never really got the answers I wanted and I ran
> out of time. Now with time on my hands I'll try again.
>
> I am looking at the example that comes with win32lib 57.9.
> When a user creates a treeview as in the example titled tvedit.exw how
> can I save this TV to disk and rebuild it again later when I don't know
> how many branches/sub branches a user may have created.
>
> Thanks
> Tony Steward
>
> Give your hardest tasks to your lasiest workers.
> They will always find the easiest way to complete it.
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

5. Re: Building and Saving Treeviews

Tony,

I *think* the following routines added to the tvedit.exw demo will allow you
to save a user created tree & then re-create it properly;  you'll have to
add two menu choices for save & get the tv, & should probably make a better
check for the existence of the saved tv file, but these routines seem to
handle re-create after delete items ok, and also re-create after delete
items & then add new items, including delete "root".

You should also probably test it more than I have  :)

For test purposes, I renamed each item I created in the demo with an
in-order number, so I could more easily see what might be happening with
each after deletions & additions & re-creations.

Dan Moyer

--<routines to add to  tvedit.exw demo follow>

-- saves tree data to file:
procedure Click_MenuSave(integer self, integer event, sequence parms)
   sequence temp
   integer wrongIndex
   temp = {}

   handle = open( "Tree.txt", "w" )

-- for each element in folders, make an empty sequence in temp;
   for n = 1 to length(folders) do
     temp &= {{}}
   end for


-- look at each element in folder, one at a time;
-- put that into the sequence at same position in temp;
-- look for parent of item in temp, place that id after item,
-- then place items text right after that.

   for n = 1 to length( folders) do
     temp[n] &= folders[n]
     temp[n] &= {getTVParent(temp[n][1])}
     temp[n] &= {getTVText(temp[n][1])}
   end for


-- now see if any of the items, because of deletions, might have "wrong"
-- "index" (?) numbers, & "correct" (?) them:
-- ("correct" means the items should be listed "in order", and all items
--  which are children of any corrected item should be referred to that
--  corrected reference)

   for m = 1 to length(temp) do
     if not equal (m, temp[m][1]) then
        wrongIndex = temp[m][1]
        temp[m][1] = m
        for n = 1 to length(temp) do
            if equal (wrongIndex, temp[n][2]) then
               temp[n][2] = m
            end if
        end for
     end if
   end for


   print(handle, temp)
   close(handle)

end procedure
-----------------------------------------------------------------
--  clears existing tree, retrieves saved tree info,
--  then rebuilds tree with the saved info:
procedure Click_MenuGetTree(integer self, integer event, sequence parms)
sequence savedTree, result
savedTree = {}

-- first empty the tree:
  eraseItems(TV)

-- now get saved items:
   handle = open( "Tree.txt", "r" )
   result = get(handle)
   if result[1] = GET_SUCCESS then
      savedTree = result[2]
      folders = {}
      for n = 1 to length(savedTree) do
         folders &= addTVItem( TV, closefolder, openfolder, savedTree[n][3]
, savedTree[n][2] )
      end for
   end if
   close(handle)
end procedure
-------------------------------------------------

<end routines>


----- Original Message -----
From: "Tony Steward" <lockmaster67 at aol.com>
To: "EUforum" <EUforum at topica.com>
Sent: Friday, October 18, 2002 11:00 PM
Subject: RE: Building and Saving Treeviews


>
> Hi Dan
> Thanks for the help keep it coming (when you have time), I started
> heading in a similar direction last nite.
>
> Thanks
> Tony
>
> Dan Moyer wrote:
> > Tony,
> >
> > The routines I sent which worked don't seem to work if an item is
> > *deleted*
> > from the tree  :(
> >
> > Dan
> >
> > ----- Original Message -----
> > From: "Tony Steward" <lockmaster67 at aol.com>
> > To: "EUforum" <EUforum at topica.com>
> > Sent: Wednesday, October 16, 2002 11:56 PM
> > Subject: Building and Saving Treeviews
> >
> >
> > > Hi All,
> > > I asked this before but never really got the answers I wanted and I
ran
> > > out of time. Now with time on my hands I'll try again.
> > >
> > > I am looking at the example that comes with win32lib 57.9.
> > > When a user creates a treeview as in the example titled tvedit.exw how
> > > can I save this TV to disk and rebuild it again later when I don't
know
> > > how many branches/sub branches a user may have created.
> > >
> > > Thanks
> > > Tony Steward
> > >
> > > Give your hardest tasks to your lasiest workers.
> > > They will always find the easiest way to complete it.
> > >
> > >
> Give your hardest tasks to your lasiest workers.
> They will always find the easiest way to complete it.
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu