Class: RubyCurses::DefaultTableModel

Inherits:
TableModel show all
Includes:
EventHandler
Defined in:
lib/rbcurse/rtable.rb

Overview

DTM

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Methods inherited from TableModel

#get_total_column_width

Constructor Details

#initialize(data, colnames_array) ⇒ DefaultTableModel

Returns a new instance of DefaultTableModel.



1385
1386
1387
1388
# File 'lib/rbcurse/rtable.rb', line 1385

def initialize data, colnames_array
  @data = data
  @column_identifiers = colnames_array
end

Instance Attribute Details

#last_regexObject (readonly)

Returns the value of attribute last_regex.



1383
1384
1385
# File 'lib/rbcurse/rtable.rb', line 1383

def last_regex
  @last_regex
end

Instance Method Details

#<<(obj) ⇒ Object



1416
1417
1418
1419
1420
1421
# File 'lib/rbcurse/rtable.rb', line 1416

def << obj
  @data << obj
  tme = TableModelEvent.new(@data.length-1,@data.length-1, :ALL_COLUMNS, self, :INSERT)
  fire_handler :TABLE_MODEL_EVENT, tme
  # create tablemodelevent and fire_table_changed for all listeners 
end

#ask_search_forwardObject



1463
1464
1465
1466
1467
1468
1469
1470
1471
# File 'lib/rbcurse/rtable.rb', line 1463

def ask_search_forward
  regex = get_string "Enter regex to search for:"
  ix = get_list_data_model.find_match regex
  if ix.nil?
    alert("No matching data for: #{regex}")
  else
    set_focus_on(ix)
  end
end

#column_countObject



1389
1390
1391
1392
1393
# File 'lib/rbcurse/rtable.rb', line 1389

def column_count
   # 2010-01-12 19:35  changed count to size since size is supported in 1.8.6 also
  #@column_identifiers.count
  @column_identifiers.size
end

#data=(data) ⇒ Object

for those quick cases when you wish to replace all the data and not have an event per row being generated



1456
1457
1458
1459
1460
1461
1462
# File 'lib/rbcurse/rtable.rb', line 1456

def data=(data)
  raise "Data nil or invalid" if data.nil? or data.size == 0
  delete_all
  @data = data
  tme = TableModelEvent.new(0, @data.length-1,:ALL_COLUMNS,  self, :INSERT)
  fire_handler :TABLE_MODEL_EVENT, tme
end

#delete(obj) ⇒ Object

create tablemodelevent and fire_table_changed for all listeners



1428
1429
1430
1431
1432
1433
1434
1435
1436
# File 'lib/rbcurse/rtable.rb', line 1428

def delete obj
  row = @data.index obj
  return if row.nil?
  ret = @data.delete obj
  tme = TableModelEvent.new(row, row,:ALL_COLUMNS,  self, :DELETE)
  fire_handler :TABLE_MODEL_EVENT, tme
  # create tablemodelevent and fire_table_changed for all listeners
  return ret
end

#delete_allObject

added 2009-01-17 21:36 Use with caution, does not call events per row



1447
1448
1449
1450
1451
1452
# File 'lib/rbcurse/rtable.rb', line 1447

def delete_all
  len = @data.length-1
  @data=[]
  tme = TableModelEvent.new(0, len,:ALL_COLUMNS,  self, :DELETE)
  fire_handler :TABLE_MODEL_EVENT, tme
end

#delete_at(row) ⇒ Object



1437
1438
1439
1440
1441
1442
1443
# File 'lib/rbcurse/rtable.rb', line 1437

def delete_at row
  ret = @data.delete_at row
  # create tablemodelevent and fire_table_changed for all listeners 
  tme = TableModelEvent.new(row, row,:ALL_COLUMNS,  self, :DELETE)
  fire_handler :TABLE_MODEL_EVENT, tme
  return ret
end

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

continues previous search



1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
# File 'lib/rbcurse/rtable.rb', line 1474

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

#find_nextObject

dtm findnext



1507
1508
1509
1510
1511
# File 'lib/rbcurse/rtable.rb', line 1507

def find_next
  raise "No more 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



1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
# File 'lib/rbcurse/rtable.rb', line 1490

def find_prev regex=@last_regex, start = @search_found_ix 
  raise "No previous search" if @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 = @data[ix]
    if row.grep(/#{regex}/) != [] 
      @search_found_ix = ix
      return ix 
    end
  end
  return nil
  #return find_match @last_regex, start, @search_end_ix
end

#get_value_at(row, col) ⇒ Object

please avoid directly hitting this. Suggested to use get_value_at of jtable since columns could have been switched.



1410
1411
1412
1413
1414
1415
# File 'lib/rbcurse/rtable.rb', line 1410

def get_value_at row, col
#$log.debug " def get_value_at #{row}, #{col} "
  
  raise "IndexError get_value_at #{row}, #{col}" if @data.nil? or row >= @data.size
  return @data[row][ col]
end

#insert(row, obj) ⇒ Object

create tablemodelevent and fire_table_changed for all listeners



1422
1423
1424
1425
1426
1427
# File 'lib/rbcurse/rtable.rb', line 1422

def insert row, obj
  @data.insert row, obj
  tme = TableModelEvent.new(row, row,:ALL_COLUMNS,  self, :INSERT)
  fire_handler :TABLE_MODEL_EVENT, tme
  # create tablemodelevent and fire_table_changed for all listeners 
end

#row_countObject



1394
1395
1396
# File 'lib/rbcurse/rtable.rb', line 1394

def row_count
  @data.length
end

#set_value_at(row, col, val) ⇒ Object

please avoid directly hitting this. Suggested to use get_value_at of jtable since columns could have been switched.



1400
1401
1402
1403
1404
1405
1406
# File 'lib/rbcurse/rtable.rb', line 1400

def set_value_at row, col, val
 # $log.debug " def set_value_at #{row}, #{col}, #{val} "
    # if editing allowed
    @data[row][col] = val
    tme = TableModelEvent.new(row, row, col, self, :UPDATE)
    fire_handler :TABLE_MODEL_EVENT, tme
end