Class: Clevic::TagDelegate

Inherits:
Delegate show all
Defined in:
lib/clevic/swing/tag_delegate.rb

Overview

Delegate for doing simple multi-value fields. Tags, basically.

Instance Attribute Summary collapse

Attributes inherited from Delegate

#entity, #field, #parent

Instance Method Summary collapse

Methods inherited from Delegate

#attribute, #editorEvent, #entity_class, #initialize, #inspect, #is_combo?, #minimal_edit, #native, #show_message, #updateEditorGeometry

Methods included from FieldValuer

#attribute_value, #attribute_value=, #display_value, #edit_value, #edit_value=, #find_related, #raw_value, #text_value=, #tooltip, valuer, #valuer, #writer

Constructor Details

This class inherits a constructor from Clevic::Delegate

Instance Attribute Details

#editorObject (readonly)

Return the GUI component / widget that is displayed when editing. Usually this will be a combo box widget, but it can be a text editor in some cases.



20
21
22
# File 'lib/clevic/swing/tag_delegate.rb', line 20

def editor
  @editor
end

Instance Method Details

#autocomplete(&block) ⇒ Object

make sure we don’t react to document change events while we’re doing autocompletion. Reentrant



72
73
74
75
76
77
# File 'lib/clevic/swing/tag_delegate.rb', line 72

def autocomplete( &block )
  @autocompleting = true
  yield
ensure
  @autocompleting = false
end

#display_for(model_value) ⇒ Object

Return a string to be shown to the user. model_value is an item stored in the combo box model.



29
30
31
# File 'lib/clevic/swing/tag_delegate.rb', line 29

def display_for( model_value )
  field.transform_attribute( model_value )
end

#filter_prefix(prefix) ⇒ Object

www.drdobbs.com/184404457 for autocompletion steps



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/clevic/swing/tag_delegate.rb', line 80

def filter_prefix( prefix )
  # search for matching item in the UI display_for for the items in the combo model
  candidate = population.map{|item| display_for( item ) }.select {|x| x =~ /^#{prefix}/i }.first
  unless candidate.nil?
    first_not_of = candidate.match( /^#{prefix}/i ).offset(0).last
    invoke_later do
      autocomplete do
        # set the shortlist, and the text editor value
        repopulate prefix, candidate

        # set the suggestion selection
        editor.editor.editor_component.with do |text_edit|
          # highlight the suggested match, and leave caret
          # at the beginning of the suggested text
          text_edit.caret_position = candidate.length
          text_edit.move_caret_position( first_not_of )
        end
      end
    end
  end
end

#full_editObject

open the combo box, just like if F4 was pressed big trouble here with JComboBox firing an comboEdited action (probably) on focusGained which causes the popup to be hidden again



106
107
108
109
110
111
112
113
114
# File 'lib/clevic/swing/tag_delegate.rb', line 106

def full_edit
  # Must request focus and then once focus is received, show popup.
  # Otherwise focus received hides popup.
  invoke_later do
    #~ editor.add_focus_listener( LittleFocusPopper.new )
    #~ editor.request_focus_in_window
    editor.show_popup
  end
end

#init_component(cell_editor = nil) ⇒ Object

Create a GUI widget and fill it with the possible values.



11
12
13
14
15
# File 'lib/clevic/swing/tag_delegate.rb', line 11

def init_component( cell_editor = nil )
  line_editor edit_value
  # This should be a collection of entities from the related table
  @items = attribute_value
end

#line_editor(value = nil) ⇒ Object



33
34
35
36
37
# File 'lib/clevic/swing/tag_delegate.rb', line 33

def line_editor( value = nil )
  @line_editor ||= javax.swing.JTextField.new( value ).tap do |line|
    line.font = Clevic.tahoma
  end
end

#needs_pre_selection?Boolean

the cell must be selected before the edit can be clicked

Returns:

  • (Boolean)


23
24
25
# File 'lib/clevic/swing/tag_delegate.rb', line 23

def needs_pre_selection?
  true
end

#repopulate(prefix = nil, text = nil) ⇒ Object

Recreate the model and fill it with anything in population that matches the prefix first, followed by anything in the population that doesn’t match the prefix. Then set the editor text value to either text, or to the previous value. Order is important: if the text is set first it’s overridden when the model is populated.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/clevic/swing/tag_delegate.rb', line 50

def repopulate( prefix = nil, text = nil )
  autocomplete do
    # save text and popup
    save_item = editor.editor.item
    dropdown_visible = editor.popup_visible?

    # repopulate based on the prefix
    prefix ||= editor.editor.item
    editor.model = editor.model.class.new
    # split set into things to display at the top, and things to display further down
    matching, non_matching = population.partition{ |item| display_for( item ) =~ /^#{prefix}/i }
    matching.each {|item| editor << item}
    non_matching.each {|item| editor << item}

    # restore text and popup
    editor.editor.item = text || save_item
    editor.popup_visible = dropdown_visible
  end
end

#valueObject

return an array of related entity objects, to be passed into the attribute setter



122
123
124
# File 'lib/clevic/swing/tag_delegate.rb', line 122

def value
  "the value"
end