| 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
121: def Editor.data2model(data, model = nil, parent = nil)
122: model ||= TreeStore.new(Gdk::Pixbuf, String, String)
123: iter = if block_given?
124: yield model
125: else
126: model.append(parent)
127: end
128: case data
129: when Hash
130: iter.type = 'Hash'
131: data.sort.each do |key, value|
132: pair_iter = model.append(iter)
133: pair_iter.type = 'Key'
134: pair_iter.content = key.to_s
135: Editor.data2model(value, model, pair_iter)
136: end
137: when Array
138: iter.type = 'Array'
139: data.each do |value|
140: Editor.data2model(value, model, iter)
141: end
142: when Numeric
143: iter.type = 'Numeric'
144: iter.content = data.to_s
145: when String, true, false, nil
146: iter.type = data.class.name
147: iter.content = data.nil? ? 'null' : data.to_s
148: else
149: iter.type = 'String'
150: iter.content = data.to_s
151: end
152: model
153: end
Opens an error dialog on top of window showing the error message text.
# File lib/json/editor.rb, line 50
50: def Editor.error_dialog(window, text)
51: dialog = MessageDialog.new(window, Dialog::MODAL,
52: MessageDialog::ERROR,
53: MessageDialog::BUTTONS_CLOSE, text)
54: dialog.show_all
55: dialog.run
56: rescue TypeError
57: dialog = MessageDialog.new(Editor.window, Dialog::MODAL,
58: MessageDialog::ERROR,
59: MessageDialog::BUTTONS_CLOSE, text)
60: dialog.show_all
61: dialog.run
62: ensure
63: dialog.destroy if dialog
64: end
Returns the Gdk::Pixbuf of the icon named name from the icon cache.
# File lib/json/editor.rb, line 39
39: def Editor.fetch_icon(name)
40: @icon_cache ||= {}
41: unless @icon_cache.key?(name)
42: path = '/usr/share/edit-json'
43: @icon_cache[name] = Gdk::Pixbuf.new(File.join(path, name + '.xpm'))
44: end
45: @icon_cache[name]
46: 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
83: def Editor.model2data(iter)
84: return nil if iter.nil?
85: case iter.type
86: when 'Hash'
87: hash = {}
88: iter.each { |c| hash[c.content] = Editor.model2data(c.first_child) }
89: hash
90: when 'Array'
91: array = Array.new(iter.n_children)
92: iter.each_with_index { |c, i| array[i] = Editor.model2data(c) }
93: array
94: when 'Key'
95: iter.content
96: when 'String'
97: iter.content
98: when 'Numeric'
99: content = iter.content
100: if /\./.match(content)
101: content.to_f
102: else
103: content.to_i
104: end
105: when 'TrueClass'
106: true
107: when 'FalseClass'
108: false
109: when 'NilClass'
110: nil
111: else
112: fail "Unknown type found in model: #{iter.type}"
113: end
114: 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
69: def Editor.question_dialog(window, text)
70: dialog = MessageDialog.new(window, Dialog::MODAL,
71: MessageDialog::QUESTION,
72: MessageDialog::BUTTONS_YES_NO, text)
73: dialog.show_all
74: dialog.run do |response|
75: return Gtk::Dialog::RESPONSE_YES === response
76: end
77: ensure
78: dialog.destroy if dialog
79: end