Class: Glimmer::DataBinding::TreeItemsBinding

Inherits:
Object
  • Object
show all
Includes:
DataBinding::Observable, DataBinding::Observer
Defined in:
lib/glimmer/data_binding/tree_items_binding.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, model_binding, tree_properties = nil) ⇒ TreeItemsBinding

Returns a new instance of TreeItemsBinding.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 37

def initialize(parent, model_binding, tree_properties = nil)
  @tree = parent
  @model_binding = model_binding
  @tree_properties = @model_binding.binding_options[:tree_properties] || @model_binding.binding_options[:tree_attributes] || [tree_properties].compact.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
  Glimmer::SWT::DisplayProxy.instance.auto_exec(override_sync_exec: @model_binding.binding_options[:sync_exec], override_async_exec: @model_binding.binding_options[:async_exec]) do
    call
    model = model_binding.base_model
    observe(model, model_binding.property_name_expression)
    @tree.on_widget_disposed do |dispose_event|
      unregister_all_observables
    end
  end
end

Instance Method Details

#call(new_value = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 56

def call(new_value=nil)
  Glimmer::SWT::DisplayProxy.instance.auto_exec(override_sync_exec: @model_binding.binding_options[:sync_exec], override_async_exec: @model_binding.binding_options[:async_exec]) do
    @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
end

#populate_tree(model_tree_root_node, parent, tree_properties) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 68

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_models = parent.swt_widget.getSelection.map(&:get_data)
  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.swt_widget.items.each(&:dispose)
  parent.swt_widget.removeAll
  populate_tree_node(model_tree_root_node, parent.swt_widget, tree_properties)
  parent.all_tree_items.each { |ti| ti.setExpanded(!!old_tree_item_expansion_by_data[ti.getData]) }
  selected_tree_items = parent.depth_first_search {|item| selected_tree_item_models.include?(item.get_data) }
  parent.swt_widget.setSelection(selected_tree_items)
end

#populate_tree_node(model_tree_node, parent, tree_properties) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 85

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



100
101
102
103
104
105
106
# File 'lib/glimmer/data_binding/tree_items_binding.rb', line 100

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