This class creates the Edit pulldown menu.
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 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 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
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
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 (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
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.