Class: Glimmer::SWT::TableProxy

Inherits:
WidgetProxy show all
Includes:
Glimmer
Defined in:
lib/glimmer/swt/table_proxy.rb

Defined Under Namespace

Modules: TableListenerEvent

Constant Summary

Constants included from Glimmer

GUI

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS, WidgetProxy::DEFAULT_STYLES, WidgetProxy::KEYWORD_ALIASES

Instance Attribute Summary collapse

Attributes inherited from WidgetProxy

#drag_source_proxy, #drag_source_style, #drag_source_transfer, #drop_target_proxy, #drop_target_transfer, #finished_add_content, #parent_proxy, #swt_widget

Attributes included from Custom::Drawable

#image_double_buffered, #requires_shape_disposal

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Glimmer

included

Methods inherited from WidgetProxy

#add_observer, #async_exec, #auto_exec, #can_add_observer?, #can_handle_drag_observation_request?, #can_handle_drop_observation_request?, #can_handle_observation_request?, #content, create, #dispose, #disposed?, #ensure_drag_source_proxy, #ensure_drop_target_proxy, #extract_args, #finish_add_content!, flyweight_swt_widget_classes, #get_attribute, #handle_observation_request, #has_attribute?, #has_style?, #height, #method_missing, #pack_same_size, #proxy_source_object, #remove_observer, #respond_to?, #set_attribute, swt_widget_class_for, swt_widget_class_manual_entries, #sync_exec, #timer_exec, underscored_widget_name, widget_exists?, #widget_property_listener_installers, widget_proxy_class, #width, #x, #y

Methods included from Custom::Drawable

#add_shape, #clear_shapes, #deregister_shape_painting, #expanded_shapes, #image_buffered_shapes, #paint_pixel_by_pixel, #setup_shape_painting, #shape_at_location, #shapes, #swt_drawable

Methods included from ProxyProperties

#get_attribute, #has_attribute?, #has_attribute_getter?, #has_attribute_setter?, #method_missing, #proxy_source_object, #respond_to?, #set_attribute

Methods included from Properties

attribute_getter, #attribute_getter, attribute_setter, #attribute_setter, normalized_attribute, #normalized_attribute, ruby_attribute_getter, #ruby_attribute_setter, ruby_attribute_setter

Methods included from Packages

included

Constructor Details

#initialize(underscored_widget_name, parent, args) ⇒ TableProxy

Returns a new instance of TableProxy.



252
253
254
255
256
257
258
259
260
# File 'lib/glimmer/swt/table_proxy.rb', line 252

def initialize(underscored_widget_name, parent, args)
  editable_style = args.delete(:editable)
  super
  @table_editor = TableEditor.new(swt_widget)
  @table_editor.horizontalAlignment = SWTProxy[:left]
  @table_editor.grabHorizontal = true
  @table_editor.minimumHeight = 20
  self.editable = editable_style
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.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def additional_sort_properties
  @additional_sort_properties
end

#column_propertiesObject

Returns the value of attribute column_properties.



249
250
251
# File 'lib/glimmer/swt/table_proxy.rb', line 249

def column_properties
  @column_properties
end

#editableObject Also known as: editable?

Returns the value of attribute editable.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def editable
  @editable
end

#editorObject

Returns the value of attribute editor.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def editor
  @editor
end

#sort_blockObject

Returns the value of attribute sort_block.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def sort_block
  @sort_block
end

#sort_by_blockObject

Returns the value of attribute sort_by_block.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def sort_by_block
  @sort_by_block
end

#sort_directionObject (readonly)

Returns the value of attribute sort_direction.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def sort_direction
  @sort_direction
end

#sort_propertyObject

Returns the value of attribute sort_property.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def sort_property
  @sort_property
end

#sort_typeObject (readonly)

Returns the value of attribute sort_type.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def sort_type
  @sort_type
end

#table_editorObject (readonly)

Returns the value of attribute table_editor.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def table_editor
  @table_editor
end

#table_editor_widget_proxyObject (readonly)

Returns the value of attribute table_editor_widget_proxy.



248
249
250
# File 'lib/glimmer/swt/table_proxy.rb', line 248

def table_editor_widget_proxy
  @table_editor_widget_proxy
end

Class Method Details

.editorsObject



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
# File 'lib/glimmer/swt/table_proxy.rb', line 58

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.minimumHeight = 20
        table_editor_widget_proxy = text(*args) {
          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
          }
        }
        table_editor_widget_proxy.swt_widget.selectAll
        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.minimumHeight = 25
        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.swt_widget.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.minimumHeight = 25
        date(*args) {
          date_time 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
          }
        }
      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.minimumHeight = 25
        date_drop_down(*args) {
          date_time 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
          }
        }
      end,
    },
    time: {
      widget_value_property: :date_time,
      editor_gui: lambda do |args, model, property, table_proxy|
        first_time = true
        table_proxy.table_editor.minimumHeight = 25
        time(*args) {
          date_time 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
          }
        }
      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_listener(underscored_listener_name, &block) ⇒ Object



565
566
567
568
569
570
571
# File 'lib/glimmer/swt/table_proxy.rb', line 565

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

#all_table_itemsObject

Returns all table items including descendants



443
444
445
# File 'lib/glimmer/swt/table_proxy.rb', line 443

def all_table_items
  search
end

#cancel_edit!Object



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

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

#cellsObject



426
427
428
429
430
431
# File 'lib/glimmer/swt/table_proxy.rb', line 426

def cells
  column_count = @table.column_properties.size
  auto_exec do
    swt_widget.items.map {|item| column_count.times.map {|i| item.get_text(i)} }
  end
end

#cells_for(model) ⇒ Object



422
423
424
# File 'lib/glimmer/swt/table_proxy.rb', line 422

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

#column_sort_propertiesObject



321
322
323
324
325
# File 'lib/glimmer/swt/table_proxy.rb', line 321

def column_sort_properties
  column_properties.zip(table_column_proxies.map(&:sort_property)).map do |pair|
    [pair.compact.last].flatten.compact
  end
end

#detect_sort_typeObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/glimmer/swt/table_proxy.rb', line 305

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

#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)


476
477
478
# File 'lib/glimmer/swt/table_proxy.rb', line 476

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)


462
463
464
# File 'lib/glimmer/swt/table_proxy.rb', line 462

def edit_mode?
  !!@edit_mode
end

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



480
481
482
483
484
# File 'lib/glimmer/swt/table_proxy.rb', line 480

def edit_selected_table_item(column_index, before_write: nil, after_write: nil, after_cancel: nil)
  auto_exec do
    edit_table_item(swt_widget.getSelection.first, column_index, before_write: before_write, after_write: after_write, after_cancel: after_cancel)
  end
end

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



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
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
# File 'lib/glimmer/swt/table_proxy.rb', line 486

def edit_table_item(table_item, column_index, before_write: nil, after_write: nil, after_cancel: nil, write_on_cancel: false)
  return if table_item.nil?
  model = table_item.data
  property = column_properties[column_index]
  cancel_edit!
  return unless table_column_proxies[column_index].editable?
  action_taken = false
  @edit_mode = true
  
  editor_config = table_column_proxies[column_index].editor || editor
  editor_config = editor_config.to_a
  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|
    if write_on_cancel
      @finish_edit.call(event)
    else
      @cancel_in_progress = true
      @table_editor_widget_proxy&.swt_widget&.dispose
      @table_editor_widget_proxy = nil
      if after_cancel&.arity == 0
        after_cancel&.call
      else
        after_cancel&.call(table_item)
      end
      @edit_in_progress = false
      @cancel_in_progress = false
      @cancel_edit = nil
      @edit_mode = false
    end
  end
  
  @finish_edit = lambda do |event=nil|
    new_value = @table_editor_widget_proxy&.send(widget_value_property)
    if table_item.isDisposed
      @cancel_edit.call unless write_on_cancel
    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 unless write_on_cancel
      else
        if before_write&.arity == 0
          before_write&.call
        else
          before_write&.call(edited_table_item)
        end
        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.getData == model }.first
        auto_exec do
          swt_widget.showItem(edited_table_item)
        end
        @table_editor_widget_proxy&.swt_widget&.dispose
        @table_editor_widget_proxy = nil
        if after_write&.arity == 0
          after_write&.call
        else
          after_write&.call(edited_table_item)
        end
        @edit_in_progress = false
      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.setEditor(@table_editor_widget_proxy.swt_widget, table_item, column_index)
rescue => e
  Glimmer::Config.logger.error {e.full_message}
  raise e
end

#finish_edit!Object



470
471
472
# File 'lib/glimmer/swt/table_proxy.rb', line 470

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

#initial_sort!Object



376
377
378
# File 'lib/glimmer/swt/table_proxy.rb', line 376

def initial_sort!
  sort_by_column!
end

#itemsObject



262
263
264
265
266
# File 'lib/glimmer/swt/table_proxy.rb', line 262

def items
  auto_exec do
    swt_widget.get_items
  end
end

#model_bindingObject



268
269
270
271
272
# File 'lib/glimmer/swt/table_proxy.rb', line 268

def model_binding
  auto_exec do
    swt_widget.data
  end
end

#post_add_contentObject



451
452
453
454
455
# File 'lib/glimmer/swt/table_proxy.rb', line 451

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

#post_initialize_child(table_column_proxy) ⇒ Object



447
448
449
# File 'lib/glimmer/swt/table_proxy.rb', line 447

def post_initialize_child(table_column_proxy)
  table_column_proxies << table_column_proxy
end

#search(&condition) ⇒ Object

Performs a search for table items matching block condition If no condition block is passed, returns all table items Returns a Java TableItem array to easily set as selection on org.eclipse.swt.Table if needed



436
437
438
439
440
# File 'lib/glimmer/swt/table_proxy.rb', line 436

def search(&condition)
  auto_exec do
    swt_widget.getItems.select {|item| condition.nil? || condition.call(item)}.to_java(TableItem)
  end
end

#sort!(internal_sort: false) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/glimmer/swt/table_proxy.rb', line 384

def sort!(internal_sort: false)
  return unless sort_property && (sort_type || sort_block || sort_by_block)
  original_array = 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
  if model_binding.binding_options.symbolize_keys[:read_only_sort]
    table_items_binding.call(sorted_array, internal_sort: true) unless internal_sort
  else
    model_binding.call(sorted_array)
  end
  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.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/glimmer/swt/table_proxy.rb', line 328

def sort_by_column!(table_column_proxy=nil)
  index = nil
  auto_exec do
    index = swt_widget.columns.to_a.index(table_column_proxy.swt_widget) unless table_column_proxy.nil?
  end
  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 = table_column_proxies[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
  
  @sort_direction = @sort_direction.nil? || @sort_property.first != new_sort_property.first || @sort_direction == :descending ? :ascending : :descending
  auto_exec do
    swt_widget.sort_direction = @sort_direction == :ascending ? SWTProxy[:up] : SWTProxy[:down]
  end
  
  @sort_property = [new_sort_property].flatten.compact
  table_column_index = column_properties.index(new_sort_property.to_s.to_sym)
  table_column_proxy ||= table_column_proxies[table_column_index] if table_column_index
  auto_exec do
    swt_widget.sort_column = table_column_proxy.swt_widget if table_column_proxy
  end
          
  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

#table_column_proxiesObject



457
458
459
# File 'lib/glimmer/swt/table_proxy.rb', line 457

def table_column_proxies
  @table_column_proxies ||= []
end

#table_items_bindingObject



274
275
276
277
278
# File 'lib/glimmer/swt/table_proxy.rb', line 274

def table_items_binding
  auto_exec do
    swt_widget.get_data('table_items_binding')
  end
end