In Files

  • json/lib/json/editor.rb

JSON::Editor::EditMenu

This class creates the Edit pulldown menu.

Public Instance Methods

copy(item) click to toggle source

Copy data from model into primary clipboard.

 
               # File json/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
            
create() click to toggle source

Create the menu.

 
               # File json/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) click to toggle source

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

 
               # File json/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) click to toggle source

Repeat the last search given by find.

 
               # File json/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) click to toggle source

Copy json text from primary clipboard into model.

 
               # File json/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) click to toggle source

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

 
               # File json/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