| Class | JSON::Editor::MainWindow |
| In: |
lib/json/editor.rb
|
| Parent: | Gtk::Window |
The editor main window
# File lib/json/editor.rb, line 964
964: def initialize(encoding)
965: @changed = false
966: @encoding = encoding
967: super(TOPLEVEL)
968: display_title
969: set_default_size(800, 600)
970: signal_connect(:delete_event) { quit }
971:
972: vbox = VBox.new(false, 0)
973: add(vbox)
974: #vbox.border_width = 0
975:
976: @treeview = JSONTreeView.new(self)
977: @treeview.signal_connect('cursor-changed''cursor-changed') do
978: display_status('')
979: end
980:
981: menu_bar = create_menu_bar
982: vbox.pack_start(menu_bar, false, false, 0)
983:
984: sw = ScrolledWindow.new(nil, nil)
985: sw.shadow_type = SHADOW_ETCHED_IN
986: sw.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
987: vbox.pack_start(sw, true, true, 0)
988: sw.add(@treeview)
989:
990: @status_bar = Statusbar.new
991: vbox.pack_start(@status_bar, false, false, 0)
992:
993: @filename ||= nil
994: if @filename
995: data = read_data(@filename)
996: view_new_model Editor.data2model(data)
997: end
998: end
Opens a dialog, asking, if changes should be saved to a file.
# File lib/json/editor.rb, line 1043
1043: def ask_save
1044: if Editor.question_dialog(self,
1045: "Unsaved changes to JSON model. Save?")
1046: if @filename
1047: file_save
1048: else
1049: file_save_as
1050: end
1051: end
1052: end
Sets editor status to changed, to indicate that the edited data containts unsaved changes.
# File lib/json/editor.rb, line 1014
1014: def change
1015: @changed = true
1016: display_title
1017: end
Clear the current model, after asking to save all unsaved changes.
# File lib/json/editor.rb, line 1071
1071: def clear
1072: ask_save if @changed
1073: @filename = nil
1074: self.view_new_model nil
1075: end
Creates the menu bar with the pulldown menus and returns it.
# File lib/json/editor.rb, line 1001
1001: def create_menu_bar
1002: menu_bar = MenuBar.new
1003: @file_menu = FileMenu.new(@treeview)
1004: menu_bar.append @file_menu.create
1005: @edit_menu = EditMenu.new(@treeview)
1006: menu_bar.append @edit_menu.create
1007: @options_menu = OptionsMenu.new(@treeview)
1008: menu_bar.append @options_menu.create
1009: menu_bar
1010: end
Displays text in the status bar.
# File lib/json/editor.rb, line 1035
1035: def display_status(text)
1036: @cid ||= nil
1037: @status_bar.pop(@cid) if @cid
1038: @cid = @status_bar.get_context_id('dummy')
1039: @status_bar.push(@cid, text)
1040: end
Open the file filename or call the select_file method to ask for a filename.
# File lib/json/editor.rb, line 1079
1079: def file_open(filename = nil)
1080: filename = select_file('Open as a JSON file') unless filename
1081: data = load_file(filename) or return
1082: view_new_model Editor.data2model(data)
1083: end
Save the current file.
# File lib/json/editor.rb, line 1086
1086: def file_save
1087: if @filename
1088: store_file(@filename)
1089: else
1090: file_save_as
1091: end
1092: end
Save the current file as the filename
# File lib/json/editor.rb, line 1095
1095: def file_save_as
1096: filename = select_file('Save as a JSON file')
1097: store_file(filename)
1098: end
Load the file named filename into the editor as a JSON document.
# File lib/json/editor.rb, line 1122
1122: def load_file(filename)
1123: if filename
1124: if File.directory?(filename)
1125: Editor.error_dialog(self, "Try to select a JSON file!")
1126: return
1127: else
1128: data = read_data(filename)
1129: @filename = filename
1130: toplevel.display_status("Loaded data from '#@filename'.")
1131: display_title
1132: return data
1133: end
1134: end
1135: end
Quit this editor, that is, leave this editor‘s main loop.
# File lib/json/editor.rb, line 1055
1055: def quit
1056: ask_save if @changed
1057: destroy
1058: Gtk.main_quit
1059: true
1060: end
Read a JSON document from the file named filename, parse it into a ruby data structure, and return the data.
# File lib/json/editor.rb, line 1145
1145: def read_data(filename)
1146: json = File.read(filename)
1147: check_pretty_printed(json)
1148: if @encoding && !/^utf8$/i.match(@encoding)
1149: iconverter = Iconv.new('utf8', @encoding)
1150: json = iconverter.iconv(json)
1151: end
1152: JSON::parse(json)
1153: rescue JSON::JSONError => e
1154: Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
1155: return
1156: rescue SystemCallError => e
1157: quit
1158: end
Open a file selecton dialog, displaying message, and return the selected filename or nil, if no file was selected.
# File lib/json/editor.rb, line 1162
1162: def select_file(message)
1163: filename = nil
1164: fs = FileSelection.new(message).set_modal(true).
1165: set_filename(Dir.pwd + "/").set_transient_for(self)
1166: fs.signal_connect(:destroy) { Gtk.main_quit }
1167: fs.ok_button.signal_connect(:clicked) do
1168: filename = fs.filename
1169: fs.destroy
1170: Gtk.main_quit
1171: end
1172: fs.cancel_button.signal_connect(:clicked) do
1173: fs.destroy
1174: Gtk.main_quit
1175: end
1176: fs.show_all
1177: Gtk.main
1178: filename
1179: end
Store the current JSON document to path.
# File lib/json/editor.rb, line 1101
1101: def store_file(path)
1102: if path
1103: data = Editor.model2data(@treeview.model.iter_first)
1104: File.open(path + '.tmp', 'wb') do |output|
1105: json = if @options_menu.pretty_item.active?
1106: JSON.pretty_unparse(data)
1107: else
1108: JSON.unparse(data)
1109: end
1110: output.write json
1111: end
1112: File.rename path + '.tmp', path
1113: @filename = path
1114: toplevel.display_status("Saved data to '#@filename'.")
1115: unchange
1116: end
1117: rescue SystemCallError => e
1118: Editor.error_dialog(self, "Failed to store JSON file: #{e}!")
1119: end
Sets editor status to unchanged, to indicate that the edited data doesn‘t containt unsaved changes.
# File lib/json/editor.rb, line 1021
1021: def unchange
1022: @changed = false
1023: display_title
1024: end