Class: RubyCurses::ListDataModel

Inherits:
Object
  • Object
show all
Includes:
Enumerable, EventHandler
Defined in:
lib/rbcurse/rlistbox.rb

Overview

www.java2s.com/Code/JavaAPI/javax.swing.event/ListDataEventCONTENTSCHANGED.htm should we extend array of will that open us to misuse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Constructor Details

#initialize(anarray = []) ⇒ ListDataModel



52
53
54
55
# File 'lib/rbcurse/rlistbox.rb', line 52

def initialize anarray=[]
  @list = anarray.dup
  @_events = [:LIST_DATA_EVENT, :ENTER_ROW]
end

Instance Attribute Details

#last_regexObject (readonly)

should i really keep here as public or maintain in listbox



50
51
52
# File 'lib/rbcurse/rlistbox.rb', line 50

def last_regex
  @last_regex
end

#selected_indexObject

Returns the value of attribute selected_index.



49
50
51
# File 'lib/rbcurse/rlistbox.rb', line 49

def selected_index
  @selected_index
end

Instance Method Details

#[](off0) ⇒ Object



89
90
91
# File 'lib/rbcurse/rlistbox.rb', line 89

def [](off0)
  @list[off0]
end

#[]=(off0, data) ⇒ Object



86
87
88
# File 'lib/rbcurse/rlistbox.rb', line 86

def []=(off0, data)
  update off0, data
end

#append(data) ⇒ Object



76
77
78
79
80
# File 'lib/rbcurse/rlistbox.rb', line 76

def append data
  @list << data
  lde = ListDataEvent.new(@list.length-1, @list.length-1, self, :INTERVAL_ADDED)
  fire_handler :LIST_DATA_EVENT, lde
end

#delete(obj) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/rbcurse/rlistbox.rb', line 105

def delete obj
  off0 = @list.index obj
  return nil if off0.nil?
  ret=@list.delete_at off0
  lde = ListDataEvent.new(off0, off0, self, :INTERVAL_REMOVED)
  fire_handler :LIST_DATA_EVENT, lde
  return ret
end

#delete_at(off0) ⇒ Object



92
93
94
95
96
97
# File 'lib/rbcurse/rlistbox.rb', line 92

def delete_at off0
  ret=@list.delete_at off0
  lde = ListDataEvent.new(off0, off0, self, :INTERVAL_REMOVED)
  fire_handler :LIST_DATA_EVENT, lde
  return ret
end

#each(&blk) ⇒ Object

changd on 2009-01-14 12:28 based on .. www.ruby-forum.com/topic/175637#769030



58
59
60
# File 'lib/rbcurse/rlistbox.rb', line 58

def each(&blk)
  @list.each(&blk)
end

#find_match(regex, ix0 = 0, ix1 = length()) ⇒ Object

## added 2009-01-14 01:00 searches between given range of rows (def 0 and end) returns row index of first match of given regex (or nil if not found)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rbcurse/rlistbox.rb', line 130

def find_match regex, ix0=0, ix1=length()
  $log.debug " find_match got #{regex} #{ix0} #{ix1}"
  @last_regex = regex
  @search_start_ix = ix0
  @search_end_ix = ix1
  #@search_found_ix = nil
  @list.each_with_index do |row, ix|
    next if ix < ix0
    break if ix > ix1
    if !row.match(regex).nil?
      @search_found_ix = ix
      return ix 
    end
  end
  return nil
end

#find_nextObject

continues previous search



148
149
150
151
152
# File 'lib/rbcurse/rlistbox.rb', line 148

def find_next
  raise "No previous search" if @last_regex.nil?
  start = @search_found_ix && @search_found_ix+1 || 0
  return find_match @last_regex, start, @search_end_ix
end

#find_prev(regex = @last_regex, start = @search_found_ix) ⇒ Object

find backwards, list_data_model Using this to start a search or continue search



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rbcurse/rlistbox.rb', line 156

def find_prev regex=@last_regex, start = @search_found_ix 
  raise "No previous search" if regex.nil? # @last_regex.nil?
  $log.debug " find_prev #{@search_found_ix} : #{@current_index}"
  start -= 1 unless start == 0
  @last_regex = regex
  @search_start_ix = start
  start.downto(0) do |ix| 
    row = @list[ix]
    if !row.match(regex).nil?
      @search_found_ix = ix
      return ix 
    end
  end
  return nil
  #return find_match @last_regex, start, @search_end_ix
end

#include?(obj) ⇒ Boolean



113
114
115
# File 'lib/rbcurse/rlistbox.rb', line 113

def include?(obj)
  return @list.include?(obj)
end

#index(obj) ⇒ Object

not sure how to do this XXX removed on 2009-01-14 12:28 def <=>(other)

@list <=> other

end



65
66
67
# File 'lib/rbcurse/rlistbox.rb', line 65

def index obj
  @list.index(obj)
end

#insert(off0, *data) ⇒ Object



71
72
73
74
75
# File 'lib/rbcurse/rlistbox.rb', line 71

def insert off0, *data
  @list.insert off0, *data
  lde = ListDataEvent.new(off0, off0+data.length-1, self, :INTERVAL_ADDED)
  fire_handler :LIST_DATA_EVENT, lde
end

#lengthObject Also known as: size



68
# File 'lib/rbcurse/rlistbox.rb', line 68

def length ; @list.length; end

#on_enter_row(object) ⇒ Object

Deprecated.

this was just eye candy for some demo

why do we have this here in data, we should remove this



122
123
124
125
# File 'lib/rbcurse/rlistbox.rb', line 122

def on_enter_row object
  $log.debug " XXX on_enter_row of list_data"
  fire_handler :ENTER_ROW, object
end

#remove_allObject



98
99
100
101
102
103
104
# File 'lib/rbcurse/rlistbox.rb', line 98

def remove_all
  return if @list.nil? || @list.empty? # 2010-09-21 13:25 
  lde = ListDataEvent.new(0, @list.size, self, :INTERVAL_REMOVED)
  @list = []
  @current_index = 0
  fire_handler :LIST_DATA_EVENT, lde
end

#slice!(line, howmany) ⇒ Object

added 2010-05-23 12:10 for listeditable



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

def slice!(line, howmany)
  ret = @list.slice!(line, howmany)
  lde = ListDataEvent.new(line, line+howmany-1, self, :INTERVAL_REMOVED)
  fire_handler :LIST_DATA_EVENT, lde
  return ret
end

#update(off0, data) ⇒ Object



81
82
83
84
85
# File 'lib/rbcurse/rlistbox.rb', line 81

def update off0, data
  @list[off0] = data
  lde = ListDataEvent.new(off0, off0, self, :CONTENTS_CHANGED)
  fire_handler :LIST_DATA_EVENT, lde
end

#valuesObject Also known as: to_array

returns a ‘dup()` of the list



117
118
119
# File 'lib/rbcurse/rlistbox.rb', line 117

def values
  @list.dup
end