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