Class MainWindow
In: lib/json/editor.rb
Parent: Gtk::Window
OptionsMenu EditMenu PopUpMenu FileMenu JSONTreeView MainWindow ParserError NestingError GeneratorError CircularDatastructure StandardError JSONError MissingUnicodeSupport StringScanner Parser State Enumerable TreeIter Gtk::TreeView Gtk::Window MenuExtension lib/json/common.rb Ext Editor lib/json/pure/parser.rb lib/json/pure/generator.rb Integer FalseClass Array Hash Float NilClass Object TrueClass Extend String GeneratorMethods Generator Pure JSON Editor lib/json/editor.rb Gtk dot/f_8.png

The editor main window

Methods

Included Modules

Gtk

Public Class methods

[Source]

      # File lib/json/editor.rb, line 1009
1009:       def initialize(encoding)
1010:         @changed  = false
1011:         @encoding = encoding
1012:         super(TOPLEVEL)
1013:         display_title
1014:         set_default_size(800, 600)
1015:         signal_connect(:delete_event) { quit }
1016: 
1017:         vbox = VBox.new(false, 0)
1018:         add(vbox)
1019:         #vbox.border_width = 0
1020: 
1021:         @treeview = JSONTreeView.new(self)
1022:         @treeview.signal_connect('cursor-changed''cursor-changed') do
1023:           display_status('')
1024:         end
1025: 
1026:         menu_bar = create_menu_bar
1027:         vbox.pack_start(menu_bar, false, false, 0)
1028: 
1029:         sw = ScrolledWindow.new(nil, nil)
1030:         sw.shadow_type = SHADOW_ETCHED_IN
1031:         sw.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
1032:         vbox.pack_start(sw, true, true, 0)
1033:         sw.add(@treeview)
1034: 
1035:         @status_bar = Statusbar.new
1036:         vbox.pack_start(@status_bar, false, false, 0)
1037: 
1038:         @filename ||= nil
1039:         if @filename
1040:           data = read_data(@filename)
1041:           view_new_model Editor.data2model(data)
1042:         end
1043:       end

Public Instance methods

Ask for location URI a to load data from. Returns the URI as a string.

[Source]

      # File lib/json/editor.rb, line 1247
1247:       def ask_for_location
1248:         dialog = Dialog.new(
1249:           "Load data from location...",
1250:           nil, nil,
1251:           [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
1252:           [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
1253:         )
1254:         hbox = HBox.new(false, 5)
1255: 
1256:         hbox.add(Label.new("Location:"))
1257:         hbox.pack_start(location_input = Entry.new)
1258:         location_input.width_chars = 60
1259:         location_input.text = @location || ''
1260: 
1261:         dialog.vbox.add(hbox)
1262: 
1263:         dialog.signal_connect('key-press-event''key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
1264:         dialog.show_all
1265:         dialog.run do |response| 
1266:           if response == Dialog::RESPONSE_ACCEPT
1267:             return @location = location_input.text
1268:           end
1269:         end
1270:         return
1271:       ensure
1272:         dialog.destroy if dialog
1273:       end

Opens a dialog, asking, if changes should be saved to a file.

[Source]

      # File lib/json/editor.rb, line 1088
1088:       def ask_save
1089:         if Editor.question_dialog(self,
1090:           "Unsaved changes to JSON model. Save?")
1091:           if @filename
1092:             file_save
1093:           else
1094:             file_save_as
1095:           end
1096:         end
1097:       end

Sets editor status to changed, to indicate that the edited data containts unsaved changes.

[Source]

      # File lib/json/editor.rb, line 1059
1059:       def change
1060:         @changed = true
1061:         display_title
1062:       end

Clear the current model, after asking to save all unsaved changes.

[Source]

      # File lib/json/editor.rb, line 1118
1118:       def clear
1119:         ask_save if @changed
1120:         @filename = nil
1121:         self.view_new_model nil
1122:       end

Creates the menu bar with the pulldown menus and returns it.

[Source]

      # File lib/json/editor.rb, line 1046
1046:       def create_menu_bar
1047:         menu_bar = MenuBar.new
1048:         @file_menu = FileMenu.new(@treeview)
1049:         menu_bar.append @file_menu.create
1050:         @edit_menu = EditMenu.new(@treeview)
1051:         menu_bar.append @edit_menu.create
1052:         @options_menu = OptionsMenu.new(@treeview)
1053:         menu_bar.append @options_menu.create
1054:         menu_bar
1055:       end

Displays text in the status bar.

[Source]

      # File lib/json/editor.rb, line 1080
1080:       def display_status(text)
1081:         @cid ||= nil
1082:         @status_bar.pop(@cid) if @cid
1083:         @cid = @status_bar.get_context_id('dummy')
1084:         @status_bar.push(@cid, text)
1085:       end

Display the new title according to the editor‘s current state.

[Source]

      # File lib/json/editor.rb, line 1110
1110:       def display_title
1111:         title = TITLE.dup
1112:         title << ": #@filename" if @filename
1113:         title << " *" if @changed
1114:         self.title = title
1115:       end

Open the file filename or call the select_file method to ask for a filename.

[Source]

      # File lib/json/editor.rb, line 1141
1141:       def file_open(filename = nil)
1142:         filename = select_file('Open as a JSON file') unless filename
1143:         data = load_file(filename) or return
1144:         view_new_model Editor.data2model(data)
1145:       end

Save the current file.

[Source]

      # File lib/json/editor.rb, line 1148
1148:       def file_save
1149:         if @filename
1150:           store_file(@filename)
1151:         else
1152:           file_save_as
1153:         end
1154:       end

Save the current file as the filename

[Source]

      # File lib/json/editor.rb, line 1157
1157:       def file_save_as
1158:         filename = select_file('Save as a JSON file')
1159:         store_file(filename)
1160:       end

Load the file named filename into the editor as a JSON document.

[Source]

      # File lib/json/editor.rb, line 1183
1183:       def load_file(filename)
1184:         if filename
1185:           if File.directory?(filename)
1186:             Editor.error_dialog(self, "Try to select a JSON file!")
1187:             nil
1188:           else
1189:             @filename = filename
1190:             if data = read_data(filename)
1191:               toplevel.display_status("Loaded data from '#@filename'.")
1192:             end
1193:             display_title
1194:             data
1195:           end
1196:         end
1197:       end

Load the data at location uri into the editor as a JSON document.

[Source]

      # File lib/json/editor.rb, line 1200
1200:       def load_location(uri)
1201:         data = read_data(uri) or return
1202:         @filename = nil
1203:         toplevel.display_status("Loaded data from '#{uri}'.")
1204:         display_title
1205:         data
1206:       end

Open the data at the location uri, if given. Otherwise open a dialog to ask for the uri.

[Source]

      # File lib/json/editor.rb, line 1132
1132:       def location_open(uri = nil)
1133:         uri = ask_for_location unless uri
1134:         uri or return
1135:         data = load_location(uri) or return
1136:         view_new_model Editor.data2model(data)
1137:       end

Quit this editor, that is, leave this editor‘s main loop.

[Source]

      # File lib/json/editor.rb, line 1100
1100:       def quit
1101:         ask_save if @changed
1102:         if Gtk.main_level > 0
1103:           destroy
1104:           Gtk.main_quit
1105:         end
1106:         nil
1107:       end

Read a JSON document from the file named filename, parse it into a ruby data structure, and return the data.

[Source]

      # File lib/json/editor.rb, line 1210
1210:       def read_data(filename)
1211:         open(filename) do |f|
1212:           json = f.read
1213:           check_pretty_printed(json)
1214:           if @encoding && !/^utf8$/i.match(@encoding)
1215:             iconverter = Iconv.new('utf8', @encoding)
1216:             json = iconverter.iconv(json)
1217:           end
1218:           return JSON::parse(json, :max_nesting => false)
1219:         end
1220:       rescue => e
1221:         Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
1222:         return
1223:       end

Open a file selecton dialog, displaying message, and return the selected filename or nil, if no file was selected.

[Source]

      # File lib/json/editor.rb, line 1227
1227:       def select_file(message)
1228:         filename = nil
1229:         fs = FileSelection.new(message).set_modal(true).
1230:           set_filename(Dir.pwd + "/").set_transient_for(self)
1231:         fs.signal_connect(:destroy) { Gtk.main_quit }
1232:         fs.ok_button.signal_connect(:clicked) do
1233:           filename = fs.filename
1234:           fs.destroy
1235:           Gtk.main_quit
1236:         end
1237:         fs.cancel_button.signal_connect(:clicked) do
1238:           fs.destroy
1239:           Gtk.main_quit
1240:         end
1241:         fs.show_all
1242:         Gtk.main
1243:         filename
1244:       end

Store the current JSON document to path.

[Source]

      # File lib/json/editor.rb, line 1163
1163:       def store_file(path)
1164:         if path
1165:           data = Editor.model2data(@treeview.model.iter_first)
1166:           File.open(path + '.tmp', 'wb') do |output|
1167:             if @options_menu.pretty_item.active?
1168:               output.puts JSON.pretty_generate(data)
1169:             else
1170:               output.write JSON.unparse(data)
1171:             end
1172:           end
1173:           File.rename path + '.tmp', path
1174:           @filename = path
1175:           toplevel.display_status("Saved data to '#@filename'.")
1176:           unchange
1177:         end
1178:       rescue SystemCallError => e
1179:         Editor.error_dialog(self, "Failed to store JSON file: #{e}!")
1180:       end

Sets editor status to unchanged, to indicate that the edited data doesn‘t containt unsaved changes.

[Source]

      # File lib/json/editor.rb, line 1066
1066:       def unchange
1067:         @changed = false
1068:         display_title
1069:       end

Puts a new model model into the Gtk::TreeView to be edited.

[Source]

      # File lib/json/editor.rb, line 1072
1072:       def view_new_model(model)
1073:         @treeview.model     = model
1074:         @treeview.expanded  = true
1075:         @treeview.expand_all
1076:         unchange
1077:       end

Private Instance methods

[Source]

      # File lib/json/editor.rb, line 1124
1124:       def check_pretty_printed(json)
1125:         pretty = !!((nl_index = json.index("\n")) && nl_index != json.size - 1)
1126:         @options_menu.pretty_item.active = pretty
1127:       end

[Validate]