Parent

Included Modules

Files

JSON::Editor::MainWindow

The editor main window

Public Class Methods

new(encoding) click to toggle source
      # File lib/json/editor.rb, line 1050
1050:       def initialize(encoding)
1051:         @changed  = false
1052:         @encoding = encoding
1053:         super(TOPLEVEL)
1054:         display_title
1055:         set_default_size(800, 600)
1056:         signal_connect(:delete_event) { quit }
1057: 
1058:         vbox = VBox.new(false, 0)
1059:         add(vbox)
1060:         #vbox.border_width = 0
1061: 
1062:         @treeview = JSONTreeView.new(self)
1063:         @treeview.signal_connect(:'cursor-changed') do
1064:           display_status('')
1065:         end
1066: 
1067:         menu_bar = create_menu_bar
1068:         vbox.pack_start(menu_bar, false, false, 0)
1069: 
1070:         sw = ScrolledWindow.new(nil, nil)
1071:         sw.shadow_type = SHADOW_ETCHED_IN
1072:         sw.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
1073:         vbox.pack_start(sw, true, true, 0)
1074:         sw.add(@treeview)
1075: 
1076:         @status_bar = Statusbar.new
1077:         vbox.pack_start(@status_bar, false, false, 0)
1078: 
1079:         @filename ||= nil
1080:         if @filename
1081:           data = read_data(@filename)
1082:           view_new_model Editor.data2model(data)
1083:         end
1084: 
1085:         signal_connect(:button_release_event) do |_,event|
1086:           if event.button == 2
1087:             c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
1088:             if url = c.wait_for_text
1089:               location_open url
1090:             end
1091:             false
1092:           else
1093:             true
1094:           end
1095:         end
1096:       end

Public Instance Methods

ask_for_location() click to toggle source

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

      # File lib/json/editor.rb, line 1320
1320:       def ask_for_location
1321:         dialog = Dialog.new(
1322:           "Load data from location...",
1323:           nil, nil,
1324:           [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
1325:           [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
1326:         )
1327:         hbox = HBox.new(false, 5)
1328: 
1329:         hbox.pack_start(Label.new("Location:"), false)
1330:         hbox.pack_start(location_input = Entry.new)
1331:         location_input.width_chars = 60
1332:         location_input.text = @location || ''
1333: 
1334:         dialog.vbox.pack_start(hbox, false)
1335: 
1336:         dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
1337:         dialog.show_all
1338:         dialog.run do |response| 
1339:           if response == Dialog::RESPONSE_ACCEPT
1340:             return @location = location_input.text
1341:           end
1342:         end
1343:         return
1344:       ensure
1345:         dialog.destroy if dialog
1346:       end
ask_save() click to toggle source

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

      # File lib/json/editor.rb, line 1141
1141:       def ask_save
1142:         if Editor.question_dialog(self,
1143:           "Unsaved changes to JSON model. Save?")
1144:           if @filename
1145:             file_save
1146:           else
1147:             file_save_as
1148:           end
1149:         end
1150:       end
change() click to toggle source

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

      # File lib/json/editor.rb, line 1112
1112:       def change
1113:         @changed = true
1114:         display_title
1115:       end
clear() click to toggle source

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

      # File lib/json/editor.rb, line 1171
1171:       def clear
1172:         ask_save if @changed
1173:         @filename = nil
1174:         self.view_new_model nil
1175:       end
create_menu_bar() click to toggle source

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

      # File lib/json/editor.rb, line 1099
1099:       def create_menu_bar
1100:         menu_bar = MenuBar.new
1101:         @file_menu = FileMenu.new(@treeview)
1102:         menu_bar.append @file_menu.create
1103:         @edit_menu = EditMenu.new(@treeview)
1104:         menu_bar.append @edit_menu.create
1105:         @options_menu = OptionsMenu.new(@treeview)
1106:         menu_bar.append @options_menu.create
1107:         menu_bar
1108:       end
display_status(text) click to toggle source

Displays text in the status bar.

      # File lib/json/editor.rb, line 1133
1133:       def display_status(text)
1134:         @cid ||= nil
1135:         @status_bar.pop(@cid) if @cid
1136:         @cid = @status_bar.get_context_id('dummy')
1137:         @status_bar.push(@cid, text)
1138:       end
display_title() click to toggle source

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

      # File lib/json/editor.rb, line 1163
1163:       def display_title
1164:         title = TITLE.dup
1165:         title << ": #@filename" if @filename
1166:         title << " *" if @changed
1167:         self.title = title
1168:       end
edit(json) click to toggle source

Edit the string json in the editor.

      # File lib/json/editor.rb, line 1202
1202:       def edit(json)
1203:         if json.respond_to? :read
1204:           json = json.read
1205:         end
1206:         data = parse_json json
1207:         view_new_model Editor.data2model(data)
1208:       end
file_open(filename = nil) click to toggle source

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

      # File lib/json/editor.rb, line 1195
1195:       def file_open(filename = nil)
1196:         filename = select_file('Open as a JSON file') unless filename
1197:         data = load_file(filename) or return
1198:         view_new_model Editor.data2model(data)
1199:       end
file_save() click to toggle source

Save the current file.

      # File lib/json/editor.rb, line 1211
1211:       def file_save
1212:         if @filename
1213:           store_file(@filename)
1214:         else
1215:           file_save_as
1216:         end
1217:       end
file_save_as() click to toggle source

Save the current file as the filename

      # File lib/json/editor.rb, line 1220
1220:       def file_save_as
1221:         filename = select_file('Save as a JSON file')
1222:         store_file(filename)
1223:       end
load_file(filename) click to toggle source

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

      # File lib/json/editor.rb, line 1247
1247:       def load_file(filename)
1248:         if filename
1249:           if File.directory?(filename)
1250:             Editor.error_dialog(self, "Try to select a JSON file!")
1251:             nil
1252:           else
1253:             @filename = filename
1254:             if data = read_data(filename)
1255:               toplevel.display_status("Loaded data from '#@filename'.")
1256:             end
1257:             display_title
1258:             data
1259:           end
1260:         end
1261:       end
load_location(uri) click to toggle source

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

      # File lib/json/editor.rb, line 1264
1264:       def load_location(uri)
1265:         data = read_data(uri) or return
1266:         @filename = nil
1267:         toplevel.display_status("Loaded data from '#{uri}'.")
1268:         display_title
1269:         data
1270:       end
location_open(uri = nil) click to toggle source

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

      # File lib/json/editor.rb, line 1185
1185:       def location_open(uri = nil)
1186:         uri = ask_for_location unless uri
1187:         uri or return
1188:         ask_save if @changed
1189:         data = load_location(uri) or return
1190:         view_new_model Editor.data2model(data)
1191:       end
quit() click to toggle source

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

      # File lib/json/editor.rb, line 1153
1153:       def quit
1154:         ask_save if @changed
1155:         if Gtk.main_level > 0
1156:           destroy
1157:           Gtk.main_quit
1158:         end
1159:         nil
1160:       end
read_data(filename) click to toggle source

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 1284
1284:       def read_data(filename)
1285:         open(filename) do |f|
1286:           json = f.read
1287:           return parse_json(json)
1288:         end
1289:       rescue => e
1290:         Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
1291:         return
1292:       end
select_file(message) click to toggle source

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 1296
1296:       def select_file(message)
1297:         filename = nil
1298:         fs = FileSelection.new(message)
1299:         fs.set_modal(true)
1300:         @default_dir = File.join(Dir.pwd, '') unless @default_dir
1301:         fs.set_filename(@default_dir)
1302:         fs.set_transient_for(self)
1303:         fs.signal_connect(:destroy) { Gtk.main_quit }
1304:         fs.ok_button.signal_connect(:clicked) do
1305:           filename = fs.filename
1306:           @default_dir = File.join(File.dirname(filename), '')
1307:           fs.destroy
1308:           Gtk.main_quit
1309:         end
1310:         fs.cancel_button.signal_connect(:clicked) do
1311:           fs.destroy
1312:           Gtk.main_quit
1313:         end
1314:         fs.show_all
1315:         Gtk.main
1316:         filename
1317:       end
store_file(path) click to toggle source

Store the current JSON document to path.

      # File lib/json/editor.rb, line 1226
1226:       def store_file(path)
1227:         if path
1228:           data = Editor.model2data(@treeview.model.iter_first)
1229:           File.open(path + '.tmp', 'wb') do |output|
1230:             data or break
1231:             if @options_menu.pretty_item.active?
1232:               output.puts JSON.pretty_generate(data, :max_nesting => false)
1233:             else
1234:               output.write JSON.generate(data, :max_nesting => false)
1235:             end
1236:           end
1237:           File.rename path + '.tmp', path
1238:           @filename = path
1239:           toplevel.display_status("Saved data to '#@filename'.")
1240:           unchange
1241:         end
1242:       rescue SystemCallError => e
1243:         Editor.error_dialog(self, "Failed to store JSON file: #{e}!")
1244:       end
unchange() click to toggle source

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

      # File lib/json/editor.rb, line 1119
1119:       def unchange
1120:         @changed = false
1121:         display_title
1122:       end
view_new_model(model) click to toggle source

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

      # File lib/json/editor.rb, line 1125
1125:       def view_new_model(model)
1126:         @treeview.model     = model
1127:         @treeview.expanded  = true
1128:         @treeview.expand_all
1129:         unchange
1130:       end

Private Instance Methods

check_pretty_printed(json) click to toggle source
      # File lib/json/editor.rb, line 1177
1177:       def check_pretty_printed(json)
1178:         pretty = !!((nl_index = json.index("\n")) && nl_index != json.size - 1)
1179:         @options_menu.pretty_item.active = pretty
1180:       end
parse_json(json) click to toggle source
      # File lib/json/editor.rb, line 1272
1272:       def parse_json(json)
1273:         check_pretty_printed(json)
1274:         if @encoding && !/^utf8$/.match(@encoding)
1275:           iconverter = Iconv.new('utf8', @encoding)
1276:           json = iconverter.iconv(json)
1277:         end
1278:         JSON::parse(json, :max_nesting => false, :create_additions => false)
1279:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.