Class: Glimmer::DataBinding::TableItemsBinding
- Inherits:
-
Object
- Object
- Glimmer::DataBinding::TableItemsBinding
- Includes:
- DataBinding::Observable, DataBinding::Observer
- Defined in:
- lib/glimmer/data_binding/table_items_binding.rb
Constant Summary collapse
- TABLE_ITEM_PROPERTIES =
%w[background foreground font image]
Instance Method Summary collapse
- #call(*args) ⇒ Object
-
#initialize(parent, model_binding, column_properties = nil) ⇒ TableItemsBinding
constructor
A new instance of TableItemsBinding.
- #model_attribute_values_for_index(model_attribute_values, index) ⇒ Object
- #model_collection_attribute_values(model_collection) ⇒ Object
- #populate_table(model_collection, parent, column_properties, internal_sort: false) ⇒ Object
- #same_model_collection?(new_model_collection) ⇒ Boolean
- #same_model_collection_with_different_sort?(new_model_collection) ⇒ Boolean
- #same_table_data?(new_model_collection) ⇒ Boolean
- #table_item_model_collection ⇒ Object
- #update_table_item_properties_from_model(table_item, row_index, column_index, model, model_attribute) ⇒ Object
Constructor Details
#initialize(parent, model_binding, column_properties = nil) ⇒ TableItemsBinding
Returns a new instance of TableItemsBinding.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 41 def initialize(parent, model_binding, column_properties = nil) @table = parent @model_binding = model_binding @read_only_sort = @model_binding.[:read_only_sort] @column_properties = @model_binding.[:column_properties] || @model_binding.[:column_attributes] || column_properties @table.editable = false if @model_binding.[:read_only] if @table.respond_to?(:column_properties=) @table.column_properties = @column_properties else # assume custom widget @table.body_root.column_properties = @column_properties end Glimmer::SWT::DisplayProxy.instance.auto_exec(override_sync_exec: @model_binding.[:sync_exec], override_async_exec: @model_binding.[:async_exec]) do @table..data = @model_binding @table..set_data('table_items_binding', self) @table. do |dispose_event| unregister_all_observables end @table_observer_registration = observe(model_binding) call end end |
Instance Method Details
#call(*args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 63 def call(*args) = args.last.is_a?(Hash) ? args.pop : {} internal_sort = [:internal_sort] || false new_model_collection = args.first Glimmer::SWT::DisplayProxy.instance.auto_exec(override_sync_exec: @model_binding.[:sync_exec], override_async_exec: @model_binding.[:async_exec]) do new_model_collection = model_binding_evaluated_property = @model_binding.evaluate_property unless internal_sort # this ensures applying converters (e.g. :on_read) return if same_table_data?(new_model_collection) if same_model_collection?(new_model_collection) new_model_collection_attribute_values = model_collection_attribute_values(new_model_collection) @table..items.each_with_index do |table_item, row_index| next if new_model_collection_attribute_values[row_index] == @last_model_collection_attribute_values[row_index] model = table_item.get_data (0..(@column_properties.size-1)).each do |column_index| new_model_attribute_values_for_index = model_attribute_values_for_index(new_model_collection_attribute_values[row_index], column_index) last_model_attribute_values_for_index = model_attribute_values_for_index(@last_model_collection_attribute_values[row_index], column_index) next if new_model_attribute_values_for_index == last_model_attribute_values_for_index model_attribute = @column_properties[column_index] update_table_item_properties_from_model(table_item, row_index, column_index, model, model_attribute) end end @last_model_collection_attribute_values = new_model_collection_attribute_values else if new_model_collection and new_model_collection.is_a?(Array) remove_dependent(@table_observer_registration => @table_items_observer_registration) if @table_items_observer_registration @table_items_observer_registration&.unobserve # TODO observe and update table items piecemeal per model instead of passing @column_properties # TODO ensure unobserving models when they are no longer part of the table @table_items_observer_registration = observe(new_model_collection, @column_properties) add_dependent(@table_observer_registration => @table_items_observer_registration) @table_items_property_observer_registration ||= {} if !same_model_collection_with_different_sort?(new_model_collection) TABLE_ITEM_PROPERTIES.each do |table_item_property| remove_dependent(@table_observer_registration => @table_items_property_observer_registration[table_item_property]) if @table_items_property_observer_registration[table_item_property] @table_items_property_observer_registration[table_item_property]&.unobserve property_properties = @column_properties.map {|property| "#{property}_#{table_item_property}" } @table_items_property_observer_registration[table_item_property] = observe(new_model_collection, property_properties) add_dependent(@table_observer_registration => @table_items_property_observer_registration[table_item_property]) end end @model_collection = new_model_collection end populate_table(@model_collection, @table, @column_properties, internal_sort: internal_sort) end end end |
#model_attribute_values_for_index(model_attribute_values, index) ⇒ Object
214 215 216 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 214 def model_attribute_values_for_index(model_attribute_values, index) model_attribute_values.map {|attribute_values| attribute_values[index]} end |
#model_collection_attribute_values(model_collection) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 206 def model_collection_attribute_values(model_collection) model_collection.map do |model| (["text"] + TABLE_ITEM_PROPERTIES).map do |table_item_property| @table.cells_for(model, table_item_property: table_item_property) end end end |
#populate_table(model_collection, parent, column_properties, internal_sort: false) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 109 def populate_table(model_collection, parent, column_properties, internal_sort: false) selected_table_item_models = parent..getSelection.map(&:get_data) parent.finish_edit! dispose_start_index = @last_model_collection_attribute_values && (model_collection.count < @last_model_collection_attribute_values.count) && (@last_model_collection_attribute_values.count - (@last_model_collection_attribute_values.count - model_collection.count)) if dispose_start_index table_items_to_dispose = parent..items[dispose_start_index..-1] parent..remove(dispose_start_index, (@last_model_collection_attribute_values.count-1)) table_items_to_dispose.each(&:dispose) end model_collection.each_with_index do |model, row_index| table_item_exists = @last_model_collection_attribute_values && @last_model_collection_attribute_values.count > 0 && row_index < @last_model_collection_attribute_values.count table_item = table_item_exists ? parent..items[row_index] : TableItem.new(parent., SWT::SWTProxy[:none]) (0..(column_properties.size-1)).each do |column_index| model_attribute = column_properties[column_index] update_table_item_properties_from_model(table_item, row_index, column_index, model, model_attribute) end table_item.set_data(model) end @last_model_collection_attribute_values = model_collection_attribute_values(model_collection) selected_table_items = parent.search {|item| selected_table_item_models.include?(item.get_data) } parent..setSelection(selected_table_items) sorted_model_collection = parent.sort!(internal_sort: internal_sort) call(sorted_model_collection, internal_sort: true) if @read_only_sort && !internal_sort && !sorted_model_collection.nil? parent..redraw if parent&.&.respond_to?(:redraw) end |
#same_model_collection?(new_model_collection) ⇒ Boolean
194 195 196 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 194 def same_model_collection?(new_model_collection) new_model_collection == table_item_model_collection end |
#same_model_collection_with_different_sort?(new_model_collection) ⇒ Boolean
198 199 200 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 198 def same_model_collection_with_different_sort?(new_model_collection) Set.new(new_model_collection) == Set.new(table_item_model_collection) end |
#same_table_data?(new_model_collection) ⇒ Boolean
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 162 def same_table_data?(new_model_collection) (["text"] + TABLE_ITEM_PROPERTIES).all? do |table_item_property| table_cells = @table..items.map do |item| model = item.get_data @table.column_properties.each_with_index.map do |column_property, i| model_attribute = "#{column_property}" model_attribute = "#{model_attribute}_#{table_item_property}" if TABLE_ITEM_PROPERTIES.include?(table_item_property) item.send("get_#{table_item_property}", i) if model.respond_to?(model_attribute) end end model_cells = new_model_collection.to_a.map do |m| @table.cells_for(m, table_item_property: table_item_property) end model_cells = model_cells.map do |row| row.map do |model_cell| if model_cell if %w[background foreground].include?(table_item_property.to_s) Glimmer::SWT::ColorProxy.create(*model_cell).swt_color elsif table_item_property.to_s == 'font' Glimmer::SWT::FontProxy.create(model_cell).swt_font elsif table_item_property.to_s == 'image' Glimmer::SWT::ImageProxy.create(*model_cell).swt_image else model_cell end end end end table_cells == model_cells end end |
#table_item_model_collection ⇒ Object
202 203 204 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 202 def table_item_model_collection @table..items.map(&:get_data) end |
#update_table_item_properties_from_model(table_item, row_index, column_index, model, model_attribute) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 139 def update_table_item_properties_from_model(table_item, row_index, column_index, model, model_attribute) Glimmer::SWT::DisplayProxy.instance.sync_exec do old_table_item_values = @last_model_collection_attribute_values && @last_model_collection_attribute_values[row_index] && model_attribute_values_for_index(@last_model_collection_attribute_values[row_index], column_index) text_value = model.send(model_attribute).to_s old_table_item_value = old_table_item_values && old_table_item_values[0] table_item.set_text(column_index, text_value) if old_table_item_value.nil? || text_value != old_table_item_value TABLE_ITEM_PROPERTIES.each do |table_item_property| if model.respond_to?("#{model_attribute}_#{table_item_property}") table_item_value = model.send("#{model_attribute}_#{table_item_property}") old_table_item_value = old_table_item_values && old_table_item_values[1 + TABLE_ITEM_PROPERTIES.index(table_item_property)] if old_table_item_value.nil? || (table_item_value != old_table_item_value) table_item_value = Glimmer::SWT::ColorProxy.create(*table_item_value).swt_color if table_item_value && %w[background foreground].include?(table_item_property.to_s) table_item_value = Glimmer::SWT::FontProxy.create(table_item_value).swt_font if table_item_value && table_item_property.to_s == 'font' table_item_value = Glimmer::SWT::ImageProxy.create(*table_item_value).swt_image if table_item_value && table_item_property.to_s == 'image' table_item.send("set_#{table_item_property}", column_index, table_item_value) end end end end end |