| Module | Editor |
| In: |
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 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
Opens an error dialog on top of window showing the error message text.
# File 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
Returns the Gdk::Pixbuf of the icon named name from the icon cache.
# File 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
Convert the tree model starting from Gtk::TreeIter iter into a Ruby data structure and return it.
# File 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
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 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