Module: JSON::Editor

Includes:
Gtk
Defined in:
lib/vendor/json_pure/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
CONTAINER_TYPES =

JSON primitive types (Containers)

%w[Array Hash].sort
ALL_TYPES =

All JSON primitive types

(%w[TrueClass FalseClass Numeric String NilClass] +
CONTAINER_TYPES).sort
ALL_NODES =

The Nodes necessary for the tree representation of a JSON document

(ALL_TYPES + %w[Key]).sort
DEFAULT_DIALOG_KEY_PRESS_HANDLER =
lambda do |dialog, event|
  case event.keyval
  when Gdk::Keyval::GDK_Return
    dialog.response Dialog::RESPONSE_ACCEPT
  when Gdk::Keyval::GDK_Escape
    dialog.response Dialog::RESPONSE_REJECT
  end
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.windowObject (readonly)

Returns the value of attribute window.



1368
1369
1370
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 1368

def window
  @window
end

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/vendor/json_pure/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') ⇒ Object

Edit the string json with encoding encoding in the editor.



1362
1363
1364
1365
1366
# File 'lib/vendor/json_pure/lib/json/editor.rb', line 1362

def edit(json, encoding = 'utf8')
  start(encoding) do |window|
    window.edit json
  end
end

.error_dialog(window, text) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vendor/json_pure/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) ⇒ Object

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



39
40
41
42
43
44
45
46
# File 'lib/vendor/json_pure/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) ⇒ Object

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vendor/json_pure/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) ⇒ 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.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vendor/json_pure/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') {|@window| ... } ⇒ Object

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

Yields:



1352
1353
1354
1355
1356
1357
1358
1359
# File 'lib/vendor/json_pure/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