Update of /cvsroot/neelix/neelix/view
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11443/view
Modified Files:
fox.rb
Log Message:
fixed synchronization of treelist
Index: fox.rb
===================================================================
RCS file: /cvsroot/neelix/neelix/view/fox.rb,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- fox.rb 10 Jun 2004 02:33:16 -0000 1.18
+++ fox.rb 10 Jun 2004 06:25:29 -0000 1.19
@@ -43,49 +43,89 @@
splitter = FXSplitter.new(mw,SPLITTER_HORIZONTAL|LAYOUT_FILL_XY)
# shelf
+class Tree
+ attr_accessor :data, :children
+ def initialize(data=nil)
+ @data = data
+ @children = []
+ end
+end
class Shelf < FXTreeList
def initialize(parent)
super(parent, 1, nil, 0,
TREELIST_SINGLESELECT|
TREELIST_SHOWS_LINES|TREELIST_SHOWS_BOXES|TREELIST_ROOT_BOXES|
LAYOUT_FILL_XY)
- @items = {}
$replicator.cookbooks.add_observer { rebuild }
rebuild
end
- # rebuild the tree because something has changed
- # This is the brute-force way. It may be good enough.
- # discard the current tree
- # walk the data:
- # if the items hash has a matching node, bring it in
- # else make a new node
- def rebuild
- self.clearItems
+ # build a simple tree representation of the data
+ def buildtree
+ t = Tree.new
$replicator.cookbooks.each do |cookbook|
- if not @items[cookbook]
- @items[cookbook] = FXTreeItem.new(cookbook.name)
- @items[cookbook].data = cookbook
- cookbook.add_observer { @items[cookbook].text = cookbook.name; update }
- end
- self.addItemLast(nil, @items[cookbook])
+ a = Tree.new(cookbook)
cookbook.categories.each do |category|
- if not @items[category]
- @items[category] = FXTreeItem.new(category.name)
- @items[category].data = category
- category.add_observer { @items[category].text = category.name; update }
- end
- self.addItemLast(@items[cookbook], @items[category])
+ b = Tree.new(category)
category.recipes.each do |recipe|
- if not @items[recipe]
- @items[recipe] = FXTreeItem.new(recipe.name)
- @items[recipe].data = recipe
- recipe.add_observer { @items[recipe].text = recipe.name; update }
- end
- self.addItemLast(@items[category], @items[recipe])
+ c = Tree.new(recipe)
+ b.children << c
+ end
+ a.children << b
+ end
+ t.children << a
+ end
+ t
+ end
+
+ def findItemByData(data)
+ def rec(ti,data)
+ return ti if ti.data == data
+ n = ti.first
+ while n
+ r = rec(n,data)
+ return r if r
+ n = n.next
+ end
+ nil
+ end
+
+ self.each do |root|
+ r = rec(root,data)
+ return r if r
+ end
+ nil
+ end
+
+ # synchronize an FXTreeItem with a Tree
+ # ti=nil means the imaginary root node
+ def synctree(t,ti=nil)
+ if ti
+ ti.data = t.data
+ n = ti.first
+ while n
+ if not t.children.inject(false) {|memo,obj| memo or obj.data == n.data}
+ removeItem(n)
end
+ n = n.next
end
end
+ t.children.each do |n|
+ a = findItemByData(n.data)
+ if not a
+ a = FXTreeItem.new(n.data.name)
+ a.data = n.data
+ addItemLast(ti,a)
+ end
+ synctree(n,a)
+ end
+ end
+
+ # rebuild the tree because something has changed
+ # This is the brute-force way. It may be good enough.
+ def rebuild
+ synctree(buildtree)
+ update
end
end
|