Class MainWindow
In: lib/json/editor.rb
Parent: Gtk::Window

The editor main window

Methods

Included Modules

Gtk

Public Class methods

[Source]

# File lib/json/editor.rb, line 1041
      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''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 URI a to load data from. Returns the URI as a string.

[Source]

# File lib/json/editor.rb, line 1311
      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''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

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

[Source]

# File lib/json/editor.rb, line 1132
      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

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

[Source]

# File lib/json/editor.rb, line 1103
      def change
        @changed = true
        display_title
      end

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

[Source]

# File lib/json/editor.rb, line 1162
      def clear
        ask_save if @changed
        @filename = nil
        self.view_new_model nil
      end

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

[Source]

# File lib/json/editor.rb, line 1090
      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

Displays text in the status bar.

[Source]

# File lib/json/editor.rb, line 1124
      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 the new title according to the editor‘s current state.

[Source]

# File lib/json/editor.rb, line 1154
      def display_title
        title = TITLE.dup
        title << ": #@filename" if @filename
        title << " *" if @changed
        self.title = title
      end

Edit the string json in the editor.

[Source]

# File lib/json/editor.rb, line 1193
      def edit(json)
        if json.respond_to? :read
          json = json.read
        end
        data = parse_json json
        view_new_model Editor.data2model(data)
      end

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

[Source]

# File lib/json/editor.rb, line 1186
      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

Save the current file.

[Source]

# File lib/json/editor.rb, line 1202
      def file_save
        if @filename
          store_file(@filename)
        else
          file_save_as
        end
      end

Save the current file as the filename

[Source]

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

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

[Source]

# File lib/json/editor.rb, line 1238
      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 the data at location uri into the editor as a JSON document.

[Source]

# File lib/json/editor.rb, line 1255
      def load_location(uri)
        data = read_data(uri) or return
        @filename = nil
        toplevel.display_status("Loaded data from '#{uri}'.")
        display_title
        data
      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 1176
      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 this editor, that is, leave this editor‘s main loop.

[Source]

# File lib/json/editor.rb, line 1144
      def quit
        ask_save if @changed
        if Gtk.main_level > 0
          destroy
          Gtk.main_quit
        end
        nil
      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 1275
      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

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 1287
      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 the current JSON document to path.

[Source]

# File lib/json/editor.rb, line 1217
      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

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

[Source]

# File lib/json/editor.rb, line 1110
      def unchange
        @changed = false
        display_title
      end

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

[Source]

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

[Validate]