Class: Glimmer::DataBinding::TreeItemsBinding
- Inherits:
-
Object
- Object
- Glimmer::DataBinding::TreeItemsBinding
- Includes:
- DataBinding::Observable, DataBinding::Observer
- Defined in:
- lib/glimmer/data_binding/tree_items_binding.rb
Instance Method Summary collapse
- #call(new_value = nil) ⇒ Object
-
#initialize(parent, model_binding, tree_properties) ⇒ TreeItemsBinding
constructor
A new instance of TreeItemsBinding.
- #populate_tree(model_tree_root_node, parent, tree_properties) ⇒ Object
- #populate_tree_node(model_tree_node, parent, tree_properties) ⇒ Object
- #recursive_depth_first_search(model_tree_node, tree_properties, collection) ⇒ Object
Constructor Details
#initialize(parent, model_binding, tree_properties) ⇒ TreeItemsBinding
Returns a new instance of TreeItemsBinding.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 37 def initialize(parent, model_binding, tree_properties) @tree = parent @model_binding = model_binding @tree_properties = [tree_properties].flatten.first.to_h if @tree.respond_to?(:tree_properties=) @tree.tree_properties = @tree_properties else # assume custom widget @tree.body_root.tree_properties = @tree_properties end call model = model_binding.base_model observe(model, model_binding.property_name_expression) @tree. do |dispose_event| unregister_all_observables end end |
Instance Method Details
#call(new_value = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 54 def call(new_value=nil) @model_tree_root_node = @model_binding.evaluate_property old_tree_items = @tree.all_tree_items old_model_tree_nodes = old_tree_items.map(&:get_data) new_model_tree_nodes = [] recursive_depth_first_search(@model_tree_root_node, @tree_properties, new_model_tree_nodes) return if old_model_tree_nodes == new_model_tree_nodes && old_tree_items.map(&:text) == new_model_tree_nodes.map {|model_tree_node| model_tree_node.send(@tree_properties[:text])} populate_tree(@model_tree_root_node, @tree, @tree_properties) end |
#populate_tree(model_tree_root_node, parent, tree_properties) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 64 def populate_tree(model_tree_root_node, parent, tree_properties) # TODO get rid of model_tree_root_node, parent, tree_properties as an argument given it is stored as an instance variable # TODO make it change things by delta instead of removing all old_tree_items = parent.all_tree_items selected_tree_item_model = parent..getSelection.map(&:get_data).first old_tree_item_expansion_by_data = old_tree_items.reduce({}) {|hash, ti| hash.merge(ti.getData => ti.getExpanded)} old_tree_items.each do |tree_item| tree_item.getData('observer_registrations').each(&:unregister) end parent..items.each(&:dispose) parent..removeAll populate_tree_node(model_tree_root_node, parent., tree_properties) parent.all_tree_items.each { |ti| ti.setExpanded(!!old_tree_item_expansion_by_data[ti.getData]) } tree_item_to_select = parent.depth_first_search {|ti| ti.getData == selected_tree_item_model} parent..setSelection(tree_item_to_select) end |
#populate_tree_node(model_tree_node, parent, tree_properties) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 81 def populate_tree_node(model_tree_node, parent, tree_properties) return if model_tree_node.nil? tree_item = TreeItem.new(parent, SWT::SWTProxy[:none]) observer_registrations = @tree_properties.reduce([]) do |array, key_value_pair| array + [observe(model_tree_node, key_value_pair.last)] end tree_item.set_data('observer_registrations', observer_registrations) tree_item.set_data(model_tree_node) tree_item.text = (model_tree_node && model_tree_node.send(tree_properties[:text])).to_s [model_tree_node && model_tree_node.send(tree_properties[:children])].flatten.to_a.compact.each do |child| populate_tree_node(child, tree_item, tree_properties) end end |
#recursive_depth_first_search(model_tree_node, tree_properties, collection) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 96 def recursive_depth_first_search(model_tree_node, tree_properties, collection) return if model_tree_node.nil? collection << model_tree_node model_tree_node.send(tree_properties[:children]).to_a.each do |child_model_tree_node| recursive_depth_first_search(child_model_tree_node, tree_properties, collection) end end |