Class: JSON::Editor::EditMenu

Inherits:
Object
  • Object
show all
Includes:
MenuExtension
Defined in:
lib/crazy_ivan/vendor/json-1.1.7/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

#copy(item) ⇒ Object

Copy data from model into primary clipboard.



548
549
550
551
552
553
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/editor.rb', line 548

def copy(item)
  data = Editor.model2data(model.iter_first)
  json = JSON.pretty_generate(data, :max_nesting => false)
  c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
  c.text = json
end

#createObject

Create the menu.



645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/editor.rb', line 645

def create
  title = MenuItem.new('Edit')
  title.submenu = menu
  add_item('Copy', ?c, &method(:copy))
  add_item('Paste', ?v, &method(:paste))
  add_separator
  add_item('Find', ?f, &method(:find))
  add_item('Find Again', ?g, &method(:find_again))
  add_separator
  add_item('Sort', ?S, &method(:sort))
  title
end

#find(item) ⇒ Object

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



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/editor.rb', line 570

def find(item)
  @search = ask_for_find_term(@search) or return
  iter = model.get_iter('0') or return
  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.



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/editor.rb', line 590

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

#paste(item) ⇒ Object

Copy json text from primary clipboard into model.



556
557
558
559
560
561
562
563
564
565
566
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/editor.rb', line 556

def paste(item)
  c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
  if json = c.wait_for_text
    window.ask_save if @changed
    begin
      window.edit json
    rescue JSON::ParserError
      window.clear
    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.



611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/crazy_ivan/vendor/json-1.1.7/lib/json/editor.rb', line 611

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