| Class | JSON::Editor::EditMenu |
| In: |
lib/json/editor.rb
|
| Parent: | Object |
This class creates the Edit pulldown menu.
Create the menu.
# File lib/json/editor.rb, line 594
594: def create
595: title = MenuItem.new('Edit')
596: title.submenu = menu
597: add_item('Find', &method(:find))
598: add_item('Find Again', &method(:find_again))
599: add_separator
600: add_item('Sort', &method(:sort))
601: title
602: end
Find a string in all nodes’ contents and select the found node in the treeview.
# File lib/json/editor.rb, line 513
513: def find(item)
514: search = ask_for_find_term or return
515: begin
516: @search = Regexp.new(search)
517: rescue => e
518: Editor.error_dialog(self, "Evaluation of regex /#{search}/ failed: #{e}!")
519: return
520: end
521: iter = model.get_iter('0')
522: iter.recursive_each do |i|
523: if @iter
524: if @iter != i
525: next
526: else
527: @iter = nil
528: next
529: end
530: elsif @search.match(i[CONTENT_COL])
531: set_cursor(i.path, nil, false)
532: @iter = i
533: break
534: end
535: end
536: end
Repeat the last search given by find.
# File lib/json/editor.rb, line 539
539: def find_again(item)
540: @search or return
541: iter = model.get_iter('0')
542: iter.recursive_each do |i|
543: if @iter
544: if @iter != i
545: next
546: else
547: @iter = nil
548: next
549: end
550: elsif @search.match(i[CONTENT_COL])
551: set_cursor(i.path, nil, false)
552: @iter = i
553: break
554: end
555: end
556: end
Sort (Reverse sort) all elements of the selected array by the given expression. x is the element in question.
# File lib/json/editor.rb, line 560
560: def sort(item)
561: if current = selection.selected
562: if current.type == 'Array'
563: parent = current.parent
564: ary = Editor.model2data(current)
565: order, reverse = ask_for_order
566: order or return
567: begin
568: block = eval "lambda { |x| #{order} }"
569: if reverse
570: ary.sort! { |a,b| block[b] <=> block[a] }
571: else
572: ary.sort! { |a,b| block[a] <=> block[b] }
573: end
574: rescue => e
575: Editor.error_dialog(self, "Failed to sort Array with #{order}: #{e}!")
576: else
577: Editor.data2model(ary, model, parent) do |m|
578: m.insert_before(parent, current)
579: end
580: model.remove(current)
581: expand_collapse(parent)
582: window.change
583: toplevel.display_status("Array has been sorted.")
584: end
585: else
586: toplevel.display_status("Only Array nodes can be sorted!")
587: end
588: else
589: toplevel.display_status("Select an Array to sort first!")
590: end
591: end