Class: Glimmer::SWT::TableProxy

Inherits:
CompositeProxy show all
Extended by:
Glimmer
Defined in:
lib/glimmer/swt/table_proxy.rb

Constant Summary collapse

STYLE =
<<~CSS
  table {
    border-spacing: 0;
  }
    
  table tr th,td {
    cursor: default;
  }
CSS

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS, WidgetProxy::JS_KEY_CODE_TO_SWT_KEY_CODE_MAP, WidgetProxy::JS_LOCATION_TO_SWT_KEY_LOCATION_MAP, WidgetProxy::SWT_CURSOR_TO_CSS_CURSOR_MAP

Instance Attribute Summary collapse

Attributes inherited from WidgetProxy

#args, #background, #children, #cursor, #disposed?, #enabled, #focus, #font, #foreground, #menu, #menu_requested, #menu_x, #menu_y, #parent, #path, #rendered

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CompositeProxy

#background_image, #background_image=, #get_layout, #layout=, #layout​, #pack

Methods inherited from WidgetProxy

#add_content_on_render, #add_css_class, #add_css_classes, #add_observer, #apply_property_type_converters, #attach, #build_dom, #can_handle_observation_request?, #clear_css_classes, #content, #content_on_render_blocks, #css_classes, #default_observation_request_to_event_mapping, #dialog_ancestor, #dispose, #dom_element, #effective_observation_request_to_event_mapping, #event_handling_suspended?, #event_listener_proxies, for, #handle_javascript_observation_request, #handle_observation_request, #has_style?, #id, #id=, #listener_dom_element, #listener_path, #listeners, #listeners_for, max_id_number_for, max_id_numbers, #method_missing, #name, next_id_number_for, #observation_requests, #parent_dom_element, #parent_path, #parents, #print, #reattach, #remove_all_listeners, #remove_css_class, #remove_css_classes, #remove_event_listener_proxies, #render, reset_max_id_numbers!, #resume_event_handling, #set_attribute, #set_data, #set_focus, #shell, #skip_content_on_render_blocks?, #style_element, #suspend_event_handling, #swt_data, #swt_widget, underscored_widget_name, widget_class, widget_exists?, widget_handling_listener, #widget_property_listener_installers

Methods included from PropertyOwner

#attribute_getter, #attribute_setter, #get_attribute, #set_attribute

Constructor Details

#initialize(parent, args, block) ⇒ TableProxy

Returns a new instance of TableProxy.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/glimmer/swt/table_proxy.rb', line 252

def initialize(parent, args, block)
  super(parent, args, block)
  @columns = []
  @children = []
  @editors = []
  @selection = []
  @table_editor = TableEditor.new(self)
  @table_editor.horizontalAlignment = SWTProxy[:left]
  @table_editor.grabHorizontal = true
  @table_editor.minimumWidth = 90
  @table_editor.minimumHeight = 20
  if editable?
    add_editable_event_listener
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::SWT::WidgetProxy

Instance Attribute Details

#additional_sort_propertiesObject

Returns the value of attribute additional_sort_properties.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def additional_sort_properties
  @additional_sort_properties
end

#column_propertiesObject

Returns the value of attribute column_properties.



43
44
45
# File 'lib/glimmer/swt/table_proxy.rb', line 43

def column_properties
  @column_properties
end

#columnsObject (readonly)

Returns the value of attribute columns.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def columns
  @columns
end

#dataObject Also known as: model_binding

Returns the value of attribute data.



43
44
45
# File 'lib/glimmer/swt/table_proxy.rb', line 43

def data
  @data
end

#editorObject

Returns the value of attribute editor.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def editor
  @editor
end

#item_countObject

Returns the value of attribute item_count.



43
44
45
# File 'lib/glimmer/swt/table_proxy.rb', line 43

def item_count
  @item_count
end

#selectionObject

Returns the value of attribute selection.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def selection
  @selection
end

#sort_blockObject

Returns the value of attribute sort_block.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def sort_block
  @sort_block
end

#sort_by_blockObject

Returns the value of attribute sort_by_block.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def sort_by_block
  @sort_by_block
end

#sort_columnObject (readonly)

Returns the value of attribute sort_column.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def sort_column
  @sort_column
end

#sort_propertyObject

Returns the value of attribute sort_property.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def sort_property
  @sort_property
end

#sort_typeObject (readonly)

Returns the value of attribute sort_type.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def sort_type
  @sort_type
end

#table_editorObject (readonly)

Returns the value of attribute table_editor.



40
41
42
# File 'lib/glimmer/swt/table_proxy.rb', line 40

def table_editor
  @table_editor
end

Class Method Details

.editorsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
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
108
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/glimmer/swt/table_proxy.rb', line 50

def editors
  @editors ||= {
    # ensure editor can work with string keys not just symbols (leave one string in for testing)
    text: {
      widget_value_property: :text,
      editor_gui: lambda do |args, model, property, table_proxy|
        table_proxy.table_editor.minimumWidth = 90
        table_proxy.table_editor.minimumHeight = 10
        table_editor_widget_proxy = text(*args) {
          text model.send(property)
          focus true
          on_focus_lost {
            table_proxy.finish_edit!
          }
          on_modify_text do |event|
            # No Op, just record @text changes on key up
            # TODO find a better solution than this in the future
          end
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
#                 table_editor_widget_proxy.swt_widget.selectAll # TODO select all
        table_editor_widget_proxy
      end,
    },
    combo: {
      widget_value_property: :text,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumWidth = 90
        table_proxy.table_editor.minimumHeight = 18
        table_editor_widget_proxy = combo(*args) {
          items model.send("#{property}_options")
          text model.send(property)
          focus true
          on_focus_lost {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
          on_widget_selected {
            if !OS.windows? || !first_time || first_time && model.send(property) != table_editor_widget_proxy.text
              table_proxy.finish_edit!
            end
          }
        }
        table_editor_widget_proxy
      end,
    },
    checkbox: {
      widget_value_property: :selection,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumHeight = 25
        checkbox(*args) {
          selection model.send(property)
          focus true
          on_widget_selected {
            table_proxy.finish_edit!
          }
          on_focus_lost {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
      end,
    },
    date: {
      widget_value_property: :date_time,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumWidth = 90
        table_proxy.table_editor.minimumHeight = 15
        date(*args) {
          date_time model.send(property)
          focus true
          on_widget_selected {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
      end,
    },
    date_drop_down: {
      widget_value_property: :date_time,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumWidth = 80
        table_proxy.table_editor.minimumHeight = 15
        date_drop_down(*args) {
          date_time model.send(property)
          focus true
          on_widget_selected {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
      end,
    },
    time: {
      widget_value_property: :date_time,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumWidth = 80
        table_proxy.table_editor.minimumHeight = 15
        time(*args) {
          date_time model.send(property)
          focus true
          on_widget_selected {
            table_proxy.finish_edit!
          }
          on_focus_lost {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
      end,
    },
    radio: {
      widget_value_property: :selection,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumHeight = 25
        radio(*args) {
          selection model.send(property)
          focus true
          on_widget_selected {
            table_proxy.finish_edit!
          }
          on_focus_lost {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
      end,
    },
    spinner: {
      widget_value_property: :selection,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumHeight = 25
        table_editor_widget_proxy = spinner(*args) {
          selection model.send(property)
          focus true
          on_focus_lost {
            table_proxy.finish_edit!
          }
          on_key_pressed { |key_event|
            if key_event.keyCode == swt(:cr)
              table_proxy.finish_edit!
            elsif key_event.keyCode == swt(:esc)
              table_proxy.cancel_edit!
            end
          }
        }
        table_editor_widget_proxy
      end,
    },
  }
end

Instance Method Details

#add_editable_event_listenerObject



328
329
330
331
332
# File 'lib/glimmer/swt/table_proxy.rb', line 328

def add_editable_event_listener
  @editable_on_mouse_up_event_listener = on_mouse_up { |event|
    edit_table_item(event.table_item, event.column_index) if editable?
  }
end

#add_listener(underscored_listener_name, &block) ⇒ Object



606
607
608
609
610
611
612
# File 'lib/glimmer/swt/table_proxy.rb', line 606

def add_listener(underscored_listener_name, &block)
  enhanced_block = lambda do |event|
    event.extend(TableListenerEvent)
    block.call(event)
  end
  super(underscored_listener_name, &enhanced_block)
end

#cancel_edit!Object



511
512
513
# File 'lib/glimmer/swt/table_proxy.rb', line 511

def cancel_edit!
  @cancel_edit&.call if @edit_mode
end

#cells_for(model) ⇒ Object



356
357
358
# File 'lib/glimmer/swt/table_proxy.rb', line 356

def cells_for(model)
  column_properties.map {|property| model.send(property)}
end

#column_sort_propertiesObject



408
409
410
411
412
# File 'lib/glimmer/swt/table_proxy.rb', line 408

def column_sort_properties
  column_properties.zip(columns.map(&:sort_property)).map do |pair|
    pair.compact.last.to_collection
  end
end

#columns_domObject



703
704
705
706
# File 'lib/glimmer/swt/table_proxy.rb', line 703

def columns_dom
  tr {
  }
end

#columns_dom_elementObject



691
692
693
# File 'lib/glimmer/swt/table_proxy.rb', line 691

def columns_dom_element
  Document.find(columns_path)
end

#columns_pathObject



687
688
689
# File 'lib/glimmer/swt/table_proxy.rb', line 687

def columns_path
  path + ' thead tr'
end

#default_layoutObject



298
299
300
# File 'lib/glimmer/swt/table_proxy.rb', line 298

def default_layout
  nil
end

#detect_sort_typeObject



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/glimmer/swt/table_proxy.rb', line 392

def detect_sort_type
  @sort_type = sort_property.size.times.map { String }
  array = model_binding.evaluate_property
  sort_property.each_with_index do |a_sort_property, i|
    values = array.map { |object| object.send(a_sort_property) }
    value_classes = values.map(&:class).uniq
    if value_classes.size == 1
      @sort_type[i] = value_classes.first
    elsif value_classes.include?(Integer)
      @sort_type[i] = Integer
    elsif value_classes.include?(Float)
      @sort_type[i] = Float
    end
  end
end

#domObject



723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/glimmer/swt/table_proxy.rb', line 723

def dom
  table_id = id
  table_id_style = css
  table_id_css_classes = css_classes
  table_id_css_classes << 'table' unless table_id_css_classes.include?('table')
  table_id_css_classes << 'editable' if editable? && !table_id_css_classes.include?('editable')
  table_id_css_classes_string = table_id_css_classes.to_a.join(' ')
  @dom ||= html {
    table(id: table_id, style: table_id_style, class: table_id_css_classes_string) {
      thead_dom
      items_dom
    }
  }.to_s
end

#edit_in_progress?Boolean

Indicates if table is editing a table item because the user hit ENTER or focused out after making a change in edit mode to a table item cell. It is set to false once change is saved to model

Returns:

  • (Boolean)


521
522
523
# File 'lib/glimmer/swt/table_proxy.rb', line 521

def edit_in_progress?
  !!@edit_in_progress
end

#edit_mode?Boolean

Indicates if table is in edit mode, thus displaying a text widget for a table item cell

Returns:

  • (Boolean)


507
508
509
# File 'lib/glimmer/swt/table_proxy.rb', line 507

def edit_mode?
  !!@edit_mode
end

#edit_selected_table_item(column_index, before_write: nil, after_write: nil, after_cancel: nil) ⇒ Object



525
526
527
# File 'lib/glimmer/swt/table_proxy.rb', line 525

def edit_selected_table_item(column_index, before_write: nil, after_write: nil, after_cancel: nil)
  edit_table_item(selection.first, column_index, before_write: before_write, after_write: after_write, after_cancel: after_cancel)
end

#edit_table_item(table_item, column_index, before_write: nil, after_write: nil, after_cancel: nil) ⇒ Object

def edit_table_item(table_item, column_index)

  table_item&.edit(column_index) unless column_index.nil?
end


534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/glimmer/swt/table_proxy.rb', line 534

def edit_table_item(table_item, column_index, before_write: nil, after_write: nil, after_cancel: nil)
  return if table_item.nil? || (@edit_mode && @edit_table_item == table_item && @edit_column_index == column_index)
  @edit_column_index = column_index
  @edit_table_item = table_item
  column_index = column_index.to_i
  model = table_item.data
  property = column_properties[column_index]
  cancel_edit!
  return unless columns[column_index].editable?
  action_taken = false
  @edit_mode = true
  
  editor_config = columns[column_index].editor || editor
  editor_config = editor_config.to_collection
  editor_widget_options = editor_config.last.is_a?(Hash) ? editor_config.last : {}
  editor_widget_arg_last_index = editor_config.last.is_a?(Hash) ? -2 : -1
  editor_widget = (editor_config[0] || :text).to_sym
  editor_widget_args = editor_config[1..editor_widget_arg_last_index]
  model_editing_property = editor_widget_options[:property] || property
  widget_value_property = TableProxy::editors.symbolize_keys[editor_widget][:widget_value_property]
  
  @cancel_edit = lambda do |event=nil|
    @cancel_in_progress = true
    @table_editor.cancel!
    @table_editor_widget_proxy&.dispose
    @table_editor_widget_proxy = nil
    after_cancel&.call
    @edit_in_progress = false
    @cancel_in_progress = false
    @cancel_edit = nil
    @edit_table_item = @edit_column_index = nil if (@edit_mode && @edit_table_item == table_item && @edit_column_index == column_index)
    @edit_mode = false
  end
  
  @finish_edit = lambda do |event=nil|
    new_value = @table_editor_widget_proxy&.send(widget_value_property)
    if table_item.disposed?
      @cancel_edit.call
    elsif !new_value.nil? && !action_taken && !@edit_in_progress && !@cancel_in_progress
      action_taken = true
      @edit_in_progress = true
      if new_value == model.send(model_editing_property)
        @cancel_edit.call
      else
        before_write&.call
        @table_editor.save!(widget_value_property: widget_value_property)
        model.send("#{model_editing_property}=", new_value) # makes table update itself, so must search for selected table item again
        # Table refresh happens here because of model update triggering observers, so must retrieve table item again
        edited_table_item = search { |ti| ti.data == model }.first
        show_item(edited_table_item)
        @table_editor_widget_proxy&.dispose
        @table_editor_widget_proxy = nil
        after_write&.call(edited_table_item)
        @edit_in_progress = false
        @edit_table_item = @edit_column_index = nil
      end
    end
  end

  content {
    @table_editor_widget_proxy = TableProxy::editors.symbolize_keys[editor_widget][:editor_gui].call(editor_widget_args, model, model_editing_property, self)
  }
  @table_editor.set_editor(@table_editor_widget_proxy, table_item, column_index)
rescue => e
  Glimmer::Config.logger.error {e.full_message}
  raise e
end

#editable=(value) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/glimmer/swt/table_proxy.rb', line 316

def editable=(value)
  if value
    args.push(:editable)
    dom_element.addClass('editable')
    add_editable_event_listener
  else
    args.delete(:editable)
    dom_element.removeClass('editable')
    @editable_on_mouse_up_event_listener.deregister # TODO see why table event listener deregistration is not working
  end
end

#editable?Boolean Also known as: editable

Returns:

  • (Boolean)


311
312
313
# File 'lib/glimmer/swt/table_proxy.rb', line 311

def editable?
  args.include?(:editable)
end

#elementObject



683
684
685
# File 'lib/glimmer/swt/table_proxy.rb', line 683

def element
  'table'
end

#finish_edit!Object



515
516
517
# File 'lib/glimmer/swt/table_proxy.rb', line 515

def finish_edit!
  @finish_edit&.call if @edit_mode
end

#get_data(key = nil) ⇒ Object



302
303
304
# File 'lib/glimmer/swt/table_proxy.rb', line 302

def get_data(key=nil)
  data
end

#header_visibleObject



624
625
626
# File 'lib/glimmer/swt/table_proxy.rb', line 624

def header_visible
  @header_visible
end

#header_visible=(value) ⇒ Object



615
616
617
618
619
620
621
622
# File 'lib/glimmer/swt/table_proxy.rb', line 615

def header_visible=(value)
  @header_visible = value
  if @header_visible
    thead_dom_element.remove_class('hide')
  else
    thead_dom_element.add_class('hide')
  end
end

#index_of(item) ⇒ Object



364
365
366
# File 'lib/glimmer/swt/table_proxy.rb', line 364

def index_of(item)
  items.index(item)
end

#initial_sort!Object



465
466
467
# File 'lib/glimmer/swt/table_proxy.rb', line 465

def initial_sort!
  sort_by_column!
end

#items=(new_items) ⇒ Object



345
346
347
348
349
# File 'lib/glimmer/swt/table_proxy.rb', line 345

def items=(new_items)
  @children = new_items
  # TODO optimize in the future by sorting elements in DOM directly when no change to elements occur other than sort
  redraw
end

#items_domObject



718
719
720
721
# File 'lib/glimmer/swt/table_proxy.rb', line 718

def items_dom
  tbody {
  }
end

#items_dom_elementObject



699
700
701
# File 'lib/glimmer/swt/table_proxy.rb', line 699

def items_dom_element
  Document.find(items_path)
end

#items_pathObject



695
696
697
# File 'lib/glimmer/swt/table_proxy.rb', line 695

def items_path
  path + ' tbody'
end

#observation_request_to_event_mappingObject



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/glimmer/swt/table_proxy.rb', line 632

def observation_request_to_event_mapping
  mouse_handler = -> (event_listener) {
    -> (event) {
      event.singleton_class.send(:define_method, :table_item=) do |item|
        @table_item = item
      end
      event.singleton_class.send(:define_method, :table_item) do
        @table_item
      end
      table_row = event.target.parents('tr').first
      table_data = event.target.parents('td').first
      event.table_item = items.detect {|item| item.id == table_row.attr('id')}
      event.singleton_class.send(:define_method, :column_index) do
        (table_data || event.target).attr('data-column-index')
      end
      
      event_listener.call(event) unless event.table_item.nil? && event.column_index.nil?
    }
  }

  {
    'on_mouse_down' => {
      event: 'mousedown',
      event_handler: mouse_handler,
    },
    'on_mouse_up' => {
      event: 'mouseup',
      event_handler: mouse_handler,
    },
    'on_widget_selected' => {
      event: 'mouseup',
      event_handler: mouse_handler,
    },
  }
end

#post_add_contentObject



292
293
294
295
296
# File 'lib/glimmer/swt/table_proxy.rb', line 292

def post_add_content
  return if @initially_sorted
  initial_sort!
  @initially_sorted = true
end

#post_dispose_child(child) ⇒ Object

Executes for the parent of a child that just got disposed



282
283
284
285
286
287
288
289
290
# File 'lib/glimmer/swt/table_proxy.rb', line 282

def post_dispose_child(child)
  if child.is_a?(TableColumnProxy)
    @columns&.delete(child)
  elsif child.is_a?(TableItemProxy)
    @children&.delete(child)
  else
    @editors&.delete(child)
  end
end

#post_initialize_child(child) ⇒ Object

Only table_columns may be added as children



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/glimmer/swt/table_proxy.rb', line 269

def post_initialize_child(child)
  if child.is_a?(TableColumnProxy)
    @columns << child
    child.render
  elsif child.is_a?(TableItemProxy)
    @children << child
    child.render
  else
    @editors << child
  end
end

#redrawObject



668
669
670
671
672
# File 'lib/glimmer/swt/table_proxy.rb', line 668

def redraw
  super()
  @columns.to_a.each(&:redraw)
  redraw_empty_items
end

#redraw_empty_itemsObject



674
675
676
677
678
679
680
681
# File 'lib/glimmer/swt/table_proxy.rb', line 674

def redraw_empty_items
  if @children&.size.to_i < item_count.to_i
    item_count.to_i.times do
      empty_columns = column_properties&.size.to_i.times.map { |i| "<td data-column-index='#{i}'></td>" }
      items_dom_element.append("<tr class='table-item empty-table-item'>#{empty_columns}</tr>")
    end
  end
end

#remove_allObject



306
307
308
309
# File 'lib/glimmer/swt/table_proxy.rb', line 306

def remove_all
  items.clear
  redraw
end

#search(&condition) ⇒ Object



360
361
362
# File 'lib/glimmer/swt/table_proxy.rb', line 360

def search(&condition)
  items.select {|item| condition.nil? || condition.call(item)}
end

#select(index, meta = false) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
# File 'lib/glimmer/swt/table_proxy.rb', line 368

def select(index, meta = false)
  new_selection = @selection.clone
  selected_item = items[index]
  if @selection.include?(selected_item)
    new_selection.delete(selected_item) if meta
  else
    new_selection = [] if !meta || (!has_style?(:multi) && @selection.to_a.size >= 1)
    new_selection << selected_item
  end
  self.selection = new_selection
end

#selectorObject



628
629
630
# File 'lib/glimmer/swt/table_proxy.rb', line 628

def selector
  super + ' tbody'
end

#show_item(table_item) ⇒ Object



602
603
604
# File 'lib/glimmer/swt/table_proxy.rb', line 602

def show_item(table_item)
  table_item.dom_element.focus
end

#sort!Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/glimmer/swt/table_proxy.rb', line 469

def sort!
  return unless sort_property && (sort_type || sort_block || sort_by_block)
  array = model_binding.evaluate_property
  array = array.sort_by(&:hash) # this ensures consistent subsequent sorting in case there are equivalent sorts to avoid an infinite loop
  # Converting value to_s first to handle nil cases. Should work with numeric, boolean, and date fields
  if sort_block
    sorted_array = array.sort(&sort_block)
  elsif sort_by_block
    sorted_array = array.sort_by(&sort_by_block)
  else
    sorted_array = array.sort_by do |object|
      sort_property.each_with_index.map do |a_sort_property, i|
        value = object.send(a_sort_property)
        # handle nil and difficult to compare types gracefully
        if sort_type[i] == Integer
          value = value.to_i
        elsif sort_type[i] == Float
          value = value.to_f
        elsif sort_type[i] == String
          value = value.to_s
        end
        value
      end
    end
  end
  sorted_array = sorted_array.reverse if @sort_direction == :descending
  model_binding.call(sorted_array)
end

#sort_by_column!(table_column_proxy = nil) ⇒ Object

Sorts by specified TableColumnProxy object. If nil, it uses the table default sort instead.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/glimmer/swt/table_proxy.rb', line 423

def sort_by_column!(table_column_proxy=nil)
  index = columns.to_a.index(table_column_proxy) unless table_column_proxy.nil?
  new_sort_property = table_column_proxy.nil? ? @sort_property : table_column_proxy.sort_property || [column_properties[index]]
  
  return if table_column_proxy.nil? && new_sort_property.nil? && @sort_block.nil? && @sort_by_block.nil?
  if new_sort_property && table_column_proxy.nil? && new_sort_property.size == 1 && (index = column_sort_properties.index(new_sort_property))
    table_column_proxy = columns[index]
  end
  if new_sort_property && new_sort_property.size == 1 && !additional_sort_properties.to_a.empty?
    selected_additional_sort_properties = additional_sort_properties.clone
    if selected_additional_sort_properties.include?(new_sort_property.first)
      selected_additional_sort_properties.delete(new_sort_property.first)
      new_sort_property += selected_additional_sort_properties
    else
      new_sort_property += additional_sort_properties
    end
  end
  
  new_sort_property = new_sort_property.to_collection unless new_sort_property.is_a?(Array)
  @sort_direction = @sort_direction.nil? || @sort_property.first != new_sort_property.first || @sort_direction == :descending ? :ascending : :descending
  
  @sort_property = new_sort_property
  table_column_index = column_properties.index(new_sort_property.to_s.to_sym)
  table_column_proxy ||= columns[table_column_index] if table_column_index
  @sort_column = table_column_proxy if table_column_proxy
          
  if table_column_proxy
    @sort_by_block = nil
    @sort_block = nil
  end
  @sort_type = nil
  if table_column_proxy&.sort_by_block
    @sort_by_block = table_column_proxy.sort_by_block
  elsif table_column_proxy&.sort_block
    @sort_block = table_column_proxy.sort_block
  else
    detect_sort_type
  end
          
  sort!
end

#sort_directionObject



414
415
416
# File 'lib/glimmer/swt/table_proxy.rb', line 414

def sort_direction
  @sort_direction == :ascending ? SWTProxy[:up] : SWTProxy[:down]
end

#sort_direction=(value) ⇒ Object



418
419
420
# File 'lib/glimmer/swt/table_proxy.rb', line 418

def sort_direction=(value)
  @sort_direction = value == SWTProxy[:up] ? :ascending : :descending
end

#thead_domObject



708
709
710
711
712
# File 'lib/glimmer/swt/table_proxy.rb', line 708

def thead_dom
  thead {
    columns_dom
  }
end

#thead_dom_elementObject



714
715
716
# File 'lib/glimmer/swt/table_proxy.rb', line 714

def thead_dom_element
  dom_element.find('thead')
end