Class: JSON::Editor::EditMenu

Inherits:
Object
  • Object
show all
Includes:
MenuExtension
Defined in:
lib/json/editor.rb

Overview

This class creates the Edit pulldown menu.

Instance Attribute Summary

Attributes included from MenuExtension

#menu, #treeview

Instance Method Summary collapse

Methods included from MenuExtension

#add_item, #add_separator, #initialize, #method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class JSON::Editor::MenuExtension

Instance Method Details

#createObject

Create the menu.



594
595
596
597
598
599
600
601
602
# File 'lib/json/editor.rb', line 594

def create
  title = MenuItem.new('Edit')
  title.submenu = menu
  add_item('Find', &method(:find))
  add_item('Find Again', &method(:find_again))
  add_separator
  add_item('Sort', &method(:sort))
  title
end

#find(item) ⇒ Object

Find a string in all nodes’ contents and select the found node in the treeview.



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/json/editor.rb', line 513

def find(item)
  search = ask_for_find_term or return
  begin
    @search = Regexp.new(search)
  rescue => e
    Editor.error_dialog(self, "Evaluation of regex /#{search}/ failed: #{e}!")
    return
  end
  iter = model.get_iter('0')
  iter.recursive_each do |i|
    if @iter
      if @iter != i
        next
      else
        @iter = nil
        next
      end
    elsif @search.match(i[CONTENT_COL])
       set_cursor(i.path, nil, false)
       @iter = i
       break
    end
  end
end

#find_again(item) ⇒ Object

Repeat the last search given by #find.



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/json/editor.rb', line 539

def find_again(item)
  @search or return
  iter = model.get_iter('0')
  iter.recursive_each do |i|
    if @iter
      if @iter != i
        next
      else
        @iter = nil
        next
      end
    elsif @search.match(i[CONTENT_COL])
       set_cursor(i.path, nil, false)
       @iter = i
       break
    end
  end
end

#sort(item) ⇒ Object

Sort (Reverse sort) all elements of the selected array by the given expression. x is the element in question.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/json/editor.rb', line 560

def sort(item)
  if current = selection.selected
    if current.type == 'Array'
      parent = current.parent
      ary = Editor.model2data(current)
      order, reverse = ask_for_order
      order or return
      begin
        block = eval "lambda { |x| #{order} }"
        if reverse
          ary.sort! { |a,b| block[b] <=> block[a] }
        else
          ary.sort! { |a,b| block[a] <=> block[b] }
        end
      rescue => e
        Editor.error_dialog(self, "Failed to sort Array with #{order}: #{e}!")
      else
        Editor.data2model(ary, model, parent) do |m|
          m.insert_before(parent, current)
        end
        model.remove(current)
        expand_collapse(parent)
        window.change
        toplevel.display_status("Array has been sorted.")
      end
    else
      toplevel.display_status("Only Array nodes can be sorted!")
    end
  else
      toplevel.display_status("Select an Array to sort first!")
  end
end