Class EditMenu
In: lib/json/editor.rb
Parent: Object

This class creates the Edit pulldown menu.

Methods

copy   create   find   find_again   paste   sort  

Included Modules

MenuExtension

Public Instance methods

Copy data from model into primary clipboard.

[Source]

# File 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.

[Source]

# File 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.

[Source]

# File 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.

[Source]

# File 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.

[Source]

# File 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.

[Source]

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

[Validate]