Class: Packet_List

Inherits:
FXIconList
  • Object
show all
Includes:
Responder
Defined in:
lib/Packet_List.rb

Overview

Packet_List is a more convenient wrapper for FXIconList. This class has to be used in combination with Packet_Item.

Instance Method Summary collapse

Constructor Details

#initialize(data, *args) ⇒ Packet_List

Create a new Packet_List. All parameters are passed to the parental constructor FXIconList.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/Packet_List.rb', line 10

def initialize(data, *args)
  @data = data
  @sort_mutex = Mutex.new
  @sort_thread = nil
  if FOXVERSION=="1.0"
    def getItem(num)
      retrieveItem(num)
    end
  end

  @header_item_index = 0
  @reversed = true
  @conversions = Array.new
  @items = Set.new
  super(*args)

  header.connect(SEL_COMMAND) do |sender, sel, item_number|
    on_cmd_header(item_number)
  end


  # HACK: only works when one header is there.
  self.connect(SEL_CONFIGURE) do |sender, sel, data|
    update_header_width
  end
end

Instance Method Details

#add_header(text, width, &conversion) ⇒ Object

Add a new header. The block that you need to specify is used to convert a string of this column into something that can be used for sorting. The yielded parameter item_colum_text is the text of one item of this column.



94
95
96
97
98
# File 'lib/Packet_List.rb', line 94

def add_header(text, width, &conversion) # :yields: item_column_text
  appendHeader(text, nil, width)
  header.setArrowDir(0, false) if @conversions.empty?
  @conversions.push conversion
end

#add_item(item) ⇒ Object

Add a new Packet_Item to this list.Called from within Packet_Item during Packet_Item#parent=



108
109
110
111
112
# File 'lib/Packet_List.rb', line 108

def add_item(item)
  @items.add item
  appendItem(item.fox_item)
  item
end

#appendItem(*args) ⇒ Object



126
127
128
129
130
131
# File 'lib/Packet_List.rb', line 126

def appendItem(*args)
  $fx_icon_item_remove_mutex.synchronize do
    #puts "appendItem"
    super
  end
end

#clearObject

Removes all items from the list.



174
175
176
177
178
179
# File 'lib/Packet_List.rb', line 174

def clear
  @items.each { |item| item.clear }
  @items.clear
  # todo: mutex!?
  clearItems
end

#clearItems(*args) ⇒ Object

Remove all items, but do this within the mutex to be sure everything goes ok.



146
147
148
149
150
151
# File 'lib/Packet_List.rb', line 146

def clearItems(*args)
  $fx_icon_item_remove_mutex.synchronize do
    #puts "clearitems"
    super
  end
end

#createObject



37
38
39
40
# File 'lib/Packet_List.rb', line 37

def create
  super
  recalc
end

#create_item(icon, *args) ⇒ Object

Create a new Packet_Item. After the icon, you can specify the text for each of the columns. Here is an example:

list.create_item(my_icon, "Martin", "Ankerl", "2005")


103
104
105
# File 'lib/Packet_List.rb', line 103

def create_item(icon, *args)
  Packet_Item.new(self, icon, *args)
end

#dirty_clearObject

use in combination with Packet_Item#show



182
183
184
185
# File 'lib/Packet_List.rb', line 182

def dirty_clear
  clearItems
  @items.clear
end

#getContentHeight(*args) ⇒ Object

This may be called when the item is currently beeing deleted. this method works only before or after the delete, therefore the mutex is necessary.



166
167
168
169
170
171
# File 'lib/Packet_List.rb', line 166

def getContentHeight(*args)
  $fx_icon_item_remove_mutex.synchronize do
    #puts "contentheight"
    super
  end
end

#getContentWidth(*args) ⇒ Object

This may be called when the item is currently beeing deleted. this method works only before or after the delete, therefore the mutex is necessary.



156
157
158
159
160
161
# File 'lib/Packet_List.rb', line 156

def getContentWidth(*args)
  $fx_icon_item_remove_mutex.synchronize do
    #puts "contentwidth"
    super
  end
end

#on_cmd_header(header_item_index) ⇒ Object

Called whenever a header is clicked



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/Packet_List.rb', line 50

def on_cmd_header(header_item_index)
  @data.gui_mutex.synchronize do
    header.setArrowDir(@header_item_index, MAYBE)
    if @header_item_index == header_item_index
      @reversed = !@reversed
    else
      @reversed = true
    end
    @header_item_index = header_item_index
    header.setArrowDir(@header_item_index, @reversed)
    sortItems
  end
  # sort array
  @data.items.sort! do |a, b|
    cmp = a.sortable(header_item_index) <=> b.sortable(header_item_index)
    if @reversed
      -cmp
    else
      cmp
    end
  end
  return 0
end

#remove_item(item) ⇒ Object

Remove the given Packet_Item from the list. This is slow, because the item has to be searched with a linear search. To remove all items use #clear.



117
118
119
120
121
122
123
124
# File 'lib/Packet_List.rb', line 117

def remove_item(item)
  i = 0
  @items.delete item
  fox_item = item.fox_item
  i += 1 while i < numItems && fox_item != getItem(i)
  removeItem(i) if i < numItems
  item
end

#removeItem(*args) ⇒ Object

Before actually deleting, the item has to be marked as deleted to prevent drawing afterwards.



135
136
137
138
139
140
141
142
143
# File 'lib/Packet_List.rb', line 135

def removeItem(*args)
  $fx_icon_item_remove_mutex.synchronize do
    #puts "removeitem"
    # at first, mark item as deleted to prevent drawing afterwards
    getItem(args[0]).deleted
    # do the evil delete
    super
  end
end

#reversed?Boolean

Check if the search order is reversed (happens when clicking on the same header twice)

Returns:

  • (Boolean)


82
83
84
# File 'lib/Packet_List.rb', line 82

def reversed?
  @reversed
end

#sort_function(header_item_index) ⇒ Object

Get the sort for a given header index. The sort function has been supplied when calling #new. This is used from Packet_Item.



189
190
191
# File 'lib/Packet_List.rb', line 189

def sort_function(header_item_index)
  @conversions[header_item_index]
end

#sort_indexObject

Get index of the header that should be used for sorting



87
88
89
# File 'lib/Packet_List.rb', line 87

def sort_index
  @header_item_index
end

#sortItemsObject



74
75
76
77
78
79
# File 'lib/Packet_List.rb', line 74

def sortItems
  @items.each do |item|
    item.update_sort_key
  end
  super
end

#update_header_widthObject

HACK: only works when one header is there.



43
44
45
46
# File 'lib/Packet_List.rb', line 43

def update_header_width
  header.setItemSize(0, width-verticalScrollBar.width)
  recalc
end