Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • json/lib/json/editor.rb

JSON::Editor::MainWindow

The editor main window

Public Class Methods

new(encoding) click to toggle source
 
               # File json/lib/json/editor.rb, line 1050
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

  menu_bar = create_menu_bar
  vbox.pack_start(menu_bar, 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

  signal_connect(:button_release_event) do |_,event|
    if event.button == 2
      c = Gtk::Clipboard.get(Gdk::Selection::PRIMARY)
      if url = c.wait_for_text
        location_open url
      end
      false
    else
      true
    end
  end
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 json/lib/json/editor.rb, line 1320
def ask_for_location
  dialog = Dialog.new(
    "Load data from location...",
    nil, nil,
    [ Stock::OK, Dialog::RESPONSE_ACCEPT ],
    [ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
  )
  hbox = HBox.new(false, 5)

  hbox.pack_start(Label.new("Location:"), false)
  hbox.pack_start(location_input = Entry.new)
  location_input.width_chars = 60
  location_input.text = @location || ''

  dialog.vbox.pack_start(hbox, false)

  dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
  dialog.show_all
  dialog.run do |response| 
    if response == Dialog::RESPONSE_ACCEPT
      return @location = location_input.text
    end
  end
  return
ensure
  dialog.destroy if dialog
end
            
ask_save() click to toggle source

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

 
               # File json/lib/json/editor.rb, line 1141
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() click to toggle source

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

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

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

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

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

 
               # File json/lib/json/editor.rb, line 1099
def create_menu_bar
  menu_bar = MenuBar.new
  @file_menu = FileMenu.new(@treeview)
  menu_bar.append @file_menu.create
  @edit_menu = EditMenu.new(@treeview)
  menu_bar.append @edit_menu.create
  @options_menu = OptionsMenu.new(@treeview)
  menu_bar.append @options_menu.create
  menu_bar
end
            
display_status(text) click to toggle source

Displays text in the status bar.

 
               # File json/lib/json/editor.rb, line 1133
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() click to toggle source

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

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

Edit the string json in the editor.

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

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

 
               # File json/lib/json/editor.rb, line 1195
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() click to toggle source

Save the current file.

 
               # File json/lib/json/editor.rb, line 1211
def file_save
  if @filename
    store_file(@filename)
  else
    file_save_as
  end
end
            
file_save_as() click to toggle source

Save the current file as the filename

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

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

 
               # File json/lib/json/editor.rb, line 1247
def load_file(filename)
  if filename
    if File.directory?(filename)
      Editor.error_dialog(self, "Try to select a JSON file!")
      nil
    else
      @filename = filename
      if data = read_data(filename)
        toplevel.display_status("Loaded data from '#@filename'.")
      end
      display_title
      data
    end
  end
end
            
load_location(uri) click to toggle source

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

 
               # File json/lib/json/editor.rb, line 1264
def load_location(uri)
  data = read_data(uri) or return
  @filename = nil
  toplevel.display_status("Loaded data from '#{uri}'.")
  display_title
  data
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 json/lib/json/editor.rb, line 1185
def location_open(uri = nil)
  uri = ask_for_location unless uri
  uri or return
  ask_save if @changed
  data = load_location(uri) or return
  view_new_model Editor.data2model(data)
end
            
quit() click to toggle source

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

 
               # File json/lib/json/editor.rb, line 1153
def quit
  ask_save if @changed
  if Gtk.main_level > 0
    destroy
    Gtk.main_quit
  end
  nil
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 json/lib/json/editor.rb, line 1284
def read_data(filename)
  open(filename) do |f|
    json = f.read
    return parse_json(json)
  end
rescue => e
  Editor.error_dialog(self, "Failed to parse JSON file: #{e}!")
  return
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 json/lib/json/editor.rb, line 1296
def select_file(message)
  filename = nil
  fs = FileSelection.new(message)
  fs.set_modal(true)
  @default_dir = File.join(Dir.pwd, '') unless @default_dir
  fs.set_filename(@default_dir)
  fs.set_transient_for(self)
  fs.signal_connect(:destroy) { Gtk.main_quit }
  fs.ok_button.signal_connect(:clicked) do
    filename = fs.filename
    @default_dir = File.join(File.dirname(filename), '')
    fs.destroy
    Gtk.main_quit
  end
  fs.cancel_button.signal_connect(:clicked) do
    fs.destroy
    Gtk.main_quit
  end
  fs.show_all
  Gtk.main
  filename
end
            
store_file(path) click to toggle source

Store the current JSON document to path.

 
               # File json/lib/json/editor.rb, line 1226
def store_file(path)
  if path
    data = Editor.model2data(@treeview.model.iter_first)
    File.open(path + '.tmp', 'wb') do |output|
      data or break
      if @options_menu.pretty_item.active?
        output.puts JSON.pretty_generate(data, :max_nesting => false)
      else
        output.write JSON.generate(data, :max_nesting => false)
      end
    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() click to toggle source

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

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

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

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

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus