Module: JSON::Editor

Includes:
Gtk
Defined in:
lib/json/editor.rb

Defined Under Namespace

Modules: MenuExtension Classes: EditMenu, FileMenu, JSONTreeView, MainWindow, OptionsMenu, PopUpMenu

Constant Summary collapse

TITLE =

Beginning of the editor window title

'JSON Editor'.freeze
ALL_TYPES =

All JSON primitive types

%w[TrueClass FalseClass Numeric String Array Hash NilClass].sort
ALL_NODES =

The Nodes necessary for the tree representation of a JSON document

(ALL_TYPES + %w[Key]).sort

Class Method Summary collapse

Class Method Details

.data2model(data, model = nil, parent = nil) ⇒ Object

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/json/editor.rb', line 99

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

.error_dialog(window, text) ⇒ Object

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



37
38
39
40
41
42
43
44
# File 'lib/json/editor.rb', line 37

def Editor.error_dialog(window, text)
  dialog = MessageDialog.new(window, Dialog::MODAL, 
    MessageDialog::ERROR, 
    MessageDialog::BUTTONS_CLOSE, text)
  dialog.run
ensure
  dialog.destroy if dialog
end

.fetch_icon(name) ⇒ Object

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



26
27
28
29
30
31
32
33
# File 'lib/json/editor.rb', line 26

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) ⇒ Object

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/json/editor.rb', line 62

def Editor.model2data(iter)
  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) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/json/editor.rb', line 49

def Editor.question_dialog(window, text)
  dialog = MessageDialog.new(window, Dialog::MODAL, 
    MessageDialog::QUESTION, 
    MessageDialog::BUTTONS_YES_NO, text)
  dialog.run do |response|
    return Gtk::Dialog::RESPONSE_YES === response
  end
ensure
  dialog.destroy if dialog
end

.start(encoding = nil) {|window| ... } ⇒ Object

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

Yields:

  • (window)


1184
1185
1186
1187
1188
1189
1190
1191
1192
# File 'lib/json/editor.rb', line 1184

def Editor.start(encoding = nil) # :yield: window
  encoding ||= 'utf8'
  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