| Module | JSON::Editor |
| In: |
lib/json/editor.rb
|
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 lib/json/editor.rb, line 99
99: def Editor.data2model(data, model = nil, parent = nil)
100: model ||= TreeStore.new(Gdk::Pixbuf, String, String)
101: iter = if block_given?
102: yield model
103: else
104: model.append(parent)
105: end
106: case data
107: when Hash
108: iter.type = 'Hash'
109: data.sort.each do |key, value|
110: pair_iter = model.append(iter)
111: pair_iter.type = 'Key'
112: pair_iter.content = key.to_s
113: Editor.data2model(value, model, pair_iter)
114: end
115: when Array
116: iter.type = 'Array'
117: data.each do |value|
118: Editor.data2model(value, model, iter)
119: end
120: when Numeric
121: iter.type = 'Numeric'
122: iter.content = data.to_s
123: when String, true, false, nil
124: iter.type = data.class.name
125: iter.content = data.nil? ? 'null' : data.to_s
126: else
127: iter.type = 'String'
128: iter.content = data.to_s
129: end
130: model
131: end
Opens an error dialog on top of window showing the error message text.
# File lib/json/editor.rb, line 37
37: def Editor.error_dialog(window, text)
38: dialog = MessageDialog.new(window, Dialog::MODAL,
39: MessageDialog::ERROR,
40: MessageDialog::BUTTONS_CLOSE, text)
41: dialog.run
42: ensure
43: dialog.destroy if dialog
44: end
Returns the Gdk::Pixbuf of the icon named name from the icon cache.
# File lib/json/editor.rb, line 26
26: def Editor.fetch_icon(name)
27: @icon_cache ||= {}
28: unless @icon_cache.key?(name)
29: path = '/usr/share/edit-json'
30: @icon_cache[name] = Gdk::Pixbuf.new(File.join(path, name + '.xpm'))
31: end
32: @icon_cache[name]
33: end
Convert the tree model starting from Gtk::TreeIter iter into a Ruby data structure and return it.
# File lib/json/editor.rb, line 62
62: def Editor.model2data(iter)
63: case iter.type
64: when 'Hash'
65: hash = {}
66: iter.each { |c| hash[c.content] = Editor.model2data(c.first_child) }
67: hash
68: when 'Array'
69: array = Array.new(iter.n_children)
70: iter.each_with_index { |c, i| array[i] = Editor.model2data(c) }
71: array
72: when 'Key'
73: iter.content
74: when 'String'
75: iter.content
76: when 'Numeric'
77: content = iter.content
78: if /\./.match(content)
79: content.to_f
80: else
81: content.to_i
82: end
83: when 'TrueClass'
84: true
85: when 'FalseClass'
86: false
87: when 'NilClass'
88: nil
89: else
90: fail "Unknown type found in model: #{iter.type}"
91: end
92: end
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 lib/json/editor.rb, line 49
49: def Editor.question_dialog(window, text)
50: dialog = MessageDialog.new(window, Dialog::MODAL,
51: MessageDialog::QUESTION,
52: MessageDialog::BUTTONS_YES_NO, text)
53: dialog.run do |response|
54: return Gtk::Dialog::RESPONSE_YES === response
55: end
56: ensure
57: dialog.destroy if dialog
58: end
Starts a JSON Editor. If a block was given, it yields to the JSON::Editor::MainWindow instance.
# File lib/json/editor.rb, line 1184
1184: def Editor.start(encoding = nil) # :yield: window
1185: encoding ||= 'utf8'
1186: Gtk.init
1187: window = Editor::MainWindow.new(encoding)
1188: window.icon_list = [ Editor.fetch_icon('json') ]
1189: yield window if block_given?
1190: window.show_all
1191: Gtk.main
1192: end