In Files

  • json/lib/json/editor.rb

JSON::Editor

Constants

ALL_NODES

The Nodes necessary for the tree representation of a JSON document

ALL_TYPES

All JSON primitive types

CONTAINER_TYPES

JSON primitive types (Containers)

CONTENT_COL
DEFAULT_DIALOG_KEY_PRESS_HANDLER
TITLE

Beginning of the editor window title

Attributes

window[R]

Public Class Methods

data2model(data, model = nil, parent = nil) click to toggle source

Convert the Ruby data structure data into tree model data for Gtk and returns the whole model. If the parameter model wasn't given a new Gtk::TreeStore is created as the model. The parent parameter specifies the parent node (iter, Gtk:TreeIter instance) to which the data is appended, alternativeley the result of the yielded block is used as iter.

 
               # File json/lib/json/editor.rb, line 121
def Editor.data2model(data, model = nil, parent = nil)
  model ||= TreeStore.new(Gdk::Pixbuf, String, String)
  iter = if block_given?
    yield model
  else
    model.append(parent)
  end
  case data
  when Hash
    iter.type = 'Hash'
    data.sort.each do |key, value|
      pair_iter = model.append(iter)
      pair_iter.type    = 'Key'
      pair_iter.content = key.to_s
      Editor.data2model(value, model, pair_iter)
    end
  when Array
    iter.type = 'Array'
    data.each do |value|
      Editor.data2model(value, model, iter)
    end
  when Numeric
    iter.type = 'Numeric'
    iter.content = data.to_s
  when String, true, false, nil
    iter.type    = data.class.name
    iter.content = data.nil? ? 'null' : data.to_s
  else
    iter.type    = 'String'
    iter.content = data.to_s
  end
  model
end
            
edit(json, encoding = 'utf8') click to toggle source

Edit the string json with encoding encoding in the editor.

 
               # File json/lib/json/editor.rb, line 1362
def edit(json, encoding = 'utf8')
  start(encoding) do |window|
    window.edit json
  end
end
            
error_dialog(window, text) click to toggle source

Opens an error dialog on top of window showing the error message text.

 
               # File json/lib/json/editor.rb, line 50
def Editor.error_dialog(window, text)
  dialog = MessageDialog.new(window, Dialog::MODAL, 
    MessageDialog::ERROR, 
    MessageDialog::BUTTONS_CLOSE, text)
  dialog.show_all
  dialog.run
rescue TypeError
  dialog = MessageDialog.new(Editor.window, Dialog::MODAL, 
    MessageDialog::ERROR, 
    MessageDialog::BUTTONS_CLOSE, text)
  dialog.show_all
  dialog.run
ensure
  dialog.destroy if dialog
end
            
fetch_icon(name) click to toggle source

Returns the Gdk::Pixbuf of the icon named name from the icon cache.

 
               # File json/lib/json/editor.rb, line 39
def Editor.fetch_icon(name)
  @icon_cache ||= {}
  unless @icon_cache.key?(name)
    path = File.dirname(__FILE__)
    @icon_cache[name] = Gdk::Pixbuf.new(File.join(path, name + '.xpm'))
  end
 @icon_cache[name]
end
            
model2data(iter) click to toggle source

Convert the tree model starting from Gtk::TreeIter iter into a Ruby data structure and return it.

 
               # File json/lib/json/editor.rb, line 83
def Editor.model2data(iter)
  return nil if iter.nil?
  case iter.type
  when 'Hash'
    hash = {}
    iter.each { |c| hash[c.content] = Editor.model2data(c.first_child) }
    hash
  when 'Array'
    array = Array.new(iter.n_children)
    iter.each_with_index { |c, i| array[i] = Editor.model2data(c) }
    array
  when 'Key'
    iter.content
  when 'String'
    iter.content
  when 'Numeric'
    content = iter.content
    if /\./.match(content)
      content.to_f
    else
      content.to_i
    end
  when 'TrueClass'
    true
  when 'FalseClass'
    false
  when 'NilClass'
    nil
  else
    fail "Unknown type found in model: #{iter.type}"
  end
end
            
question_dialog(window, text) click to toggle source

Opens a yes/no question dialog on top of window showing the error message text. If yes was answered true is returned, otherwise false.

 
               # File json/lib/json/editor.rb, line 69
def Editor.question_dialog(window, text)
  dialog = MessageDialog.new(window, Dialog::MODAL, 
    MessageDialog::QUESTION, 
    MessageDialog::BUTTONS_YES_NO, text)
  dialog.show_all
  dialog.run do |response|
    return Gtk::Dialog::RESPONSE_YES === response
  end
ensure
  dialog.destroy if dialog
end
            
start(encoding = 'utf8') click to toggle source

Starts a JSON Editor. If a block was given, it yields to the JSON::Editor::MainWindow instance.

 
               # File json/lib/json/editor.rb, line 1352
def start(encoding = 'utf8') # :yield: window
  Gtk.init
  @window = Editor::MainWindow.new(encoding)
  @window.icon_list = [ Editor.fetch_icon('json') ]
  yield @window if block_given?
  @window.show_all
  Gtk.main
end