Class: Glimmer::SWT::TreeProxy

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

Constant Summary

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

Instance Method Summary collapse

Methods included from Glimmer

included

Methods inherited from WidgetProxy

#add_observer, #async_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_attribute_getter?, #has_attribute_setter?, #has_style?, #method_missing, #pack_same_size, #post_add_content, #post_initialize_child, #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_proxy_class

Methods included from Custom::Drawable

#add_shape, #clear_shapes, #deregister_shape_painting, #image_buffered_shapes, #setup_shape_painting, #shapes

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) ⇒ TreeProxy

Returns a new instance of TreeProxy.



32
33
34
35
36
37
38
# File 'lib/glimmer/swt/tree_proxy.rb', line 32

def initialize(underscored_widget_name, parent, args)
  super
  @tree_editor = TreeEditor.new(swt_widget)
  @tree_editor.horizontalAlignment = SWTProxy[:left]
  @tree_editor.grabHorizontal = true
  @tree_editor.minimumHeight = 20
end

Dynamic Method Handling

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

Instance Attribute Details

#tree_editorObject (readonly)

Returns the value of attribute tree_editor.



29
30
31
# File 'lib/glimmer/swt/tree_proxy.rb', line 29

def tree_editor
  @tree_editor
end

#tree_editor_text_proxyObject (readonly)

Returns the value of attribute tree_editor_text_proxy.



29
30
31
# File 'lib/glimmer/swt/tree_proxy.rb', line 29

def tree_editor_text_proxy
  @tree_editor_text_proxy
end

#tree_propertiesObject

Returns the value of attribute tree_properties.



30
31
32
# File 'lib/glimmer/swt/tree_proxy.rb', line 30

def tree_properties
  @tree_properties
end

Instance Method Details

#all_tree_itemsObject

Returns all tree items including descendants



50
51
52
# File 'lib/glimmer/swt/tree_proxy.rb', line 50

def all_tree_items
  depth_first_search
end

#depth_first_search(&condition) ⇒ Object

Performs depth first search for tree items matching block condition If no condition block is passed, returns all tree items Returns a Java TreeItem array to easily set as selection on org.eclipse.swt.Tree if needed



43
44
45
46
47
# File 'lib/glimmer/swt/tree_proxy.rb', line 43

def depth_first_search(&condition)
  found = []
  recursive_depth_first_search(swt_widget.getItems.first, found, &condition)
  found.to_java(TreeItem)
end

#edit_in_progress?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/glimmer/swt/tree_proxy.rb', line 66

def edit_in_progress?
  !!@edit_in_progress
end

#edit_selected_tree_item(before_write: nil, after_write: nil, after_cancel: nil) ⇒ Object



70
71
72
# File 'lib/glimmer/swt/tree_proxy.rb', line 70

def edit_selected_tree_item(before_write: nil, after_write: nil, after_cancel: nil)
  edit_tree_item(swt_widget.getSelection.first, before_write: before_write, after_write: after_write, after_cancel: after_cancel)
end

#edit_tree_item(tree_item, before_write: nil, after_write: nil, after_cancel: nil) ⇒ Object



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

def edit_tree_item(tree_item, before_write: nil, after_write: nil, after_cancel: nil)
  return if tree_item.nil?
  content {
    @tree_editor_text_proxy = text {
      focus true
      text tree_item.getText
      action_taken = false
      cancel = lambda {
        @tree_editor_text_proxy.swt_widget.dispose
        @tree_editor_text_proxy = nil
        after_cancel&.call
        @edit_in_progress = false
      }
      action = lambda { |event|
        begin
          if !action_taken && !@edit_in_progress
            action_taken = true
            @edit_in_progress = true
            new_text = @tree_editor_text_proxy.swt_widget.getText
            if new_text == tree_item.getText
              cancel.call
            else
              before_write&.call
              tree_item.setText(new_text)
              model = tree_item.getData
              model.send("#{tree_properties[:text]}=", new_text) # makes tree update itself, so must search for selected tree item again
              edited_tree_item = depth_first_search { |ti| ti.getData == model }.first
              swt_widget.showItem(edited_tree_item)
              @tree_editor_text_proxy.swt_widget.dispose
              @tree_editor_text_proxy = nil
              after_write&.call(edited_tree_item)
              @edit_in_progress = false
            end
          end
        rescue => e
          Glimmer::Config.logger.error {"Error encountered while editing tree item!\n#{e.full_message}"}
        end
      }
      on_focus_lost(&action)
      on_key_pressed { |key_event|
        if key_event.keyCode == swt(:cr)
          action.call(key_event)
        elsif key_event.keyCode == swt(:esc)
          cancel.call
        end
      }
    }
    @tree_editor_text_proxy.swt_widget.selectAll
  }
  @tree_editor.setEditor(@tree_editor_text_proxy.swt_widget, tree_item)
end

#widget_property_listener_installersObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/glimmer/swt/tree_proxy.rb', line 54

def widget_property_listener_installers
  super.merge({
    Java::OrgEclipseSwtWidgets::Tree => {
      selection: lambda do |observer|
        on_widget_selected { |selection_event|
          observer.call(@swt_widget.getSelection)
        }
      end
    },
  })
end